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