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 <ivan.donchevskii@qt.io>
This commit is contained in:
Marco Bubke
2018-08-14 13:07:04 +02:00
parent b592339b4d
commit 7bae47642c
8 changed files with 42 additions and 15 deletions

View File

@@ -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));
}
}

View File

@@ -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);
}

View File

@@ -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));

View File

@@ -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())};

View File

@@ -29,6 +29,7 @@
#include <coreplugin/vcsmanager.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/project.h>
#include <utils/fileutils.h>
#include <utils/qtcassert.h>
@@ -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 &macro) const

View File

@@ -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

View File

@@ -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();

View File

@@ -36,6 +36,10 @@ public:
Utils::FileName projectDirectory() const {
return Utils::FileName();
}
Utils::FileName rootProjectDirectory() const {
return Utils::FileName();
}
};
}