Commit 36d4d01c authored by Denis Klychkov's avatar Denis Klychkov Committed by Nikolai Kosjar

Clang: Take precompiled headers into account when parsing source files

Task-number: QTCREATORBUG-15590
Change-Id: Icbfce4ffd6e9f9802641d35d898fa1dc1c4c18ee
Reviewed-by: Nikolai Kosjar's avatarNikolai Kosjar <nikolai.kosjar@qt.io>
parent 569f4499
......@@ -401,9 +401,30 @@ static QStringList warningOptions(CppTools::ProjectPart *projectPart)
return CppTools::codeModelSettings()->clangDiagnosticConfig().commandLineOptions();
}
static QStringList precompiledHeaderOptions(
const QString& filePath,
CppTools::ProjectPart *projectPart)
{
using namespace CppTools;
if (CppTools::getPchUsage() == CompilerOptionsBuilder::PchUsage::None)
return QStringList();
if (projectPart->precompiledHeaders.contains(filePath))
return QStringList();
const CppTools::ProjectPart theProjectPart = projectPartForLanguageOption(projectPart);
CppTools::CompilerOptionsBuilder builder(theProjectPart);
builder.addPrecompiledHeaderOptions(CompilerOptionsBuilder::PchUsage::Use);
return builder.options();
}
static QStringList fileArguments(const QString &filePath, CppTools::ProjectPart *projectPart)
{
return languageOptions(filePath, projectPart) + warningOptions(projectPart);
return languageOptions(filePath, projectPart)
+ warningOptions(projectPart)
+ precompiledHeaderOptions(filePath, projectPart);
}
ClangBackEnd::FileContainer
......
......@@ -192,27 +192,6 @@ QStringList createClangOptions(const ProjectPart::Ptr &pPart, ProjectFile::Kind
return LibClangOptionsBuilder::build(pPart, fileKind);
}
/// @return Option to speed up parsing with precompiled header
QStringList createPCHInclusionOptions(const QStringList &pchFiles)
{
QStringList opts;
foreach (const QString &pchFile, pchFiles) {
if (QFile(pchFile).exists()) {
opts += QLatin1String("-include-pch");
opts += pchFile;
}
}
return opts;
}
/// @return Option to speed up parsing with precompiled header
QStringList createPCHInclusionOptions(const QString &pchFile)
{
return createPCHInclusionOptions(QStringList() << pchFile);
}
ProjectPart::Ptr projectPartForFile(const QString &filePath)
{
if (const auto parser = CppTools::BaseEditorDocumentParser::get(filePath))
......
......@@ -34,7 +34,6 @@ QStringList createClangOptions(const CppTools::ProjectPart::Ptr &pPart,
CppTools::ProjectFile::Kind fileKind);
QStringList createClangOptions(const CppTools::ProjectPart::Ptr &pPart,
const QString &fileName = QString());
QStringList createPCHInclusionOptions(const QString &pchFile);
CppTools::ProjectPart::Ptr projectPartForFile(const QString &filePath);
CppTools::ProjectPart::Ptr projectPartForFileBasedOnProcessor(const QString &filePath);
......
......@@ -117,7 +117,8 @@ void RefactoringCompilerOptionsBuilder::addExtraOptions()
}
Utils::SmallStringVector RefactoringCompilerOptionsBuilder::build(CppTools::ProjectPart *projectPart,
CppTools::ProjectFile::Kind fileKind)
CppTools::ProjectFile::Kind fileKind,
PchUsage pchUsage)
{
if (projectPart == nullptr)
return Utils::SmallStringVector();
......@@ -138,6 +139,7 @@ Utils::SmallStringVector RefactoringCompilerOptionsBuilder::build(CppTools::Proj
optionsBuilder.addPredefinedMacrosAndHeaderPathsOptions();
optionsBuilder.addWrappedQtHeadersIncludePath();
optionsBuilder.addHeaderPathOptions();
optionsBuilder.addPrecompiledHeaderOptions(pchUsage);
optionsBuilder.addProjectConfigFileInclude();
optionsBuilder.addMsvcCompatibilityVersion();
......
......@@ -41,7 +41,8 @@ class RefactoringCompilerOptionsBuilder : public CppTools::CompilerOptionsBuilde
{
public:
static Utils::SmallStringVector build(CppTools::ProjectPart *projectPart,
CppTools::ProjectFile::Kind fileKind);
CppTools::ProjectFile::Kind fileKind,
PchUsage pchUsage);
private:
RefactoringCompilerOptionsBuilder(CppTools::ProjectPart *projectPart);
......
......@@ -30,6 +30,8 @@
#include <refactoringserverinterface.h>
#include <requestsourcelocationforrenamingmessage.h>
#include <cpptools/cpptoolsreuse.h>
#include <QTextCursor>
#include <QTextDocument>
......@@ -89,7 +91,8 @@ void RefactoringEngine::startLocalRenaming(const QTextCursor &textCursor,
client.setLocalRenamingCallback(std::move(renameSymbolsCallback));
auto commandLine = RefactoringCompilerOptionsBuilder::build(projectPart,
fileKind(projectPart, filePath.toString()));
fileKind(projectPart, filePath.toString()),
CppTools::getPchUsage());
commandLine.push_back(filePath.toString());
qDebug() << commandLine.join(" ");
......
......@@ -40,6 +40,7 @@
#include <cpptools/compileroptionsbuilder.h>
#include <cpptools/cppmodelmanager.h>
#include <cpptools/cppprojectfile.h>
#include <cpptools/cpptoolsreuse.h>
#include <cpptools/projectinfo.h>
#include <projectexplorer/abi.h>
......@@ -165,7 +166,8 @@ class ClangStaticAnalyzerOptionsBuilder : public CompilerOptionsBuilder
{
public:
static QStringList build(const CppTools::ProjectPart &projectPart,
CppTools::ProjectFile::Kind fileKind)
CppTools::ProjectFile::Kind fileKind,
PchUsage pchUsage)
{
ClangStaticAnalyzerOptionsBuilder optionsBuilder(projectPart);
......@@ -184,6 +186,7 @@ public:
optionsBuilder.undefineClangVersionMacrosForMsvc();
optionsBuilder.undefineCppLanguageFeatureMacrosForMsvc2015();
optionsBuilder.addHeaderPathOptions();
optionsBuilder.addPrecompiledHeaderOptions(pchUsage);
optionsBuilder.addMsvcCompatibilityVersion();
if (type != ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID)
......@@ -243,10 +246,17 @@ private:
QString includeOption() const override
{
if (m_isMsvcToolchain)
return QLatin1String("/I");
return QLatin1String("/FI");
return CompilerOptionsBuilder::includeOption();
}
QString includeDirOption() const override
{
if (m_isMsvcToolchain)
return QLatin1String("/I");
return CompilerOptionsBuilder::includeDirOption();
}
QString defineOption() const override
{
if (m_isMsvcToolchain)
......@@ -376,8 +386,9 @@ static AnalyzeUnits unitsToAnalyzeFromProjectParts(const QList<ProjectPart::Ptr>
continue;
QTC_CHECK(file.kind != ProjectFile::Unclassified);
if (ProjectFile::isSource(file.kind)) {
const CompilerOptionsBuilder::PchUsage pchUsage = CppTools::getPchUsage();
const QStringList arguments
= ClangStaticAnalyzerOptionsBuilder::build(*projectPart.data(), file.kind);
= ClangStaticAnalyzerOptionsBuilder::build(*projectPart.data(), file.kind, pchUsage);
unitsToAnalyze << AnalyzeUnit(file.path, arguments);
}
}
......
......@@ -112,7 +112,7 @@ void CompilerOptionsBuilder::enableExceptions()
void CompilerOptionsBuilder::addHeaderPathOptions()
{
typedef ProjectPartHeaderPath HeaderPath;
const QString defaultPrefix = includeOption();
const QString defaultPrefix = includeDirOption();
QStringList result;
......@@ -141,6 +141,24 @@ void CompilerOptionsBuilder::addHeaderPathOptions()
m_options.append(result);
}
void CompilerOptionsBuilder::addPrecompiledHeaderOptions(PchUsage pchUsage)
{
if (pchUsage == PchUsage::None)
return;
QStringList result;
const QString includeOptionString = includeOption();
foreach (const QString &pchFile, m_projectPart.precompiledHeaders) {
if (QFile::exists(pchFile)) {
result += includeOptionString;
result += QDir::toNativeSeparators(pchFile);
}
}
m_options.append(result);
}
void CompilerOptionsBuilder::addToolchainAndProjectDefines()
{
addDefines(m_projectPart.toolchainDefines);
......@@ -376,7 +394,7 @@ void CompilerOptionsBuilder::addDefineFloat128ForMingw()
addDefine("#define __float128 void");
}
QString CompilerOptionsBuilder::includeOption() const
QString CompilerOptionsBuilder::includeDirOption() const
{
return QLatin1String("-I");
}
......@@ -399,6 +417,11 @@ QString CompilerOptionsBuilder::undefineOption() const
return QLatin1String("-U");
}
QString CompilerOptionsBuilder::includeOption() const
{
return QLatin1String("-include");
}
static bool isGccOrMinGwToolchain(const Core::Id &toolchainType)
{
return toolchainType == ProjectExplorer::Constants::GCC_TOOLCHAIN_TYPEID
......
......@@ -34,6 +34,11 @@ namespace CppTools {
class CPPTOOLS_EXPORT CompilerOptionsBuilder
{
public:
enum class PchUsage {
None,
Use
};
CompilerOptionsBuilder(const ProjectPart &projectPart);
virtual ~CompilerOptionsBuilder() {}
......@@ -48,6 +53,7 @@ public:
virtual void addTargetTriple();
virtual void enableExceptions();
void addHeaderPathOptions();
void addPrecompiledHeaderOptions(PchUsage pchUsage);
void addToolchainAndProjectDefines();
void addDefines(const QByteArray &defineDirectives);
virtual void addLanguageOption(ProjectFile::Kind fileKind);
......@@ -67,6 +73,7 @@ protected:
virtual QString defineOption() const;
virtual QString undefineOption() const;
virtual QString includeOption() const;
virtual QString includeDirOption() const;
const ProjectPart m_projectPart;
......
......@@ -288,4 +288,12 @@ bool fileSizeExceedsLimit(const QFileInfo &fileInfo, int sizeLimitInMb)
return false;
}
CompilerOptionsBuilder::PchUsage getPchUsage()
{
const QSharedPointer<CppCodeModelSettings> cms = codeModelSettings();
if (cms->pchUsage() == CppCodeModelSettings::PchUse_None)
return CompilerOptionsBuilder::PchUsage::None;
return CompilerOptionsBuilder::PchUsage::Use;
}
} // CppTools
......@@ -29,6 +29,8 @@
#include <texteditor/texteditor.h>
#include <cpptools/compileroptionsbuilder.h>
#include <cplusplus/CppDocument.h>
QT_BEGIN_NAMESPACE
......@@ -72,6 +74,8 @@ void CPPTOOLS_EXPORT switchHeaderSource();
class CppCodeModelSettings;
QSharedPointer<CppCodeModelSettings> CPPTOOLS_EXPORT codeModelSettings();
CompilerOptionsBuilder::PchUsage CPPTOOLS_EXPORT getPchUsage();
int indexerFileSizeLimitInMb();
bool fileSizeExceedsLimit(const QFileInfo &fileInfo, int sizeLimitInMb);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment