clang API Documentation

CompilationDatabase.h
Go to the documentation of this file.
00001 //===--- CompilationDatabase.h - --------------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file provides an interface and multiple implementations for
00011 //  CompilationDatabases.
00012 //
00013 //  While C++ refactoring and analysis tools are not compilers, and thus
00014 //  don't run as part of the build system, they need the exact information
00015 //  of a build in order to be able to correctly understand the C++ code of
00016 //  the project. This information is provided via the CompilationDatabase
00017 //  interface.
00018 //
00019 //  To create a CompilationDatabase from a build directory one can call
00020 //  CompilationDatabase::loadFromDirectory(), which deduces the correct
00021 //  compilation database from the root of the build tree.
00022 //
00023 //  See the concrete subclasses of CompilationDatabase for currently supported
00024 //  formats.
00025 //
00026 //===----------------------------------------------------------------------===//
00027 
00028 #ifndef LLVM_CLANG_TOOLING_COMPILATIONDATABASE_H
00029 #define LLVM_CLANG_TOOLING_COMPILATIONDATABASE_H
00030 
00031 #include "clang/Basic/LLVM.h"
00032 #include "llvm/ADT/ArrayRef.h"
00033 #include "llvm/ADT/StringRef.h"
00034 #include "llvm/ADT/Twine.h"
00035 #include <memory>
00036 #include <string>
00037 #include <vector>
00038 
00039 namespace clang {
00040 namespace tooling {
00041 
00042 /// \brief Specifies the working directory and command of a compilation.
00043 struct CompileCommand {
00044   CompileCommand() {}
00045   CompileCommand(Twine Directory, std::vector<std::string> CommandLine)
00046       : Directory(Directory.str()), CommandLine(std::move(CommandLine)) {}
00047 
00048   /// \brief The working directory the command was executed from.
00049   std::string Directory;
00050 
00051   /// \brief The command line that was executed.
00052   std::vector<std::string> CommandLine;
00053 
00054   /// \brief An optional mapping from each file's path to its content for all
00055   /// files needed for the compilation that are not available via the file
00056   /// system.
00057   ///
00058   /// Note that a tool implementation is required to fall back to the file
00059   /// system if a source file is not provided in the mapped sources, as
00060   /// compilation databases will usually not provide all files in mapped sources
00061   /// for performance reasons.
00062   std::vector<std::pair<std::string, std::string> > MappedSources;
00063 };
00064 
00065 /// \brief Interface for compilation databases.
00066 ///
00067 /// A compilation database allows the user to retrieve all compile command lines
00068 /// that a specified file is compiled with in a project.
00069 /// The retrieved compile command lines can be used to run clang tools over
00070 /// a subset of the files in a project.
00071 class CompilationDatabase {
00072 public:
00073   virtual ~CompilationDatabase();
00074 
00075   /// \brief Loads a compilation database from a build directory.
00076   ///
00077   /// Looks at the specified 'BuildDirectory' and creates a compilation database
00078   /// that allows to query compile commands for source files in the
00079   /// corresponding source tree.
00080   ///
00081   /// Returns NULL and sets ErrorMessage if we were not able to build up a
00082   /// compilation database for the build directory.
00083   ///
00084   /// FIXME: Currently only supports JSON compilation databases, which
00085   /// are named 'compile_commands.json' in the given directory. Extend this
00086   /// for other build types (like ninja build files).
00087   static std::unique_ptr<CompilationDatabase>
00088   loadFromDirectory(StringRef BuildDirectory, std::string &ErrorMessage);
00089 
00090   /// \brief Tries to detect a compilation database location and load it.
00091   ///
00092   /// Looks for a compilation database in all parent paths of file 'SourceFile'
00093   /// by calling loadFromDirectory.
00094   static std::unique_ptr<CompilationDatabase>
00095   autoDetectFromSource(StringRef SourceFile, std::string &ErrorMessage);
00096 
00097   /// \brief Tries to detect a compilation database location and load it.
00098   ///
00099   /// Looks for a compilation database in directory 'SourceDir' and all
00100   /// its parent paths by calling loadFromDirectory.
00101   static std::unique_ptr<CompilationDatabase>
00102   autoDetectFromDirectory(StringRef SourceDir, std::string &ErrorMessage);
00103 
00104   /// \brief Returns all compile commands in which the specified file was
00105   /// compiled.
00106   ///
00107   /// This includes compile comamnds that span multiple source files.
00108   /// For example, consider a project with the following compilations:
00109   /// $ clang++ -o test a.cc b.cc t.cc
00110   /// $ clang++ -o production a.cc b.cc -DPRODUCTION
00111   /// A compilation database representing the project would return both command
00112   /// lines for a.cc and b.cc and only the first command line for t.cc.
00113   virtual std::vector<CompileCommand> getCompileCommands(
00114     StringRef FilePath) const = 0;
00115 
00116   /// \brief Returns the list of all files available in the compilation database.
00117   virtual std::vector<std::string> getAllFiles() const = 0;
00118 
00119   /// \brief Returns all compile commands for all the files in the compilation
00120   /// database.
00121   ///
00122   /// FIXME: Add a layer in Tooling that provides an interface to run a tool
00123   /// over all files in a compilation database. Not all build systems have the
00124   /// ability to provide a feasible implementation for \c getAllCompileCommands.
00125   virtual std::vector<CompileCommand> getAllCompileCommands() const = 0;
00126 };
00127 
00128 /// \brief Interface for compilation database plugins.
00129 ///
00130 /// A compilation database plugin allows the user to register custom compilation
00131 /// databases that are picked up as compilation database if the corresponding
00132 /// library is linked in. To register a plugin, declare a static variable like:
00133 ///
00134 /// \code
00135 /// static CompilationDatabasePluginRegistry::Add<MyDatabasePlugin>
00136 /// X("my-compilation-database", "Reads my own compilation database");
00137 /// \endcode
00138 class CompilationDatabasePlugin {
00139 public:
00140   virtual ~CompilationDatabasePlugin();
00141 
00142   /// \brief Loads a compilation database from a build directory.
00143   ///
00144   /// \see CompilationDatabase::loadFromDirectory().
00145   virtual std::unique_ptr<CompilationDatabase>
00146   loadFromDirectory(StringRef Directory, std::string &ErrorMessage) = 0;
00147 };
00148 
00149 /// \brief A compilation database that returns a single compile command line.
00150 ///
00151 /// Useful when we want a tool to behave more like a compiler invocation.
00152 class FixedCompilationDatabase : public CompilationDatabase {
00153 public:
00154   /// \brief Creates a FixedCompilationDatabase from the arguments after "--".
00155   ///
00156   /// Parses the given command line for "--". If "--" is found, the rest of
00157   /// the arguments will make up the command line in the returned
00158   /// FixedCompilationDatabase.
00159   /// The arguments after "--" must not include positional parameters or the
00160   /// argv[0] of the tool. Those will be added by the FixedCompilationDatabase
00161   /// when a CompileCommand is requested. The argv[0] of the returned command
00162   /// line will be "clang-tool".
00163   ///
00164   /// Returns NULL in case "--" is not found.
00165   ///
00166   /// The argument list is meant to be compatible with normal llvm command line
00167   /// parsing in main methods.
00168   /// int main(int argc, char **argv) {
00169   ///   std::unique_ptr<FixedCompilationDatabase> Compilations(
00170   ///     FixedCompilationDatabase::loadFromCommandLine(argc, argv));
00171   ///   cl::ParseCommandLineOptions(argc, argv);
00172   ///   ...
00173   /// }
00174   ///
00175   /// \param Argc The number of command line arguments - will be changed to
00176   /// the number of arguments before "--", if "--" was found in the argument
00177   /// list.
00178   /// \param Argv Points to the command line arguments.
00179   /// \param Directory The base directory used in the FixedCompilationDatabase.
00180   static FixedCompilationDatabase *loadFromCommandLine(int &Argc,
00181                                                        const char **Argv,
00182                                                        Twine Directory = ".");
00183 
00184   /// \brief Constructs a compilation data base from a specified directory
00185   /// and command line.
00186   FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine);
00187 
00188   /// \brief Returns the given compile command.
00189   ///
00190   /// Will always return a vector with one entry that contains the directory
00191   /// and command line specified at construction with "clang-tool" as argv[0]
00192   /// and 'FilePath' as positional argument.
00193   std::vector<CompileCommand>
00194   getCompileCommands(StringRef FilePath) const override;
00195 
00196   /// \brief Returns the list of all files available in the compilation database.
00197   ///
00198   /// Note: This is always an empty list for the fixed compilation database.
00199   std::vector<std::string> getAllFiles() const override;
00200 
00201   /// \brief Returns all compile commands for all the files in the compilation
00202   /// database.
00203   ///
00204   /// Note: This is always an empty list for the fixed compilation database.
00205   std::vector<CompileCommand> getAllCompileCommands() const override;
00206 
00207 private:
00208   /// This is built up to contain a single entry vector to be returned from
00209   /// getCompileCommands after adding the positional argument.
00210   std::vector<CompileCommand> CompileCommands;
00211 };
00212 
00213 } // end namespace tooling
00214 } // end namespace clang
00215 
00216 #endif