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