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/Tooling/CompilationDatabase.h"
13 #include "llvm/Support/FileSystem.h"
14 #include "llvm/Support/Path.h"
15 
16 namespace clang {
17 namespace clangd {
18 
19 tooling::CompileCommand
20 GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
21   std::vector<std::string> Argv = {"clang"};
22   // Clang treats .h files as C by default, resulting in unhelpful diagnostics.
23   // Parsing as Objective C++ is friendly to more cases.
24   if (llvm::sys::path::extension(File) == ".h")
25     Argv.push_back("-xobjective-c++-header");
26   Argv.push_back(File);
27   return tooling::CompileCommand(llvm::sys::path::parent_path(File),
28                                  llvm::sys::path::filename(File),
29                                  std::move(Argv),
30                                  /*Output=*/"");
31 }
32 
33 DirectoryBasedGlobalCompilationDatabase::
34     DirectoryBasedGlobalCompilationDatabase(
35         llvm::Optional<Path> CompileCommandsDir)
36     : CompileCommandsDir(std::move(CompileCommandsDir)) {}
37 
38 DirectoryBasedGlobalCompilationDatabase::
39     ~DirectoryBasedGlobalCompilationDatabase() = default;
40 
41 llvm::Optional<tooling::CompileCommand>
42 DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const {
43   if (auto CDB = getCDBForFile(File)) {
44     auto Candidates = CDB->getCompileCommands(File);
45     if (!Candidates.empty()) {
46       addExtraFlags(File, Candidates.front());
47       return std::move(Candidates.front());
48     }
49   } else {
50     log("Failed to find compilation database for {0}", File);
51   }
52   return llvm::None;
53 }
54 
55 tooling::CompileCommand
56 DirectoryBasedGlobalCompilationDatabase::getFallbackCommand(
57     PathRef File) const {
58   auto C = GlobalCompilationDatabase::getFallbackCommand(File);
59   addExtraFlags(File, C);
60   return C;
61 }
62 
63 void DirectoryBasedGlobalCompilationDatabase::setCompileCommandsDir(Path P) {
64   std::lock_guard<std::mutex> Lock(Mutex);
65   CompileCommandsDir = P;
66   CompilationDatabases.clear();
67 }
68 
69 void DirectoryBasedGlobalCompilationDatabase::setExtraFlagsForFile(
70     PathRef File, std::vector<std::string> ExtraFlags) {
71   std::lock_guard<std::mutex> Lock(Mutex);
72   ExtraFlagsForFile[File] = std::move(ExtraFlags);
73 }
74 
75 void DirectoryBasedGlobalCompilationDatabase::addExtraFlags(
76     PathRef File, tooling::CompileCommand &C) const {
77   std::lock_guard<std::mutex> Lock(Mutex);
78 
79   auto It = ExtraFlagsForFile.find(File);
80   if (It == ExtraFlagsForFile.end())
81     return;
82 
83   auto &Args = C.CommandLine;
84   assert(Args.size() >= 2 && "Expected at least [compiler, source file]");
85   // The last argument of CommandLine is the name of the input file.
86   // Add ExtraFlags before it.
87   Args.insert(Args.end() - 1, It->second.begin(), It->second.end());
88 }
89 
90 tooling::CompilationDatabase *
91 DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
92   // FIXME(ibiryukov): Invalidate cached compilation databases on changes
93   auto CachedIt = CompilationDatabases.find(Dir);
94   if (CachedIt != CompilationDatabases.end())
95     return CachedIt->second.get();
96   std::string Error = "";
97   auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
98   auto Result = CDB.get();
99   CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
100   return Result;
101 }
102 
103 tooling::CompilationDatabase *
104 DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) 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   std::lock_guard<std::mutex> Lock(Mutex);
111   if (CompileCommandsDir)
112     return getCDBInDirLocked(*CompileCommandsDir);
113   for (auto Path = path::parent_path(File); !Path.empty();
114        Path = path::parent_path(Path))
115     if (auto CDB = getCDBInDirLocked(Path))
116       return CDB;
117   return nullptr;
118 }
119 
120 CachingCompilationDb::CachingCompilationDb(
121     const GlobalCompilationDatabase &InnerCDB)
122     : InnerCDB(InnerCDB) {}
123 
124 llvm::Optional<tooling::CompileCommand>
125 CachingCompilationDb::getCompileCommand(PathRef File) const {
126   std::unique_lock<std::mutex> Lock(Mut);
127   auto It = Cached.find(File);
128   if (It != Cached.end())
129     return It->second;
130 
131   Lock.unlock();
132   llvm::Optional<tooling::CompileCommand> Command =
133       InnerCDB.getCompileCommand(File);
134   Lock.lock();
135   return Cached.try_emplace(File, std::move(Command)).first->getValue();
136 }
137 
138 tooling::CompileCommand
139 CachingCompilationDb::getFallbackCommand(PathRef File) const {
140   return InnerCDB.getFallbackCommand(File);
141 }
142 
143 void CachingCompilationDb::invalidate(PathRef File) {
144   std::unique_lock<std::mutex> Lock(Mut);
145   Cached.erase(File);
146 }
147 
148 void CachingCompilationDb::clear() {
149   std::unique_lock<std::mutex> Lock(Mut);
150   Cached.clear();
151 }
152 
153 llvm::Optional<tooling::CompileCommand>
154 InMemoryCompilationDb::getCompileCommand(PathRef File) const {
155   std::lock_guard<std::mutex> Lock(Mutex);
156   auto It = Commands.find(File);
157   if (It == Commands.end())
158     return None;
159   return It->second;
160 }
161 
162 bool InMemoryCompilationDb::setCompilationCommandForFile(
163     PathRef File, tooling::CompileCommand CompilationCommand) {
164   std::unique_lock<std::mutex> Lock(Mutex);
165   auto ItInserted = Commands.insert(std::make_pair(File, CompilationCommand));
166   if (ItInserted.second)
167     return true;
168   ItInserted.first->setValue(std::move(CompilationCommand));
169   return false;
170 }
171 
172 void InMemoryCompilationDb::invalidate(PathRef File) {
173   std::unique_lock<std::mutex> Lock(Mutex);
174   Commands.erase(File);
175 }
176 
177 } // namespace clangd
178 } // namespace clang
179