ClangCodeModel: Use more suitable location for compile_commands.json

... for clangd.
Putting it in the build directory seems sensible in principle, but that
can be problematic for in-source builds. So introduce another level of
nesting to prevent conflicts.

Fixes: QTCREATORBUG-26431
Change-Id: Id66aa0852d206695f2fc2ec42292b1cecefe2b59
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2021-10-15 16:24:25 +02:00
parent 71e6e30dbf
commit 19119b9533
4 changed files with 10 additions and 13 deletions

View File

@@ -82,7 +82,7 @@ void ClangCodeModelPlugin::generateCompilationDB()
QFuture<GenerateCompilationDbResult> task QFuture<GenerateCompilationDbResult> task
= QtConcurrent::run(&Internal::generateCompilationDB, projectInfo, = QtConcurrent::run(&Internal::generateCompilationDB, projectInfo,
CompilationDbPurpose::Project, projectInfo->buildRoot(), CompilationDbPurpose::Project,
warningsConfigForProject(target->project()), warningsConfigForProject(target->project()),
optionsForProject(target->project())); optionsForProject(target->project()));
Core::ProgressManager::addTask(task, tr("Generating Compilation DB"), "generate compilation db"); Core::ProgressManager::addTask(task, tr("Generating Compilation DB"), "generate compilation db");

View File

@@ -286,7 +286,7 @@ void ClangModelManagerSupport::updateLanguageClient(
if (const ProjectExplorer::Target * const target = project->activeTarget()) { if (const ProjectExplorer::Target * const target = project->activeTarget()) {
if (const ProjectExplorer::BuildConfiguration * const bc if (const ProjectExplorer::BuildConfiguration * const bc
= target->activeBuildConfiguration()) { = target->activeBuildConfiguration()) {
return bc->buildDirectory(); return bc->buildDirectory() / ".qtc_clangd";
} }
} }
return Utils::FilePath(); return Utils::FilePath();
@@ -363,7 +363,7 @@ void ClangModelManagerSupport::updateLanguageClient(
}); });
}); });
auto future = Utils::runAsync(&Internal::generateCompilationDB, projectInfo, auto future = Utils::runAsync(&Internal::generateCompilationDB, projectInfo, jsonDbDir,
CompilationDbPurpose::CodeModel, CompilationDbPurpose::CodeModel,
warningsConfigForProject(project), warningsConfigForProject(project),
optionsForProject(project)); optionsForProject(project));

View File

@@ -372,18 +372,15 @@ static QJsonObject createFileObject(const FilePath &buildDir,
} }
GenerateCompilationDbResult generateCompilationDB(const CppEditor::ProjectInfo::ConstPtr projectInfo, GenerateCompilationDbResult generateCompilationDB(const CppEditor::ProjectInfo::ConstPtr projectInfo,
const Utils::FilePath &baseDir,
CompilationDbPurpose purpose, CompilationDbPurpose purpose,
const ClangDiagnosticConfig &warningsConfig, const ClangDiagnosticConfig &warningsConfig,
const QStringList &projectOptions) const QStringList &projectOptions)
{ {
const FilePath buildDir = projectInfo->buildRoot(); QTC_ASSERT(!baseDir.isEmpty(), return GenerateCompilationDbResult(QString(),
QTC_ASSERT(!buildDir.isEmpty(), return GenerateCompilationDbResult(QString(),
QCoreApplication::translate("ClangUtils", "Could not retrieve build directory."))); QCoreApplication::translate("ClangUtils", "Could not retrieve build directory.")));
QTC_CHECK(baseDir.ensureWritableDir());
QDir dir(buildDir.toString()); QFile compileCommandsFile(baseDir.toString() + "/compile_commands.json");
if (!dir.exists())
dir.mkpath(dir.path());
QFile compileCommandsFile(buildDir.toString() + "/compile_commands.json");
const bool fileOpened = compileCommandsFile.open(QIODevice::WriteOnly | QIODevice::Truncate); const bool fileOpened = compileCommandsFile.open(QIODevice::WriteOnly | QIODevice::Truncate);
if (!fileOpened) { if (!fileOpened) {
return GenerateCompilationDbResult(QString(), return GenerateCompilationDbResult(QString(),
@@ -397,7 +394,7 @@ GenerateCompilationDbResult generateCompilationDB(const CppEditor::ProjectInfo::
if (purpose == CompilationDbPurpose::Project) if (purpose == CompilationDbPurpose::Project)
args = projectPartArguments(*projectPart); args = projectPartArguments(*projectPart);
for (const ProjectFile &projFile : projectPart->files) { for (const ProjectFile &projFile : projectPart->files) {
const QJsonObject json = createFileObject(buildDir, args, *projectPart, projFile, const QJsonObject json = createFileObject(baseDir, args, *projectPart, projFile,
purpose, warningsConfig, projectOptions); purpose, warningsConfig, projectOptions);
if (compileCommandsFile.size() > 1) if (compileCommandsFile.size() > 1)
compileCommandsFile.write(","); compileCommandsFile.write(",");

View File

@@ -88,8 +88,8 @@ public:
enum class CompilationDbPurpose { Project, CodeModel }; enum class CompilationDbPurpose { Project, CodeModel };
GenerateCompilationDbResult generateCompilationDB(const CppEditor::ProjectInfo::ConstPtr projectInfo, GenerateCompilationDbResult generateCompilationDB(const CppEditor::ProjectInfo::ConstPtr projectInfo,
CompilationDbPurpose purpose, const CppEditor::ClangDiagnosticConfig &warningsConfig, const Utils::FilePath &baseDir, CompilationDbPurpose purpose,
const QStringList &projectOptions); const CppEditor::ClangDiagnosticConfig &warningsConfig, const QStringList &projectOptions);
class DiagnosticTextInfo class DiagnosticTextInfo
{ {