blob: f024821e86d5f3908645b62a334a64421d7d1bfd [file] [log] [blame]
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00001//===--- GlobalCompilationDatabase.cpp ---------------------------*- C++-*-===//
Ilya Biryukov38d79772017-05-16 09:38:59 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ilya Biryukov38d79772017-05-16 09:38:59 +00006//
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00007//===----------------------------------------------------------------------===//
Ilya Biryukov38d79772017-05-16 09:38:59 +00008
9#include "GlobalCompilationDatabase.h"
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +000010#include "Logger.h"
Kadir Cetinkayabe6b35d2019-01-22 09:10:20 +000011#include "clang/Frontend/CompilerInvocation.h"
12#include "clang/Tooling/ArgumentsAdjusters.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000013#include "clang/Tooling/CompilationDatabase.h"
Kadir Cetinkayabe6b35d2019-01-22 09:10:20 +000014#include "llvm/ADT/Optional.h"
Ilya Biryukov38d79772017-05-16 09:38:59 +000015#include "llvm/Support/FileSystem.h"
16#include "llvm/Support/Path.h"
17
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000018namespace clang {
19namespace clangd {
Kadir Cetinkayabe6b35d2019-01-22 09:10:20 +000020namespace {
21
22void adjustArguments(tooling::CompileCommand &Cmd,
23 llvm::StringRef ResourceDir) {
Kadir Cetinkaya65944ab2019-03-08 08:38:25 +000024 tooling::ArgumentsAdjuster ArgsAdjuster = tooling::combineAdjusters(
25 // clangd should not write files to disk, including dependency files
26 // requested on the command line.
27 tooling::getClangStripDependencyFileAdjuster(),
28 // Strip plugin related command line arguments. Clangd does
29 // not support plugins currently. Therefore it breaks if
30 // compiler tries to load plugins.
31 tooling::combineAdjusters(tooling::getStripPluginsAdjuster(),
32 tooling::getClangSyntaxOnlyAdjuster()));
33
34 Cmd.CommandLine = ArgsAdjuster(Cmd.CommandLine, Cmd.Filename);
Kadir Cetinkayabe6b35d2019-01-22 09:10:20 +000035 // Inject the resource dir.
36 // FIXME: Don't overwrite it if it's already there.
37 if (!ResourceDir.empty())
38 Cmd.CommandLine.push_back(("-resource-dir=" + ResourceDir).str());
39}
40
41std::string getStandardResourceDir() {
42 static int Dummy; // Just an address in this process.
43 return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
44}
45
46} // namespace
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000047
Haojian Wue1ced5c2019-01-07 12:35:02 +000048static std::string getFallbackClangPath() {
49 static int Dummy;
50 std::string ClangdExecutable =
51 llvm::sys::fs::getMainExecutable("clangd", (void *)&Dummy);
52 SmallString<128> ClangPath;
53 ClangPath = llvm::sys::path::parent_path(ClangdExecutable);
54 llvm::sys::path::append(ClangPath, "clang");
55 return ClangPath.str();
56}
57
Sam McCallecbeab02017-12-04 10:08:45 +000058tooling::CompileCommand
59GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
Haojian Wue1ced5c2019-01-07 12:35:02 +000060 std::vector<std::string> Argv = {getFallbackClangPath()};
Sam McCall690dcf12018-04-20 11:35:17 +000061 // Clang treats .h files as C by default, resulting in unhelpful diagnostics.
62 // Parsing as Objective C++ is friendly to more cases.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000063 if (llvm::sys::path::extension(File) == ".h")
Sam McCall690dcf12018-04-20 11:35:17 +000064 Argv.push_back("-xobjective-c++-header");
65 Argv.push_back(File);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000066 return tooling::CompileCommand(llvm::sys::path::parent_path(File),
67 llvm::sys::path::filename(File),
68 std::move(Argv),
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000069 /*Output=*/"");
70}
Ilya Biryukov38d79772017-05-16 09:38:59 +000071
Ilya Biryukove5128f72017-09-20 07:24:15 +000072DirectoryBasedGlobalCompilationDatabase::
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000073 DirectoryBasedGlobalCompilationDatabase(
74 llvm::Optional<Path> CompileCommandsDir)
Ilya Biryukov940901e2017-12-13 12:51:22 +000075 : CompileCommandsDir(std::move(CompileCommandsDir)) {}
Ilya Biryukove5128f72017-09-20 07:24:15 +000076
Sam McCall690dcf12018-04-20 11:35:17 +000077DirectoryBasedGlobalCompilationDatabase::
78 ~DirectoryBasedGlobalCompilationDatabase() = default;
79
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000080llvm::Optional<tooling::CompileCommand>
Sam McCall6e2d2a32018-11-26 09:51:50 +000081DirectoryBasedGlobalCompilationDatabase::getCompileCommand(
82 PathRef File, ProjectInfo *Project) const {
83 if (auto CDB = getCDBForFile(File, Project)) {
Sam McCallecbeab02017-12-04 10:08:45 +000084 auto Candidates = CDB->getCompileCommands(File);
Sam McCall6e2d2a32018-11-26 09:51:50 +000085 if (!Candidates.empty()) {
Sam McCallecbeab02017-12-04 10:08:45 +000086 return std::move(Candidates.front());
Sam McCall6e2d2a32018-11-26 09:51:50 +000087 }
Sam McCallc02ba722017-12-22 09:47:34 +000088 } else {
Sam McCallbed58852018-07-11 10:35:11 +000089 log("Failed to find compilation database for {0}", File);
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000090 }
Sam McCallc008af62018-10-20 15:30:37 +000091 return None;
Sam McCallecbeab02017-12-04 10:08:45 +000092}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000093
Sam McCall2bebc3d2018-11-20 10:56:03 +000094std::pair<tooling::CompilationDatabase *, /*Cached*/ bool>
Sam McCallc02ba722017-12-22 09:47:34 +000095DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
96 // FIXME(ibiryukov): Invalidate cached compilation databases on changes
97 auto CachedIt = CompilationDatabases.find(Dir);
98 if (CachedIt != CompilationDatabases.end())
Sam McCall2bebc3d2018-11-20 10:56:03 +000099 return {CachedIt->second.get(), true};
Sam McCallc02ba722017-12-22 09:47:34 +0000100 std::string Error = "";
101 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
102 auto Result = CDB.get();
103 CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
Sam McCall2bebc3d2018-11-20 10:56:03 +0000104 return {Result, false};
Sam McCallc02ba722017-12-22 09:47:34 +0000105}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000106
Sam McCallc02ba722017-12-22 09:47:34 +0000107tooling::CompilationDatabase *
Sam McCall6e2d2a32018-11-26 09:51:50 +0000108DirectoryBasedGlobalCompilationDatabase::getCDBForFile(
109 PathRef File, ProjectInfo *Project) const {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000110 namespace path = llvm::sys::path;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000111 assert((path::is_absolute(File, path::Style::posix) ||
112 path::is_absolute(File, path::Style::windows)) &&
113 "path must be absolute");
114
Sam McCall2bebc3d2018-11-20 10:56:03 +0000115 tooling::CompilationDatabase *CDB = nullptr;
116 bool Cached = false;
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000117 std::lock_guard<std::mutex> Lock(Mutex);
Sam McCall2bebc3d2018-11-20 10:56:03 +0000118 if (CompileCommandsDir) {
119 std::tie(CDB, Cached) = getCDBInDirLocked(*CompileCommandsDir);
Sam McCall6e2d2a32018-11-26 09:51:50 +0000120 if (Project && CDB)
121 Project->SourceRoot = *CompileCommandsDir;
Sam McCall2bebc3d2018-11-20 10:56:03 +0000122 } else {
123 for (auto Path = path::parent_path(File); !CDB && !Path.empty();
124 Path = path::parent_path(Path)) {
125 std::tie(CDB, Cached) = getCDBInDirLocked(Path);
Sam McCall6e2d2a32018-11-26 09:51:50 +0000126 if (Project && CDB)
127 Project->SourceRoot = Path;
Sam McCall2bebc3d2018-11-20 10:56:03 +0000128 }
129 }
Sam McCall422c8282018-11-26 16:00:11 +0000130 // FIXME: getAllFiles() may return relative paths, we need absolute paths.
131 // Hopefully the fix is to change JSONCompilationDatabase and the interface.
Sam McCall2bebc3d2018-11-20 10:56:03 +0000132 if (CDB && !Cached)
133 OnCommandChanged.broadcast(CDB->getAllFiles());
134 return CDB;
135}
136
137OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base,
Kadir Cetinkayabe6b35d2019-01-22 09:10:20 +0000138 std::vector<std::string> FallbackFlags,
139 llvm::Optional<std::string> ResourceDir)
140 : Base(Base), ResourceDir(ResourceDir ? std::move(*ResourceDir)
141 : getStandardResourceDir()),
142 FallbackFlags(std::move(FallbackFlags)) {
Sam McCall2bebc3d2018-11-20 10:56:03 +0000143 if (Base)
144 BaseChanged = Base->watch([this](const std::vector<std::string> Changes) {
145 OnCommandChanged.broadcast(Changes);
146 });
Ilya Biryukov38d79772017-05-16 09:38:59 +0000147}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000148
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000149llvm::Optional<tooling::CompileCommand>
Sam McCall6e2d2a32018-11-26 09:51:50 +0000150OverlayCDB::getCompileCommand(PathRef File, ProjectInfo *Project) const {
Kadir Cetinkayabe6b35d2019-01-22 09:10:20 +0000151 llvm::Optional<tooling::CompileCommand> Cmd;
Sam McCallc55d09a2018-11-02 13:09:36 +0000152 {
153 std::lock_guard<std::mutex> Lock(Mutex);
154 auto It = Commands.find(File);
Sam McCall6e2d2a32018-11-26 09:51:50 +0000155 if (It != Commands.end()) {
156 if (Project)
157 Project->SourceRoot = "";
Kadir Cetinkayabe6b35d2019-01-22 09:10:20 +0000158 Cmd = It->second;
Sam McCall6e2d2a32018-11-26 09:51:50 +0000159 }
Sam McCallc55d09a2018-11-02 13:09:36 +0000160 }
Kadir Cetinkayabe6b35d2019-01-22 09:10:20 +0000161 if (!Cmd && Base)
162 Cmd = Base->getCompileCommand(File, Project);
163 if (!Cmd)
164 return llvm::None;
165 adjustArguments(*Cmd, ResourceDir);
166 return Cmd;
Alex Lorenzf8087862018-08-01 17:39:29 +0000167}
168
Sam McCallc55d09a2018-11-02 13:09:36 +0000169tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const {
170 auto Cmd = Base ? Base->getFallbackCommand(File)
171 : GlobalCompilationDatabase::getFallbackCommand(File);
172 std::lock_guard<std::mutex> Lock(Mutex);
173 Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
174 FallbackFlags.end());
175 return Cmd;
176}
177
178void OverlayCDB::setCompileCommand(
179 PathRef File, llvm::Optional<tooling::CompileCommand> Cmd) {
Sam McCall2bebc3d2018-11-20 10:56:03 +0000180 {
181 std::unique_lock<std::mutex> Lock(Mutex);
182 if (Cmd)
183 Commands[File] = std::move(*Cmd);
184 else
185 Commands.erase(File);
186 }
187 OnCommandChanged.broadcast({File});
Alex Lorenzf8087862018-08-01 17:39:29 +0000188}
189
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000190} // namespace clangd
191} // namespace clang