]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Tooling/CompilationDatabase.h
MFV r320905: Import upstream fix for CVE-2017-11103.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Tooling / CompilationDatabase.h
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 <vector>
38
39 namespace clang {
40 namespace tooling {
41
42 /// \brief Specifies the working directory and command of a compilation.
43 struct CompileCommand {
44   CompileCommand() {}
45   CompileCommand(Twine Directory, Twine Filename,
46                  std::vector<std::string> CommandLine, Twine Output)
47       : Directory(Directory.str()),
48         Filename(Filename.str()),
49         CommandLine(std::move(CommandLine)),
50         Output(Output.str()){}
51
52   /// \brief The working directory the command was executed from.
53   std::string Directory;
54
55   /// The source file associated with the command.
56   std::string Filename;
57
58   /// \brief The command line that was executed.
59   std::vector<std::string> CommandLine;
60
61   /// The output file associated with the command.
62   std::string Output;
63
64   /// \brief An optional mapping from each file's path to its content for all
65   /// files needed for the compilation that are not available via the file
66   /// system.
67   ///
68   /// Note that a tool implementation is required to fall back to the file
69   /// system if a source file is not provided in the mapped sources, as
70   /// compilation databases will usually not provide all files in mapped sources
71   /// for performance reasons.
72   std::vector<std::pair<std::string, std::string> > MappedSources;
73 };
74
75 /// \brief Interface for compilation databases.
76 ///
77 /// A compilation database allows the user to retrieve all compile command lines
78 /// that a specified file is compiled with in a project.
79 /// The retrieved compile command lines can be used to run clang tools over
80 /// a subset of the files in a project.
81 class CompilationDatabase {
82 public:
83   virtual ~CompilationDatabase();
84
85   /// \brief Loads a compilation database from a build directory.
86   ///
87   /// Looks at the specified 'BuildDirectory' and creates a compilation database
88   /// that allows to query compile commands for source files in the
89   /// corresponding source tree.
90   ///
91   /// Returns NULL and sets ErrorMessage if we were not able to build up a
92   /// compilation database for the build directory.
93   ///
94   /// FIXME: Currently only supports JSON compilation databases, which
95   /// are named 'compile_commands.json' in the given directory. Extend this
96   /// for other build types (like ninja build files).
97   static std::unique_ptr<CompilationDatabase>
98   loadFromDirectory(StringRef BuildDirectory, std::string &ErrorMessage);
99
100   /// \brief Tries to detect a compilation database location and load it.
101   ///
102   /// Looks for a compilation database in all parent paths of file 'SourceFile'
103   /// by calling loadFromDirectory.
104   static std::unique_ptr<CompilationDatabase>
105   autoDetectFromSource(StringRef SourceFile, std::string &ErrorMessage);
106
107   /// \brief Tries to detect a compilation database location and load it.
108   ///
109   /// Looks for a compilation database in directory 'SourceDir' and all
110   /// its parent paths by calling loadFromDirectory.
111   static std::unique_ptr<CompilationDatabase>
112   autoDetectFromDirectory(StringRef SourceDir, std::string &ErrorMessage);
113
114   /// \brief Returns all compile commands in which the specified file was
115   /// compiled.
116   ///
117   /// This includes compile comamnds that span multiple source files.
118   /// For example, consider a project with the following compilations:
119   /// $ clang++ -o test a.cc b.cc t.cc
120   /// $ clang++ -o production a.cc b.cc -DPRODUCTION
121   /// A compilation database representing the project would return both command
122   /// lines for a.cc and b.cc and only the first command line for t.cc.
123   virtual std::vector<CompileCommand> getCompileCommands(
124     StringRef FilePath) const = 0;
125
126   /// \brief Returns the list of all files available in the compilation database.
127   virtual std::vector<std::string> getAllFiles() const = 0;
128
129   /// \brief Returns all compile commands for all the files in the compilation
130   /// database.
131   ///
132   /// FIXME: Add a layer in Tooling that provides an interface to run a tool
133   /// over all files in a compilation database. Not all build systems have the
134   /// ability to provide a feasible implementation for \c getAllCompileCommands.
135   virtual std::vector<CompileCommand> getAllCompileCommands() const = 0;
136 };
137
138 /// \brief Interface for compilation database plugins.
139 ///
140 /// A compilation database plugin allows the user to register custom compilation
141 /// databases that are picked up as compilation database if the corresponding
142 /// library is linked in. To register a plugin, declare a static variable like:
143 ///
144 /// \code
145 /// static CompilationDatabasePluginRegistry::Add<MyDatabasePlugin>
146 /// X("my-compilation-database", "Reads my own compilation database");
147 /// \endcode
148 class CompilationDatabasePlugin {
149 public:
150   virtual ~CompilationDatabasePlugin();
151
152   /// \brief Loads a compilation database from a build directory.
153   ///
154   /// \see CompilationDatabase::loadFromDirectory().
155   virtual std::unique_ptr<CompilationDatabase>
156   loadFromDirectory(StringRef Directory, std::string &ErrorMessage) = 0;
157 };
158
159 /// \brief A compilation database that returns a single compile command line.
160 ///
161 /// Useful when we want a tool to behave more like a compiler invocation.
162 class FixedCompilationDatabase : public CompilationDatabase {
163 public:
164   /// \brief Creates a FixedCompilationDatabase from the arguments after "--".
165   ///
166   /// Parses the given command line for "--". If "--" is found, the rest of
167   /// the arguments will make up the command line in the returned
168   /// FixedCompilationDatabase.
169   /// The arguments after "--" must not include positional parameters or the
170   /// argv[0] of the tool. Those will be added by the FixedCompilationDatabase
171   /// when a CompileCommand is requested. The argv[0] of the returned command
172   /// line will be "clang-tool".
173   ///
174   /// Returns NULL in case "--" is not found.
175   ///
176   /// The argument list is meant to be compatible with normal llvm command line
177   /// parsing in main methods.
178   /// int main(int argc, char **argv) {
179   ///   std::unique_ptr<FixedCompilationDatabase> Compilations(
180   ///     FixedCompilationDatabase::loadFromCommandLine(argc, argv));
181   ///   cl::ParseCommandLineOptions(argc, argv);
182   ///   ...
183   /// }
184   ///
185   /// \param Argc The number of command line arguments - will be changed to
186   /// the number of arguments before "--", if "--" was found in the argument
187   /// list.
188   /// \param Argv Points to the command line arguments.
189   /// \param Directory The base directory used in the FixedCompilationDatabase.
190   static FixedCompilationDatabase *loadFromCommandLine(int &Argc,
191                                                        const char *const *Argv,
192                                                        Twine Directory = ".");
193
194   /// \brief Constructs a compilation data base from a specified directory
195   /// and command line.
196   FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine);
197
198   /// \brief Returns the given compile command.
199   ///
200   /// Will always return a vector with one entry that contains the directory
201   /// and command line specified at construction with "clang-tool" as argv[0]
202   /// and 'FilePath' as positional argument.
203   std::vector<CompileCommand>
204   getCompileCommands(StringRef FilePath) const override;
205
206   /// \brief Returns the list of all files available in the compilation database.
207   ///
208   /// Note: This is always an empty list for the fixed compilation database.
209   std::vector<std::string> getAllFiles() const override;
210
211   /// \brief Returns all compile commands for all the files in the compilation
212   /// database.
213   ///
214   /// Note: This is always an empty list for the fixed compilation database.
215   std::vector<CompileCommand> getAllCompileCommands() const override;
216
217 private:
218   /// This is built up to contain a single entry vector to be returned from
219   /// getCompileCommands after adding the positional argument.
220   std::vector<CompileCommand> CompileCommands;
221 };
222
223 } // end namespace tooling
224 } // end namespace clang
225
226 #endif