clang-tools 8.0.0

GlobalCompilationDatabase.cpp
Go to the documentation of this file.
1 //===--- GlobalCompilationDatabase.cpp ---------------------------*- C++-*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "GlobalCompilationDatabase.h"
11 #include "Logger.h"
12 #include "clang/Frontend/CompilerInvocation.h"
13 #include "clang/Tooling/ArgumentsAdjusters.h"
14 #include "clang/Tooling/CompilationDatabase.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/Path.h"
18
19 namespace clang {
20 namespace clangd {
21 namespace {
22
23 void adjustArguments(tooling::CompileCommand &Cmd,
24 llvm::StringRef ResourceDir) {
25 // Strip plugin related command line arguments. Clangd does
26 // not support plugins currently. Therefore it breaks if
27 // compiler tries to load plugins.
28 Cmd.CommandLine =
29 tooling::getStripPluginsAdjuster()(Cmd.CommandLine, Cmd.Filename);
30 // Inject the resource dir.
31 // FIXME: Don't overwrite it if it's already there.
32 if (!ResourceDir.empty())
33 Cmd.CommandLine.push_back(("-resource-dir=" + ResourceDir).str());
34 }
35
36 std::string getStandardResourceDir() {
37 static int Dummy; // Just an address in this process.
38 return CompilerInvocation::GetResourcesPath("clangd", (void *)&Dummy);
39 }
40
41 } // namespace
42
43 static std::string getFallbackClangPath() {
44 static int Dummy;
45 std::string ClangdExecutable =
46 llvm::sys::fs::getMainExecutable("clangd", (void *)&Dummy);
47 SmallString<128> ClangPath;
48 ClangPath = llvm::sys::path::parent_path(ClangdExecutable);
49 llvm::sys::path::append(ClangPath, "clang");
50 return ClangPath.str();
51 }
52
53 tooling::CompileCommand
54 GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
55 std::vector<std::string> Argv = {getFallbackClangPath()};
56 // Clang treats .h files as C by default, resulting in unhelpful diagnostics.
57 // Parsing as Objective C++ is friendly to more cases.
58 if (llvm::sys::path::extension(File) == ".h")
59 Argv.push_back("-xobjective-c++-header");
60 Argv.push_back(File);
61 return tooling::CompileCommand(llvm::sys::path::parent_path(File),
62 llvm::sys::path::filename(File),
63 std::move(Argv),
64 /*Output=*/"");
65 }
66
67 DirectoryBasedGlobalCompilationDatabase::
68 DirectoryBasedGlobalCompilationDatabase(
69 llvm::Optional<Path> CompileCommandsDir)
70 : CompileCommandsDir(std::move(CompileCommandsDir)) {}
71
72 DirectoryBasedGlobalCompilationDatabase::
73 ~DirectoryBasedGlobalCompilationDatabase() = default;
74
75 llvm::Optional<tooling::CompileCommand>
76 DirectoryBasedGlobalCompilationDatabase::getCompileCommand(
77 PathRef File, ProjectInfo *Project) const {
78 if (auto CDB = getCDBForFile(File, Project)) {
79 auto Candidates = CDB->getCompileCommands(File);
80 if (!Candidates.empty()) {
81 return std::move(Candidates.front());
82 }
83 } else {
84 log("Failed to find compilation database for {0}", File);
85 }
86 return None;
87 }
88
89 std::pair<tooling::CompilationDatabase *, /*Cached*/ bool>
90 DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
91 // FIXME(ibiryukov): Invalidate cached compilation databases on changes
92 auto CachedIt = CompilationDatabases.find(Dir);
93 if (CachedIt != CompilationDatabases.end())
94 return {CachedIt->second.get(), true};
95 std::string Error = "";
96 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
97 auto Result = CDB.get();
98 CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
99 return {Result, false};
100 }
101
102 tooling::CompilationDatabase *
103 DirectoryBasedGlobalCompilationDatabase::getCDBForFile(
104 PathRef File, ProjectInfo *Project) const {
105 namespace path = llvm::sys::path;
106 assert((path::is_absolute(File, path::Style::posix) ||
107 path::is_absolute(File, path::Style::windows)) &&
108 "path must be absolute");
109
110 tooling::CompilationDatabase *CDB = nullptr;
111 bool Cached = false;
112 std::lock_guard<std::mutex> Lock(Mutex);
113 if (CompileCommandsDir) {
114 std::tie(CDB, Cached) = getCDBInDirLocked(*CompileCommandsDir);
115 if (Project && CDB)
116 Project->SourceRoot = *CompileCommandsDir;
117 } else {
118 for (auto Path = path::parent_path(File); !CDB && !Path.empty();
119 Path = path::parent_path(Path)) {
120 std::tie(CDB, Cached) = getCDBInDirLocked(Path);
121 if (Project && CDB)
122 Project->SourceRoot = Path;
123 }
124 }
125 // FIXME: getAllFiles() may return relative paths, we need absolute paths.
126 // Hopefully the fix is to change JSONCompilationDatabase and the interface.
127 if (CDB && !Cached)
128 OnCommandChanged.broadcast(CDB->getAllFiles());
129 return CDB;
130 }
131
132 OverlayCDB::OverlayCDB(const GlobalCompilationDatabase *Base,
133 std::vector<std::string> FallbackFlags,
134 llvm::Optional<std::string> ResourceDir)
135 : Base(Base), ResourceDir(ResourceDir ? std::move(*ResourceDir)
136 : getStandardResourceDir()),
137 FallbackFlags(std::move(FallbackFlags)) {
138 if (Base)
139 BaseChanged = Base->watch([this](const std::vector<std::string> Changes) {
140 OnCommandChanged.broadcast(Changes);
141 });
142 }
143
144 llvm::Optional<tooling::CompileCommand>
145 OverlayCDB::getCompileCommand(PathRef File, ProjectInfo *Project) const {
146 llvm::Optional<tooling::CompileCommand> Cmd;
147 {
148 std::lock_guard<std::mutex> Lock(Mutex);
149 auto It = Commands.find(File);
150 if (It != Commands.end()) {
151 if (Project)
152 Project->SourceRoot = "";
153 Cmd = It->second;
154 }
155 }
156 if (!Cmd && Base)
157 Cmd = Base->getCompileCommand(File, Project);
158 if (!Cmd)
159 return llvm::None;
160 adjustArguments(*Cmd, ResourceDir);
161 return Cmd;
162 }
163
164 tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const {
165 auto Cmd = Base ? Base->getFallbackCommand(File)
166 : GlobalCompilationDatabase::getFallbackCommand(File);
167 std::lock_guard<std::mutex> Lock(Mutex);
168 Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
169 FallbackFlags.end());
170 return Cmd;
171 }
172
173 void OverlayCDB::setCompileCommand(
174 PathRef File, llvm::Optional<tooling::CompileCommand> Cmd) {
175 {
176 std::unique_lock<std::mutex> Lock(Mutex);
177 if (Cmd)
178 Commands[File] = std::move(*Cmd);
179 else
180 Commands.erase(File);
181 }
182 OnCommandChanged.broadcast({File});
183 }
184
185 } // namespace clangd
186 } // namespace clang
clang::clangd::DirectoryBasedGlobalCompilationDatabase::getCompileCommand
llvm::Optional< tooling::CompileCommand > getCompileCommand(PathRef File, ProjectInfo *=nullptr) const override
Scans File&#39;s parents looking for compilation databases.
Definition: GlobalCompilationDatabase.cpp:76
clang::clangd::getFallbackClangPath
static std::string getFallbackClangPath()
Definition: GlobalCompilationDatabase.cpp:43
clang::clangd::GlobalCompilationDatabase::getCompileCommand
virtual llvm::Optional< tooling::CompileCommand > getCompileCommand(PathRef File, ProjectInfo *=nullptr) const =0
If there are any known-good commands for building this file, returns one.
Commands
static cl::list< std::string > Commands("c", cl::desc("Specify command to run"), cl::value_desc("command"), cl::cat(ClangQueryCategory))
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:24
clang::clangd::DirectoryBasedGlobalCompilationDatabase::~DirectoryBasedGlobalCompilationDatabase
~DirectoryBasedGlobalCompilationDatabase() override
clang::clangd::TextDocumentSyncKind::None
Documents should not be synced at all.
GlobalCompilationDatabase.h
clang::clangd::GlobalCompilationDatabase
Provides compilation arguments used for parsing C and C++ files.
Definition: GlobalCompilationDatabase.h:39
clang::clangd::ProjectInfo
Definition: GlobalCompilationDatabase.h:32
clang::clangd::OverlayCDB::getCompileCommand
llvm::Optional< tooling::CompileCommand > getCompileCommand(PathRef File, ProjectInfo *=nullptr) const override
If there are any known-good commands for building this file, returns one.
Definition: GlobalCompilationDatabase.cpp:145
clang::clangd::Event::broadcast
void broadcast(const T &V)
Definition: Function.h:140
clang::clangd::log
void log(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:63
clang::clangd::Path
std::string Path
A typedef to represent a file path.
Definition: Path.h:21
clang::clangd::ProjectInfo::SourceRoot
std::string SourceRoot
Definition: GlobalCompilationDatabase.h:35
Result
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
Definition: ClangdServer.cpp:60
clang::clangd::DirectoryBasedGlobalCompilationDatabase::DirectoryBasedGlobalCompilationDatabase
DirectoryBasedGlobalCompilationDatabase(llvm::Optional< Path > CompileCommandsDir)
Definition: GlobalCompilationDatabase.cpp:68
clang::clangd::OverlayCDB::getFallbackCommand
tooling::CompileCommand getFallbackCommand(PathRef File) const override
Makes a guess at how to build a file.
Definition: GlobalCompilationDatabase.cpp:164
clang::clangd::OverlayCDB::setCompileCommand
void setCompileCommand(PathRef File, llvm::Optional< tooling::CompileCommand > CompilationCommand)
Sets or clears the compilation command for a particular file.
Definition: GlobalCompilationDatabase.cpp:173
clang::clangd::ResourceDir
static llvm::cl::opt< Path > ResourceDir("resource-dir", llvm::cl::desc("Directory for system clang headers"), llvm::cl::init(""), llvm::cl::Hidden)
clang::clangd::OverlayCDB::OverlayCDB
OverlayCDB(const GlobalCompilationDatabase *Base, std::vector< std::string > FallbackFlags={}, llvm::Optional< std::string > ResourceDir=llvm::None)
Definition: GlobalCompilationDatabase.cpp:132
Logger.h
clang::clangd::GlobalCompilationDatabase::watch
CommandChanged::Subscription watch(CommandChanged::Listener L) const
The callback is notified when files may have new compile commands.
Definition: GlobalCompilationDatabase.h:56
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::clangd::CompileCommandsDir
static llvm::cl::opt< Path > CompileCommandsDir("compile-commands-dir", llvm::cl::desc("Specify a path to look for compile_commands.json. If path " "is invalid, clangd will look in the current directory and " "parent paths of each source file."))
clang::clangd::CompletionItemKind::File
clang::clangd::GlobalCompilationDatabase::OnCommandChanged
CommandChanged OnCommandChanged
Definition: GlobalCompilationDatabase.h:61
clang::clangd::GlobalCompilationDatabase::getFallbackCommand
virtual tooling::CompileCommand getFallbackCommand(PathRef File) const
Makes a guess at how to build a file.
Definition: GlobalCompilationDatabase.cpp:54