CompilationDatabase.cpp 15 KB

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