Clang: Always force the built-in includes order

C++ includes must always come first, then clang resource
directory and then everything else.
This prevents both c++ standard headers and intrinsics issues.

Change-Id: Ia21bfa2fe99884c9adf58f7ef6beba1bede1724b
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Ivan Donchevskii
2018-12-05 10:36:24 +01:00
parent 867befc5ae
commit aa70799795
2 changed files with 94 additions and 83 deletions

View File

@@ -221,39 +221,43 @@ static QString clangIncludeDirectory(const QString &clangVersion,
#endif
}
static int lastIncludeIndex(const QStringList &options, const QRegularExpression &includePathRegEx)
{
int index = options.lastIndexOf(includePathRegEx);
while (index > 0 && options[index - 1] != "-I" && options[index - 1] != "-isystem")
index = options.lastIndexOf(includePathRegEx, index - 1);
if (index == 0)
index = -1;
return index;
}
static int includeIndexForResourceDirectory(const QStringList &options, bool isMacOs = false)
static QStringList insertResourceDirectory(const QStringList &options,
const QString &resourceDir,
bool isMacOs = false)
{
// include/c++, include/g++, libc++\include and libc++abi\include
static const QString cppIncludes = R"((.*[\/\\]include[\/\\].*(g\+\+|c\+\+).*))"
R"(|(.*libc\+\+[\/\\]include))"
R"(|(.*libc\+\+abi[\/\\]include))";
static const QRegularExpression includeRegExp("\\A(" + cppIncludes + ")\\z");
// The same as includeRegExp but also matches /usr/local/include
static const QRegularExpression includeRegExpMac(
"\\A(" + cppIncludes + R"(|([\/\\]usr[\/\\]local[\/\\]include))" + ")\\z");
QStringList optionsBeforeResourceDirectory;
QStringList optionsAfterResourceDirectory;
QRegularExpression includeRegExp;
if (!isMacOs) {
includeRegExp = QRegularExpression("\\A(" + cppIncludes + ")\\z");
} else {
// The same as includeRegExp but also matches /usr/local/include
includeRegExp = QRegularExpression(
"\\A(" + cppIncludes + R"(|([\/\\]usr[\/\\]local[\/\\]include))" + ")\\z");
}
const int cppIncludeIndex = lastIncludeIndex(options, isMacOs
? includeRegExpMac
: includeRegExp);
for (const QString &option : options) {
if (option == "-isystem")
continue;
if (cppIncludeIndex > 0)
return cppIncludeIndex + 1;
if (includeRegExp.match(option).hasMatch()) {
optionsBeforeResourceDirectory.push_back("-isystem");
optionsBeforeResourceDirectory.push_back(option);
} else {
optionsAfterResourceDirectory.push_back("-isystem");
optionsAfterResourceDirectory.push_back(option);
}
}
return -1;
optionsBeforeResourceDirectory.push_back("-isystem");
optionsBeforeResourceDirectory.push_back(resourceDir);
return optionsBeforeResourceDirectory + optionsAfterResourceDirectory;
}
void CompilerOptionsBuilder::insertWrappedQtHeaders()
@@ -325,16 +329,11 @@ void CompilerOptionsBuilder::addHeaderPathOptions()
const QString clangIncludePath
= clangIncludeDirectory(m_clangVersion, m_clangResourceDirectory);
int includeIndexForResourceDir = includeIndexForResourceDirectory(
builtInIncludes, m_projectPart.toolChainTargetTriple.contains("darwin"));
if (includeIndexForResourceDir >= 0) {
builtInIncludes.insert(includeIndexForResourceDir, clangIncludePath);
builtInIncludes.insert(includeIndexForResourceDir, "-isystem");
} else {
builtInIncludes.prepend(clangIncludePath);
builtInIncludes.prepend("-isystem");
}
builtInIncludes = insertResourceDirectory(builtInIncludes,
clangIncludePath,
m_projectPart.toolChainTargetTriple.contains(
"darwin"));
}
m_options.append(builtInIncludes);