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::setExtraFlagsForFile( 55 PathRef File, std::vector<std::string> ExtraFlags) { 56 std::lock_guard<std::mutex> Lock(Mutex); 57 ExtraFlagsForFile[File] = std::move(ExtraFlags); 58 } 59 60 void DirectoryBasedGlobalCompilationDatabase::addExtraFlags( 61 PathRef File, tooling::CompileCommand &C) const { 62 std::lock_guard<std::mutex> Lock(Mutex); 63 64 auto It = ExtraFlagsForFile.find(File); 65 if (It == ExtraFlagsForFile.end()) 66 return; 67 68 auto &Args = C.CommandLine; 69 assert(Args.size() >= 2 && "Expected at least [compiler, source file]"); 70 // The last argument of CommandLine is the name of the input file. 71 // Add ExtraFlags before it. 72 Args.insert(Args.end() - 1, It->second.begin(), It->second.end()); 73 } 74 75 tooling::CompilationDatabase * 76 DirectoryBasedGlobalCompilationDatabase::getCDBInDirLocked(PathRef Dir) const { 77 // FIXME(ibiryukov): Invalidate cached compilation databases on changes 78 auto CachedIt = CompilationDatabases.find(Dir); 79 if (CachedIt != CompilationDatabases.end()) 80 return CachedIt->second.get(); 81 std::string Error = ""; 82 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Dir, Error); 83 auto Result = CDB.get(); 84 CompilationDatabases.insert(std::make_pair(Dir, std::move(CDB))); 85 return Result; 86 } 87 88 tooling::CompilationDatabase * 89 DirectoryBasedGlobalCompilationDatabase::getCDBForFile(PathRef File) const { 90 namespace path = llvm::sys::path; 91 assert((path::is_absolute(File, path::Style::posix) || 92 path::is_absolute(File, path::Style::windows)) && 93 "path must be absolute"); 94 95 std::lock_guard<std::mutex> Lock(Mutex); 96 if (CompileCommandsDir) 97 return getCDBInDirLocked(*CompileCommandsDir); 98 for (auto Path = path::parent_path(File); !Path.empty(); 99 Path = path::parent_path(Path)) 100 if (auto CDB = getCDBInDirLocked(Path)) 101 return CDB; 102 return nullptr; 103 } 104 105 } // namespace clangd 106 } // namespace clang 107