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 using namespace llvm;
17 namespace clang {
18 namespace clangd {
19 
20 tooling::CompileCommand
21 GlobalCompilationDatabase::getFallbackCommand(PathRef File) const {
22   std::vector<std::string> Argv = {"clang"};
23   // Clang treats .h files as C by default, resulting in unhelpful diagnostics.
24   // Parsing as Objective C++ is friendly to more cases.
25   if (sys::path::extension(File) == ".h")
26     Argv.push_back("-xobjective-c++-header");
27   Argv.push_back(File);
28   return tooling::CompileCommand(sys::path::parent_path(File),
29                                  sys::path::filename(File), std::move(Argv),
30                                  /*Output=*/"");
31 }
32 
33 DirectoryBasedGlobalCompilationDatabase::
34     DirectoryBasedGlobalCompilationDatabase(Optional<Path> CompileCommandsDir)
35     : CompileCommandsDir(std::move(CompileCommandsDir)) {}
36 
37 DirectoryBasedGlobalCompilationDatabase::
38     ~DirectoryBasedGlobalCompilationDatabase() = default;
39 
40 Optional<tooling::CompileCommand>
41 DirectoryBasedGlobalCompilationDatabase::getCompileCommand(PathRef File) const {
42   if (auto CDB = getCDBForFile(File)) {
43     auto Candidates = CDB->getCompileCommands(File);
44     if (!Candidates.empty())
45       return std::move(Candidates.front());
46   } else {
47     log("Failed to find compilation database for {0}", File);
48   }
49   return None;
50 }
51 
52 tooling::CompilationDatabase *
53 DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const {
54   // FIXME(ibiryukov): Invalidate cached compilation databases on changes
55   auto CachedIt = CompilationDatabases.find(Dir);
56   if (CachedIt != CompilationDatabases.end())
57     return CachedIt->second.get();
58   std::string Error = "";
59   auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error);
60   auto Result = CDB.get();
61   CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB)));
62   return Result;
63 }
64 
65 tooling::CompilationDatabase *
66 DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const {
67   namespace path = sys::path;
68   assert((path::is_absolute(File, path::Style::posix) ||
69           path::is_absolute(File, path::Style::windows)) &&
70          "path must be absolute");
71 
72   std::lock_guard<std::mutex> Lock(Mutex);
73   if (CompileCommandsDir)
74     return getCDBInDirLocked(*CompileCommandsDir);
75   for (auto Path = path::parent_path(File); !Path.empty();
76        Path = path::parent_path(Path))
77     if (auto CDB = getCDBInDirLocked(Path))
78       return CDB;
79   return nullptr;
80 }
81 
82 Optional<tooling::CompileCommand>
83 OverlayCDB::getCompileCommand(PathRef File) const {
84   {
85     std::lock_guard<std::mutex> Lock(Mutex);
86     auto It = Commands.find(File);
87     if (It != Commands.end())
88       return It->second;
89   }
90   return Base ? Base->getCompileCommand(File) : None;
91 }
92 
93 tooling::CompileCommand OverlayCDB::getFallbackCommand(PathRef File) const {
94   auto Cmd = Base ? Base->getFallbackCommand(File)
95                   : GlobalCompilationDatabase::getFallbackCommand(File);
96   std::lock_guard<std::mutex> Lock(Mutex);
97   Cmd.CommandLine.insert(Cmd.CommandLine.end(), FallbackFlags.begin(),
98                          FallbackFlags.end());
99   return Cmd;
100 }
101 
102 void OverlayCDB::setCompileCommand(
103     PathRef File, llvm::Optional<tooling::CompileCommand> Cmd) {
104   std::unique_lock<std::mutex> Lock(Mutex);
105   if (Cmd)
106     Commands[File] = std::move(*Cmd);
107   else
108     Commands.erase(File);
109 }
110 
111 } // namespace clangd
112 } // namespace clang
113