From 7bae47642c3af7316ffe816bbf7a092b7f536c1e Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Tue, 14 Aug 2018 13:07:04 +0200 Subject: [PATCH] Add optional system include to compiler option builder System includes suppress warnings and prevent indexing of unwanted symbols. Using system includes for all includes outside of the project can be quite advantageous. The rootProjectDirectory() can be extended to be set in the project settings. An automatic generation could be possible but could create an unwanted path which includes files outside of the perceived project. Change-Id: Ib9d3158f14f41efe1f6657f962d5c4437bb324b2 Reviewed-by: Ivan Donchevskii --- src/plugins/clangcodemodel/clangutils.cpp | 6 +++--- .../clangpchmanager/projectupdater.cpp | 2 +- .../clangqueryprojectsfindfilter.cpp | 2 +- .../clangrefactoring/refactoringengine.cpp | 2 +- .../cpptools/compileroptionsbuilder.cpp | 19 +++++++++++++------ src/plugins/cpptools/compileroptionsbuilder.h | 13 ++++++++++--- src/plugins/projectexplorer/project.cpp | 9 +++++++++ tests/unit/mockup/projectexplorer/project.h | 4 ++++ 8 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/plugins/clangcodemodel/clangutils.cpp b/src/plugins/clangcodemodel/clangutils.cpp index b58304eb2b4..8f11758ac4d 100644 --- a/src/plugins/clangcodemodel/clangutils.cpp +++ b/src/plugins/clangcodemodel/clangutils.cpp @@ -153,9 +153,9 @@ private: if (m_projectPart.qtVersion != CppTools::ProjectPart::NoQt) { const QString wrappedQtCoreHeaderPath = wrappedQtHeadersPath + "/QtCore"; - add(includeDirOption()); + add(includeDirOptionForPath(wrappedQtHeadersPath)); add(QDir::toNativeSeparators(wrappedQtHeadersPath)); - add(includeDirOption()); + add(includeDirOptionForPath(wrappedQtHeadersPath)); add(QDir::toNativeSeparators(wrappedQtCoreHeaderPath)); } } @@ -164,7 +164,7 @@ private: { const QString path = ModelManagerSupportClang::instance()->dummyUiHeaderOnDiskDirPath(); if (!path.isEmpty()) { - add(includeDirOption()); + add("-I"); add(QDir::toNativeSeparators(path)); } } diff --git a/src/plugins/clangpchmanager/projectupdater.cpp b/src/plugins/clangpchmanager/projectupdater.cpp index 66c63d1387b..ce890ee1174 100644 --- a/src/plugins/clangpchmanager/projectupdater.cpp +++ b/src/plugins/clangpchmanager/projectupdater.cpp @@ -149,7 +149,7 @@ HeaderAndSources ProjectUpdater::headerAndSourcesFromProjectPart( QStringList ProjectUpdater::compilerArguments(CppTools::ProjectPart *projectPart) { using CppTools::CompilerOptionsBuilder; - CompilerOptionsBuilder builder(*projectPart); + CompilerOptionsBuilder builder(*projectPart, CppTools::UseSystemHeader::Yes); return builder.build(CppTools::ProjectFile::CXXHeader, CompilerOptionsBuilder::PchUsage::None); } diff --git a/src/plugins/clangrefactoring/clangqueryprojectsfindfilter.cpp b/src/plugins/clangrefactoring/clangqueryprojectsfindfilter.cpp index 336a681ab2a..77d4a9ef6e2 100644 --- a/src/plugins/clangrefactoring/clangqueryprojectsfindfilter.cpp +++ b/src/plugins/clangrefactoring/clangqueryprojectsfindfilter.cpp @@ -153,7 +153,7 @@ Utils::SmallStringVector ClangQueryProjectsFindFilter::compilerArguments(CppTool { using CppTools::CompilerOptionsBuilder; - CompilerOptionsBuilder builder(*projectPart); + CompilerOptionsBuilder builder(*projectPart, CppTools::UseSystemHeader::Yes); return Utils::SmallStringVector(builder.build(fileKind, CompilerOptionsBuilder::PchUsage::None)); diff --git a/src/plugins/clangrefactoring/refactoringengine.cpp b/src/plugins/clangrefactoring/refactoringengine.cpp index b5127e2b88f..57122fd3bea 100644 --- a/src/plugins/clangrefactoring/refactoringengine.cpp +++ b/src/plugins/clangrefactoring/refactoringengine.cpp @@ -74,7 +74,7 @@ void RefactoringEngine::startLocalRenaming(const CppTools::CursorInEditor &data, QString filePath = data.filePath().toString(); QTextCursor textCursor = data.cursor(); - CompilerOptionsBuilder optionsBuilder{*projectPart}; + CompilerOptionsBuilder optionsBuilder{*projectPart, CppTools::UseSystemHeader::Yes}; Utils::SmallStringVector commandLine{optionsBuilder.build( fileKindInProjectPart(projectPart, filePath), CppTools::getPchUsage())}; diff --git a/src/plugins/cpptools/compileroptionsbuilder.cpp b/src/plugins/cpptools/compileroptionsbuilder.cpp index 041cbf607b2..aeb60a0f282 100644 --- a/src/plugins/cpptools/compileroptionsbuilder.cpp +++ b/src/plugins/cpptools/compileroptionsbuilder.cpp @@ -29,6 +29,7 @@ #include #include +#include #include #include @@ -38,8 +39,10 @@ namespace CppTools { -CompilerOptionsBuilder::CompilerOptionsBuilder(const ProjectPart &projectPart) - : m_projectPart(projectPart) +CompilerOptionsBuilder::CompilerOptionsBuilder(const ProjectPart &projectPart, + UseSystemHeader useSystemHeader) + : m_projectPart(projectPart), + m_useSystemHeader(useSystemHeader) { } @@ -188,7 +191,6 @@ void CompilerOptionsBuilder::enableExceptions() void CompilerOptionsBuilder::addHeaderPathOptions() { typedef ProjectPartHeaderPath HeaderPath; - const QString defaultPrefix = includeDirOption(); QStringList result; @@ -208,7 +210,7 @@ void CompilerOptionsBuilder::addHeaderPathOptions() default: // This shouldn't happen, but let's be nice..: // intentional fall-through: case HeaderPath::IncludePath: - prefix = defaultPrefix; + prefix = includeDirOptionForPath(headerPath.path); break; } @@ -423,9 +425,14 @@ void CompilerOptionsBuilder::addDefineFunctionMacrosMsvc() addMacros({{"__FUNCSIG__", "\"\""}, {"__FUNCTION__", "\"\""}, {"__FUNCDNAME__", "\"\""}}); } -QString CompilerOptionsBuilder::includeDirOption() const +QString CompilerOptionsBuilder::includeDirOptionForPath(const QString &path) const { - return QLatin1String("-I"); + if (m_useSystemHeader == UseSystemHeader::No + || path.startsWith(m_projectPart.project->rootProjectDirectory().toString())) { + return QString("-I"); + } else { + return QString("-isystem"); + } } QByteArray CompilerOptionsBuilder::macroOption(const ProjectExplorer::Macro ¯o) const diff --git a/src/plugins/cpptools/compileroptionsbuilder.h b/src/plugins/cpptools/compileroptionsbuilder.h index eddbd6d3fb6..75241dd0231 100644 --- a/src/plugins/cpptools/compileroptionsbuilder.h +++ b/src/plugins/cpptools/compileroptionsbuilder.h @@ -31,6 +31,12 @@ namespace CppTools { +enum class UseSystemHeader +{ + Yes, + No +}; + class CPPTOOLS_EXPORT CompilerOptionsBuilder { public: @@ -39,7 +45,8 @@ public: Use }; - CompilerOptionsBuilder(const ProjectPart &projectPart); + CompilerOptionsBuilder(const ProjectPart &projectPart, + UseSystemHeader useSystemHeader = UseSystemHeader::No); virtual ~CompilerOptionsBuilder() {} virtual void addTargetTriple(); @@ -80,8 +87,7 @@ protected: virtual QString defineOption() const; virtual QString undefineOption() const; virtual QString includeOption() const; - virtual QString includeDirOption() const; - + QString includeDirOptionForPath(const QString &path) const; const ProjectPart m_projectPart; private: @@ -90,6 +96,7 @@ private: QString defineDirectiveToDefineOption(const ProjectExplorer::Macro &marco) const; QStringList m_options; + UseSystemHeader m_useSystemHeader; }; } // namespace CppTools diff --git a/src/plugins/projectexplorer/project.cpp b/src/plugins/projectexplorer/project.cpp index 803dd3be672..43188661dfa 100644 --- a/src/plugins/projectexplorer/project.cpp +++ b/src/plugins/projectexplorer/project.cpp @@ -642,6 +642,15 @@ Utils::FileName Project::projectDirectory(const Utils::FileName &top) return Utils::FileName::fromString(top.toFileInfo().absoluteDir().path()); } +/*! + Returns the common root directory that contains all files which belongs to a project. +*/ + +Utils::FileName Project::rootProjectDirectory() const +{ + return projectDirectory(); // TODO parse all files and find the common path +} + ProjectNode *Project::rootProjectNode() const { return d->m_rootProjectNode.get(); diff --git a/tests/unit/mockup/projectexplorer/project.h b/tests/unit/mockup/projectexplorer/project.h index ea040fbc490..78b4211dca4 100644 --- a/tests/unit/mockup/projectexplorer/project.h +++ b/tests/unit/mockup/projectexplorer/project.h @@ -36,6 +36,10 @@ public: Utils::FileName projectDirectory() const { return Utils::FileName(); } + + Utils::FileName rootProjectDirectory() const { + return Utils::FileName(); + } }; }