|
|
|||
File indexing completed on 2026-05-10 08:37:12
0001 //===- JSONCompilationDatabase.h --------------------------------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // The JSONCompilationDatabase finds compilation databases supplied as a file 0010 // 'compile_commands.json'. 0011 // 0012 //===----------------------------------------------------------------------===// 0013 0014 #ifndef LLVM_CLANG_TOOLING_JSONCOMPILATIONDATABASE_H 0015 #define LLVM_CLANG_TOOLING_JSONCOMPILATIONDATABASE_H 0016 0017 #include "clang/Basic/LLVM.h" 0018 #include "clang/Tooling/CompilationDatabase.h" 0019 #include "clang/Tooling/FileMatchTrie.h" 0020 #include "llvm/ADT/ArrayRef.h" 0021 #include "llvm/ADT/StringMap.h" 0022 #include "llvm/ADT/StringRef.h" 0023 #include "llvm/Support/MemoryBuffer.h" 0024 #include "llvm/Support/SourceMgr.h" 0025 #include "llvm/Support/YAMLParser.h" 0026 #include <memory> 0027 #include <string> 0028 #include <tuple> 0029 #include <utility> 0030 #include <vector> 0031 0032 namespace clang { 0033 namespace tooling { 0034 0035 /// A JSON based compilation database. 0036 /// 0037 /// JSON compilation database files must contain a list of JSON objects which 0038 /// provide the command lines in the attributes 'directory', 'command', 0039 /// 'arguments' and 'file': 0040 /// [ 0041 /// { "directory": "<working directory of the compile>", 0042 /// "command": "<compile command line>", 0043 /// "file": "<path to source file>" 0044 /// }, 0045 /// { "directory": "<working directory of the compile>", 0046 /// "arguments": ["<raw>", "<command>" "<line>" "<parameters>"], 0047 /// "file": "<path to source file>" 0048 /// }, 0049 /// ... 0050 /// ] 0051 /// Each object entry defines one compile action. The specified file is 0052 /// considered to be the main source file for the translation unit. 0053 /// 0054 /// 'command' is a full command line that will be unescaped. 0055 /// 0056 /// 'arguments' is a list of command line arguments that will not be unescaped. 0057 /// 0058 /// JSON compilation databases can for example be generated in CMake projects 0059 /// by setting the flag -DCMAKE_EXPORT_COMPILE_COMMANDS. 0060 enum class JSONCommandLineSyntax { Windows, Gnu, AutoDetect }; 0061 class JSONCompilationDatabase : public CompilationDatabase { 0062 public: 0063 /// Loads a JSON compilation database from the specified file. 0064 /// 0065 /// Returns NULL and sets ErrorMessage if the database could not be 0066 /// loaded from the given file. 0067 static std::unique_ptr<JSONCompilationDatabase> 0068 loadFromFile(StringRef FilePath, std::string &ErrorMessage, 0069 JSONCommandLineSyntax Syntax); 0070 0071 /// Loads a JSON compilation database from a data buffer. 0072 /// 0073 /// Returns NULL and sets ErrorMessage if the database could not be loaded. 0074 static std::unique_ptr<JSONCompilationDatabase> 0075 loadFromBuffer(StringRef DatabaseString, std::string &ErrorMessage, 0076 JSONCommandLineSyntax Syntax); 0077 0078 /// Returns all compile commands in which the specified file was 0079 /// compiled. 0080 /// 0081 /// FIXME: Currently FilePath must be an absolute path inside the 0082 /// source directory which does not have symlinks resolved. 0083 std::vector<CompileCommand> 0084 getCompileCommands(StringRef FilePath) const override; 0085 0086 /// Returns the list of all files available in the compilation database. 0087 /// 0088 /// These are the 'file' entries of the JSON objects. 0089 std::vector<std::string> getAllFiles() const override; 0090 0091 /// Returns all compile commands for all the files in the compilation 0092 /// database. 0093 std::vector<CompileCommand> getAllCompileCommands() const override; 0094 0095 private: 0096 /// Constructs a JSON compilation database on a memory buffer. 0097 JSONCompilationDatabase(std::unique_ptr<llvm::MemoryBuffer> Database, 0098 JSONCommandLineSyntax Syntax) 0099 : Database(std::move(Database)), Syntax(Syntax), 0100 YAMLStream(this->Database->getBuffer(), SM) {} 0101 0102 /// Parses the database file and creates the index. 0103 /// 0104 /// Returns whether parsing succeeded. Sets ErrorMessage if parsing 0105 /// failed. 0106 bool parse(std::string &ErrorMessage); 0107 0108 // Tuple (directory, filename, commandline, output) where 'commandline' 0109 // points to the corresponding scalar nodes in the YAML stream. 0110 // If the command line contains a single argument, it is a shell-escaped 0111 // command line. 0112 // Otherwise, each entry in the command line vector is a literal 0113 // argument to the compiler. 0114 // The output field may be a nullptr. 0115 using CompileCommandRef = 0116 std::tuple<llvm::yaml::ScalarNode *, llvm::yaml::ScalarNode *, 0117 std::vector<llvm::yaml::ScalarNode *>, 0118 llvm::yaml::ScalarNode *>; 0119 0120 /// Converts the given array of CompileCommandRefs to CompileCommands. 0121 void getCommands(ArrayRef<CompileCommandRef> CommandsRef, 0122 std::vector<CompileCommand> &Commands) const; 0123 0124 // Maps file paths to the compile command lines for that file. 0125 llvm::StringMap<std::vector<CompileCommandRef>> IndexByFile; 0126 0127 /// All the compile commands in the order that they were provided in the 0128 /// JSON stream. 0129 std::vector<CompileCommandRef> AllCommands; 0130 0131 FileMatchTrie MatchTrie; 0132 0133 std::unique_ptr<llvm::MemoryBuffer> Database; 0134 JSONCommandLineSyntax Syntax; 0135 llvm::SourceMgr SM; 0136 llvm::yaml::Stream YAMLStream; 0137 }; 0138 0139 } // namespace tooling 0140 } // namespace clang 0141 0142 #endif // LLVM_CLANG_TOOLING_JSONCOMPILATIONDATABASE_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|