clang API Documentation
00001 //===--- JSONCompilationDatabase.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 // The JSONCompilationDatabase finds compilation databases supplied as a file 00011 // 'compile_commands.json'. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_CLANG_TOOLING_JSONCOMPILATIONDATABASE_H 00016 #define LLVM_CLANG_TOOLING_JSONCOMPILATIONDATABASE_H 00017 00018 #include "clang/Basic/LLVM.h" 00019 #include "clang/Tooling/CompilationDatabase.h" 00020 #include "clang/Tooling/FileMatchTrie.h" 00021 #include "llvm/ADT/StringMap.h" 00022 #include "llvm/ADT/StringRef.h" 00023 #include "llvm/Support/MemoryBuffer.h" 00024 #include "llvm/Support/SourceMgr.h" 00025 #include "llvm/Support/YAMLParser.h" 00026 #include <memory> 00027 #include <string> 00028 #include <vector> 00029 00030 namespace clang { 00031 namespace tooling { 00032 00033 /// \brief A JSON based compilation database. 00034 /// 00035 /// JSON compilation database files must contain a list of JSON objects which 00036 /// provide the command lines in the attributes 'directory', 'command' and 00037 /// 'file': 00038 /// [ 00039 /// { "directory": "<working directory of the compile>", 00040 /// "command": "<compile command line>", 00041 /// "file": "<path to source file>" 00042 /// }, 00043 /// ... 00044 /// ] 00045 /// Each object entry defines one compile action. The specified file is 00046 /// considered to be the main source file for the translation unit. 00047 /// 00048 /// JSON compilation databases can for example be generated in CMake projects 00049 /// by setting the flag -DCMAKE_EXPORT_COMPILE_COMMANDS. 00050 class JSONCompilationDatabase : public CompilationDatabase { 00051 public: 00052 /// \brief Loads a JSON compilation database from the specified file. 00053 /// 00054 /// Returns NULL and sets ErrorMessage if the database could not be 00055 /// loaded from the given file. 00056 static std::unique_ptr<JSONCompilationDatabase> 00057 loadFromFile(StringRef FilePath, std::string &ErrorMessage); 00058 00059 /// \brief Loads a JSON compilation database from a data buffer. 00060 /// 00061 /// Returns NULL and sets ErrorMessage if the database could not be loaded. 00062 static std::unique_ptr<JSONCompilationDatabase> 00063 loadFromBuffer(StringRef DatabaseString, std::string &ErrorMessage); 00064 00065 /// \brief Returns all compile comamnds in which the specified file was 00066 /// compiled. 00067 /// 00068 /// FIXME: Currently FilePath must be an absolute path inside the 00069 /// source directory which does not have symlinks resolved. 00070 std::vector<CompileCommand> 00071 getCompileCommands(StringRef FilePath) const override; 00072 00073 /// \brief Returns the list of all files available in the compilation database. 00074 /// 00075 /// These are the 'file' entries of the JSON objects. 00076 std::vector<std::string> getAllFiles() const override; 00077 00078 /// \brief Returns all compile commands for all the files in the compilation 00079 /// database. 00080 std::vector<CompileCommand> getAllCompileCommands() const override; 00081 00082 private: 00083 /// \brief Constructs a JSON compilation database on a memory buffer. 00084 JSONCompilationDatabase(std::unique_ptr<llvm::MemoryBuffer> Database) 00085 : Database(std::move(Database)), 00086 YAMLStream(this->Database->getBuffer(), SM) {} 00087 00088 /// \brief Parses the database file and creates the index. 00089 /// 00090 /// Returns whether parsing succeeded. Sets ErrorMessage if parsing 00091 /// failed. 00092 bool parse(std::string &ErrorMessage); 00093 00094 // Tuple (directory, commandline) where 'commandline' pointing to the 00095 // corresponding nodes in the YAML stream. 00096 typedef std::pair<llvm::yaml::ScalarNode*, 00097 llvm::yaml::ScalarNode*> CompileCommandRef; 00098 00099 /// \brief Converts the given array of CompileCommandRefs to CompileCommands. 00100 void getCommands(ArrayRef<CompileCommandRef> CommandsRef, 00101 std::vector<CompileCommand> &Commands) const; 00102 00103 // Maps file paths to the compile command lines for that file. 00104 llvm::StringMap< std::vector<CompileCommandRef> > IndexByFile; 00105 00106 FileMatchTrie MatchTrie; 00107 00108 std::unique_ptr<llvm::MemoryBuffer> Database; 00109 llvm::SourceMgr SM; 00110 llvm::yaml::Stream YAMLStream; 00111 }; 00112 00113 } // end namespace tooling 00114 } // end namespace clang 00115 00116 #endif