1 //===--- CompilationDatabase.cpp - ----------------------------------------===// 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 // This file contains implementations of the CompilationDatabase base class 11 // and the FixedCompilationDatabase. 12 // 13 // FIXME: Various functions that take a string &ErrorMessage should be upgraded 14 // to Expected. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "clang/Tooling/CompilationDatabase.h" 19 #include "clang/Basic/Diagnostic.h" 20 #include "clang/Basic/DiagnosticOptions.h" 21 #include "clang/Driver/Action.h" 22 #include "clang/Driver/Compilation.h" 23 #include "clang/Driver/Driver.h" 24 #include "clang/Driver/DriverDiagnostic.h" 25 #include "clang/Driver/Job.h" 26 #include "clang/Frontend/TextDiagnosticPrinter.h" 27 #include "clang/Tooling/CompilationDatabasePluginRegistry.h" 28 #include "clang/Tooling/Tooling.h" 29 #include "llvm/ADT/SmallString.h" 30 #include "llvm/Option/Arg.h" 31 #include "llvm/Support/Host.h" 32 #include "llvm/Support/LineIterator.h" 33 #include "llvm/Support/Path.h" 34 #include "llvm/Support/raw_ostream.h" 35 #include <sstream> 36 #include <system_error> 37 using namespace clang; 38 using namespace tooling; 39 40 LLVM_INSTANTIATE_REGISTRY(CompilationDatabasePluginRegistry) 41 42 CompilationDatabase::~CompilationDatabase() {} 43 44 std::unique_ptr<CompilationDatabase> 45 CompilationDatabase::loadFromDirectory(StringRef BuildDirectory, 46 std::string &ErrorMessage) { 47 llvm::raw_string_ostream ErrorStream(ErrorMessage); 48 for (CompilationDatabasePluginRegistry::iterator 49 It = CompilationDatabasePluginRegistry::begin(), 50 Ie = CompilationDatabasePluginRegistry::end(); 51 It != Ie; ++It) { 52 std::string DatabaseErrorMessage; 53 std::unique_ptr<CompilationDatabasePlugin> Plugin(It->instantiate()); 54 if (std::unique_ptr<CompilationDatabase> DB = 55 Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage)) 56 return DB; 57 ErrorStream << It->getName() << ": " << DatabaseErrorMessage << "\n"; 58 } 59 return nullptr; 60 } 61 62 static std::unique_ptr<CompilationDatabase> 63 findCompilationDatabaseFromDirectory(StringRef Directory, 64 std::string &ErrorMessage) { 65 std::stringstream ErrorStream; 66 bool HasErrorMessage = false; 67 while (!Directory.empty()) { 68 std::string LoadErrorMessage; 69 70 if (std::unique_ptr<CompilationDatabase> DB = 71 CompilationDatabase::loadFromDirectory(Directory, LoadErrorMessage)) 72 return DB; 73 74 if (!HasErrorMessage) { 75 ErrorStream << "No compilation database found in " << Directory.str() 76 << " or any parent directory\n" << LoadErrorMessage; 77 HasErrorMessage = true; 78 } 79 80 Directory = llvm::sys::path::parent_path(Directory); 81 } 82 ErrorMessage = ErrorStream.str(); 83 return nullptr; 84 } 85 86 std::unique_ptr<CompilationDatabase> 87 CompilationDatabase::autoDetectFromSource(StringRef SourceFile, 88 std::string &ErrorMessage) { 89 SmallString<1024> AbsolutePath(getAbsolutePath(SourceFile)); 90 StringRef Directory = llvm::sys::path::parent_path(AbsolutePath); 91 92 std::unique_ptr<CompilationDatabase> DB = 93 findCompilationDatabaseFromDirectory(Directory, ErrorMessage); 94 95 if (!DB) 96 ErrorMessage = ("Could not auto-detect compilation database for file \"" + 97 SourceFile + "\"\n" + ErrorMessage).str(); 98 return DB; 99 } 100 101 std::unique_ptr<CompilationDatabase> 102 CompilationDatabase::autoDetectFromDirectory(StringRef SourceDir, 103 std::string &ErrorMessage) { 104 SmallString<1024> AbsolutePath(getAbsolutePath(SourceDir)); 105 106 std::unique_ptr<CompilationDatabase> DB = 107 findCompilationDatabaseFromDirectory(AbsolutePath, ErrorMessage); 108 109 if (!DB) 110 ErrorMessage = ("Could not auto-detect compilation database from directory \"" + 111 SourceDir + "\"\n" + ErrorMessage).str(); 112 return DB; 113 } 114 115 CompilationDatabasePlugin::~CompilationDatabasePlugin() {} 116 117 namespace { 118 // Helper for recursively searching through a chain of actions and collecting 119 // all inputs, direct and indirect, of compile jobs. 120 struct CompileJobAnalyzer { 121 void run(const driver::Action *A) { 122 runImpl(A, false); 123 } 124 125 SmallVector<std::string, 2> Inputs; 126 127 private: 128 129 void runImpl(const driver::Action *A, bool Collect) { 130 bool CollectChildren = Collect; 131 switch (A->getKind()) { 132 case driver::Action::CompileJobClass: 133 CollectChildren = true; 134 break; 135 136 case driver::Action::InputClass: { 137 if (Collect) { 138 const driver::InputAction *IA = cast<driver::InputAction>(A); 139 Inputs.push_back(IA->getInputArg().getSpelling()); 140 } 141 } break; 142 143 default: 144 // Don't care about others 145 ; 146 } 147 148 for (const driver::Action *AI : A->inputs()) 149 runImpl(AI, CollectChildren); 150 } 151 }; 152 153 // Special DiagnosticConsumer that looks for warn_drv_input_file_unused 154 // diagnostics from the driver and collects the option strings for those unused 155 // options. 156 class UnusedInputDiagConsumer : public DiagnosticConsumer { 157 public: 158 UnusedInputDiagConsumer(DiagnosticConsumer &Other) : Other(Other) {} 159 160 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, 161 const Diagnostic &Info) override { 162 if (Info.getID() == clang::diag::warn_drv_input_file_unused) { 163 // Arg 1 for this diagnostic is the option that didn't get used. 164 UnusedInputs.push_back(Info.getArgStdStr(0)); 165 } else if (DiagLevel >= DiagnosticsEngine::Error) { 166 // If driver failed to create compilation object, show the diagnostics 167 // to user. 168 Other.HandleDiagnostic(DiagLevel, Info); 169 } 170 } 171 172 DiagnosticConsumer &Other; 173 SmallVector<std::string, 2> UnusedInputs; 174 }; 175 176 // Unary functor for asking "Given a StringRef S1, does there exist a string 177 // S2 in Arr where S1 == S2?" 178 struct MatchesAny { 179 MatchesAny(ArrayRef<std::string> Arr) : Arr(Arr) {} 180 bool operator() (StringRef S) { 181 for (const std::string *I = Arr.begin(), *E = Arr.end(); I != E; ++I) 182 if (*I == S) 183 return true; 184 return false; 185 } 186 private: 187 ArrayRef<std::string> Arr; 188 }; 189 } // namespace 190 191 /// \brief Strips any positional args and possible argv[0] from a command-line 192 /// provided by the user to construct a FixedCompilationDatabase. 193 /// 194 /// FixedCompilationDatabase requires a command line to be in this format as it 195 /// constructs the command line for each file by appending the name of the file 196 /// to be compiled. FixedCompilationDatabase also adds its own argv[0] to the 197 /// start of the command line although its value is not important as it's just 198 /// ignored by the Driver invoked by the ClangTool using the 199 /// FixedCompilationDatabase. 200 /// 201 /// FIXME: This functionality should probably be made available by 202 /// clang::driver::Driver although what the interface should look like is not 203 /// clear. 204 /// 205 /// \param[in] Args Args as provided by the user. 206 /// \return Resulting stripped command line. 207 /// \li true if successful. 208 /// \li false if \c Args cannot be used for compilation jobs (e.g. 209 /// contains an option like -E or -version). 210 static bool stripPositionalArgs(std::vector<const char *> Args, 211 std::vector<std::string> &Result, 212 std::string &ErrorMsg) { 213 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); 214 llvm::raw_string_ostream Output(ErrorMsg); 215 TextDiagnosticPrinter DiagnosticPrinter(Output, &*DiagOpts); 216 UnusedInputDiagConsumer DiagClient(DiagnosticPrinter); 217 DiagnosticsEngine Diagnostics( 218 IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), 219 &*DiagOpts, &DiagClient, false); 220 221 // The clang executable path isn't required since the jobs the driver builds 222 // will not be executed. 223 std::unique_ptr<driver::Driver> NewDriver(new driver::Driver( 224 /* ClangExecutable= */ "", llvm::sys::getDefaultTargetTriple(), 225 Diagnostics)); 226 NewDriver->setCheckInputsExist(false); 227 228 // This becomes the new argv[0]. The value is actually not important as it 229 // isn't used for invoking Tools. 230 Args.insert(Args.begin(), "clang-tool"); 231 232 // By adding -c, we force the driver to treat compilation as the last phase. 233 // It will then issue warnings via Diagnostics about un-used options that 234 // would have been used for linking. If the user provided a compiler name as 235 // the original argv[0], this will be treated as a linker input thanks to 236 // insertng a new argv[0] above. All un-used options get collected by 237 // UnusedInputdiagConsumer and get stripped out later. 238 Args.push_back("-c"); 239 240 // Put a dummy C++ file on to ensure there's at least one compile job for the 241 // driver to construct. If the user specified some other argument that 242 // prevents compilation, e.g. -E or something like -version, we may still end 243 // up with no jobs but then this is the user's fault. 244 Args.push_back("placeholder.cpp"); 245 246 // Remove -no-integrated-as; it's not used for syntax checking, 247 // and it confuses targets which don't support this option. 248 Args.erase(std::remove_if(Args.begin(), Args.end(), 249 MatchesAny(std::string("-no-integrated-as"))), 250 Args.end()); 251 252 const std::unique_ptr<driver::Compilation> Compilation( 253 NewDriver->BuildCompilation(Args)); 254 if (!Compilation) 255 return false; 256 257 const driver::JobList &Jobs = Compilation->getJobs(); 258 259 CompileJobAnalyzer CompileAnalyzer; 260 261 for (const auto &Cmd : Jobs) { 262 // Collect only for Assemble and Compile jobs. If we do all jobs we get 263 // duplicates since Link jobs point to Assemble jobs as inputs. 264 if (Cmd.getSource().getKind() == driver::Action::AssembleJobClass || 265 Cmd.getSource().getKind() == driver::Action::CompileJobClass) { 266 CompileAnalyzer.run(&Cmd.getSource()); 267 } 268 } 269 270 if (CompileAnalyzer.Inputs.empty()) { 271 ErrorMsg = "warning: no compile jobs found\n"; 272 return false; 273 } 274 275 // Remove all compilation input files from the command line. This is 276 // necessary so that getCompileCommands() can construct a command line for 277 // each file. 278 std::vector<const char *>::iterator End = std::remove_if( 279 Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs)); 280 281 // Remove all inputs deemed unused for compilation. 282 End = std::remove_if(Args.begin(), End, MatchesAny(DiagClient.UnusedInputs)); 283 284 // Remove the -c add above as well. It will be at the end right now. 285 assert(strcmp(*(End - 1), "-c") == 0); 286 --End; 287 288 Result = std::vector<std::string>(Args.begin() + 1, End); 289 return true; 290 } 291 292 std::unique_ptr<FixedCompilationDatabase> 293 FixedCompilationDatabase::loadFromCommandLine(int &Argc, 294 const char *const *Argv, 295 std::string &ErrorMsg, 296 Twine Directory) { 297 ErrorMsg.clear(); 298 if (Argc == 0) 299 return nullptr; 300 const char *const *DoubleDash = std::find(Argv, Argv + Argc, StringRef("--")); 301 if (DoubleDash == Argv + Argc) 302 return nullptr; 303 std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc); 304 Argc = DoubleDash - Argv; 305 306 std::vector<std::string> StrippedArgs; 307 if (!stripPositionalArgs(CommandLine, StrippedArgs, ErrorMsg)) 308 return nullptr; 309 return llvm::make_unique<FixedCompilationDatabase>(Directory, StrippedArgs); 310 } 311 312 std::unique_ptr<FixedCompilationDatabase> 313 FixedCompilationDatabase::loadFromFile(StringRef Path, std::string &ErrorMsg) { 314 ErrorMsg.clear(); 315 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File = 316 llvm::MemoryBuffer::getFile(Path); 317 if (std::error_code Result = File.getError()) { 318 ErrorMsg = "Error while opening fixed database: " + Result.message(); 319 return nullptr; 320 } 321 std::vector<std::string> Args{llvm::line_iterator(**File), 322 llvm::line_iterator()}; 323 return llvm::make_unique<FixedCompilationDatabase>( 324 llvm::sys::path::parent_path(Path), std::move(Args)); 325 } 326 327 FixedCompilationDatabase:: 328 FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine) { 329 std::vector<std::string> ToolCommandLine(1, "clang-tool"); 330 ToolCommandLine.insert(ToolCommandLine.end(), 331 CommandLine.begin(), CommandLine.end()); 332 CompileCommands.emplace_back(Directory, StringRef(), 333 std::move(ToolCommandLine), 334 StringRef()); 335 } 336 337 std::vector<CompileCommand> 338 FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const { 339 std::vector<CompileCommand> Result(CompileCommands); 340 Result[0].CommandLine.push_back(FilePath); 341 Result[0].Filename = FilePath; 342 return Result; 343 } 344 345 std::vector<std::string> 346 FixedCompilationDatabase::getAllFiles() const { 347 return std::vector<std::string>(); 348 } 349 350 std::vector<CompileCommand> 351 FixedCompilationDatabase::getAllCompileCommands() const { 352 return std::vector<CompileCommand>(); 353 } 354 355 namespace { 356 357 class FixedCompilationDatabasePlugin : public CompilationDatabasePlugin { 358 std::unique_ptr<CompilationDatabase> 359 loadFromDirectory(StringRef Directory, std::string &ErrorMessage) override { 360 SmallString<1024> DatabasePath(Directory); 361 llvm::sys::path::append(DatabasePath, "compile_flags.txt"); 362 return FixedCompilationDatabase::loadFromFile(DatabasePath, ErrorMessage); 363 } 364 }; 365 366 static CompilationDatabasePluginRegistry::Add<FixedCompilationDatabasePlugin> 367 X("fixed-compilation-database", "Reads plain-text flags file"); 368 369 } // namespace 370 371 namespace clang { 372 namespace tooling { 373 374 // This anchor is used to force the linker to link in the generated object file 375 // and thus register the JSONCompilationDatabasePlugin. 376 extern volatile int JSONAnchorSource; 377 static int LLVM_ATTRIBUTE_UNUSED JSONAnchorDest = JSONAnchorSource; 378 379 } // end namespace tooling 380 } // end namespace clang 381