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 | |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame^] | 15 | #include <sstream> |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 16 | #include "clang/Tooling/CompilationDatabase.h" |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame^] | 17 | #include "clang/Tooling/CompilationDatabasePluginRegistry.h" |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 18 | #include "clang/Tooling/Tooling.h" |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Path.h" |
| 21 | #include "llvm/Support/system_error.h" |
| 22 | |
| 23 | namespace clang { |
| 24 | namespace tooling { |
| 25 | |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 26 | CompilationDatabase::~CompilationDatabase() {} |
| 27 | |
| 28 | CompilationDatabase * |
| 29 | CompilationDatabase::loadFromDirectory(StringRef BuildDirectory, |
| 30 | std::string &ErrorMessage) { |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame^] | 31 | std::stringstream ErrorStream; |
| 32 | for (CompilationDatabasePluginRegistry::iterator |
| 33 | It = CompilationDatabasePluginRegistry::begin(), |
| 34 | Ie = CompilationDatabasePluginRegistry::end(); |
| 35 | It != Ie; ++It) { |
| 36 | std::string DatabaseErrorMessage; |
| 37 | OwningPtr<CompilationDatabasePlugin> Plugin(It->instantiate()); |
| 38 | if (CompilationDatabase *DB = |
| 39 | Plugin->loadFromDirectory(BuildDirectory, DatabaseErrorMessage)) |
| 40 | return DB; |
| 41 | else |
| 42 | ErrorStream << It->getName() << ": " << DatabaseErrorMessage << "\n"; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 43 | } |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame^] | 44 | ErrorMessage = ErrorStream.str(); |
| 45 | return NULL; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 48 | static CompilationDatabase * |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame^] | 49 | findCompilationDatabaseFromDirectory(StringRef Directory, |
| 50 | std::string &ErrorMessage) { |
| 51 | std::stringstream ErrorStream; |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 52 | while (!Directory.empty()) { |
| 53 | std::string LoadErrorMessage; |
| 54 | |
| 55 | if (CompilationDatabase *DB = |
| 56 | CompilationDatabase::loadFromDirectory(Directory, LoadErrorMessage)) |
| 57 | return DB; |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame^] | 58 | ErrorStream << "No compilation database found in " << Directory.str() |
| 59 | << "\n" << LoadErrorMessage; |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 60 | |
| 61 | Directory = llvm::sys::path::parent_path(Directory); |
| 62 | } |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame^] | 63 | ErrorMessage = ErrorStream.str(); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 64 | return NULL; |
| 65 | } |
| 66 | |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 67 | CompilationDatabase * |
| 68 | CompilationDatabase::autoDetectFromSource(StringRef SourceFile, |
| 69 | std::string &ErrorMessage) { |
| 70 | llvm::SmallString<1024> AbsolutePath(getAbsolutePath(SourceFile)); |
| 71 | StringRef Directory = llvm::sys::path::parent_path(AbsolutePath); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 72 | |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame^] | 73 | CompilationDatabase *DB = findCompilationDatabaseFromDirectory(Directory, |
| 74 | ErrorMessage); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 75 | |
| 76 | if (!DB) |
| 77 | ErrorMessage = ("Could not auto-detect compilation database for file \"" + |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame^] | 78 | SourceFile + "\"\n" + ErrorMessage).str(); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 79 | return DB; |
| 80 | } |
| 81 | |
| 82 | CompilationDatabase * |
| 83 | CompilationDatabase::autoDetectFromDirectory(StringRef SourceDir, |
| 84 | std::string &ErrorMessage) { |
| 85 | llvm::SmallString<1024> AbsolutePath(getAbsolutePath(SourceDir)); |
| 86 | |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame^] | 87 | CompilationDatabase *DB = findCompilationDatabaseFromDirectory(AbsolutePath, |
| 88 | ErrorMessage); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 89 | |
| 90 | if (!DB) |
| 91 | ErrorMessage = ("Could not auto-detect compilation database from directory \"" + |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame^] | 92 | SourceDir + "\"\n" + ErrorMessage).str(); |
Arnaud A. de Grandmaison | 4187df5 | 2012-07-10 16:56:35 +0000 | [diff] [blame] | 93 | return DB; |
Manuel Klimek | 8fa2fb8 | 2012-07-10 13:10:51 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame^] | 96 | CompilationDatabasePlugin::~CompilationDatabasePlugin() {} |
| 97 | |
Manuel Klimek | 30318e6 | 2012-04-18 07:41:50 +0000 | [diff] [blame] | 98 | FixedCompilationDatabase * |
| 99 | FixedCompilationDatabase::loadFromCommandLine(int &Argc, |
| 100 | const char **Argv, |
| 101 | Twine Directory) { |
| 102 | const char **DoubleDash = std::find(Argv, Argv + Argc, StringRef("--")); |
| 103 | if (DoubleDash == Argv + Argc) |
| 104 | return NULL; |
| 105 | std::vector<std::string> CommandLine(DoubleDash + 1, Argv + Argc); |
| 106 | Argc = DoubleDash - Argv; |
| 107 | return new FixedCompilationDatabase(Directory, CommandLine); |
| 108 | } |
| 109 | |
| 110 | FixedCompilationDatabase:: |
| 111 | FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine) { |
| 112 | std::vector<std::string> ToolCommandLine(1, "clang-tool"); |
| 113 | ToolCommandLine.insert(ToolCommandLine.end(), |
| 114 | CommandLine.begin(), CommandLine.end()); |
| 115 | CompileCommands.push_back(CompileCommand(Directory, ToolCommandLine)); |
| 116 | } |
| 117 | |
| 118 | std::vector<CompileCommand> |
| 119 | FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const { |
| 120 | std::vector<CompileCommand> Result(CompileCommands); |
| 121 | Result[0].CommandLine.push_back(FilePath); |
| 122 | return Result; |
| 123 | } |
| 124 | |
Manuel Klimek | a3c7096 | 2012-07-13 12:31:45 +0000 | [diff] [blame] | 125 | std::vector<std::string> |
| 126 | FixedCompilationDatabase::getAllFiles() const { |
| 127 | return std::vector<std::string>(); |
| 128 | } |
| 129 | |
Daniel Jasper | 7fd90b0 | 2012-08-24 05:50:27 +0000 | [diff] [blame^] | 130 | // This anchor is used to force the linker to link in the generated object file |
| 131 | // and thus register the JSONCompilationDatabasePlugin. |
| 132 | extern volatile int JSONAnchorSource; |
| 133 | static int JSONAnchorDest = JSONAnchorSource; |
Manuel Klimek | cb971c6 | 2012-04-04 12:07:46 +0000 | [diff] [blame] | 134 | |
| 135 | } // end namespace tooling |
| 136 | } // end namespace clang |