clang  7.0.0svn
CompilationDatabase.h
Go to the documentation of this file.
1 //===- CompilationDatabase.h ------------------------------------*- C++ -*-===//
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 //
10 // This file provides an interface and multiple implementations for
11 // CompilationDatabases.
12 //
13 // While C++ refactoring and analysis tools are not compilers, and thus
14 // don't run as part of the build system, they need the exact information
15 // of a build in order to be able to correctly understand the C++ code of
16 // the project. This information is provided via the CompilationDatabase
17 // interface.
18 //
19 // To create a CompilationDatabase from a build directory one can call
20 // CompilationDatabase::loadFromDirectory(), which deduces the correct
21 // compilation database from the root of the build tree.
22 //
23 // See the concrete subclasses of CompilationDatabase for currently supported
24 // formats.
25 //
26 //===----------------------------------------------------------------------===//
27 
28 #ifndef LLVM_CLANG_TOOLING_COMPILATIONDATABASE_H
29 #define LLVM_CLANG_TOOLING_COMPILATIONDATABASE_H
30 
31 #include "clang/Basic/LLVM.h"
32 #include "llvm/ADT/ArrayRef.h"
33 #include "llvm/ADT/StringRef.h"
34 #include "llvm/ADT/Twine.h"
35 #include <memory>
36 #include <string>
37 #include <utility>
38 #include <vector>
39 
40 namespace clang {
41 namespace tooling {
42 
43 /// Specifies the working directory and command of a compilation.
45  CompileCommand() = default;
47  std::vector<std::string> CommandLine, Twine Output)
48  : Directory(Directory.str()), Filename(Filename.str()),
49  CommandLine(std::move(CommandLine)), Output(Output.str()){}
50 
51  /// The working directory the command was executed from.
52  std::string Directory;
53 
54  /// The source file associated with the command.
55  std::string Filename;
56 
57  /// The command line that was executed.
58  std::vector<std::string> CommandLine;
59 
60  /// The output file associated with the command.
61  std::string Output;
62 };
63 
64 /// Interface for compilation databases.
65 ///
66 /// A compilation database allows the user to retrieve compile command lines
67 /// for the files in a project.
68 ///
69 /// Many implementations are enumerable, allowing all command lines to be
70 /// retrieved. These can be used to run clang tools over a subset of the files
71 /// in a project.
73 public:
74  virtual ~CompilationDatabase();
75 
76  /// Loads a compilation database from a build directory.
77  ///
78  /// Looks at the specified 'BuildDirectory' and creates a compilation database
79  /// that allows to query compile commands for source files in the
80  /// corresponding source tree.
81  ///
82  /// Returns NULL and sets ErrorMessage if we were not able to build up a
83  /// compilation database for the build directory.
84  ///
85  /// FIXME: Currently only supports JSON compilation databases, which
86  /// are named 'compile_commands.json' in the given directory. Extend this
87  /// for other build types (like ninja build files).
88  static std::unique_ptr<CompilationDatabase>
89  loadFromDirectory(StringRef BuildDirectory, std::string &ErrorMessage);
90 
91  /// Tries to detect a compilation database location and load it.
92  ///
93  /// Looks for a compilation database in all parent paths of file 'SourceFile'
94  /// by calling loadFromDirectory.
95  static std::unique_ptr<CompilationDatabase>
96  autoDetectFromSource(StringRef SourceFile, std::string &ErrorMessage);
97 
98  /// Tries to detect a compilation database location and load it.
99  ///
100  /// Looks for a compilation database in directory 'SourceDir' and all
101  /// its parent paths by calling loadFromDirectory.
102  static std::unique_ptr<CompilationDatabase>
103  autoDetectFromDirectory(StringRef SourceDir, std::string &ErrorMessage);
104 
105  /// Returns all compile commands in which the specified file was
106  /// compiled.
107  ///
108  /// This includes compile commands that span multiple source files.
109  /// For example, consider a project with the following compilations:
110  /// $ clang++ -o test a.cc b.cc t.cc
111  /// $ clang++ -o production a.cc b.cc -DPRODUCTION
112  /// A compilation database representing the project would return both command
113  /// lines for a.cc and b.cc and only the first command line for t.cc.
114  virtual std::vector<CompileCommand> getCompileCommands(
115  StringRef FilePath) const = 0;
116 
117  /// Returns the list of all files available in the compilation database.
118  ///
119  /// By default, returns nothing. Implementations should override this if they
120  /// can enumerate their source files.
121  virtual std::vector<std::string> getAllFiles() const { return {}; }
122 
123  /// Returns all compile commands for all the files in the compilation
124  /// database.
125  ///
126  /// FIXME: Add a layer in Tooling that provides an interface to run a tool
127  /// over all files in a compilation database. Not all build systems have the
128  /// ability to provide a feasible implementation for \c getAllCompileCommands.
129  ///
130  /// By default, this is implemented in terms of getAllFiles() and
131  /// getCompileCommands(). Subclasses may override this for efficiency.
132  virtual std::vector<CompileCommand> getAllCompileCommands() const;
133 };
134 
135 /// Interface for compilation database plugins.
136 ///
137 /// A compilation database plugin allows the user to register custom compilation
138 /// databases that are picked up as compilation database if the corresponding
139 /// library is linked in. To register a plugin, declare a static variable like:
140 ///
141 /// \code
142 /// static CompilationDatabasePluginRegistry::Add<MyDatabasePlugin>
143 /// X("my-compilation-database", "Reads my own compilation database");
144 /// \endcode
146 public:
147  virtual ~CompilationDatabasePlugin();
148 
149  /// Loads a compilation database from a build directory.
150  ///
151  /// \see CompilationDatabase::loadFromDirectory().
152  virtual std::unique_ptr<CompilationDatabase>
153  loadFromDirectory(StringRef Directory, std::string &ErrorMessage) = 0;
154 };
155 
156 /// A compilation database that returns a single compile command line.
157 ///
158 /// Useful when we want a tool to behave more like a compiler invocation.
159 /// This compilation database is not enumerable: getAllFiles() returns {}.
161 public:
162  /// Creates a FixedCompilationDatabase from the arguments after "--".
163  ///
164  /// Parses the given command line for "--". If "--" is found, the rest of
165  /// the arguments will make up the command line in the returned
166  /// FixedCompilationDatabase.
167  /// The arguments after "--" must not include positional parameters or the
168  /// argv[0] of the tool. Those will be added by the FixedCompilationDatabase
169  /// when a CompileCommand is requested. The argv[0] of the returned command
170  /// line will be "clang-tool".
171  ///
172  /// Returns NULL in case "--" is not found.
173  ///
174  /// The argument list is meant to be compatible with normal llvm command line
175  /// parsing in main methods.
176  /// int main(int argc, char **argv) {
177  /// std::unique_ptr<FixedCompilationDatabase> Compilations(
178  /// FixedCompilationDatabase::loadFromCommandLine(argc, argv));
179  /// cl::ParseCommandLineOptions(argc, argv);
180  /// ...
181  /// }
182  ///
183  /// \param Argc The number of command line arguments - will be changed to
184  /// the number of arguments before "--", if "--" was found in the argument
185  /// list.
186  /// \param Argv Points to the command line arguments.
187  /// \param ErrorMsg Contains error text if the function returns null pointer.
188  /// \param Directory The base directory used in the FixedCompilationDatabase.
189  static std::unique_ptr<FixedCompilationDatabase> loadFromCommandLine(
190  int &Argc, const char *const *Argv, std::string &ErrorMsg,
191  Twine Directory = ".");
192 
193  /// Reads flags from the given file, one-per line.
194  /// Returns nullptr and sets ErrorMessage if we can't read the file.
195  static std::unique_ptr<FixedCompilationDatabase>
196  loadFromFile(StringRef Path, std::string &ErrorMsg);
197 
198  /// Constructs a compilation data base from a specified directory
199  /// and command line.
201 
202  /// Returns the given compile command.
203  ///
204  /// Will always return a vector with one entry that contains the directory
205  /// and command line specified at construction with "clang-tool" as argv[0]
206  /// and 'FilePath' as positional argument.
207  std::vector<CompileCommand>
208  getCompileCommands(StringRef FilePath) const override;
209 
210 private:
211  /// This is built up to contain a single entry vector to be returned from
212  /// getCompileCommands after adding the positional argument.
213  std::vector<CompileCommand> CompileCommands;
214 };
215 
216 /// Returns a wrapped CompilationDatabase that defers to the provided one,
217 /// but getCompileCommands() will infer commands for unknown files.
218 /// The return value of getAllFiles() or getAllCompileCommands() is unchanged.
219 /// See InterpolatingCompilationDatabase.cpp for details on heuristics.
220 std::unique_ptr<CompilationDatabase>
221  inferMissingCompileCommands(std::unique_ptr<CompilationDatabase>);
222 
223 } // namespace tooling
224 } // namespace clang
225 
226 #endif // LLVM_CLANG_TOOLING_COMPILATIONDATABASE_H
std::string Output
The output file associated with the command.
Interface for compilation database plugins.
virtual std::vector< std::string > getAllFiles() const
Returns the list of all files available in the compilation database.
Definition: Format.h:2031
std::string Directory
The working directory the command was executed from.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
std::string Filename
The source file associated with the command.
Interface for compilation databases.
Specifies the working directory and command of a compilation.
std::unique_ptr< CompilationDatabase > inferMissingCompileCommands(std::unique_ptr< CompilationDatabase >)
Returns a wrapped CompilationDatabase that defers to the provided one, but getCompileCommands() will ...
Dataflow Directional Tag Classes.
std::vector< std::string > CommandLine
The command line that was executed.
CompileCommand(Twine Directory, Twine Filename, std::vector< std::string > CommandLine, Twine Output)
A compilation database that returns a single compile command line.