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