Home | History | Annotate | Download | only in Tooling
      1 //===--- JSONCompilationDatabase.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 //  The JSONCompilationDatabase finds compilation databases supplied as a file
     11 //  'compile_commands.json'.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H
     16 #define LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H
     17 
     18 #include "clang/Basic/LLVM.h"
     19 #include "clang/Tooling/CompilationDatabase.h"
     20 #include "clang/Tooling/FileMatchTrie.h"
     21 #include "llvm/ADT/StringMap.h"
     22 #include "llvm/ADT/StringRef.h"
     23 #include "llvm/Support/MemoryBuffer.h"
     24 #include "llvm/Support/SourceMgr.h"
     25 #include "llvm/Support/YAMLParser.h"
     26 #include <memory>
     27 #include <string>
     28 #include <vector>
     29 
     30 namespace clang {
     31 namespace tooling {
     32 
     33 /// \brief A JSON based compilation database.
     34 ///
     35 /// JSON compilation database files must contain a list of JSON objects which
     36 /// provide the command lines in the attributes 'directory', 'command' and
     37 /// 'file':
     38 /// [
     39 ///   { "directory": "<working directory of the compile>",
     40 ///     "command": "<compile command line>",
     41 ///     "file": "<path to source file>"
     42 ///   },
     43 ///   ...
     44 /// ]
     45 /// Each object entry defines one compile action. The specified file is
     46 /// considered to be the main source file for the translation unit.
     47 ///
     48 /// JSON compilation databases can for example be generated in CMake projects
     49 /// by setting the flag -DCMAKE_EXPORT_COMPILE_COMMANDS.
     50 class JSONCompilationDatabase : public CompilationDatabase {
     51 public:
     52   /// \brief Loads a JSON compilation database from the specified file.
     53   ///
     54   /// Returns NULL and sets ErrorMessage if the database could not be
     55   /// loaded from the given file.
     56   static JSONCompilationDatabase *loadFromFile(StringRef FilePath,
     57                                                std::string &ErrorMessage);
     58 
     59   /// \brief Loads a JSON compilation database from a data buffer.
     60   ///
     61   /// Returns NULL and sets ErrorMessage if the database could not be loaded.
     62   static JSONCompilationDatabase *loadFromBuffer(StringRef DatabaseString,
     63                                                  std::string &ErrorMessage);
     64 
     65   /// \brief Returns all compile comamnds in which the specified file was
     66   /// compiled.
     67   ///
     68   /// FIXME: Currently FilePath must be an absolute path inside the
     69   /// source directory which does not have symlinks resolved.
     70   std::vector<CompileCommand>
     71   getCompileCommands(StringRef FilePath) const override;
     72 
     73   /// \brief Returns the list of all files available in the compilation database.
     74   ///
     75   /// These are the 'file' entries of the JSON objects.
     76   std::vector<std::string> getAllFiles() const override;
     77 
     78   /// \brief Returns all compile commands for all the files in the compilation
     79   /// database.
     80   std::vector<CompileCommand> getAllCompileCommands() const override;
     81 
     82 private:
     83   /// \brief Constructs a JSON compilation database on a memory buffer.
     84   JSONCompilationDatabase(llvm::MemoryBuffer *Database)
     85     : Database(Database), YAMLStream(Database->getBuffer(), SM) {}
     86 
     87   /// \brief Parses the database file and creates the index.
     88   ///
     89   /// Returns whether parsing succeeded. Sets ErrorMessage if parsing
     90   /// failed.
     91   bool parse(std::string &ErrorMessage);
     92 
     93   // Tuple (directory, commandline) where 'commandline' pointing to the
     94   // corresponding nodes in the YAML stream.
     95   typedef std::pair<llvm::yaml::ScalarNode*,
     96                     llvm::yaml::ScalarNode*> CompileCommandRef;
     97 
     98   /// \brief Converts the given array of CompileCommandRefs to CompileCommands.
     99   void getCommands(ArrayRef<CompileCommandRef> CommandsRef,
    100                    std::vector<CompileCommand> &Commands) const;
    101 
    102   // Maps file paths to the compile command lines for that file.
    103   llvm::StringMap< std::vector<CompileCommandRef> > IndexByFile;
    104 
    105   FileMatchTrie MatchTrie;
    106 
    107   std::unique_ptr<llvm::MemoryBuffer> Database;
    108   llvm::SourceMgr SM;
    109   llvm::yaml::Stream YAMLStream;
    110 };
    111 
    112 } // end namespace tooling
    113 } // end namespace clang
    114 
    115 #endif // LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H
    116