Commit 1e3f2eec authored by Marco Bubke's avatar Marco Bubke

Clang: Consolidate all ClangCompilerOptionsBuilder into one implementation

We used the same code in different but don't shared it. From a bug fixing
perspective it's actually not that smart.

Change-Id: Iaaffc883229e259ff77f95b5cf0a8f0d8b5117df
Reviewed-by: Nikolai Kosjar's avatarNikolai Kosjar <nikolai.kosjar@qt.io>
parent 11e0eaac
......@@ -31,7 +31,7 @@
#include <coreplugin/icore.h>
#include <coreplugin/idocument.h>
#include <cpptools/baseeditordocumentparser.h>
#include <cpptools/compileroptionsbuilder.h>
#include <cpptools/clangcompileroptionsbuilder.h>
#include <cpptools/cppmodelmanager.h>
#include <cpptools/editordocumenthandle.h>
#include <cpptools/projectpart.h>
......@@ -70,18 +70,7 @@ QStringList createClangOptions(const ProjectPart::Ptr &pPart, const QString &fil
return createClangOptions(pPart, fileKind);
}
static QString getResourceDir()
{
QDir dir(ICore::libexecPath()
+ QLatin1String("/clang/lib/clang/")
+ QLatin1String(CLANG_VERSION)
+ QLatin1String("/include"));
if (!dir.exists() || !QFileInfo(dir, QLatin1String("stdint.h")).exists())
dir = QDir(QLatin1String(CLANG_RESOURCE_DIR));
return dir.canonicalPath();
}
class LibClangOptionsBuilder : public CompilerOptionsBuilder
class LibClangOptionsBuilder : public ClangCompilerOptionsBuilder
{
public:
static QStringList build(const ProjectPart::Ptr &projectPart, ProjectFile::Kind fileKind)
......@@ -117,62 +106,8 @@ public:
private:
LibClangOptionsBuilder(const CppTools::ProjectPart &projectPart)
: CompilerOptionsBuilder(projectPart)
{
}
bool excludeHeaderPath(const QString &path) const override
{
if (m_projectPart.toolchainType == ProjectExplorer::Constants::CLANG_TOOLCHAIN_TYPEID) {
if (path.contains(QLatin1String("lib/gcc/i686-apple-darwin")))
return true;
}
return CompilerOptionsBuilder::excludeHeaderPath(path);
}
void addPredefinedMacrosAndHeaderPathsOptions()
{
if (m_projectPart.toolchainType == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID)
addPredefinedMacrosAndHeaderPathsOptionsForMsvc();
else
addPredefinedMacrosAndHeaderPathsOptionsForNonMsvc();
}
void addPredefinedMacrosAndHeaderPathsOptionsForMsvc()
: ClangCompilerOptionsBuilder(projectPart, CLANG_VERSION, CLANG_RESOURCE_DIR)
{
add(QLatin1String("-nostdinc"));
add(QLatin1String("-undef"));
}
void addPredefinedMacrosAndHeaderPathsOptionsForNonMsvc()
{
static const QString resourceDir = getResourceDir();
if (QTC_GUARD(!resourceDir.isEmpty())) {
add(QLatin1String("-nostdlibinc"));
add(QLatin1String("-I") + QDir::toNativeSeparators(resourceDir));
add(QLatin1String("-undef"));
}
}
void addWrappedQtHeadersIncludePath()
{
static const QString wrappedQtHeadersPath = ICore::instance()->resourcePath()
+ QLatin1String("/cplusplus/wrappedQtHeaders");
if (m_projectPart.qtVersion != ProjectPart::NoQt) {
const QString wrappedQtCoreHeaderPath = wrappedQtHeadersPath + QLatin1String("/QtCore");
add(QLatin1String("-I") + QDir::toNativeSeparators(wrappedQtHeadersPath));
add(QLatin1String("-I") + QDir::toNativeSeparators(wrappedQtCoreHeaderPath));
}
}
void addProjectConfigFileInclude()
{
if (!m_projectPart.projectConfigFile.isEmpty()) {
add(QLatin1String("-include"));
add(QDir::toNativeSeparators(m_projectPart.projectConfigFile));
}
}
void addDummyUiHeaderOnDiskIncludePath()
......@@ -181,15 +116,6 @@ private:
if (!path.isEmpty())
add(includeDirOption() + QDir::toNativeSeparators(path));
}
void addExtraOptions()
{
add(QLatin1String("-fmessage-length=0"));
add(QLatin1String("-fdiagnostics-show-note-include-stack"));
add(QLatin1String("-fmacro-backtrace-limit=0"));
add(QLatin1String("-fretain-comments-from-system-headers"));
add(QLatin1String("-ferror-limit=1000"));
}
};
/**
......
......@@ -27,12 +27,13 @@
#include "projectpartutilities.h"
#include "refactoringclient.h"
#include "refactoringcompileroptionsbuilder.h"
#include "searchinterface.h"
#include <refactoringserverinterface.h>
#include <requestsourcerangesanddiagnosticsforquerymessage.h>
#include <cpptools/clangcompileroptionsbuilder.h>
namespace ClangRefactoring {
ClangQueryProjectsFindFilter::ClangQueryProjectsFindFilter(
......@@ -112,11 +113,14 @@ Utils::SmallStringVector createCommandLine(CppTools::ProjectPart *projectPart,
const QString &documentFilePath,
CppTools::ProjectFile::Kind fileKind)
{
using ClangRefactoring::RefactoringCompilerOptionsBuilder;
using CppTools::ClangCompilerOptionsBuilder;
auto commandLine = RefactoringCompilerOptionsBuilder::build(projectPart,
fileKind,
CppTools::CompilerOptionsBuilder::PchUsage::None);
Utils::SmallStringVector commandLine{ClangCompilerOptionsBuilder::build(
projectPart,
fileKind,
CppTools::CompilerOptionsBuilder::PchUsage::None,
CLANG_VERSION,
CLANG_RESOURCE_DIR)};
commandLine.push_back(documentFilePath);
......
......@@ -3,7 +3,6 @@ INCLUDEPATH += $$PWD
HEADERS += \
$$PWD/refactoringengine.h \
$$PWD/refactoringconnectionclient.h \
$$PWD/refactoringcompileroptionsbuilder.h \
$$PWD/refactoringclient.h \
$$PWD/searchinterface.h \
$$PWD/searchhandleinterface.h \
......@@ -13,7 +12,6 @@ HEADERS += \
SOURCES += \
$$PWD/refactoringengine.cpp \
$$PWD/refactoringconnectionclient.cpp \
$$PWD/refactoringcompileroptionsbuilder.cpp \
$$PWD/refactoringclient.cpp \
$$PWD/searchinterface.cpp \
$$PWD/searchhandleinterface.cpp \
......
......@@ -27,11 +27,11 @@
#include "projectpartutilities.h"
#include <refactoringcompileroptionsbuilder.h>
#include <refactoringserverinterface.h>
#include <requestsourcelocationforrenamingmessage.h>
#include <cpptools/clangcompileroptionsbuilder.h>
#include <cpptools/cpptoolsreuse.h>
#include <QTextCursor>
......@@ -56,13 +56,18 @@ void RefactoringEngine::startLocalRenaming(const QTextCursor &textCursor,
CppTools::ProjectPart *projectPart,
RenameCallback &&renameSymbolsCallback)
{
using CppTools::ClangCompilerOptionsBuilder;
setUsable(false);
client.setLocalRenamingCallback(std::move(renameSymbolsCallback));
auto commandLine = RefactoringCompilerOptionsBuilder::build(projectPart,
fileKindInProjectPart(projectPart, filePath.toString()),
CppTools::getPchUsage());
Utils::SmallStringVector commandLine{ClangCompilerOptionsBuilder::build(
projectPart,
fileKindInProjectPart(projectPart, filePath.toString()),
CppTools::getPchUsage(),
CLANG_VERSION,
CLANG_RESOURCE_DIR)};
commandLine.push_back(filePath.toString());
......
......@@ -23,13 +23,19 @@
**
****************************************************************************/
#include "refactoringcompileroptionsbuilder.h"
#include "clangcompileroptionsbuilder.h"
namespace ClangRefactoring {
#include <coreplugin/icore.h>
namespace {
#include <projectexplorer/projectexplorerconstants.h>
QString getCreatorResourcePath()
#include <utils/qtcassert.h>
#include <QDir>
namespace CppTools {
static QString creatorResourcePath()
{
#ifndef UNIT_TESTS
return Core::ICore::instance()->resourcePath();
......@@ -38,33 +44,62 @@ QString getCreatorResourcePath()
#endif
}
QString getClangIncludeDirectory()
QStringList ClangCompilerOptionsBuilder::build(const CppTools::ProjectPart *projectPart,
CppTools::ProjectFile::Kind fileKind,
PchUsage pchUsage,
const QString &clangVersion,
const QString &clangResourceDirectory)
{
QDir dir(getCreatorResourcePath() + QLatin1String("/cplusplus/clang/") +
QLatin1String(CLANG_VERSION) + QLatin1String("/include"));
if (!dir.exists() || !QFileInfo(dir, QLatin1String("stdint.h")).exists())
dir = QDir(QLatin1String(CLANG_RESOURCE_DIR));
return dir.canonicalPath();
}
if (projectPart) {
ClangCompilerOptionsBuilder builder(*projectPart, clangVersion, clangResourceDirectory);
builder.addWordWidth();
builder.addTargetTriple();
builder.addLanguageOption(fileKind);
builder.addOptionsForLanguage(/*checkForBorlandExtensions*/ true);
builder.enableExceptions();
builder.addDefineToAvoidIncludingGccOrMinGwIntrinsics();
builder.addDefineFloat128ForMingw();
builder.addToolchainAndProjectDefines();
builder.undefineCppLanguageFeatureMacrosForMsvc2015();
builder.addPredefinedMacrosAndHeaderPathsOptions();
builder.addWrappedQtHeadersIncludePath();
builder.addPrecompiledHeaderOptions(pchUsage);
builder.addHeaderPathOptions();
builder.addProjectConfigFileInclude();
builder.addMsvcCompatibilityVersion();
builder.addExtraOptions();
return builder.options();
}
return QStringList();
}
RefactoringCompilerOptionsBuilder::RefactoringCompilerOptionsBuilder(CppTools::ProjectPart *projectPart)
: CompilerOptionsBuilder(*projectPart)
ClangCompilerOptionsBuilder::ClangCompilerOptionsBuilder(const CppTools::ProjectPart &projectPart,
const QString &clangVersion,
const QString &clangResourceDirectory)
: CompilerOptionsBuilder(projectPart),
m_clangVersion(clangVersion),
m_clangResourceDirectory(clangResourceDirectory)
{
}
bool RefactoringCompilerOptionsBuilder::excludeHeaderPath(const QString &path) const
bool ClangCompilerOptionsBuilder::excludeHeaderPath(const QString &path) const
{
if (m_projectPart.toolchainType == ProjectExplorer::Constants::CLANG_TOOLCHAIN_TYPEID) {
if (path.contains(QLatin1String("lib/gcc/i686-apple-darwin")))
if (path.contains("lib/gcc/i686-apple-darwin"))
return true;
}
return CompilerOptionsBuilder::excludeHeaderPath(path);
}
void RefactoringCompilerOptionsBuilder::addPredefinedMacrosAndHeaderPathsOptions()
void ClangCompilerOptionsBuilder::addPredefinedMacrosAndHeaderPathsOptions()
{
if (m_projectPart.toolchainType == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID)
addPredefinedMacrosAndHeaderPathsOptionsForMsvc();
......@@ -72,81 +107,59 @@ void RefactoringCompilerOptionsBuilder::addPredefinedMacrosAndHeaderPathsOptions
addPredefinedMacrosAndHeaderPathsOptionsForNonMsvc();
}
void RefactoringCompilerOptionsBuilder::addPredefinedMacrosAndHeaderPathsOptionsForMsvc()
void ClangCompilerOptionsBuilder::addPredefinedMacrosAndHeaderPathsOptionsForMsvc()
{
add(QLatin1String("-nostdinc"));
add(QLatin1String("-undef"));
add("-nostdinc");
add("-undef");
}
void RefactoringCompilerOptionsBuilder::addPredefinedMacrosAndHeaderPathsOptionsForNonMsvc()
void ClangCompilerOptionsBuilder::addPredefinedMacrosAndHeaderPathsOptionsForNonMsvc()
{
static const QString resourceDir = getClangIncludeDirectory();
if (!resourceDir.isEmpty()) {
add(QLatin1String("-nostdlibinc"));
add(QLatin1String("-I") + resourceDir);
add(QLatin1String("-undef"));
static const QString resourceDir = clangIncludeDirectory();
if (QTC_GUARD(!resourceDir.isEmpty())) {
add("-nostdlibinc");
add("-I" + QDir::toNativeSeparators(resourceDir));
add("-undef");
}
}
void RefactoringCompilerOptionsBuilder::addWrappedQtHeadersIncludePath()
void ClangCompilerOptionsBuilder::addWrappedQtHeadersIncludePath()
{
static const QString wrappedQtHeaders = getCreatorResourcePath()
+ QStringLiteral("/cplusplus/wrappedQtHeaders");
static const QString wrappedQtHeadersPath = creatorResourcePath()
+ "/cplusplus/wrappedQtHeaders";
if (m_projectPart.qtVersion != CppTools::ProjectPart::NoQt) {
add(QLatin1String("-I") + wrappedQtHeaders);
add(QLatin1String("-I") + wrappedQtHeaders + QLatin1String("/QtCore"));
const QString wrappedQtCoreHeaderPath = wrappedQtHeadersPath + "/QtCore";
add("-I" + QDir::toNativeSeparators(wrappedQtHeadersPath));
add("-I" + QDir::toNativeSeparators(wrappedQtCoreHeaderPath));
}
}
void RefactoringCompilerOptionsBuilder::addProjectConfigFileInclude()
void ClangCompilerOptionsBuilder::addProjectConfigFileInclude()
{
if (!m_projectPart.projectConfigFile.isEmpty()) {
add(QLatin1String("-include"));
add(m_projectPart.projectConfigFile);
add("-include");
add(QDir::toNativeSeparators(m_projectPart.projectConfigFile));
}
}
void RefactoringCompilerOptionsBuilder::addExtraOptions()
void ClangCompilerOptionsBuilder::addExtraOptions()
{
add(QLatin1String("-fmessage-length=0"));
add(QLatin1String("-fdiagnostics-show-note-include-stack"));
add(QLatin1String("-fmacro-backtrace-limit=0"));
add(QLatin1String("-fretain-comments-from-system-headers"));
add(QLatin1String("-ferror-limit=1000"));
add("-fmessage-length=0");
add("-fdiagnostics-show-note-include-stack");
add("-fmacro-backtrace-limit=0");
add("-fretain-comments-from-system-headers");
add("-ferror-limit=1000");
}
Utils::SmallStringVector RefactoringCompilerOptionsBuilder::build(CppTools::ProjectPart *projectPart,
CppTools::ProjectFile::Kind fileKind,
PchUsage pchUsage)
QString ClangCompilerOptionsBuilder::clangIncludeDirectory() const
{
if (projectPart == nullptr)
return Utils::SmallStringVector();
RefactoringCompilerOptionsBuilder optionsBuilder(projectPart);
optionsBuilder.addWordWidth();
optionsBuilder.addTargetTriple();
optionsBuilder.addLanguageOption(fileKind);
optionsBuilder.addOptionsForLanguage(/*checkForBorlandExtensions*/ true);
optionsBuilder.enableExceptions();
QDir dir(creatorResourcePath() + "/clang/lib/clang/" + m_clangVersion + "/include");
optionsBuilder.addDefineFloat128ForMingw();
optionsBuilder.addDefineToAvoidIncludingGccOrMinGwIntrinsics();
optionsBuilder.addToolchainAndProjectDefines();
optionsBuilder.undefineCppLanguageFeatureMacrosForMsvc2015();
if (!dir.exists() || !QFileInfo(dir, "stdint.h").exists())
dir = QDir(m_clangResourceDirectory);
optionsBuilder.addPredefinedMacrosAndHeaderPathsOptions();
optionsBuilder.addWrappedQtHeadersIncludePath();
optionsBuilder.addHeaderPathOptions();
optionsBuilder.addPrecompiledHeaderOptions(pchUsage);
optionsBuilder.addProjectConfigFileInclude();
optionsBuilder.addMsvcCompatibilityVersion();
optionsBuilder.addExtraOptions();
return Utils::SmallStringVector(optionsBuilder.options());
return dir.canonicalPath();
}
} // namespace ClangRefactoring
} // namespace CppTools
......@@ -25,35 +25,46 @@
#pragma once
#include <cpptools/compileroptionsbuilder.h>
#include <utils/smallstringvector.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <coreplugin/icore.h>
#include "cpptools_global.h"
#include <QDir>
#include <cpptools/compileroptionsbuilder.h>
namespace ClangRefactoring {
namespace CppTools {
class RefactoringCompilerOptionsBuilder : public CppTools::CompilerOptionsBuilder
class CPPTOOLS_EXPORT ClangCompilerOptionsBuilder : public CompilerOptionsBuilder
{
public:
static Utils::SmallStringVector build(CppTools::ProjectPart *projectPart,
CppTools::ProjectFile::Kind fileKind,
PchUsage pchUsage);
static QStringList build(const ProjectPart *projectPart,
ProjectFile::Kind fileKind,
PchUsage pchUsage,
const QString &clangVersion,
const QString &clangResourceDirectory);
private:
RefactoringCompilerOptionsBuilder(CppTools::ProjectPart *projectPart);
protected:
ClangCompilerOptionsBuilder(const ProjectPart &projectPart,
const QString &clangVersion,
const QString &clangResourceDirectory);
bool excludeHeaderPath(const QString &path) const override;
void addPredefinedMacrosAndHeaderPathsOptions();
void addPredefinedMacrosAndHeaderPathsOptionsForMsvc();
void addPredefinedMacrosAndHeaderPathsOptionsForNonMsvc();
void addWrappedQtHeadersIncludePath();
void addProjectConfigFileInclude();
void addExtraOptions();
private:
QString clangIncludeDirectory() const;
private:
QString m_clangVersion;
QString m_clangResourceDirectory;
};
} // namespace ClangRefactoring
} // namespace CppTools
......@@ -79,6 +79,7 @@ HEADERS += \
compileroptionsbuilder.h \
refactoringengineinterface.h \
cppprojectfilecategorizer.h \
clangcompileroptionsbuilder.h \
cppprojectinterface.h \
cppbaseprojectpartbuilder.h \
......@@ -155,6 +156,7 @@ SOURCES += \
compileroptionsbuilder.cpp \
cppprojectfilecategorizer.cpp \
cppbaseprojectpartbuilder.cpp \
clangcompileroptionsbuilder.cpp
FORMS += \
clangdiagnosticconfigswidget.ui \
......
......@@ -107,6 +107,7 @@ Project {
"cppprojectfilecategorizer.cpp", "cppprojectfilecategorizer.h",
"cppprojectinterface.h",
"cppbaseprojectpartbuilder.cpp", "cppbaseprojectpartbuilder.h",
"clangcompileroptionsbuilder.cpp", "clangcompileroptionsbuilder.h",
]
Group {
......
......@@ -14,6 +14,7 @@ HEADERS += \
$$PWD/projectpart.h \
$$PWD/compileroptionsbuilder.h \
$$PWD/cppprojectfilecategorizer.h \
$$PWD/clangcompileroptionsbuilder.h \
$$PWD/cppbaseprojectpartbuilder.h \
$$PWD/projectinfo.h \
$$PWD/cppprojectinterface.h \
......@@ -25,5 +26,6 @@ SOURCES += \
$$PWD/projectpart.cpp \
$$PWD/compileroptionsbuilder.cpp \
$$PWD/cppprojectfilecategorizer.cpp \
$$PWD/clangcompileroptionsbuilder.cpp \
$$PWD/cppbaseprojectpartbuilder.cpp \
$$PWD/projectinfo.cpp \
......@@ -31,11 +31,11 @@
#include <clangqueryprojectsfindfilter.h>
#include <refactoringclient.h>
#include <refactoringcompileroptionsbuilder.h>
#include <requestsourcelocationforrenamingmessage.h>
#include <requestsourcerangesanddiagnosticsforquerymessage.h>
#include <cpptools/clangcompileroptionsbuilder.h>
#include <cpptools/projectpart.h>
namespace {
......@@ -48,7 +48,7 @@ using ::testing::ReturnNew;
using ::testing::DefaultValue;
using ::testing::ByMove;
using ClangRefactoring::RefactoringCompilerOptionsBuilder;
using CppTools::ClangCompilerOptionsBuilder;
class ClangQueryProjectFindFilter : public ::testing::Test
{
......@@ -178,9 +178,12 @@ createCommandLines(const std::vector<CppTools::ProjectPart::Ptr> &projectParts)
for (const CppTools::ProjectPart::Ptr &projectPart : projectParts) {
for (const CppTools::ProjectFile &projectFile : projectPart->files) {
auto commandLine = RefactoringCompilerOptionsBuilder::build(projectPart.data(),
projectFile.kind,
CppTools::CompilerOptionsBuilder::PchUsage::None);
Utils::SmallStringVector commandLine{ClangCompilerOptionsBuilder::build(
projectPart.data(),
projectFile.kind,
ClangCompilerOptionsBuilder::PchUsage::None,
CLANG_VERSION,
CLANG_RESOURCE_DIR)};
commandLine.emplace_back(projectFile.path);
commandLines.push_back(commandLine);
}
......
......@@ -28,13 +28,13 @@
#include "mocksearchhandle.h"
#include <refactoringclient.h>
#include <refactoringcompileroptionsbuilder.h>
#include <refactoringengine.h>
#include <refactoringconnectionclient.h>
#include <sourcelocationsforrenamingmessage.h>
#include <sourcerangesanddiagnosticsforquerymessage.h>
#include <cpptools/clangcompileroptionsbuilder.h>
#include <cpptools/projectpart.h>
#include <utils/smallstringvector.h>
......@@ -44,7 +44,8 @@
namespace {
using ClangRefactoring::RefactoringCompilerOptionsBuilder;
using CppTools::ClangCompilerOptionsBuilder;
using ClangRefactoring::RefactoringEngine;
using ClangBackEnd::SourceLocationsForRenamingMessage;
......@@ -251,9 +252,12 @@ void RefactoringClient::SetUp()
projectPart = CppTools::ProjectPart::Ptr(new CppTools::ProjectPart);
projectPart->files.push_back(projectFile);
commandLine = RefactoringCompilerOptionsBuilder::build(projectPart.data(),
projectFile.kind,
RefactoringCompilerOptionsBuilder::PchUsage::None);
commandLine = Utils::SmallStringVector{ClangCompilerOptionsBuilder::build(
projectPart.data(),
projectFile.kind,
ClangCompilerOptionsBuilder::PchUsage::None,
CLANG_VERSION,
CLANG_RESOURCE_DIR)};
client.setSearchHandle(&mockSearchHandle);
client.setExpectedResultCount(1);
......
......@@ -28,7 +28,6 @@
#include "mockrefactoringserver.h"
#include "mockrefactoringclient.h"
#include <refactoringcompileroptionsbuilder.h>
#include <refactoringengine.h>
#include <requestsourcelocationforrenamingmessage.h>
......@@ -36,8 +35,11 @@
#include <sourcelocationsforrenamingmessage.h>
#include <sourcerangesanddiagnosticsforquerymessage.h>
#include <cpptools/clangcompileroptionsbuilder.h>
#include <cpptools/projectpart.h>
#include <utils/smallstringvector.h>
#include <QTextCursor>
#include <QTextDocument>
......@@ -45,7 +47,7 @@ namespace {
using testing::_;
using ClangRefactoring::RefactoringCompilerOptionsBuilder;
using CppTools::ClangCompilerOptionsBuilder;
using ClangBackEnd::RequestSourceLocationsForRenamingMessage;
......@@ -128,9 +130,12 @@ void RefactoringEngine::SetUp()
projectPart = CppTools::ProjectPart::Ptr(new CppTools::ProjectPart);
projectPart->files.push_back(projectFile);
commandLine = RefactoringCompilerOptionsBuilder::build(projectPart.data(),
projectFile.kind,
RefactoringCompilerOptionsBuilder::PchUsage::None);
commandLine = Utils::SmallStringVector(ClangCompilerOptionsBuilder::build(
projectPart.data(),
projectFile.kind,
CppTools::CompilerOptionsBuilder::PchUsage::None,
CLANG_VERSION,
CLANG_RESOURCE_DIR));
commandLine.push_back(qStringFilePath);
}
......
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