17fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper//===--- JSONCompilationDatabase.h - ----------------------------*- C++ -*-===// 27fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper// 37fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper// The LLVM Compiler Infrastructure 47fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper// 57fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper// This file is distributed under the University of Illinois Open Source 67fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper// License. See LICENSE.TXT for details. 77fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper// 87fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper//===----------------------------------------------------------------------===// 97fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper// 107fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper// The JSONCompilationDatabase finds compilation databases supplied as a file 117fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper// 'compile_commands.json'. 127fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper// 137fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper//===----------------------------------------------------------------------===// 147fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper 157fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper#ifndef LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H 167fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper#define LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H 177fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper 187fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper#include "clang/Basic/LLVM.h" 197fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper#include "clang/Tooling/CompilationDatabase.h" 20d3420c906e3605d94c084e8b8b1f3fa490093c86Daniel Jasper#include "clang/Tooling/FileMatchTrie.h" 217fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper#include "llvm/ADT/StringMap.h" 227fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper#include "llvm/ADT/StringRef.h" 237fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper#include "llvm/Support/MemoryBuffer.h" 247fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper#include "llvm/Support/SourceMgr.h" 257fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper#include "llvm/Support/YAMLParser.h" 26651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include <memory> 277fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper#include <string> 287fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper#include <vector> 297fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper 307fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jaspernamespace clang { 317fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jaspernamespace tooling { 327fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper 337fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// \brief A JSON based compilation database. 347fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// 357fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// JSON compilation database files must contain a list of JSON objects which 367fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// provide the command lines in the attributes 'directory', 'command' and 377fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// 'file': 387fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// [ 397fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// { "directory": "<working directory of the compile>", 407fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// "command": "<compile command line>", 417fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// "file": "<path to source file>" 427fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// }, 437fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// ... 447fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// ] 457fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// Each object entry defines one compile action. The specified file is 467fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// considered to be the main source file for the translation unit. 477fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// 487fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// JSON compilation databases can for example be generated in CMake projects 497fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper/// by setting the flag -DCMAKE_EXPORT_COMPILE_COMMANDS. 507fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasperclass JSONCompilationDatabase : public CompilationDatabase { 517fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasperpublic: 527fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// \brief Loads a JSON compilation database from the specified file. 537fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// 547fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// Returns NULL and sets ErrorMessage if the database could not be 557fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// loaded from the given file. 567fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper static JSONCompilationDatabase *loadFromFile(StringRef FilePath, 577fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper std::string &ErrorMessage); 587fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper 597fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// \brief Loads a JSON compilation database from a data buffer. 607fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// 617fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// Returns NULL and sets ErrorMessage if the database could not be loaded. 627fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper static JSONCompilationDatabase *loadFromBuffer(StringRef DatabaseString, 637fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper std::string &ErrorMessage); 647fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper 657fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// \brief Returns all compile comamnds in which the specified file was 667fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// compiled. 677fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// 687fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// FIXME: Currently FilePath must be an absolute path inside the 697fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// source directory which does not have symlinks resolved. 70651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines std::vector<CompileCommand> 71651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines getCompileCommands(StringRef FilePath) const override; 727fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper 737fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// \brief Returns the list of all files available in the compilation database. 747fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// 757fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// These are the 'file' entries of the JSON objects. 76651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines std::vector<std::string> getAllFiles() const override; 777fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper 787e96bfb4d507700a122f270a11ce3fc0e8e36c85Argyrios Kyrtzidis /// \brief Returns all compile commands for all the files in the compilation 797e96bfb4d507700a122f270a11ce3fc0e8e36c85Argyrios Kyrtzidis /// database. 80651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines std::vector<CompileCommand> getAllCompileCommands() const override; 817e96bfb4d507700a122f270a11ce3fc0e8e36c85Argyrios Kyrtzidis 827fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasperprivate: 837fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// \brief Constructs a JSON compilation database on a memory buffer. 847fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper JSONCompilationDatabase(llvm::MemoryBuffer *Database) 857fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper : Database(Database), YAMLStream(Database->getBuffer(), SM) {} 867fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper 877fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// \brief Parses the database file and creates the index. 887fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// 897fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// Returns whether parsing succeeded. Sets ErrorMessage if parsing 907fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper /// failed. 917fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper bool parse(std::string &ErrorMessage); 927fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper 937fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper // Tuple (directory, commandline) where 'commandline' pointing to the 947fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper // corresponding nodes in the YAML stream. 957fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper typedef std::pair<llvm::yaml::ScalarNode*, 967fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper llvm::yaml::ScalarNode*> CompileCommandRef; 977fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper 987e96bfb4d507700a122f270a11ce3fc0e8e36c85Argyrios Kyrtzidis /// \brief Converts the given array of CompileCommandRefs to CompileCommands. 997e96bfb4d507700a122f270a11ce3fc0e8e36c85Argyrios Kyrtzidis void getCommands(ArrayRef<CompileCommandRef> CommandsRef, 1007e96bfb4d507700a122f270a11ce3fc0e8e36c85Argyrios Kyrtzidis std::vector<CompileCommand> &Commands) const; 1017e96bfb4d507700a122f270a11ce3fc0e8e36c85Argyrios Kyrtzidis 1027fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper // Maps file paths to the compile command lines for that file. 1037fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper llvm::StringMap< std::vector<CompileCommandRef> > IndexByFile; 1047fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper 105d3420c906e3605d94c084e8b8b1f3fa490093c86Daniel Jasper FileMatchTrie MatchTrie; 106d3420c906e3605d94c084e8b8b1f3fa490093c86Daniel Jasper 107651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines std::unique_ptr<llvm::MemoryBuffer> Database; 1087fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper llvm::SourceMgr SM; 1097fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper llvm::yaml::Stream YAMLStream; 1107fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper}; 1117fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper 1127fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper} // end namespace tooling 1137fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper} // end namespace clang 1147fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper 1157fd90b03a28df0626fdb44d05be9ddcdb2562686Daniel Jasper#endif // LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H 116