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