Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 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 | // |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 10 | // This file contains implementations of the CompilationDatabase base class |
| 11 | // and the FixedCompilationDatabase. |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Tooling/CompilationDatabase.h" |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 16 | #include "clang/Tooling/CompilationDatabasePluginRegistry.h" |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 17 | #include "clang/Tooling/Tooling.h" |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Path.h" |
| 20 | #include "llvm/Support/system_error.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include <sstream> |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 22 | |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 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 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 33 | namespace clang { |
| 34 | namespace tooling { |
| 35 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 36 | CompilationDatabase::~CompilationDatabase() {} |
| 37 | |
| 38 | CompilationDatabase * |
| 39 | CompilationDatabase::loadFromDirectory(StringRef BuildDirectory, |
| 40 | std::string &ErrorMessage) { |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 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"; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 53 | } |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 54 | ErrorMessage = ErrorStream.str(); |
| 55 | return NULL; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 58 | static CompilationDatabase * |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 59 | findCompilationDatabaseFromDirectory(StringRef Directory, |
| 60 | std::string &ErrorMessage) { |
| 61 | std::stringstream ErrorStream; |
Daniel Jasper | 3d21078 | 2012-10-15 13:12:24 +0000 | [diff] [blame] | 62 | bool HasErrorMessage = false; |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 63 | while (!Directory.empty()) { |
| 64 | std::string LoadErrorMessage; |
| 65 | |
| 66 | if (CompilationDatabase *DB = |
| 67 | CompilationDatabase::loadFromDirectory(Directory, LoadErrorMessage)) |
| 68 | return DB; |
Daniel Jasper | 3d21078 | 2012-10-15 13:12:24 +0000 | [diff] [blame] | 69 | |
| 70 | if (!HasErrorMessage) { |
| 71 | ErrorStream << "No compilation database found in " << Directory.str() |
| 72 | << " or any parent directory\n" << LoadErrorMessage; |
| 73 | HasErrorMessage = true; |
| 74 | } |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 75 | |
| 76 | Directory = llvm::sys::path::parent_path(Directory); |
| 77 | } |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 78 | ErrorMessage = ErrorStream.str(); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 79 | return NULL; |
| 80 | } |
| 81 | |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 82 | CompilationDatabase * |
| 83 | CompilationDatabase::autoDetectFromSource(StringRef SourceFile, |
| 84 | std::string &ErrorMessage) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 85 | SmallString<1024> AbsolutePath(getAbsolutePath(SourceFile)); |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 86 | StringRef Directory = llvm::sys::path::parent_path(AbsolutePath); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 87 | |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 88 | CompilationDatabase *DB = findCompilationDatabaseFromDirectory(Directory, |
| 89 | ErrorMessage); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 90 | |
| 91 | if (!DB) |
| 92 | ErrorMessage = ("Could not auto-detect compilation database for file \"" + |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 93 | SourceFile + "\"\n" + ErrorMessage).str(); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 94 | return DB; |
| 95 | } |
| 96 | |
| 97 | CompilationDatabase * |
| 98 | CompilationDatabase::autoDetectFromDirectory(StringRef SourceDir, |
| 99 | std::string &ErrorMessage) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 100 | SmallString<1024> AbsolutePath(getAbsolutePath(SourceDir)); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 101 | |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 102 | CompilationDatabase *DB = findCompilationDatabaseFromDirectory(AbsolutePath, |
| 103 | ErrorMessage); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 104 | |
| 105 | if (!DB) |
| 106 | ErrorMessage = ("Could not auto-detect compilation database from directory \"" + |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 107 | SourceDir + "\"\n" + ErrorMessage).str(); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 108 | return DB; |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 111 | CompilationDatabasePlugin::~CompilationDatabasePlugin() {} |
| 112 | |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 113 | // Helper for recursively searching through a chain of actions and collecting |
| 114 | // all inputs, direct and indirect, of compile jobs. |
| 115 | struct CompileJobAnalyzer { |
| 116 | void run(const driver::Action *A) { |
| 117 | runImpl(A, false); |
| 118 | } |
| 119 | |
| 120 | SmallVector<std::string, 2> Inputs; |
| 121 | |
| 122 | private: |
| 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. |
| 152 | class UnusedInputDiagConsumer : public DiagnosticConsumer { |
| 153 | public: |
| 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?" |
| 176 | struct 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 | } |
| 184 | private: |
| 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. |
NAKAMURA Takumi | f2e479e | 2013-11-17 18:05:49 +0000 | [diff] [blame] | 203 | /// \return Resulting stripped command line. |
| 204 | /// \li true if successful. |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 205 | /// \li false if \c Args cannot be used for compilation jobs (e.g. |
| 206 | /// contains an option like -E or -version). |
| 207 | bool stripPositionalArgs(std::vector<const char *> Args, |
| 208 | std::vector<std::string> &Result) { |
| 209 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 210 | UnusedInputDiagConsumer DiagClient; |
| 211 | DiagnosticsEngine Diagnostics( |
| 212 | IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), |
| 213 | &*DiagOpts, &DiagClient, false); |
| 214 | |
| 215 | // Neither clang executable nor default image name are required since the |
| 216 | // jobs the driver builds will not be executed. |
| 217 | OwningPtr<driver::Driver> NewDriver(new driver::Driver( |
| 218 | /* ClangExecutable= */ "", llvm::sys::getDefaultTargetTriple(), |
| 219 | /* DefaultImageName= */ "", Diagnostics)); |
| 220 | NewDriver->setCheckInputsExist(false); |
| 221 | |
| 222 | // This becomes the new argv[0]. The value is actually not important as it |
| 223 | // isn't used for invoking Tools. |
| 224 | Args.insert(Args.begin(), "clang-tool"); |
| 225 | |
| 226 | // By adding -c, we force the driver to treat compilation as the last phase. |
| 227 | // It will then issue warnings via Diagnostics about un-used options that |
| 228 | // would have been used for linking. If the user provided a compiler name as |
| 229 | // the original argv[0], this will be treated as a linker input thanks to |
| 230 | // insertng a new argv[0] above. All un-used options get collected by |
| 231 | // UnusedInputdiagConsumer and get stripped out later. |
| 232 | Args.push_back("-c"); |
| 233 | |
| 234 | // Put a dummy C++ file on to ensure there's at least one compile job for the |
| 235 | // driver to construct. If the user specified some other argument that |
| 236 | // prevents compilation, e.g. -E or something like -version, we may still end |
| 237 | // up with no jobs but then this is the user's fault. |
| 238 | Args.push_back("placeholder.cpp"); |
| 239 | |
| 240 | const OwningPtr<driver::Compilation> Compilation( |
| 241 | NewDriver->BuildCompilation(Args)); |
| 242 | |
| 243 | const driver::JobList &Jobs = Compilation->getJobs(); |
| 244 | |
| 245 | CompileJobAnalyzer CompileAnalyzer; |
| 246 | |
| 247 | for (driver::JobList::const_iterator I = Jobs.begin(), E = Jobs.end(); I != E; |
| 248 | ++I) { |
| 249 | if ((*I)->getKind() == driver::Job::CommandClass) { |
| 250 | const driver::Command *Cmd = cast<driver::Command>(*I); |
| 251 | // Collect only for Assemble jobs. If we do all jobs we get duplicates |
| 252 | // since Link jobs point to Assemble jobs as inputs. |
| 253 | if (Cmd->getSource().getKind() == driver::Action::AssembleJobClass) |
| 254 | CompileAnalyzer.run(&Cmd->getSource()); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (CompileAnalyzer.Inputs.empty()) { |
| 259 | // No compile jobs found. |
| 260 | // FIXME: Emit a warning of some kind? |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | // Remove all compilation input files from the command line. This is |
| 265 | // necessary so that getCompileCommands() can construct a command line for |
| 266 | // each file. |
| 267 | std::vector<const char *>::iterator End = std::remove_if( |
| 268 | Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs)); |
| 269 | |
| 270 | // Remove all inputs deemed unused for compilation. |
| 271 | End = std::remove_if(Args.begin(), End, MatchesAny(DiagClient.UnusedInputs)); |
| 272 | |
| 273 | // Remove the -c add above as well. It will be at the end right now. |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 274 | --End; |
| 275 | |
| 276 | Result = std::vector<std::string>(Args.begin() + 1, End); |
| 277 | return true; |
| 278 | } |
| 279 | |
Manuel Klimek | 30318e6 | 2012-04-18 07:41:50 +0000 | [diff] [blame] | 280 | FixedCompilationDatabase * |
| 281 | FixedCompilationDatabase::loadFromCommandLine(int &Argc, |
| 282 | const char **Argv, |
| 283 | Twine Directory) { |
| 284 | const char **DoubleDash = std::find(Argv, Argv + Argc, StringRef("--")); |
| 285 | if (DoubleDash == Argv + Argc) |
| 286 | return NULL; |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 287 | std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc); |
Manuel Klimek | 30318e6 | 2012-04-18 07:41:50 +0000 | [diff] [blame] | 288 | Argc = DoubleDash - Argv; |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 289 | |
| 290 | std::vector<std::string> StrippedArgs; |
| 291 | if (!stripPositionalArgs(CommandLine, StrippedArgs)) |
| 292 | return 0; |
| 293 | return new FixedCompilationDatabase(Directory, StrippedArgs); |
Manuel Klimek | 30318e6 | 2012-04-18 07:41:50 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | FixedCompilationDatabase:: |
| 297 | FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine) { |
| 298 | std::vector<std::string> ToolCommandLine(1, "clang-tool"); |
| 299 | ToolCommandLine.insert(ToolCommandLine.end(), |
| 300 | CommandLine.begin(), CommandLine.end()); |
| 301 | CompileCommands.push_back(CompileCommand(Directory, ToolCommandLine)); |
| 302 | } |
| 303 | |
| 304 | std::vector<CompileCommand> |
| 305 | FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const { |
| 306 | std::vector<CompileCommand> Result(CompileCommands); |
| 307 | Result[0].CommandLine.push_back(FilePath); |
| 308 | return Result; |
| 309 | } |
| 310 | |
Manuel Klimek | a3c7096 | 2012-07-13 12:31:45 +0000 | [diff] [blame] | 311 | std::vector<std::string> |
| 312 | FixedCompilationDatabase::getAllFiles() const { |
| 313 | return std::vector<std::string>(); |
| 314 | } |
| 315 | |
Argyrios Kyrtzidis | 7e96bfb | 2012-12-04 07:26:44 +0000 | [diff] [blame] | 316 | std::vector<CompileCommand> |
| 317 | FixedCompilationDatabase::getAllCompileCommands() const { |
| 318 | return std::vector<CompileCommand>(); |
| 319 | } |
| 320 | |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 321 | // This anchor is used to force the linker to link in the generated object file |
| 322 | // and thus register the JSONCompilationDatabasePlugin. |
| 323 | extern volatile int JSONAnchorSource; |
| 324 | static int JSONAnchorDest = JSONAnchorSource; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 325 | |
| 326 | } // end namespace tooling |
| 327 | } // end namespace clang |