Commit 68a49c79 authored by Ivan Donchevskii's avatar Ivan Donchevskii

Clang: Unify compiler options builders

Make build command the same for all builders.
Minimize differences.

Change-Id: I1cfe5071b3afb4944ed178fff1e57d3aee45d8a9
Reviewed-by: Nikolai Kosjar's avatarNikolai Kosjar <nikolai.kosjar@qt.io>
parent 736e568d
......@@ -223,8 +223,6 @@ def deploy_libclang(install_dir, llvm_install_dir, chrpath_bin):
os.path.join(install_dir, 'bin')))
deployinfo.append((os.path.join(llvm_install_dir, 'bin', 'clang.exe'),
clangbindirtarget))
deployinfo.append((os.path.join(llvm_install_dir, 'bin', 'clang-cl.exe'),
clangbindirtarget))
resourcetarget = os.path.join(clanglibdirtarget, 'clang')
else:
libsources = glob(os.path.join(llvm_install_dir, 'lib', 'libclang.so*'))
......
......@@ -70,46 +70,21 @@ QStringList createClangOptions(const ProjectPart::Ptr &pPart, const QString &fil
return createClangOptions(pPart, fileKind);
}
class LibClangOptionsBuilder : public ClangCompilerOptionsBuilder
class LibClangOptionsBuilder final : public ClangCompilerOptionsBuilder
{
public:
static QStringList build(const ProjectPart::Ptr &projectPart, ProjectFile::Kind fileKind)
LibClangOptionsBuilder(const ProjectPart &projectPart)
: ClangCompilerOptionsBuilder(projectPart, CLANG_VERSION, CLANG_RESOURCE_DIR)
{
if (projectPart.isNull())
return QStringList();
LibClangOptionsBuilder optionsBuilder(*projectPart.data());
optionsBuilder.addWordWidth();
optionsBuilder.addTargetTriple();
optionsBuilder.addLanguageOption(fileKind);
optionsBuilder.addOptionsForLanguage(/*checkForBorlandExtensions*/ true);
optionsBuilder.enableExceptions();
optionsBuilder.addDefineToAvoidIncludingGccOrMinGwIntrinsics();
optionsBuilder.addDefineFloat128ForMingw();
optionsBuilder.addToolchainAndProjectDefines();
optionsBuilder.undefineCppLanguageFeatureMacrosForMsvc2015();
optionsBuilder.addPredefinedMacrosAndHeaderPathsOptions();
optionsBuilder.addWrappedQtHeadersIncludePath();
optionsBuilder.addHeaderPathOptions();
optionsBuilder.addDummyUiHeaderOnDiskIncludePath();
optionsBuilder.addProjectConfigFileInclude();
optionsBuilder.addMsvcCompatibilityVersion();
optionsBuilder.addExtraOptions();
return optionsBuilder.options();
}
private:
LibClangOptionsBuilder(const CppTools::ProjectPart &projectPart)
: ClangCompilerOptionsBuilder(projectPart, CLANG_VERSION, CLANG_RESOURCE_DIR)
void addExtraOptions() final
{
addDummyUiHeaderOnDiskIncludePath();
ClangCompilerOptionsBuilder::addExtraOptions();
}
private:
void addDummyUiHeaderOnDiskIncludePath()
{
const QString path = ModelManagerSupportClang::instance()->dummyUiHeaderOnDiskDirPath();
......@@ -125,7 +100,9 @@ private:
*/
QStringList createClangOptions(const ProjectPart::Ptr &pPart, ProjectFile::Kind fileKind)
{
return LibClangOptionsBuilder::build(pPart, fileKind);
if (!pPart)
return QStringList();
return LibClangOptionsBuilder(*pPart).build(fileKind, CompilerOptionsBuilder::PchUsage::None);
}
ProjectPart::Ptr projectPartForFile(const QString &filePath)
......
......@@ -108,35 +108,9 @@ HeaderAndSources ProjectUpdater::headerAndSourcesFromProjectPart(
QStringList ProjectUpdater::compilerArguments(CppTools::ProjectPart *projectPart)
{
using CppTools::ClangCompilerOptionsBuilder;
ClangCompilerOptionsBuilder builder(*projectPart, CLANG_VERSION, CLANG_RESOURCE_DIR);
builder.addWordWidth();
builder.addTargetTriple();
builder.addLanguageOption(CppTools::ProjectFile::CXXHeader);
builder.addOptionsForLanguage(/*checkForBorlandExtensions*/ true);
builder.enableExceptions();
builder.addDefineToAvoidIncludingGccOrMinGwIntrinsics();
builder.addDefineFloat128ForMingw();
builder.addToolchainAndProjectDefines();
builder.undefineCppLanguageFeatureMacrosForMsvc2015();
builder.addPredefinedMacrosAndHeaderPathsOptions();
builder.addWrappedQtHeadersIncludePath();
builder.addPrecompiledHeaderOptions(ClangCompilerOptionsBuilder::PchUsage::None);
builder.addHeaderPathOptions();
builder.addProjectConfigFileInclude();
builder.addMsvcCompatibilityVersion();
builder.add("-fmessage-length=0");
builder.add("-fmacro-backtrace-limit=0");
builder.add("-w");
builder.add("-ferror-limit=100000");
return builder.options();
using ClangCOBuilder = CppTools::ClangCompilerOptionsBuilder;
ClangCOBuilder builder(*projectPart, CLANG_VERSION, CLANG_RESOURCE_DIR);
return builder.build(CppTools::ProjectFile::CXXHeader, ClangCOBuilder::PchUsage::None);
}
ClangBackEnd::V2::ProjectPartContainer ProjectUpdater::toProjectPartContainer(
......
......@@ -155,31 +155,8 @@ Utils::SmallStringVector ClangQueryProjectsFindFilter::compilerArguments(CppTool
ClangCompilerOptionsBuilder builder(*projectPart, CLANG_VERSION, CLANG_RESOURCE_DIR);
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(ClangCompilerOptionsBuilder::PchUsage::None);
builder.addHeaderPathOptions();
builder.addProjectConfigFileInclude();
builder.addMsvcCompatibilityVersion();
builder.add("-fmessage-length=0");
builder.add("-fmacro-backtrace-limit=0");
builder.add("-w");
builder.add("-ferror-limit=1000000");
return Utils::SmallStringVector(builder.options());
return Utils::SmallStringVector(builder.build(fileKind,
ClangCompilerOptionsBuilder::PchUsage::None));
}
QWidget *ClangQueryProjectsFindFilter::widget() const
......
......@@ -64,12 +64,10 @@ void RefactoringEngine::startLocalRenaming(const CppTools::CursorInEditor &data,
QString filePath = data.filePath().toString();
QTextCursor textCursor = data.cursor();
Utils::SmallStringVector commandLine{ClangCompilerOptionsBuilder::build(
projectPart,
ClangCompilerOptionsBuilder clangCOBuilder{*projectPart, CLANG_VERSION, CLANG_RESOURCE_DIR};
Utils::SmallStringVector commandLine{clangCOBuilder.build(
fileKindInProjectPart(projectPart, filePath),
CppTools::getPchUsage(),
CLANG_VERSION,
CLANG_RESOURCE_DIR)};
CppTools::getPchUsage())};
commandLine.push_back(filePath);
......
......@@ -167,7 +167,7 @@ static QList<Target *> validTargets(Project *project)
const ToolChain * const toolchain = ToolChainKitInformation::toolChain(kit, ProjectExplorer::Constants::CXX_LANGUAGE_ID);
QTC_ASSERT(toolchain, return false);
bool hasClangExecutable;
clangExecutableFromSettings(toolchain->typeId(), &hasClangExecutable);
clangExecutableFromSettings(&hasClangExecutable);
if (!hasClangExecutable) {
qWarning("Project \"%s\": Skipping target \"%s\" since no suitable clang was found for the toolchain.",
qPrintable(projectFileName),
......
......@@ -58,7 +58,7 @@ void ClangStaticAnalyzerUnitTests::initTestCase()
if (!toolchain)
QSKIP("This test requires that there is a kit with a toolchain.");
bool hasClangExecutable;
clangExecutableFromSettings(toolchain->typeId(), &hasClangExecutable);
clangExecutableFromSettings(&hasClangExecutable);
if (!hasClangExecutable)
QSKIP("No clang suitable for analyzing found");
......
......@@ -50,7 +50,7 @@ static bool isFileExecutable(const QString &executablePath)
namespace ClangStaticAnalyzer {
namespace Internal {
QString clangExecutableFromSettings(Core::Id toolchainType, bool *isValid)
QString clangExecutableFromSettings(bool *isValid)
{
QString executable = ClangStaticAnalyzerSettings::instance()->clangExecutable();
if (executable.isEmpty()) {
......@@ -62,14 +62,6 @@ QString clangExecutableFromSettings(Core::Id toolchainType, bool *isValid)
const Qt::CaseSensitivity caseSensitivity = Utils::HostOsInfo::fileNameCaseSensitivity();
const bool hasSuffix = executable.endsWith(hostExeSuffix, caseSensitivity);
if (toolchainType == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID) {
if (hasSuffix)
executable.chop(hostExeSuffix.length());
executable.append(QLatin1String("-cl"));
if (hasSuffix)
executable.append(hostExeSuffix);
}
const QFileInfo fileInfo = QFileInfo(executable);
if (fileInfo.isAbsolute()) {
if (!hasSuffix)
......
......@@ -41,7 +41,7 @@ namespace Internal {
bool isClangExecutableUsable(const QString &filePath, QString *errorMessage = 0);
QString clangExecutableFromSettings(Core::Id toolchainType, bool *isValid);
QString clangExecutableFromSettings(bool *isValid);
QString createFullLocationString(const Debugger::DiagnosticLocation &location);
......
......@@ -26,7 +26,6 @@
#include "clangcompileroptionsbuilder.h"
#include <coreplugin/icore.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <utils/qtcassert.h>
......@@ -38,46 +37,46 @@ namespace CppTools {
static QString creatorResourcePath()
{
#ifndef UNIT_TESTS
return Core::ICore::instance()->libexecPath();
return Core::ICore::instance()->resourcePath();
#else
return QString();
#endif
}
QStringList ClangCompilerOptionsBuilder::build(const CppTools::ProjectPart *projectPart,
CppTools::ProjectFile::Kind fileKind,
PchUsage pchUsage,
const QString &clangVersion,
const QString &clangResourceDirectory)
static QString creatorLibexecPath()
{
if (projectPart) {
ClangCompilerOptionsBuilder builder(*projectPart, clangVersion, clangResourceDirectory);
builder.addWordWidth();
builder.addTargetTriple();
builder.addLanguageOption(fileKind);
builder.addOptionsForLanguage(/*checkForBorlandExtensions*/ true);
builder.enableExceptions();
#ifndef UNIT_TESTS
return Core::ICore::instance()->libexecPath();
#else
return QString();
#endif
}
builder.addDefineToAvoidIncludingGccOrMinGwIntrinsics();
builder.addDefineFloat128ForMingw();
builder.addToolchainAndProjectDefines();
builder.undefineCppLanguageFeatureMacrosForMsvc2015();
QStringList ClangCompilerOptionsBuilder::build(CppTools::ProjectFile::Kind fileKind,
PchUsage pchUsage)
{
addWordWidth();
addTargetTriple();
addLanguageOption(fileKind);
addOptionsForLanguage(/*checkForBorlandExtensions*/ true);
enableExceptions();
builder.addPredefinedMacrosAndHeaderPathsOptions();
builder.addWrappedQtHeadersIncludePath();
builder.addPrecompiledHeaderOptions(pchUsage);
builder.addHeaderPathOptions();
builder.addProjectConfigFileInclude();
addDefineFloat128ForMingw();
addToolchainAndProjectMacros();
undefineClangVersionMacrosForMsvc();
undefineCppLanguageFeatureMacrosForMsvc2015();
builder.addMsvcCompatibilityVersion();
addPredefinedHeaderPathsOptions();
addWrappedQtHeadersIncludePath();
addPrecompiledHeaderOptions(pchUsage);
addHeaderPathOptions();
addProjectConfigFileInclude();
builder.addExtraOptions();
addMsvcCompatibilityVersion();
return builder.options();
}
addExtraOptions();
return QStringList();
return options();
}
ClangCompilerOptionsBuilder::ClangCompilerOptionsBuilder(const CppTools::ProjectPart &projectPart,
......@@ -91,47 +90,35 @@ ClangCompilerOptionsBuilder::ClangCompilerOptionsBuilder(const CppTools::Project
bool ClangCompilerOptionsBuilder::excludeHeaderPath(const QString &path) const
{
if (m_projectPart.toolchainType == ProjectExplorer::Constants::CLANG_TOOLCHAIN_TYPEID) {
if (path.contains("lib/gcc/i686-apple-darwin"))
return true;
if (m_projectPart.toolchainType == ProjectExplorer::Constants::CLANG_TOOLCHAIN_TYPEID
&& path.contains("lib/gcc/i686-apple-darwin")) {
return true;
}
return CompilerOptionsBuilder::excludeHeaderPath(path);
}
void ClangCompilerOptionsBuilder::addPredefinedMacrosAndHeaderPathsOptions()
void ClangCompilerOptionsBuilder::addPredefinedHeaderPathsOptions()
{
if (m_projectPart.toolchainType == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID)
addPredefinedMacrosAndHeaderPathsOptionsForMsvc();
else
addPredefinedMacrosAndHeaderPathsOptionsForNonMsvc();
}
void ClangCompilerOptionsBuilder::addPredefinedMacrosAndHeaderPathsOptionsForMsvc()
{
add("-nostdinc");
add("-undef");
}
add("-nostdinc");
add("-nostdlibinc");
void ClangCompilerOptionsBuilder::addPredefinedMacrosAndHeaderPathsOptionsForNonMsvc()
{
static const QString resourceDir = clangIncludeDirectory();
if (QTC_GUARD(!resourceDir.isEmpty())) {
add("-nostdlibinc");
add("-I" + QDir::toNativeSeparators(resourceDir));
add("-undef");
}
if (m_projectPart.toolchainType != ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID)
add(includeDirOption() + clangIncludeDirectory());
}
void ClangCompilerOptionsBuilder::addWrappedQtHeadersIncludePath()
{
static const QString wrappedQtHeadersPath = creatorResourcePath()
+ "/cplusplus/wrappedQtHeaders";
static const QString resourcePath = creatorResourcePath();
static QString wrappedQtHeadersPath = resourcePath + "/cplusplus/wrappedQtHeaders";
QDir dir(wrappedQtHeadersPath);
QTC_ASSERT(QDir(wrappedQtHeadersPath).exists(), return;);
if (m_projectPart.qtVersion != CppTools::ProjectPart::NoQt) {
const QString wrappedQtCoreHeaderPath = wrappedQtHeadersPath + "/QtCore";
add("-I" + QDir::toNativeSeparators(wrappedQtHeadersPath));
add("-I" + QDir::toNativeSeparators(wrappedQtCoreHeaderPath));
add(includeDirOption() + QDir::toNativeSeparators(wrappedQtHeadersPath));
add(includeDirOption() + QDir::toNativeSeparators(wrappedQtCoreHeaderPath));
}
}
......@@ -154,12 +141,26 @@ void ClangCompilerOptionsBuilder::addExtraOptions()
QString ClangCompilerOptionsBuilder::clangIncludeDirectory() const
{
QDir dir(creatorResourcePath() + "/clang/lib/clang/" + m_clangVersion + "/include");
QDir dir(creatorLibexecPath() + "/clang/lib/clang/" + m_clangVersion + "/include");
if (!dir.exists() || !QFileInfo(dir, "stdint.h").exists())
dir = QDir(m_clangResourceDirectory);
return QDir::toNativeSeparators(dir.canonicalPath());
}
return dir.canonicalPath();
void ClangCompilerOptionsBuilder::undefineClangVersionMacrosForMsvc()
{
if (m_projectPart.toolchainType == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID) {
static QStringList macroNames {
"__clang__",
"__clang_major__",
"__clang_minor__",
"__clang_patchlevel__",
"__clang_version__"
};
foreach (const QString &macroName, macroNames)
add(undefineOption() + macroName);
}
}
} // namespace CppTools
......@@ -34,33 +34,24 @@ namespace CppTools {
class CPPTOOLS_EXPORT ClangCompilerOptionsBuilder : public CompilerOptionsBuilder
{
public:
static QStringList build(const ProjectPart *projectPart,
ProjectFile::Kind fileKind,
PchUsage pchUsage,
const QString &clangVersion,
const QString &clangResourceDirectory);
QStringList build(ProjectFile::Kind fileKind,
PchUsage pchUsage);
ClangCompilerOptionsBuilder(const ProjectPart &projectPart,
const QString &clangVersion,
const QString &clangResourceDirectory);
const QString &clangVersion = QString(),
const QString &clangResourceDirectory = QString());
bool excludeHeaderPath(const QString &path) const override;
void addPredefinedMacrosAndHeaderPathsOptions();
void addPredefinedMacrosAndHeaderPathsOptionsForMsvc();
void addPredefinedMacrosAndHeaderPathsOptionsForNonMsvc();
virtual void addPredefinedHeaderPathsOptions();
virtual void addExtraOptions();
void addWrappedQtHeadersIncludePath();
bool excludeHeaderPath(const QString &path) const override;
virtual void addWrappedQtHeadersIncludePath();
void addProjectConfigFileInclude();
void addExtraOptions();
void undefineClangVersionMacrosForMsvc();
private:
QString clangIncludeDirectory() const;
private:
QString m_clangVersion;
QString m_clangResourceDirectory;
};
......
......@@ -125,7 +125,7 @@ void CompilerOptionsBuilder::addPrecompiledHeaderOptions(PchUsage pchUsage)
m_options.append(result);
}
void CompilerOptionsBuilder::addToolchainAndProjectDefines()
void CompilerOptionsBuilder::addToolchainAndProjectMacros()
{
addMacros(m_projectPart.toolChainMacros);
addMacros(m_projectPart.projectMacros);
......@@ -258,21 +258,6 @@ void CompilerOptionsBuilder::addOptionsForLanguage(bool checkForBorlandExtension
m_options.append(opts);
}
void CompilerOptionsBuilder::addDefineToAvoidIncludingGccOrMinGwIntrinsics()
{
// In gcc headers, lots of built-ins are referenced that clang does not understand.
// Therefore, prevent the inclusion of the header that references them. Of course, this
// will break if code actually requires stuff from there, but that should be the less common
// case.
const Core::Id type = m_projectPart.toolchainType;
if (type == ProjectExplorer::Constants::MINGW_TOOLCHAIN_TYPEID
|| type == ProjectExplorer::Constants::GCC_TOOLCHAIN_TYPEID) {
addDefine({"_X86INTRIN_H_INCLUDED"});
addDefine({"BOOST_UUID_NO_SIMD"});
}
}
static QByteArray toMsCompatibilityVersionFormat(const QByteArray &mscFullVer)
{
return mscFullVer.left(2)
......
......@@ -54,13 +54,11 @@ public:
virtual void enableExceptions();
void addHeaderPathOptions();
void addPrecompiledHeaderOptions(PchUsage pchUsage);
void addToolchainAndProjectDefines();
void addToolchainAndProjectMacros();
void addMacros(const ProjectExplorer::Macros &macros);
virtual void addLanguageOption(ProjectFile::Kind fileKind);
virtual void addOptionsForLanguage(bool checkForBorlandExtensions = true);
void addDefineToAvoidIncludingGccOrMinGwIntrinsics();
void addMsvcCompatibilityVersion();
void undefineCppLanguageFeatureMacrosForMsvc2015();
......
......@@ -129,12 +129,12 @@ void RefactoringEngine::SetUp()
projectPart = CppTools::ProjectPart::Ptr(new CppTools::ProjectPart);
projectPart->files.push_back(projectFile);
commandLine = Utils::SmallStringVector(ClangCompilerOptionsBuilder::build(
projectPart.data(),
projectFile.kind,
CppTools::CompilerOptionsBuilder::PchUsage::None,
ClangCompilerOptionsBuilder clangCOBuilder(*projectPart,
CLANG_VERSION,
CLANG_RESOURCE_DIR));
CLANG_RESOURCE_DIR);
commandLine = Utils::SmallStringVector(clangCOBuilder.build(
projectFile.kind,
CppTools::CompilerOptionsBuilder::PchUsage::None));
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