diff options
author | Joerg Bornemann <joerg.bornemann@theqtcompany.com> | 2016-01-18 11:44:03 +0100 |
---|---|---|
committer | Joerg Bornemann <joerg.bornemann@theqtcompany.com> | 2016-01-18 13:25:55 +0000 |
commit | 30e4967e8985ad6e0d2414c5e4a1dea1f1f9f8f4 (patch) | |
tree | 9325ca81626f49627d4dfffc09b3f8bfd5c6f5c2 | |
parent | d38722eff3c7b07a452e5ce97b01c4f31bcedfcd (diff) |
Fix glitches in !include directive
Includes like <"foo.mk"> were not possible.
Includes like "foo.mk" did not recursively search through parent
makefiles directories.
Change-Id: I20daccae52c5af83a98ec2b95522a4ac03a99ddd
Reviewed-by: Oliver Wolff <oliver.wolff@theqtcompany.com>
-rw-r--r-- | src/jomlib/preprocessor.cpp | 73 | ||||
-rw-r--r-- | src/jomlib/preprocessor.h | 1 | ||||
-rw-r--r-- | tests/makefiles/include_test.mk | 1 | ||||
-rw-r--r-- | tests/makefiles/subdir/include8.mk | 3 | ||||
-rw-r--r-- | tests/makefiles/subdir/include9.mk | 3 | ||||
-rw-r--r-- | tests/makefiles/subdir/subsub/include6.mk | 1 | ||||
-rw-r--r-- | tests/tests.cpp | 2 |
7 files changed, 53 insertions, 31 deletions
diff --git a/src/jomlib/preprocessor.cpp b/src/jomlib/preprocessor.cpp index 1bf0682..566c926 100644 --- a/src/jomlib/preprocessor.cpp +++ b/src/jomlib/preprocessor.cpp @@ -62,36 +62,6 @@ bool Preprocessor::openFile(const QString& fileName) bool Preprocessor::internalOpenFile(QString fileName) { - if (fileName.startsWith(QLatin1Char('"')) && fileName.endsWith(QLatin1Char('"'))) - fileName = fileName.mid(1, fileName.length() - 2); - else if (fileName.startsWith(QLatin1Char('<')) && fileName.endsWith(QLatin1Char('>'))) { - fileName = fileName.mid(1, fileName.length() - 2); - QString includeVar = m_macroTable->macroValue(QLatin1String("INCLUDE")).replace(QLatin1Char('\t'), QLatin1Char(' ')); - QStringList includeDirs = includeVar.split(QLatin1Char(';'), QString::SkipEmptyParts); - QString fullFileName; - foreach (const QString& includeDir, includeDirs) { - fullFileName = includeDir + QDir::separator() + fileName; - if (QFile::exists(fullFileName)) { - fileName = fullFileName; - break; - } - } - } else { - QString fullFileName = fileName; - QStack<TextFile> tmpStack; - while (m_fileStack.count() >= 1) { - if (QFile::exists(fullFileName)) { - fileName = fullFileName; - break; - } - TextFile textFile = m_fileStack.pop(); - tmpStack.push(textFile); - fullFileName = textFile.fileDirectory + QDir::separator() + fileName; - } - while (!tmpStack.isEmpty()) - m_fileStack.push(tmpStack.pop()); - } - // make file name absolute for safe cycle detection const QString origFileName = fileName; QFileInfo fileInfo(fileName); @@ -227,7 +197,7 @@ bool Preprocessor::parsePreprocessingDirective(const QString& line) } else if (directive == QLatin1String("MESSAGE")) { puts(qPrintable(value)); } else if (directive == QLatin1String("INCLUDE")) { - internalOpenFile(value); + internalOpenFile(findIncludeFile(value)); } else if (directive == QLatin1String("IF")) { bool followElseBranch = evaluateExpression(value) == 0; enterConditional(followElseBranch); @@ -288,6 +258,47 @@ bool Preprocessor::parsePreprocessingDirective(const QString& line) return true; } +QString Preprocessor::findIncludeFile(const QString &filePathToInclude) +{ + QString filePath = filePathToInclude; + bool angleBrackets = false; + if (filePath.startsWith(QLatin1Char('<')) && filePath.endsWith(QLatin1Char('>'))) { + angleBrackets = true; + filePath.chop(1); + filePath.remove(0, 1); + } + removeDoubleQuotes(filePath); + + QFileInfo fi(filePath); + if (fi.exists()) + return fi.absoluteFilePath(); + + // Search recursively through all directories of all parent makefiles. + for (QStack<TextFile>::const_iterator it = m_fileStack.constEnd(); + it != m_fileStack.constBegin();) { + --it; + fi.setFile(it->fileDirectory + QLatin1Char('/') + filePath); + if (fi.exists()) + return fi.absoluteFilePath(); + } + + if (angleBrackets) { + // Search through all directories in the INCLUDE macro. + const QString includeVar = m_macroTable->macroValue(QLatin1String("INCLUDE")) + .replace(QLatin1Char('\t'), QLatin1Char(' ')); + const QStringList includeDirs = includeVar.split(QLatin1Char(';'), QString::SkipEmptyParts); + foreach (const QString& includeDir, includeDirs) { + fi.setFile(includeDir + QLatin1Char('/') + filePath); + if (fi.exists()) + return fi.absoluteFilePath(); + } + } + + const QString msg = QLatin1String("File %1 cannot be found."); + error(msg.arg(filePathToInclude)); + return QString(); +} + bool Preprocessor::isPreprocessingDirective(const QString& line, QString& directive, QString& value) { if (line.isEmpty()) diff --git a/src/jomlib/preprocessor.h b/src/jomlib/preprocessor.h index bf6773a..c3974e7 100644 --- a/src/jomlib/preprocessor.h +++ b/src/jomlib/preprocessor.h @@ -55,6 +55,7 @@ private: void basicReadLine(QString& line); bool parseMacro(const QString& line); bool parsePreprocessingDirective(const QString& line); + QString findIncludeFile(const QString &filePathToInclude); bool isPreprocessingDirective(const QString& line, QString& directive, QString& value); void skipUntilNextMatchingConditional(); void error(const QString& msg); diff --git a/tests/makefiles/include_test.mk b/tests/makefiles/include_test.mk index 0aa96fd..a45fb55 100644 --- a/tests/makefiles/include_test.mk +++ b/tests/makefiles/include_test.mk @@ -4,6 +4,7 @@ include include2.mk # old style include directive INCLUDE = $(INCLUDE);subdir ! include <include3.mk> !include "subdir\include4.mk" +!include <"include9.mk"> includeFoo: # this is not an include directive! diff --git a/tests/makefiles/subdir/include8.mk b/tests/makefiles/subdir/include8.mk new file mode 100644 index 0000000..cbcbd77 --- /dev/null +++ b/tests/makefiles/subdir/include8.mk @@ -0,0 +1,3 @@ +!message "include 8" +INCLUDE8 = TRUE + diff --git a/tests/makefiles/subdir/include9.mk b/tests/makefiles/subdir/include9.mk new file mode 100644 index 0000000..a4ace05 --- /dev/null +++ b/tests/makefiles/subdir/include9.mk @@ -0,0 +1,3 @@ +!message "include 9" +INCLUDE9 = TRUE + diff --git a/tests/makefiles/subdir/subsub/include6.mk b/tests/makefiles/subdir/subsub/include6.mk index 98a8bb2..4637fa5 100644 --- a/tests/makefiles/subdir/subsub/include6.mk +++ b/tests/makefiles/subdir/subsub/include6.mk @@ -1,3 +1,4 @@ !message "include 6" INCLUDE6=TRUE !include include7.mk +!include "include8.mk" diff --git a/tests/tests.cpp b/tests/tests.cpp index 4041eb0..f983e24 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -80,6 +80,8 @@ void Tests::includeFiles() QCOMPARE(macroTable.macroValue("INCLUDE5"), QLatin1String("TRUE")); QCOMPARE(macroTable.macroValue("INCLUDE6"), QLatin1String("TRUE")); QCOMPARE(macroTable.macroValue("INCLUDE7"), QLatin1String("TRUE")); + QCOMPARE(macroTable.macroValue("INCLUDE8"), QLatin1String("TRUE")); + QCOMPARE(macroTable.macroValue("INCLUDE9"), QLatin1String("TRUE")); } void Tests::includeCycle() |