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 "clang/Tooling/CompilationDatabase.h" 12 #include "llvm/Support/FileSystem.h" 13 #include "llvm/Support/Path.h" 14 #include "Logger.h" 15 16 namespace clang { 17 namespace clangd { 18 19 static void addExtraFlags(tooling::CompileCommand &Command, 20 const std::vector<std::string> &ExtraFlags) { 21 if (ExtraFlags.empty()) 22 return; 23 assert(Command.CommandLine.size() >= 2 && 24 "Expected a command line containing at least 2 arguments, the " 25 "compiler binary and the output file"); 26 // The last argument of CommandLine is the name of the input file. 27 // Add ExtraFlags before it. 28 auto It = Command.CommandLine.end(); 29 --It; 30 Command.CommandLine.insert(It, ExtraFlags.begin(), ExtraFlags.end()); 31 } 32 33 tooling::CompileCommand getDefaultCompileCommand(PathRef File) { 34 std::vector<std::string> CommandLine{"clang", "-fsyntax-only", File.str()}; 35 return tooling::CompileCommand(llvm::sys::path::parent_path(File), 36 llvm::sys::path::filename(File), CommandLine, 37 /*Output=*/""); 38 } 39 40 DirectoryBasedGlobalCompilationDatabase:: 41 DirectoryBasedGlobalCompilationDatabase(clangd::Logger &Logger) 42 : Logger(Logger) {} 43 44 std::vector<tooling::CompileCommand> 45 DirectoryBasedGlobalCompilationDatabase::getCompileCommands(PathRef File) { 46 std::vector<tooling::CompileCommand> Commands; 47 48 auto CDB = getCompilationDatabase(File); 49 if (CDB) 50 Commands = CDB->getCompileCommands(File); 51 if (Commands.empty()) 52 Commands.push_back(getDefaultCompileCommand(File)); 53 54 auto It = ExtraFlagsForFile.find(File); 55 if (It != ExtraFlagsForFile.end()) { 56 // Append the user-specified flags to the compile commands. 57 for (tooling::CompileCommand &Command : Commands) 58 addExtraFlags(Command, It->second); 59 } 60 61 return Commands; 62 } 63 64 void DirectoryBasedGlobalCompilationDatabase::setExtraFlagsForFile( 65 PathRef File, std::vector<std::string> ExtraFlags) { 66 ExtraFlagsForFile[File] = std::move(ExtraFlags); 67 } 68 69 tooling::CompilationDatabase * 70 DirectoryBasedGlobalCompilationDatabase::getCompilationDatabase(PathRef File) { 71 std::lock_guard<std::mutex> Lock(Mutex); 72 73 namespace path = llvm::sys::path; 74 75 assert((path::is_absolute(File, path::Style::posix) || 76 path::is_absolute(File, path::Style::windows)) && 77 "path must be absolute"); 78 79 for (auto Path = path::parent_path(File); !Path.empty(); 80 Path = path::parent_path(Path)) { 81 82 auto CachedIt = CompilationDatabases.find(Path); 83 if (CachedIt != CompilationDatabases.end()) 84 return CachedIt->second.get(); 85 86 std::string Error; 87 auto CDB = tooling::CompilationDatabase::loadFromDirectory(Path, Error); 88 if (!CDB) 89 continue; 90 91 // FIXME(ibiryukov): Invalidate cached compilation databases on changes 92 auto Result = CDB.get(); 93 CompilationDatabases.insert(std::make_pair(Path, std::move(CDB))); 94 return Result; 95 } 96 97 Logger.log("Failed to find compilation database for " + Twine(File) + "\n"); 98 return nullptr; 99 } 100 101 } // namespace clangd 102 } // namespace clang 103