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   return tooling::CompileCommand(llvm::sys::path::parent_path(File),
22                                  llvm::sys::path::filename(File),
23                                  {"clang", File.str()},
24                                  /*Output=*/"");
25 }
26 
27 DirectoryBasedGlobalCompilationDatabase::
28     DirectoryBasedGlobalCompilationDatabase(
29         llvm::Optional<Path> CompileCommandsDir)
30     : CompileCommandsDir(std::move(CompileCommandsDir)) {}
31 
32 llvm::Optional<tooling::CompileCommand>
33 DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const {
34   if (auto CDB = getCDBForFile(File)) {
35     auto Candidates = CDB->getCompileCommands(File);
36     if (!Candidates.empty()) {
37       addExtraFlags(File, Candidates.front());
38       return std::move(Candidates.front());
39     }
40   } else {
41     log("Failed to find compilation database for " + Twine(File));
42   }
43   return llvm::None;
44 }
45 
46 tooling::CompileCommand
47 DirectoryBasedGlobalCompilationDatabase::getFallbackCommand(
48     PathRef File) const {
49   auto C = GlobalCompilationDatabase::getFallbackCommand(File);
50   addExtraFlags(File, C);
51   return C;
52 }
53 
54 void DirectoryBasedGlobalCompilationDatabase::setCompileCommandsDir(Path P) {
55   std::lock_guard<std::mutex> Lock(Mutex);
56   CompileCommandsDir = P;
57   CompilationDatabases.clear();
58 }
59 
60 void DirectoryBasedGlobalCompilationDatabase::setExtraFlagsForFile(
61     PathRef File, std::vector<std::string> ExtraFlags) {
62   std::lock_guard<std::mutex> Lock(Mutex);
63   ExtraFlagsForFile[File] = std::move(ExtraFlags);
64 }
65 
66 void DirectoryBasedGlobalCompilationDatabase::addExtraFlags(
67     PathRef File, tooling::CompileCommand &C) const {
68   std::lock_guard<std::mutex> Lock(Mutex);
69 
70   auto It = ExtraFlagsForFile.find(File);
71   if (It == ExtraFlagsForFile.end())
72     return;
73 
74   auto &Args = C.CommandLine;
75   assert(Args.size() >= 2 && "Expected at least [compiler, source file]");
76   // The last argument of CommandLine is the name of the input file.
77   // Add ExtraFlags before it.
78   Args.insert(Args.end() - 1, It->second.begin(), It->second.end());
79 }
80 
81 tooling::CompilationDatabase *
82 DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
83   // FIXME(ibiryukov): Invalidate cached compilation databases on changes
84   auto CachedIt = CompilationDatabases.find(Dir);
85   if (CachedIt != CompilationDatabases.end())
86     return CachedIt->second.get();
87   std::string Error = "";
88   auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
89   auto Result = CDB.get();
90   CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
91   return Result;
92 }
93 
94 tooling::CompilationDatabase *
95 DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const {
96   namespace path = llvm::sys::path;
97   assert((path::is_absolute(File, path::Style::posix) ||
98           path::is_absolute(File, path::Style::windows)) &&
99          "path must be absolute");
100 
101   std::lock_guard<std::mutex> Lock(Mutex);
102   if (CompileCommandsDir)
103     return getCDBInDirLocked(*CompileCommandsDir);
104   for (auto Path = path::parent_path(File); !Path.empty();
105        Path = path::parent_path(Path))
106     if (auto CDB = getCDBInDirLocked(Path))
107       return CDB;
108   return nullptr;
109 }
110 
111 } // namespace clangd
112 } // namespace clang
113