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" |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 17 | #include "clang/Basic/DiagnosticOptions.h" |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 18 | #include "clang/Driver/Action.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 19 | #include "clang/Driver/Compilation.h" |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 20 | #include "clang/Driver/Driver.h" |
| 21 | #include "clang/Driver/DriverDiagnostic.h" |
| 22 | #include "clang/Driver/Job.h" |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 24 | #include "clang/Tooling/CompilationDatabasePluginRegistry.h" |
| 25 | #include "clang/Tooling/Tooling.h" |
| 26 | #include "llvm/ADT/SmallString.h" |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 27 | #include "llvm/Option/Arg.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 28 | #include "llvm/Support/Host.h" |
| 29 | #include "llvm/Support/Path.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 30 | #include <sstream> |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 31 | #include <system_error> |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 32 | using namespace clang; |
| 33 | using namespace tooling; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 34 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 35 | CompilationDatabase::~CompilationDatabase() {} |
| 36 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 37 | std::unique_ptr<CompilationDatabase> |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 38 | CompilationDatabase::loadFromDirectory(StringRef BuildDirectory, |
| 39 | std::string &ErrorMessage) { |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 40 | std::stringstream ErrorStream; |
| 41 | for (CompilationDatabasePluginRegistry::iterator |
| 42 | It = CompilationDatabasePluginRegistry::begin(), |
| 43 | Ie = CompilationDatabasePluginRegistry::end(); |
| 44 | It != Ie; ++It) { |
| 45 | std::string DatabaseErrorMessage; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 46 | std::unique_ptr<CompilationDatabasePlugin> Plugin(It->instantiate()); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 47 | if (std::unique_ptr<CompilationDatabase> DB = |
| 48 | Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage)) |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 49 | return DB; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 50 | ErrorStream << It->getName() << ": " << DatabaseErrorMessage << "\n"; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 51 | } |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 52 | ErrorMessage = ErrorStream.str(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 53 | return nullptr; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 56 | static std::unique_ptr<CompilationDatabase> |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 57 | findCompilationDatabaseFromDirectory(StringRef Directory, |
| 58 | std::string &ErrorMessage) { |
| 59 | std::stringstream ErrorStream; |
Daniel Jasper | 3d21078 | 2012-10-15 13:12:24 +0000 | [diff] [blame] | 60 | bool HasErrorMessage = false; |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 61 | while (!Directory.empty()) { |
| 62 | std::string LoadErrorMessage; |
| 63 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 64 | if (std::unique_ptr<CompilationDatabase> DB = |
| 65 | CompilationDatabase::loadFromDirectory(Directory, LoadErrorMessage)) |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 66 | return DB; |
Daniel Jasper | 3d21078 | 2012-10-15 13:12:24 +0000 | [diff] [blame] | 67 | |
| 68 | if (!HasErrorMessage) { |
| 69 | ErrorStream << "No compilation database found in " << Directory.str() |
| 70 | << " or any parent directory\n" << LoadErrorMessage; |
| 71 | HasErrorMessage = true; |
| 72 | } |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 73 | |
| 74 | Directory = llvm::sys::path::parent_path(Directory); |
| 75 | } |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 76 | ErrorMessage = ErrorStream.str(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 77 | return nullptr; |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 80 | std::unique_ptr<CompilationDatabase> |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 81 | CompilationDatabase::autoDetectFromSource(StringRef SourceFile, |
| 82 | std::string &ErrorMessage) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 83 | SmallString<1024> AbsolutePath(getAbsolutePath(SourceFile)); |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 84 | StringRef Directory = llvm::sys::path::parent_path(AbsolutePath); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 85 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 86 | std::unique_ptr<CompilationDatabase> DB = |
| 87 | findCompilationDatabaseFromDirectory(Directory, ErrorMessage); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 88 | |
| 89 | if (!DB) |
| 90 | ErrorMessage = ("Could not auto-detect compilation database for file \"" + |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 91 | SourceFile + "\"\n" + ErrorMessage).str(); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 92 | return DB; |
| 93 | } |
| 94 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 95 | std::unique_ptr<CompilationDatabase> |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 96 | CompilationDatabase::autoDetectFromDirectory(StringRef SourceDir, |
| 97 | std::string &ErrorMessage) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 98 | SmallString<1024> AbsolutePath(getAbsolutePath(SourceDir)); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 99 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 100 | std::unique_ptr<CompilationDatabase> DB = |
| 101 | findCompilationDatabaseFromDirectory(AbsolutePath, ErrorMessage); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 102 | |
| 103 | if (!DB) |
| 104 | ErrorMessage = ("Could not auto-detect compilation database from directory \"" + |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 105 | SourceDir + "\"\n" + ErrorMessage).str(); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 106 | return DB; |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 109 | CompilationDatabasePlugin::~CompilationDatabasePlugin() {} |
| 110 | |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 111 | namespace { |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 112 | // Helper for recursively searching through a chain of actions and collecting |
| 113 | // all inputs, direct and indirect, of compile jobs. |
| 114 | struct CompileJobAnalyzer { |
| 115 | void run(const driver::Action *A) { |
| 116 | runImpl(A, false); |
| 117 | } |
| 118 | |
| 119 | SmallVector<std::string, 2> Inputs; |
| 120 | |
| 121 | private: |
| 122 | |
| 123 | void runImpl(const driver::Action *A, bool Collect) { |
| 124 | bool CollectChildren = Collect; |
| 125 | switch (A->getKind()) { |
| 126 | case driver::Action::CompileJobClass: |
| 127 | CollectChildren = true; |
| 128 | break; |
| 129 | |
| 130 | case driver::Action::InputClass: { |
| 131 | if (Collect) { |
| 132 | const driver::InputAction *IA = cast<driver::InputAction>(A); |
| 133 | Inputs.push_back(IA->getInputArg().getSpelling()); |
| 134 | } |
| 135 | } break; |
| 136 | |
| 137 | default: |
| 138 | // Don't care about others |
| 139 | ; |
| 140 | } |
| 141 | |
| 142 | for (driver::ActionList::const_iterator I = A->begin(), E = A->end(); |
| 143 | I != E; ++I) |
| 144 | runImpl(*I, CollectChildren); |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | // Special DiagnosticConsumer that looks for warn_drv_input_file_unused |
| 149 | // diagnostics from the driver and collects the option strings for those unused |
| 150 | // options. |
| 151 | class UnusedInputDiagConsumer : public DiagnosticConsumer { |
| 152 | public: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 153 | UnusedInputDiagConsumer() : Other(nullptr) {} |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 154 | |
| 155 | // Useful for debugging, chain diagnostics to another consumer after |
| 156 | // recording for our own purposes. |
| 157 | UnusedInputDiagConsumer(DiagnosticConsumer *Other) : Other(Other) {} |
| 158 | |
| 159 | virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 160 | const Diagnostic &Info) override { |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 161 | if (Info.getID() == clang::diag::warn_drv_input_file_unused) { |
| 162 | // Arg 1 for this diagnostic is the option that didn't get used. |
| 163 | UnusedInputs.push_back(Info.getArgStdStr(0)); |
| 164 | } |
| 165 | if (Other) |
| 166 | Other->HandleDiagnostic(DiagLevel, Info); |
| 167 | } |
| 168 | |
| 169 | DiagnosticConsumer *Other; |
| 170 | SmallVector<std::string, 2> UnusedInputs; |
| 171 | }; |
| 172 | |
| 173 | // Unary functor for asking "Given a StringRef S1, does there exist a string |
| 174 | // S2 in Arr where S1 == S2?" |
| 175 | struct MatchesAny { |
| 176 | MatchesAny(ArrayRef<std::string> Arr) : Arr(Arr) {} |
| 177 | bool operator() (StringRef S) { |
| 178 | for (const std::string *I = Arr.begin(), *E = Arr.end(); I != E; ++I) |
| 179 | if (*I == S) |
| 180 | return true; |
| 181 | return false; |
| 182 | } |
| 183 | private: |
| 184 | ArrayRef<std::string> Arr; |
| 185 | }; |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 186 | } // namespace |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 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). |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 207 | static bool stripPositionalArgs(std::vector<const char *> Args, |
| 208 | std::vector<std::string> &Result) { |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 209 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
| 210 | UnusedInputDiagConsumer DiagClient; |
| 211 | DiagnosticsEngine Diagnostics( |
| 212 | IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), |
| 213 | &*DiagOpts, &DiagClient, false); |
| 214 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 215 | // The clang executable path isn't required since the jobs the driver builds |
| 216 | // will not be executed. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 217 | std::unique_ptr<driver::Driver> NewDriver(new driver::Driver( |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 218 | /* ClangExecutable= */ "", llvm::sys::getDefaultTargetTriple(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 219 | Diagnostics)); |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 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 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 240 | // Remove -no-integrated-as; it's not used for syntax checking, |
| 241 | // and it confuses targets which don't support this option. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 242 | Args.erase(std::remove_if(Args.begin(), Args.end(), |
| 243 | MatchesAny(std::string("-no-integrated-as"))), |
| 244 | Args.end()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 245 | |
| 246 | const std::unique_ptr<driver::Compilation> Compilation( |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 247 | NewDriver->BuildCompilation(Args)); |
| 248 | |
| 249 | const driver::JobList &Jobs = Compilation->getJobs(); |
| 250 | |
| 251 | CompileJobAnalyzer CompileAnalyzer; |
| 252 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 253 | for (const auto &Job : Jobs) { |
| 254 | if (Job.getKind() == driver::Job::CommandClass) { |
| 255 | const driver::Command &Cmd = cast<driver::Command>(Job); |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 256 | // Collect only for Assemble jobs. If we do all jobs we get duplicates |
| 257 | // since Link jobs point to Assemble jobs as inputs. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 258 | if (Cmd.getSource().getKind() == driver::Action::AssembleJobClass) |
| 259 | CompileAnalyzer.run(&Cmd.getSource()); |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
| 263 | if (CompileAnalyzer.Inputs.empty()) { |
| 264 | // No compile jobs found. |
| 265 | // FIXME: Emit a warning of some kind? |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | // Remove all compilation input files from the command line. This is |
| 270 | // necessary so that getCompileCommands() can construct a command line for |
| 271 | // each file. |
| 272 | std::vector<const char *>::iterator End = std::remove_if( |
| 273 | Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs)); |
| 274 | |
| 275 | // Remove all inputs deemed unused for compilation. |
| 276 | End = std::remove_if(Args.begin(), End, MatchesAny(DiagClient.UnusedInputs)); |
| 277 | |
| 278 | // Remove the -c add above as well. It will be at the end right now. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 279 | assert(strcmp(*(End - 1), "-c") == 0); |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 280 | --End; |
| 281 | |
| 282 | Result = std::vector<std::string>(Args.begin() + 1, End); |
| 283 | return true; |
| 284 | } |
| 285 | |
Manuel Klimek | 30318e6 | 2012-04-18 07:41:50 +0000 | [diff] [blame] | 286 | FixedCompilationDatabase * |
| 287 | FixedCompilationDatabase::loadFromCommandLine(int &Argc, |
| 288 | const char **Argv, |
| 289 | Twine Directory) { |
| 290 | const char **DoubleDash = std::find(Argv, Argv + Argc, StringRef("--")); |
| 291 | if (DoubleDash == Argv + Argc) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 292 | return nullptr; |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 293 | std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc); |
Manuel Klimek | 30318e6 | 2012-04-18 07:41:50 +0000 | [diff] [blame] | 294 | Argc = DoubleDash - Argv; |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 295 | |
| 296 | std::vector<std::string> StrippedArgs; |
| 297 | if (!stripPositionalArgs(CommandLine, StrippedArgs)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 298 | return nullptr; |
Edwin Vane | c8f0342 | 2013-11-17 16:08:04 +0000 | [diff] [blame] | 299 | return new FixedCompilationDatabase(Directory, StrippedArgs); |
Manuel Klimek | 30318e6 | 2012-04-18 07:41:50 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | FixedCompilationDatabase:: |
| 303 | FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine) { |
| 304 | std::vector<std::string> ToolCommandLine(1, "clang-tool"); |
| 305 | ToolCommandLine.insert(ToolCommandLine.end(), |
| 306 | CommandLine.begin(), CommandLine.end()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 307 | CompileCommands.push_back( |
| 308 | CompileCommand(Directory, std::move(ToolCommandLine))); |
Manuel Klimek | 30318e6 | 2012-04-18 07:41:50 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | std::vector<CompileCommand> |
| 312 | FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const { |
| 313 | std::vector<CompileCommand> Result(CompileCommands); |
| 314 | Result[0].CommandLine.push_back(FilePath); |
| 315 | return Result; |
| 316 | } |
| 317 | |
Manuel Klimek | a3c7096 | 2012-07-13 12:31:45 +0000 | [diff] [blame] | 318 | std::vector<std::string> |
| 319 | FixedCompilationDatabase::getAllFiles() const { |
| 320 | return std::vector<std::string>(); |
| 321 | } |
| 322 | |
Argyrios Kyrtzidis | 7e96bfb | 2012-12-04 07:26:44 +0000 | [diff] [blame] | 323 | std::vector<CompileCommand> |
| 324 | FixedCompilationDatabase::getAllCompileCommands() const { |
| 325 | return std::vector<CompileCommand>(); |
| 326 | } |
| 327 | |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 328 | namespace clang { |
| 329 | namespace tooling { |
| 330 | |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame] | 331 | // This anchor is used to force the linker to link in the generated object file |
| 332 | // and thus register the JSONCompilationDatabasePlugin. |
| 333 | extern volatile int JSONAnchorSource; |
| 334 | static int JSONAnchorDest = JSONAnchorSource; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 335 | |
| 336 | } // end namespace tooling |
| 337 | } // end namespace clang |