forked from qt-creator/qt-creator
ClangCodeModel: Move compilation database creation to CppEditor
We'd like to re-use it elsewhere. Task-number: QTCREATORBUG-29529 Change-Id: Idafd53308218932f596d57ed5c8786720c6c0314 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -44,6 +44,7 @@
|
|||||||
#include <QFutureWatcher>
|
#include <QFutureWatcher>
|
||||||
|
|
||||||
using namespace Core;
|
using namespace Core;
|
||||||
|
using namespace CppEditor;
|
||||||
using namespace ProjectExplorer;
|
using namespace ProjectExplorer;
|
||||||
using namespace Utils;
|
using namespace Utils;
|
||||||
|
|
||||||
@@ -77,7 +78,7 @@ void ClangCodeModelPlugin::initialize()
|
|||||||
TaskHub::addCategory({Constants::TASK_CATEGORY_DIAGNOSTICS,
|
TaskHub::addCategory({Constants::TASK_CATEGORY_DIAGNOSTICS,
|
||||||
Tr::tr("Clang Code Model"),
|
Tr::tr("Clang Code Model"),
|
||||||
Tr::tr("C++ code issues that Clangd found in the current document.")});
|
Tr::tr("C++ code issues that Clangd found in the current document.")});
|
||||||
CppEditor::CppModelManager::activateClangCodeModel(std::make_unique<ClangModelManagerSupport>());
|
CppModelManager::activateClangCodeModel(std::make_unique<ClangModelManagerSupport>());
|
||||||
createCompilationDBAction();
|
createCompilationDBAction();
|
||||||
|
|
||||||
ActionBuilder updateStaleIndexEntries(this, "ClangCodeModel.UpdateStaleIndexEntries");
|
ActionBuilder updateStaleIndexEntries(this, "ClangCodeModel.UpdateStaleIndexEntries");
|
||||||
@@ -102,8 +103,6 @@ void ClangCodeModelPlugin::initialize()
|
|||||||
|
|
||||||
void ClangCodeModelPlugin::generateCompilationDB()
|
void ClangCodeModelPlugin::generateCompilationDB()
|
||||||
{
|
{
|
||||||
using namespace CppEditor;
|
|
||||||
|
|
||||||
Target *target = ProjectManager::startupTarget();
|
Target *target = ProjectManager::startupTarget();
|
||||||
if (!target)
|
if (!target)
|
||||||
return;
|
return;
|
||||||
@@ -169,8 +168,7 @@ void ClangCodeModelPlugin::createCompilationDBAction()
|
|||||||
"No active project.");
|
"No active project.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const CppEditor::ProjectInfo::ConstPtr projectInfo =
|
const ProjectInfo::ConstPtr projectInfo = CppModelManager::projectInfo(project);
|
||||||
CppEditor::CppModelManager::projectInfo(project);
|
|
||||||
if (!projectInfo || projectInfo->projectParts().isEmpty()) {
|
if (!projectInfo || projectInfo->projectParts().isEmpty()) {
|
||||||
MessageManager::writeDisrupting("Cannot generate compilation database: "
|
MessageManager::writeDisrupting("Cannot generate compilation database: "
|
||||||
"Project has no C/C++ project parts.");
|
"Project has no C/C++ project parts.");
|
||||||
@@ -179,7 +177,7 @@ void ClangCodeModelPlugin::createCompilationDBAction()
|
|||||||
m_generateCompilationDBAction->setEnabled(false);
|
m_generateCompilationDBAction->setEnabled(false);
|
||||||
generateCompilationDB();
|
generateCompilationDB();
|
||||||
});
|
});
|
||||||
connect(CppEditor::CppModelManager::instance(), &CppEditor::CppModelManager::projectPartsUpdated,
|
connect(CppModelManager::instance(), &CppModelManager::projectPartsUpdated,
|
||||||
this, [this](Project *project) {
|
this, [this](Project *project) {
|
||||||
if (project != ProjectManager::startupProject())
|
if (project != ProjectManager::startupProject())
|
||||||
return;
|
return;
|
||||||
|
@@ -22,6 +22,7 @@
|
|||||||
#include <cplusplus/AST.h>
|
#include <cplusplus/AST.h>
|
||||||
#include <cplusplus/ASTPath.h>
|
#include <cplusplus/ASTPath.h>
|
||||||
#include <cplusplus/Icons.h>
|
#include <cplusplus/Icons.h>
|
||||||
|
#include <cppeditor/compilationdb.h>
|
||||||
#include <cppeditor/cppcodemodelsettings.h>
|
#include <cppeditor/cppcodemodelsettings.h>
|
||||||
#include <cppeditor/cppeditorconstants.h>
|
#include <cppeditor/cppeditorconstants.h>
|
||||||
#include <cppeditor/cppeditorwidget.h>
|
#include <cppeditor/cppeditorwidget.h>
|
||||||
|
@@ -11,6 +11,7 @@
|
|||||||
#include <cppeditor/baseeditordocumentparser.h>
|
#include <cppeditor/baseeditordocumentparser.h>
|
||||||
#include <cppeditor/clangdiagnosticconfigsmodel.h>
|
#include <cppeditor/clangdiagnosticconfigsmodel.h>
|
||||||
#include <cppeditor/clangdsettings.h>
|
#include <cppeditor/clangdsettings.h>
|
||||||
|
#include <cppeditor/compilationdb.h>
|
||||||
#include <cppeditor/compileroptionsbuilder.h>
|
#include <cppeditor/compileroptionsbuilder.h>
|
||||||
#include <cppeditor/cppmodelmanager.h>
|
#include <cppeditor/cppmodelmanager.h>
|
||||||
#include <cppeditor/cpptoolsreuse.h>
|
#include <cppeditor/cpptoolsreuse.h>
|
||||||
@@ -74,79 +75,6 @@ QString diagnosticCategoryPrefixRemoved(const QString &text)
|
|||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QStringList projectPartArguments(const ProjectPart &projectPart)
|
|
||||||
{
|
|
||||||
QStringList args;
|
|
||||||
args << projectPart.compilerFilePath.toString();
|
|
||||||
args << "-c";
|
|
||||||
if (projectPart.toolchainType != ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID) {
|
|
||||||
args << "--target=" + projectPart.toolchainTargetTriple;
|
|
||||||
if (projectPart.toolchainAbi.architecture() == Abi::X86Architecture)
|
|
||||||
args << QLatin1String(projectPart.toolchainAbi.wordWidth() == 64 ? "-m64" : "-m32");
|
|
||||||
}
|
|
||||||
args << projectPart.compilerFlags;
|
|
||||||
for (const ProjectExplorer::HeaderPath &headerPath : projectPart.headerPaths) {
|
|
||||||
if (headerPath.type == ProjectExplorer::HeaderPathType::User) {
|
|
||||||
args << "-I" + headerPath.path;
|
|
||||||
} else if (headerPath.type == ProjectExplorer::HeaderPathType::System) {
|
|
||||||
args << (projectPart.toolchainType == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID
|
|
||||||
? "-I"
|
|
||||||
: "-isystem")
|
|
||||||
+ headerPath.path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (const ProjectExplorer::Macro ¯o : projectPart.projectMacros) {
|
|
||||||
args.append(QString::fromUtf8(
|
|
||||||
macro.toKeyValue(macro.type == ProjectExplorer::MacroType::Define ? "-D" : "-U")));
|
|
||||||
}
|
|
||||||
|
|
||||||
return args;
|
|
||||||
}
|
|
||||||
|
|
||||||
static QJsonObject createFileObject(const FilePath &buildDir,
|
|
||||||
const QStringList &arguments,
|
|
||||||
const ProjectPart &projectPart,
|
|
||||||
const ProjectFile &projFile,
|
|
||||||
CompilationDbPurpose purpose,
|
|
||||||
const QJsonArray &projectPartOptions,
|
|
||||||
UsePrecompiledHeaders usePch,
|
|
||||||
bool clStyle)
|
|
||||||
{
|
|
||||||
QJsonObject fileObject;
|
|
||||||
fileObject["file"] = projFile.path.toString();
|
|
||||||
QJsonArray args;
|
|
||||||
|
|
||||||
if (purpose == CompilationDbPurpose::Project) {
|
|
||||||
args = QJsonArray::fromStringList(arguments);
|
|
||||||
|
|
||||||
const ProjectFile::Kind kind = ProjectFile::classify(projFile.path.path());
|
|
||||||
if (projectPart.toolchainType == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID
|
|
||||||
|| projectPart.toolchainType == ProjectExplorer::Constants::CLANG_CL_TOOLCHAIN_TYPEID) {
|
|
||||||
if (!ProjectFile::isObjC(kind)) {
|
|
||||||
if (ProjectFile::isC(kind))
|
|
||||||
args.append("/TC");
|
|
||||||
else if (ProjectFile::isCxx(kind))
|
|
||||||
args.append("/TP");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
QStringList langOption
|
|
||||||
= createLanguageOptionGcc(projectPart.language, kind,
|
|
||||||
projectPart.languageExtensions
|
|
||||||
& LanguageExtension::ObjectiveC);
|
|
||||||
for (const QString &langOptionPart : langOption)
|
|
||||||
args.append(langOptionPart);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
args = clangOptionsForFile(projFile, projectPart, projectPartOptions, usePch, clStyle);
|
|
||||||
args.prepend("clang"); // TODO: clang-cl for MSVC targets? Does it matter at all what we put here?
|
|
||||||
}
|
|
||||||
|
|
||||||
args.append(projFile.path.toUserOutput());
|
|
||||||
fileObject["arguments"] = args;
|
|
||||||
fileObject["directory"] = buildDir.toString();
|
|
||||||
return fileObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
void generateCompilationDB(
|
void generateCompilationDB(
|
||||||
QPromise<expected_str<FilePath>> &promise,
|
QPromise<expected_str<FilePath>> &promise,
|
||||||
const QList<ProjectInfo::ConstPtr> &projectInfoList,
|
const QList<ProjectInfo::ConstPtr> &projectInfoList,
|
||||||
@@ -156,55 +84,10 @@ void generateCompilationDB(
|
|||||||
const QStringList &projectOptions,
|
const QStringList &projectOptions,
|
||||||
const FilePath &clangIncludeDir)
|
const FilePath &clangIncludeDir)
|
||||||
{
|
{
|
||||||
QTC_ASSERT(!baseDir.isEmpty(),
|
return CppEditor::generateCompilationDB(
|
||||||
promise.addResult(make_unexpected(Tr::tr("Could not retrieve build directory."))); return);
|
promise, projectInfoList, baseDir, purpose, projectOptions, [&](const ProjectPart &pp) {
|
||||||
QTC_CHECK(baseDir.ensureWritableDir());
|
return clangOptionsBuilder(pp, warningsConfig, clangIncludeDir, {});
|
||||||
QFile compileCommandsFile(baseDir.pathAppended("compile_commands.json").toFSPathString());
|
});
|
||||||
const bool fileOpened = compileCommandsFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
|
|
||||||
if (!fileOpened) {
|
|
||||||
promise.addResult(make_unexpected(Tr::tr("Could not create \"%1\": %2")
|
|
||||||
.arg(compileCommandsFile.fileName(), compileCommandsFile.errorString())));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
compileCommandsFile.write("[");
|
|
||||||
|
|
||||||
const QJsonArray jsonProjectOptions = QJsonArray::fromStringList(projectOptions);
|
|
||||||
for (const ProjectInfo::ConstPtr &projectInfo : std::as_const(projectInfoList)) {
|
|
||||||
QTC_ASSERT(projectInfo, continue);
|
|
||||||
for (ProjectPart::ConstPtr projectPart : projectInfo->projectParts()) {
|
|
||||||
QTC_ASSERT(projectPart, continue);
|
|
||||||
QStringList args;
|
|
||||||
const CompilerOptionsBuilder optionsBuilder = clangOptionsBuilder(
|
|
||||||
*projectPart, warningsConfig, clangIncludeDir, {});
|
|
||||||
QJsonArray ppOptions;
|
|
||||||
if (purpose == CompilationDbPurpose::Project) {
|
|
||||||
args = projectPartArguments(*projectPart);
|
|
||||||
} else {
|
|
||||||
ppOptions = fullProjectPartOptions(projectPartOptions(optionsBuilder),
|
|
||||||
jsonProjectOptions);
|
|
||||||
}
|
|
||||||
for (const ProjectFile &projFile : projectPart->files) {
|
|
||||||
if (promise.isCanceled())
|
|
||||||
return;
|
|
||||||
const QJsonObject json
|
|
||||||
= createFileObject(baseDir,
|
|
||||||
args,
|
|
||||||
*projectPart,
|
|
||||||
projFile,
|
|
||||||
purpose,
|
|
||||||
ppOptions,
|
|
||||||
projectInfo->settings().usePrecompiledHeaders(),
|
|
||||||
optionsBuilder.isClStyle());
|
|
||||||
if (compileCommandsFile.size() > 1)
|
|
||||||
compileCommandsFile.write(",");
|
|
||||||
compileCommandsFile.write(QJsonDocument(json).toJson(QJsonDocument::Compact));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
compileCommandsFile.write("]");
|
|
||||||
compileCommandsFile.close();
|
|
||||||
promise.addResult(FilePath::fromUserInput(compileCommandsFile.fileName()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FilePath currentCppEditorDocumentFilePath()
|
FilePath currentCppEditorDocumentFilePath()
|
||||||
@@ -266,30 +149,6 @@ QString DiagnosticTextInfo::clazyCheckName(const QString &option)
|
|||||||
return option;
|
return option;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonArray clangOptionsForFile(const ProjectFile &file, const ProjectPart &projectPart,
|
|
||||||
const QJsonArray &generalOptions, UsePrecompiledHeaders usePch,
|
|
||||||
bool clStyle)
|
|
||||||
{
|
|
||||||
CompilerOptionsBuilder optionsBuilder(projectPart);
|
|
||||||
optionsBuilder.setClStyle(clStyle);
|
|
||||||
ProjectFile::Kind fileKind = file.kind;
|
|
||||||
if (fileKind == ProjectFile::AmbiguousHeader) {
|
|
||||||
fileKind = projectPart.languageVersion <= LanguageVersion::LatestC
|
|
||||||
? ProjectFile::CHeader : ProjectFile::CXXHeader;
|
|
||||||
}
|
|
||||||
if (usePch == UsePrecompiledHeaders::Yes
|
|
||||||
&& projectPart.precompiledHeaders.contains(file.path.path())) {
|
|
||||||
usePch = UsePrecompiledHeaders::No;
|
|
||||||
}
|
|
||||||
optionsBuilder.updateFileLanguage(fileKind);
|
|
||||||
optionsBuilder.addPrecompiledHeaderOptions(usePch);
|
|
||||||
const QJsonArray specificOptions = QJsonArray::fromStringList(optionsBuilder.options());
|
|
||||||
QJsonArray fullOptions = generalOptions;
|
|
||||||
for (const QJsonValue &opt : specificOptions)
|
|
||||||
fullOptions << opt;
|
|
||||||
return fullOptions;
|
|
||||||
}
|
|
||||||
|
|
||||||
ClangDiagnosticConfig warningsConfigForProject(Project *project)
|
ClangDiagnosticConfig warningsConfigForProject(Project *project)
|
||||||
{
|
{
|
||||||
return ClangdSettings(ClangdProjectSettings(project).settings()).diagnosticConfig();
|
return ClangdSettings(ClangdProjectSettings(project).settings()).diagnosticConfig();
|
||||||
@@ -329,34 +188,5 @@ CompilerOptionsBuilder clangOptionsBuilder(const ProjectPart &projectPart,
|
|||||||
return optionsBuilder;
|
return optionsBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonArray projectPartOptions(const CppEditor::CompilerOptionsBuilder &optionsBuilder)
|
|
||||||
{
|
|
||||||
const QStringList optionsList = optionsBuilder.options();
|
|
||||||
QJsonArray optionsArray;
|
|
||||||
for (const QString &opt : optionsList) {
|
|
||||||
// These will be added later by the file-specific code, and they trigger warnings
|
|
||||||
// if they appear twice; see QTCREATORBUG-26664.
|
|
||||||
if (opt != "-TP" && opt != "-TC")
|
|
||||||
optionsArray << opt;
|
|
||||||
}
|
|
||||||
return optionsArray;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonArray fullProjectPartOptions(const CppEditor::CompilerOptionsBuilder &optionsBuilder,
|
|
||||||
const QStringList &projectOptions)
|
|
||||||
{
|
|
||||||
return fullProjectPartOptions(projectPartOptions(optionsBuilder),
|
|
||||||
QJsonArray::fromStringList(projectOptions));
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonArray fullProjectPartOptions(const QJsonArray &projectPartOptions,
|
|
||||||
const QJsonArray &projectOptions)
|
|
||||||
{
|
|
||||||
QJsonArray fullProjectPartOptions = projectPartOptions;
|
|
||||||
for (const QJsonValue &opt : projectOptions)
|
|
||||||
fullProjectPartOptions.prepend(opt);
|
|
||||||
return fullProjectPartOptions;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
} // namespace Clang
|
} // namespace Clang
|
||||||
|
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "cppeditor/compilationdb.h"
|
||||||
#include <cplusplus/Icons.h>
|
#include <cplusplus/Icons.h>
|
||||||
|
|
||||||
#include <cppeditor/projectinfo.h>
|
#include <cppeditor/projectinfo.h>
|
||||||
@@ -35,15 +36,6 @@ CppEditor::CompilerOptionsBuilder clangOptionsBuilder(
|
|||||||
const CppEditor::ClangDiagnosticConfig &warningsConfig,
|
const CppEditor::ClangDiagnosticConfig &warningsConfig,
|
||||||
const Utils::FilePath &clangIncludeDir,
|
const Utils::FilePath &clangIncludeDir,
|
||||||
const ProjectExplorer::Macros &extraMacros);
|
const ProjectExplorer::Macros &extraMacros);
|
||||||
QJsonArray projectPartOptions(const CppEditor::CompilerOptionsBuilder &optionsBuilder);
|
|
||||||
QJsonArray fullProjectPartOptions(const CppEditor::CompilerOptionsBuilder &optionsBuilder,
|
|
||||||
const QStringList &projectOptions);
|
|
||||||
QJsonArray fullProjectPartOptions(const QJsonArray &projectPartOptions,
|
|
||||||
const QJsonArray &projectOptions);
|
|
||||||
QJsonArray clangOptionsForFile(const CppEditor::ProjectFile &file,
|
|
||||||
const CppEditor::ProjectPart &projectPart,
|
|
||||||
const QJsonArray &generalOptions,
|
|
||||||
CppEditor::UsePrecompiledHeaders usePch, bool clStyle);
|
|
||||||
|
|
||||||
CppEditor::ProjectPart::ConstPtr projectPartForFile(const Utils::FilePath &filePath);
|
CppEditor::ProjectPart::ConstPtr projectPartForFile(const Utils::FilePath &filePath);
|
||||||
|
|
||||||
@@ -51,13 +43,11 @@ Utils::FilePath currentCppEditorDocumentFilePath();
|
|||||||
|
|
||||||
QString diagnosticCategoryPrefixRemoved(const QString &text);
|
QString diagnosticCategoryPrefixRemoved(const QString &text);
|
||||||
|
|
||||||
using GenerateCompilationDbResult = Utils::expected_str<Utils::FilePath>;
|
|
||||||
enum class CompilationDbPurpose { Project, CodeModel };
|
|
||||||
void generateCompilationDB(
|
void generateCompilationDB(
|
||||||
QPromise<GenerateCompilationDbResult> &promise,
|
QPromise<CppEditor::GenerateCompilationDbResult> &promise,
|
||||||
const QList<CppEditor::ProjectInfo::ConstPtr> &projectInfoList,
|
const QList<CppEditor::ProjectInfo::ConstPtr> &projectInfoList,
|
||||||
const Utils::FilePath &baseDir,
|
const Utils::FilePath &baseDir,
|
||||||
CompilationDbPurpose purpose,
|
CppEditor::CompilationDbPurpose purpose,
|
||||||
const CppEditor::ClangDiagnosticConfig &warningsConfig,
|
const CppEditor::ClangDiagnosticConfig &warningsConfig,
|
||||||
const QStringList &projectOptions,
|
const QStringList &projectOptions,
|
||||||
const Utils::FilePath &clangIncludeDir);
|
const Utils::FilePath &clangIncludeDir);
|
||||||
|
@@ -14,6 +14,7 @@ add_qtc_plugin(CppEditor
|
|||||||
clangdiagnosticconfigsmodel.cpp clangdiagnosticconfigsmodel.h
|
clangdiagnosticconfigsmodel.cpp clangdiagnosticconfigsmodel.h
|
||||||
clangdiagnosticconfigsselectionwidget.cpp clangdiagnosticconfigsselectionwidget.h
|
clangdiagnosticconfigsselectionwidget.cpp clangdiagnosticconfigsselectionwidget.h
|
||||||
clangdiagnosticconfigswidget.cpp clangdiagnosticconfigswidget.h
|
clangdiagnosticconfigswidget.cpp clangdiagnosticconfigswidget.h
|
||||||
|
compilationdb.cpp compilationdb.h
|
||||||
compileroptionsbuilder.cpp compileroptionsbuilder.h
|
compileroptionsbuilder.cpp compileroptionsbuilder.h
|
||||||
cppautocompleter.cpp cppautocompleter.h
|
cppautocompleter.cpp cppautocompleter.h
|
||||||
cppbuiltinmodelmanagersupport.cpp cppbuiltinmodelmanagersupport.h
|
cppbuiltinmodelmanagersupport.cpp cppbuiltinmodelmanagersupport.h
|
||||||
|
210
src/plugins/cppeditor/compilationdb.cpp
Normal file
210
src/plugins/cppeditor/compilationdb.cpp
Normal file
@@ -0,0 +1,210 @@
|
|||||||
|
// Copyright (C) 2024 The Qt Company Ltd.
|
||||||
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||||
|
|
||||||
|
#include "compilationdb.h"
|
||||||
|
|
||||||
|
#include "cppeditortr.h"
|
||||||
|
|
||||||
|
#include <projectexplorer/abi.h>
|
||||||
|
#include <projectexplorer/projectexplorerconstants.h>
|
||||||
|
|
||||||
|
#include <QJsonArray>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <QJsonObject>
|
||||||
|
|
||||||
|
using namespace ProjectExplorer;
|
||||||
|
namespace PEConstants = ProjectExplorer::Constants;
|
||||||
|
using namespace Utils;
|
||||||
|
|
||||||
|
namespace CppEditor {
|
||||||
|
|
||||||
|
static QStringList projectPartArguments(const ProjectPart &projectPart)
|
||||||
|
{
|
||||||
|
QStringList args;
|
||||||
|
args << projectPart.compilerFilePath.toString();
|
||||||
|
args << "-c";
|
||||||
|
if (projectPart.toolchainType != PEConstants::MSVC_TOOLCHAIN_TYPEID) {
|
||||||
|
args << "--target=" + projectPart.toolchainTargetTriple;
|
||||||
|
if (projectPart.toolchainAbi.architecture() == Abi::X86Architecture)
|
||||||
|
args << QLatin1String(projectPart.toolchainAbi.wordWidth() == 64 ? "-m64" : "-m32");
|
||||||
|
}
|
||||||
|
args << projectPart.compilerFlags;
|
||||||
|
for (const ProjectExplorer::HeaderPath &headerPath : projectPart.headerPaths) {
|
||||||
|
if (headerPath.type == HeaderPathType::User) {
|
||||||
|
args << "-I" + headerPath.path;
|
||||||
|
} else if (headerPath.type == HeaderPathType::System) {
|
||||||
|
args << (projectPart.toolchainType == PEConstants::MSVC_TOOLCHAIN_TYPEID
|
||||||
|
? "-I"
|
||||||
|
: "-isystem")
|
||||||
|
+ headerPath.path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const Macro ¯o : projectPart.projectMacros) {
|
||||||
|
args.append(
|
||||||
|
QString::fromUtf8(macro.toKeyValue(macro.type == MacroType::Define ? "-D" : "-U")));
|
||||||
|
}
|
||||||
|
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
|
static QJsonArray projectPartOptions(const CppEditor::CompilerOptionsBuilder &optionsBuilder)
|
||||||
|
{
|
||||||
|
const QStringList optionsList = optionsBuilder.options();
|
||||||
|
QJsonArray optionsArray;
|
||||||
|
for (const QString &opt : optionsList) {
|
||||||
|
// These will be added later by the file-specific code, and they trigger warnings
|
||||||
|
// if they appear twice; see QTCREATORBUG-26664.
|
||||||
|
if (opt != "-TP" && opt != "-TC")
|
||||||
|
optionsArray << opt;
|
||||||
|
}
|
||||||
|
return optionsArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
static QJsonArray fullProjectPartOptions(const QJsonArray &projectPartOptions,
|
||||||
|
const QJsonArray &projectOptions)
|
||||||
|
{
|
||||||
|
QJsonArray fullProjectPartOptions = projectPartOptions;
|
||||||
|
for (const QJsonValue &opt : projectOptions)
|
||||||
|
fullProjectPartOptions.prepend(opt);
|
||||||
|
return fullProjectPartOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonArray fullProjectPartOptions(const CppEditor::CompilerOptionsBuilder &optionsBuilder,
|
||||||
|
const QStringList &projectOptions)
|
||||||
|
{
|
||||||
|
return fullProjectPartOptions(projectPartOptions(optionsBuilder),
|
||||||
|
QJsonArray::fromStringList(projectOptions));
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonArray clangOptionsForFile(const ProjectFile &file, const ProjectPart &projectPart,
|
||||||
|
const QJsonArray &generalOptions, UsePrecompiledHeaders usePch,
|
||||||
|
bool clStyle)
|
||||||
|
{
|
||||||
|
CompilerOptionsBuilder optionsBuilder(projectPart);
|
||||||
|
optionsBuilder.setClStyle(clStyle);
|
||||||
|
ProjectFile::Kind fileKind = file.kind;
|
||||||
|
if (fileKind == ProjectFile::AmbiguousHeader) {
|
||||||
|
fileKind = projectPart.languageVersion <= LanguageVersion::LatestC
|
||||||
|
? ProjectFile::CHeader : ProjectFile::CXXHeader;
|
||||||
|
}
|
||||||
|
if (usePch == UsePrecompiledHeaders::Yes
|
||||||
|
&& projectPart.precompiledHeaders.contains(file.path.path())) {
|
||||||
|
usePch = UsePrecompiledHeaders::No;
|
||||||
|
}
|
||||||
|
optionsBuilder.updateFileLanguage(fileKind);
|
||||||
|
optionsBuilder.addPrecompiledHeaderOptions(usePch);
|
||||||
|
const QJsonArray specificOptions = QJsonArray::fromStringList(optionsBuilder.options());
|
||||||
|
QJsonArray fullOptions = generalOptions;
|
||||||
|
for (const QJsonValue &opt : specificOptions)
|
||||||
|
fullOptions << opt;
|
||||||
|
return fullOptions;
|
||||||
|
}
|
||||||
|
|
||||||
|
static QJsonObject createFileObject(const FilePath &buildDir,
|
||||||
|
const QStringList &arguments,
|
||||||
|
const ProjectPart &projectPart,
|
||||||
|
const ProjectFile &projFile,
|
||||||
|
CompilationDbPurpose purpose,
|
||||||
|
const QJsonArray &projectPartOptions,
|
||||||
|
UsePrecompiledHeaders usePch,
|
||||||
|
bool clStyle)
|
||||||
|
{
|
||||||
|
QJsonObject fileObject;
|
||||||
|
fileObject["file"] = projFile.path.toString();
|
||||||
|
QJsonArray args;
|
||||||
|
|
||||||
|
if (purpose == CompilationDbPurpose::Project) {
|
||||||
|
args = QJsonArray::fromStringList(arguments);
|
||||||
|
|
||||||
|
const ProjectFile::Kind kind = ProjectFile::classify(projFile.path.path());
|
||||||
|
if (projectPart.toolchainType == ProjectExplorer::Constants::MSVC_TOOLCHAIN_TYPEID
|
||||||
|
|| projectPart.toolchainType == ProjectExplorer::Constants::CLANG_CL_TOOLCHAIN_TYPEID) {
|
||||||
|
if (!ProjectFile::isObjC(kind)) {
|
||||||
|
if (ProjectFile::isC(kind))
|
||||||
|
args.append("/TC");
|
||||||
|
else if (ProjectFile::isCxx(kind))
|
||||||
|
args.append("/TP");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
QStringList langOption
|
||||||
|
= createLanguageOptionGcc(projectPart.language, kind,
|
||||||
|
projectPart.languageExtensions
|
||||||
|
& LanguageExtension::ObjectiveC);
|
||||||
|
for (const QString &langOptionPart : langOption)
|
||||||
|
args.append(langOptionPart);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
args = clangOptionsForFile(projFile, projectPart, projectPartOptions, usePch, clStyle);
|
||||||
|
args.prepend("clang"); // TODO: clang-cl for MSVC targets? Does it matter at all what we put here?
|
||||||
|
}
|
||||||
|
|
||||||
|
args.append(projFile.path.toUserOutput());
|
||||||
|
fileObject["arguments"] = args;
|
||||||
|
fileObject["directory"] = buildDir.toString();
|
||||||
|
return fileObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
void generateCompilationDB(
|
||||||
|
QPromise<GenerateCompilationDbResult> &promise,
|
||||||
|
const QList<ProjectInfo::ConstPtr> &projectInfoList,
|
||||||
|
const FilePath &baseDir,
|
||||||
|
CompilationDbPurpose purpose,
|
||||||
|
const QStringList &projectOptions,
|
||||||
|
const GetOptionsBuilder &getOptionsBuilder)
|
||||||
|
{
|
||||||
|
const QString fileName = "compile_commands.json";
|
||||||
|
QTC_ASSERT(
|
||||||
|
!baseDir.isEmpty(),
|
||||||
|
promise.addResult(make_unexpected(Tr::tr("Invalid location for %1.").arg(fileName)));
|
||||||
|
return);
|
||||||
|
QTC_CHECK(baseDir.ensureWritableDir());
|
||||||
|
QFile compileCommandsFile(baseDir.pathAppended(fileName).toFSPathString());
|
||||||
|
const bool fileOpened = compileCommandsFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
|
||||||
|
if (!fileOpened) {
|
||||||
|
promise.addResult(make_unexpected(
|
||||||
|
Tr::tr("Could not create \"%1\": %2")
|
||||||
|
.arg(compileCommandsFile.fileName(), compileCommandsFile.errorString())));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
compileCommandsFile.write("[");
|
||||||
|
|
||||||
|
const QJsonArray jsonProjectOptions = QJsonArray::fromStringList(projectOptions);
|
||||||
|
for (const ProjectInfo::ConstPtr &projectInfo : std::as_const(projectInfoList)) {
|
||||||
|
QTC_ASSERT(projectInfo, continue);
|
||||||
|
for (ProjectPart::ConstPtr projectPart : projectInfo->projectParts()) {
|
||||||
|
QTC_ASSERT(projectPart, continue);
|
||||||
|
QStringList args;
|
||||||
|
const CompilerOptionsBuilder optionsBuilder = getOptionsBuilder(*projectPart);
|
||||||
|
QJsonArray ppOptions;
|
||||||
|
if (purpose == CompilationDbPurpose::Project) {
|
||||||
|
args = projectPartArguments(*projectPart);
|
||||||
|
} else {
|
||||||
|
ppOptions = fullProjectPartOptions(projectPartOptions(optionsBuilder),
|
||||||
|
jsonProjectOptions);
|
||||||
|
}
|
||||||
|
for (const ProjectFile &projFile : projectPart->files) {
|
||||||
|
if (promise.isCanceled())
|
||||||
|
return;
|
||||||
|
const QJsonObject json
|
||||||
|
= createFileObject(baseDir,
|
||||||
|
args,
|
||||||
|
*projectPart,
|
||||||
|
projFile,
|
||||||
|
purpose,
|
||||||
|
ppOptions,
|
||||||
|
projectInfo->settings().usePrecompiledHeaders(),
|
||||||
|
optionsBuilder.isClStyle());
|
||||||
|
if (compileCommandsFile.size() > 1)
|
||||||
|
compileCommandsFile.write(",");
|
||||||
|
compileCommandsFile.write(QJsonDocument(json).toJson(QJsonDocument::Compact));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
compileCommandsFile.write("]");
|
||||||
|
compileCommandsFile.close();
|
||||||
|
promise.addResult(FilePath::fromUserInput(compileCommandsFile.fileName()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace CppEditor
|
43
src/plugins/cppeditor/compilationdb.h
Normal file
43
src/plugins/cppeditor/compilationdb.h
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
// Copyright (C) 2024 The Qt Company Ltd.
|
||||||
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "compileroptionsbuilder.h"
|
||||||
|
#include "cppeditor_global.h"
|
||||||
|
#include "projectinfo.h"
|
||||||
|
|
||||||
|
#include <utils/expected.h>
|
||||||
|
#include <utils/filepath.h>
|
||||||
|
|
||||||
|
#include <QPromise>
|
||||||
|
#include <QStringList>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
namespace CppEditor {
|
||||||
|
class ClangDiagnosticConfig;
|
||||||
|
|
||||||
|
using GenerateCompilationDbResult = Utils::expected_str<Utils::FilePath>;
|
||||||
|
using GetOptionsBuilder = std::function<CompilerOptionsBuilder(const ProjectPart &)>;
|
||||||
|
enum class CompilationDbPurpose { Project, CodeModel };
|
||||||
|
|
||||||
|
QJsonArray CPPEDITOR_EXPORT fullProjectPartOptions(
|
||||||
|
const CppEditor::CompilerOptionsBuilder &optionsBuilder,
|
||||||
|
const QStringList &projectOptions);
|
||||||
|
|
||||||
|
QJsonArray CPPEDITOR_EXPORT clangOptionsForFile(
|
||||||
|
const ProjectFile &file,
|
||||||
|
const ProjectPart &projectPart,
|
||||||
|
const QJsonArray &generalOptions,
|
||||||
|
UsePrecompiledHeaders usePch,
|
||||||
|
bool clStyle);
|
||||||
|
|
||||||
|
void CPPEDITOR_EXPORT generateCompilationDB(
|
||||||
|
QPromise<GenerateCompilationDbResult> &promise,
|
||||||
|
const QList<ProjectInfo::ConstPtr> &projectInfoList,
|
||||||
|
const Utils::FilePath &baseDir,
|
||||||
|
CompilationDbPurpose purpose,
|
||||||
|
const QStringList &projectOptions,
|
||||||
|
const GetOptionsBuilder &getOptionsBuilder);
|
||||||
|
|
||||||
|
} // namespace CppEditor
|
@@ -47,6 +47,8 @@ QtcPlugin {
|
|||||||
"clangdiagnosticconfigswidget.h",
|
"clangdiagnosticconfigswidget.h",
|
||||||
"clangdsettings.cpp",
|
"clangdsettings.cpp",
|
||||||
"clangdsettings.h",
|
"clangdsettings.h",
|
||||||
|
"compilationdb.cpp",
|
||||||
|
"compilationdb.h",
|
||||||
"compileroptionsbuilder.cpp",
|
"compileroptionsbuilder.cpp",
|
||||||
"compileroptionsbuilder.h",
|
"compileroptionsbuilder.h",
|
||||||
"cppautocompleter.cpp",
|
"cppautocompleter.cpp",
|
||||||
|
Reference in New Issue
Block a user