JSONCompilationDatabase.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- JSONCompilationDatabase.h --------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // The JSONCompilationDatabase finds compilation databases supplied as a file
  15. // 'compile_commands.json'.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_TOOLING_JSONCOMPILATIONDATABASE_H
  19. #define LLVM_CLANG_TOOLING_JSONCOMPILATIONDATABASE_H
  20. #include "clang/Basic/LLVM.h"
  21. #include "clang/Tooling/CompilationDatabase.h"
  22. #include "clang/Tooling/FileMatchTrie.h"
  23. #include "llvm/ADT/ArrayRef.h"
  24. #include "llvm/ADT/StringMap.h"
  25. #include "llvm/ADT/StringRef.h"
  26. #include "llvm/Support/MemoryBuffer.h"
  27. #include "llvm/Support/SourceMgr.h"
  28. #include "llvm/Support/YAMLParser.h"
  29. #include <memory>
  30. #include <string>
  31. #include <tuple>
  32. #include <utility>
  33. #include <vector>
  34. namespace clang {
  35. namespace tooling {
  36. /// A JSON based compilation database.
  37. ///
  38. /// JSON compilation database files must contain a list of JSON objects which
  39. /// provide the command lines in the attributes 'directory', 'command',
  40. /// 'arguments' and 'file':
  41. /// [
  42. /// { "directory": "<working directory of the compile>",
  43. /// "command": "<compile command line>",
  44. /// "file": "<path to source file>"
  45. /// },
  46. /// { "directory": "<working directory of the compile>",
  47. /// "arguments": ["<raw>", "<command>" "<line>" "<parameters>"],
  48. /// "file": "<path to source file>"
  49. /// },
  50. /// ...
  51. /// ]
  52. /// Each object entry defines one compile action. The specified file is
  53. /// considered to be the main source file for the translation unit.
  54. ///
  55. /// 'command' is a full command line that will be unescaped.
  56. ///
  57. /// 'arguments' is a list of command line arguments that will not be unescaped.
  58. ///
  59. /// JSON compilation databases can for example be generated in CMake projects
  60. /// by setting the flag -DCMAKE_EXPORT_COMPILE_COMMANDS.
  61. enum class JSONCommandLineSyntax { Windows, Gnu, AutoDetect };
  62. class JSONCompilationDatabase : public CompilationDatabase {
  63. public:
  64. /// Loads a JSON compilation database from the specified file.
  65. ///
  66. /// Returns NULL and sets ErrorMessage if the database could not be
  67. /// loaded from the given file.
  68. static std::unique_ptr<JSONCompilationDatabase>
  69. loadFromFile(StringRef FilePath, std::string &ErrorMessage,
  70. JSONCommandLineSyntax Syntax);
  71. /// Loads a JSON compilation database from a data buffer.
  72. ///
  73. /// Returns NULL and sets ErrorMessage if the database could not be loaded.
  74. static std::unique_ptr<JSONCompilationDatabase>
  75. loadFromBuffer(StringRef DatabaseString, std::string &ErrorMessage,
  76. JSONCommandLineSyntax Syntax);
  77. /// Returns all compile commands in which the specified file was
  78. /// compiled.
  79. ///
  80. /// FIXME: Currently FilePath must be an absolute path inside the
  81. /// source directory which does not have symlinks resolved.
  82. std::vector<CompileCommand>
  83. getCompileCommands(StringRef FilePath) const override;
  84. /// Returns the list of all files available in the compilation database.
  85. ///
  86. /// These are the 'file' entries of the JSON objects.
  87. std::vector<std::string> getAllFiles() const override;
  88. /// Returns all compile commands for all the files in the compilation
  89. /// database.
  90. std::vector<CompileCommand> getAllCompileCommands() const override;
  91. private:
  92. /// Constructs a JSON compilation database on a memory buffer.
  93. JSONCompilationDatabase(std::unique_ptr<llvm::MemoryBuffer> Database,
  94. JSONCommandLineSyntax Syntax)
  95. : Database(std::move(Database)), Syntax(Syntax),
  96. YAMLStream(this->Database->getBuffer(), SM) {}
  97. /// Parses the database file and creates the index.
  98. ///
  99. /// Returns whether parsing succeeded. Sets ErrorMessage if parsing
  100. /// failed.
  101. bool parse(std::string &ErrorMessage);
  102. // Tuple (directory, filename, commandline, output) where 'commandline'
  103. // points to the corresponding scalar nodes in the YAML stream.
  104. // If the command line contains a single argument, it is a shell-escaped
  105. // command line.
  106. // Otherwise, each entry in the command line vector is a literal
  107. // argument to the compiler.
  108. // The output field may be a nullptr.
  109. using CompileCommandRef =
  110. std::tuple<llvm::yaml::ScalarNode *, llvm::yaml::ScalarNode *,
  111. std::vector<llvm::yaml::ScalarNode *>,
  112. llvm::yaml::ScalarNode *>;
  113. /// Converts the given array of CompileCommandRefs to CompileCommands.
  114. void getCommands(ArrayRef<CompileCommandRef> CommandsRef,
  115. std::vector<CompileCommand> &Commands) const;
  116. // Maps file paths to the compile command lines for that file.
  117. llvm::StringMap<std::vector<CompileCommandRef>> IndexByFile;
  118. /// All the compile commands in the order that they were provided in the
  119. /// JSON stream.
  120. std::vector<CompileCommandRef> AllCommands;
  121. FileMatchTrie MatchTrie;
  122. std::unique_ptr<llvm::MemoryBuffer> Database;
  123. JSONCommandLineSyntax Syntax;
  124. llvm::SourceMgr SM;
  125. llvm::yaml::Stream YAMLStream;
  126. };
  127. } // namespace tooling
  128. } // namespace clang
  129. #endif // LLVM_CLANG_TOOLING_JSONCOMPILATIONDATABASE_H
  130. #ifdef __GNUC__
  131. #pragma GCC diagnostic pop
  132. #endif