clang 24.0.0git
JSONCompilationDatabase.h
Go to the documentation of this file.
1//===- JSONCompilationDatabase.h --------------------------------*- C++ -*-===//
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// The JSONCompilationDatabase finds compilation databases supplied as a file
10// 'compile_commands.json'.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_TOOLING_JSONCOMPILATIONDATABASE_H
15#define LLVM_CLANG_TOOLING_JSONCOMPILATIONDATABASE_H
16
17#include "clang/Basic/LLVM.h"
20#include "llvm/ADT/ArrayRef.h"
21#include "llvm/ADT/StringMap.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/StringSet.h"
24#include "llvm/Support/MemoryBuffer.h"
25#include "llvm/Support/SourceMgr.h"
26#include "llvm/Support/YAMLParser.h"
27#include <memory>
28#include <string>
29#include <tuple>
30#include <utility>
31#include <vector>
32
33namespace clang {
34namespace tooling {
35
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.
62class JSONCompilationDatabase : public CompilationDatabase {
63public:
64 /// Loads a JSON compilation database from the specified file. A relative
65 /// "directory" is resolved against the directory containing the file.
66 ///
67 /// Returns NULL and sets ErrorMessage if the database could not be
68 /// loaded from the given file.
69 static std::unique_ptr<JSONCompilationDatabase>
70 loadFromFile(StringRef FilePath, std::string &ErrorMessage,
72
73 /// Loads a JSON compilation database from a data buffer. If BaseDirectory
74 /// is non-empty, a relative "directory" is resolved against it.
75 ///
76 /// Returns NULL and sets ErrorMessage if the database could not be loaded.
77 static std::unique_ptr<JSONCompilationDatabase>
78 loadFromBuffer(StringRef BaseDirectory, StringRef DatabaseString,
79 std::string &ErrorMessage, JSONCommandLineSyntax Syntax);
80
81 /// Returns all compile commands in which the specified file was
82 /// compiled.
83 ///
84 /// FIXME: Currently FilePath must be an absolute path inside the
85 /// source directory which does not have symlinks resolved.
86 std::vector<CompileCommand>
87 getCompileCommands(StringRef FilePath) const override;
88
89 /// Returns the list of all files available in the compilation database.
90 ///
91 /// These are the 'file' entries of the JSON objects.
92 std::vector<std::string> getAllFiles() const override;
93
94 /// Returns all compile commands for all the files in the compilation
95 /// database.
96 std::vector<CompileCommand> getAllCompileCommands() const override;
97
98private:
99 /// Constructs a JSON compilation database on a memory buffer.
100 JSONCompilationDatabase(std::unique_ptr<llvm::MemoryBuffer> Database,
102 : Database(std::move(Database)), Syntax(Syntax),
103 YAMLStream(this->Database->getBuffer(), SM) {}
104
105 /// Parses the database file and creates the index.
106 ///
107 /// Returns whether parsing succeeded. Sets ErrorMessage if parsing
108 /// failed.
109 bool parse(StringRef BaseDirectory, std::string &ErrorMessage);
110
111 // Tuple (directory, filename, commandline, output) where 'directory' is
112 // owned by Directories and the others point to the corresponding scalar
113 // nodes in the YAML stream.
114 // If the command line contains a single argument, it is a shell-escaped
115 // command line.
116 // Otherwise, each entry in the command line vector is a literal
117 // argument to the compiler.
118 // The output field may be a nullptr.
119 using CompileCommandRef = std::tuple<StringRef, llvm::yaml::ScalarNode *,
120 std::vector<llvm::yaml::ScalarNode *>,
121 llvm::yaml::ScalarNode *>;
122
123 /// Converts the given array of CompileCommandRefs to CompileCommands.
124 void getCommands(ArrayRef<CompileCommandRef> CommandsRef,
125 std::vector<CompileCommand> &Commands) const;
126
127 // Maps file paths to the compile command lines for that file.
128 llvm::StringMap<std::vector<CompileCommandRef>> IndexByFile;
129
130 /// All the compile commands in the order that they were provided in the
131 /// JSON stream.
132 std::vector<CompileCommandRef> AllCommands;
133
134 FileMatchTrie MatchTrie;
135
136 llvm::StringSet<> Directories;
137 std::unique_ptr<llvm::MemoryBuffer> Database;
139 llvm::SourceMgr SM;
140 llvm::yaml::Stream YAMLStream;
141};
142
143} // namespace tooling
144} // namespace clang
145
146#endif // LLVM_CLANG_TOOLING_JSONCOMPILATIONDATABASE_H
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Interface for compilation databases.
A trie to efficiently match against the entries of the compilation database in order of matching suff...
std::vector< CompileCommand > getCompileCommands(StringRef FilePath) const override
Returns all compile commands in which the specified file was compiled.
std::vector< std::string > getAllFiles() const override
Returns the list of all files available in the compilation database.
static std::unique_ptr< JSONCompilationDatabase > loadFromFile(StringRef FilePath, std::string &ErrorMessage, JSONCommandLineSyntax Syntax)
Loads a JSON compilation database from the specified file.
static std::unique_ptr< JSONCompilationDatabase > loadFromBuffer(StringRef BaseDirectory, StringRef DatabaseString, std::string &ErrorMessage, JSONCommandLineSyntax Syntax)
Loads a JSON compilation database from a data buffer.
std::vector< CompileCommand > getAllCompileCommands() const override
Returns all compile commands for all the files in the compilation database.
JSONCommandLineSyntax
A JSON based compilation database.
Top level wrappers for InstallAPI frontend operations.