blob: a9161972893b4826737b7a7d707a86208b3146e6 [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) {
24 // Strip plugin related command line arguments. Clangd does
25 // not support plugins currently. Therefore it breaks if
26 // compiler tries to load plugins.
27 Cmd.CommandLine =
28 tooling::getStripPluginsAdjuster()(Cmd.CommandLine, Cmd.Filename);
29 // Inject the resource dir.
30 // FIXME: Don't overwrite it if it's already there.
31 if (!ResourceDir.empty())
32 Cmd.CommandLine.push_back(("-resource-dir=" + ResourceDir).str());
33}
34
35std::string getStandardResourceDir() {
36 static int Dummy; // Just an address in this process.
37 return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
38}
39
40} // namespace
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000041
Haojian Wue1ced5c2019-01-07 12:35:02 +000042static std::string getFallbackClangPath() {
43 static int Dummy;
44 std::string ClangdExecutable =
45 llvm::sys::fs::getMainExecutable("clangd", (void *)&Dummy);
46 SmallString<128> ClangPath;
47 ClangPath = llvm::sys::path::parent_path(ClangdExecutable);
48 llvm::sys::path::append(ClangPath, "clang");
49 return ClangPath.str();
50}
51
Sam McCallecbeab02017-12-04 10:08:45 +000052tooling::CompileCommand
53GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
Haojian Wue1ced5c2019-01-07 12:35:02 +000054 std::vector<std::string> Argv = {getFallbackClangPath()};
Sam McCall690dcf12018-04-20 11:35:17 +000055 // Clang treats .h files as C by default, resulting in unhelpful diagnostics.
56 // Parsing as Objective C++ is friendly to more cases.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000057 if (llvm::sys::path::extension(File) == ".h")
Sam McCall690dcf12018-04-20 11:35:17 +000058 Argv.push_back("-xobjective-c++-header");
59 Argv.push_back(File);
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000060 return tooling::CompileCommand(llvm::sys::path::parent_path(File),
61 llvm::sys::path::filename(File),
62 std::move(Argv),
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000063 /*Output=*/"");
64}
Ilya Biryukov38d79772017-05-16 09:38:59 +000065
Ilya Biryukove5128f72017-09-20 07:24:15 +000066DirectoryBasedGlobalCompilationDatabase::
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000067 DirectoryBasedGlobalCompilationDatabase(
68 llvm::Optional<Path> CompileCommandsDir)
Ilya Biryukov940901e2017-12-13 12:51:22 +000069 : CompileCommandsDir(std::move(CompileCommandsDir)) {}
Ilya Biryukove5128f72017-09-20 07:24:15 +000070
Sam McCall690dcf12018-04-20 11:35:17 +000071DirectoryBasedGlobalCompilationDatabase::
72 ~DirectoryBasedGlobalCompilationDatabase() = default;
73
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000074llvm::Optional<tooling::CompileCommand>
Sam McCall6e2d2a32018-11-26 09:51:50 +000075DirectoryBasedGlobalCompilationDatabase::getCompileCommand(
76 PathRef File, ProjectInfo *Project) const {
77 if (auto CDB = getCDBForFile(File, Project)) {
Sam McCallecbeab02017-12-04 10:08:45 +000078 auto Candidates = CDB->getCompileCommands(File);
Sam McCall6e2d2a32018-11-26 09:51:50 +000079 if (!Candidates.empty()) {
Sam McCallecbeab02017-12-04 10:08:45 +000080 return std::move(Candidates.front());
Sam McCall6e2d2a32018-11-26 09:51:50 +000081 }
Sam McCallc02ba722017-12-22 09:47:34 +000082 } else {
Sam McCallbed58852018-07-11 10:35:11 +000083 log("Failed to find compilation database for {0}", File);
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000084 }
Sam McCallc008af62018-10-20 15:30:37 +000085 return None;
Sam McCallecbeab02017-12-04 10:08:45 +000086}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +000087
Sam McCall2bebc3d2018-11-20 10:56:03 +000088std::pair<tooling::CompilationDatabase *, /*Cached*/ bool>
Sam McCallc02ba722017-12-22 09:47:34 +000089DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
90 // FIXME(ibiryukov): Invalidate cached compilation databases on changes
91 auto CachedIt = CompilationDatabases.find(Dir);
92 if (CachedIt != CompilationDatabases.end())
Sam McCall2bebc3d2018-11-20 10:56:03 +000093 return {CachedIt->second.get(), true};
Sam McCallc02ba722017-12-22 09:47:34 +000094 std::string Error = "";
95 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
96 auto Result = CDB.get();
97 CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
Sam McCall2bebc3d2018-11-20 10:56:03 +000098 return {Result, false};
Sam McCallc02ba722017-12-22 09:47:34 +000099}
Ilya Biryukov38d79772017-05-16 09:38:59 +0000100
Sam McCallc02ba722017-12-22 09:47:34 +0000101tooling::CompilationDatabase *
Sam McCall6e2d2a32018-11-26 09:51:50 +0000102DirectoryBasedGlobalCompilationDatabase::getCDBForFile(
103 PathRef File, ProjectInfo *Project) const {
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000104 namespace path = llvm::sys::path;
Ilya Biryukov38d79772017-05-16 09:38:59 +0000105 assert((path::is_absolute(File, path::Style::posix) ||
106 path::is_absolute(File, path::Style::windows)) &&
107 "path must be absolute");
108
Sam McCall2bebc3d2018-11-20 10:56:03 +0000109 tooling::CompilationDatabase *CDB = nullptr;
110 bool Cached = false;
Ilya Biryukov0c1ca6b2017-10-02 15:13:20 +0000111 std::lock_guard<std::mutex> Lock(Mutex);
Sam McCall2bebc3d2018-11-20 10:56:03 +0000112 if (CompileCommandsDir) {
113 std::tie(CDB, Cached) = getCDBInDirLocked(*CompileCommandsDir);
Sam McCall6e2d2a32018-11-26 09:51:50 +0000114 if (Project && CDB)
115 Project->SourceRoot = *CompileCommandsDir;
Sam McCall2bebc3d2018-11-20 10:56:03 +0000116 } else {
117 for (auto Path = path::parent_path(File); !CDB && !Path.empty();
118 Path = path::parent_path(Path)) {
119 std::tie(CDB, Cached) = getCDBInDirLocked(Path);
Sam McCall6e2d2a32018-11-26 09:51:50 +0000120 if (Project && CDB)
121 Project->SourceRoot = Path;
Sam McCall2bebc3d2018-11-20 10:56:03 +0000122 }
123 }
Sam McCall422c8282018-11-26 16:00:11 +0000124 // FIXME: getAllFiles() may return relative paths, we need absolute paths.
125 // Hopefully the fix is to change JSONCompilationDatabase and the interface.
Sam McCall2bebc3d2018-11-20 10:56:03 +0000126 if (CDB && !Cached)
127 OnCommandChanged.broadcast(CDB->getAllFiles());
128 return CDB;
129}
130
131OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base,
Kadir Cetinkayabe6b35d2019-01-22 09:10:20 +0000132 std::vector<std::string> FallbackFlags,
133 llvm::Optional<std::string> ResourceDir)
134 : Base(Base), ResourceDir(ResourceDir ? std::move(*ResourceDir)
135 : getStandardResourceDir()),
136 FallbackFlags(std::move(FallbackFlags)) {
Sam McCall2bebc3d2018-11-20 10:56:03 +0000137 if (Base)
138 BaseChanged = Base->watch([this](const std::vector<std::string> Changes) {
139 OnCommandChanged.broadcast(Changes);
140 });
Ilya Biryukov38d79772017-05-16 09:38:59 +0000141}
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000142
Ilya Biryukovf2001aa2019-01-07 15:45:19 +0000143llvm::Optional<tooling::CompileCommand>
Sam McCall6e2d2a32018-11-26 09:51:50 +0000144OverlayCDB::getCompileCommand(PathRef File, ProjectInfo *Project) const {
Kadir Cetinkayabe6b35d2019-01-22 09:10:20 +0000145 llvm::Optional<tooling::CompileCommand> Cmd;
Sam McCallc55d09a2018-11-02 13:09:36 +0000146 {
147 std::lock_guard<std::mutex> Lock(Mutex);
148 auto It = Commands.find(File);
Sam McCall6e2d2a32018-11-26 09:51:50 +0000149 if (It != Commands.end()) {
150 if (Project)
151 Project->SourceRoot = "";
Kadir Cetinkayabe6b35d2019-01-22 09:10:20 +0000152 Cmd = It->second;
Sam McCall6e2d2a32018-11-26 09:51:50 +0000153 }
Sam McCallc55d09a2018-11-02 13:09:36 +0000154 }
Kadir Cetinkayabe6b35d2019-01-22 09:10:20 +0000155 if (!Cmd && Base)
156 Cmd = Base->getCompileCommand(File, Project);
157 if (!Cmd)
158 return llvm::None;
159 adjustArguments(*Cmd, ResourceDir);
160 return Cmd;
Alex Lorenzf8087862018-08-01 17:39:29 +0000161}
162
Sam McCallc55d09a2018-11-02 13:09:36 +0000163tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const {
164 auto Cmd = Base ? Base->getFallbackCommand(File)
165 : GlobalCompilationDatabase::getFallbackCommand(File);
166 std::lock_guard<std::mutex> Lock(Mutex);
167 Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
168 FallbackFlags.end());
169 return Cmd;
170}
171
172void OverlayCDB::setCompileCommand(
173 PathRef File, llvm::Optional<tooling::CompileCommand> Cmd) {
Sam McCall2bebc3d2018-11-20 10:56:03 +0000174 {
175 std::unique_lock<std::mutex> Lock(Mutex);
176 if (Cmd)
177 Commands[File] = std::move(*Cmd);
178 else
179 Commands.erase(File);
180 }
181 OnCommandChanged.broadcast({File});
Alex Lorenzf8087862018-08-01 17:39:29 +0000182}
183
Krasimir Georgievc2a16a32017-07-06 08:44:54 +0000184} // namespace clangd
185} // namespace clang