1 //===--- JSONCompilationDatabase.h - ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The JSONCompilationDatabase finds compilation databases supplied as a file
11 // 'compile_commands.json'.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H
16 #define LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H
18 #include "clang/Basic/LLVM.h"
19 #include "clang/Tooling/CompilationDatabase.h"
20 #include "clang/Tooling/FileMatchTrie.h"
21 #include "llvm/ADT/OwningPtr.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "llvm/Support/YAMLParser.h"
33 /// \brief A JSON based compilation database.
35 /// JSON compilation database files must contain a list of JSON objects which
36 /// provide the command lines in the attributes 'directory', 'command' and
39 /// { "directory": "<working directory of the compile>",
40 /// "command": "<compile command line>",
41 /// "file": "<path to source file>"
45 /// Each object entry defines one compile action. The specified file is
46 /// considered to be the main source file for the translation unit.
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 {
52 /// \brief Loads a JSON compilation database from the specified file.
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);
59 /// \brief Loads a JSON compilation database from a data buffer.
61 /// Returns NULL and sets ErrorMessage if the database could not be loaded.
62 static JSONCompilationDatabase *loadFromBuffer(StringRef DatabaseString,
63 std::string &ErrorMessage);
65 /// \brief Returns all compile comamnds in which the specified file was
68 /// FIXME: Currently FilePath must be an absolute path inside the
69 /// source directory which does not have symlinks resolved.
70 virtual std::vector<CompileCommand> getCompileCommands(
71 StringRef FilePath) const;
73 /// \brief Returns the list of all files available in the compilation database.
75 /// These are the 'file' entries of the JSON objects.
76 virtual std::vector<std::string> getAllFiles() const;
78 /// \brief Returns all compile commands for all the files in the compilation
80 virtual std::vector<CompileCommand> getAllCompileCommands() const;
83 /// \brief Constructs a JSON compilation database on a memory buffer.
84 JSONCompilationDatabase(llvm::MemoryBuffer *Database)
85 : Database(Database), YAMLStream(Database->getBuffer(), SM) {}
87 /// \brief Parses the database file and creates the index.
89 /// Returns whether parsing succeeded. Sets ErrorMessage if parsing
91 bool parse(std::string &ErrorMessage);
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;
98 /// \brief Converts the given array of CompileCommandRefs to CompileCommands.
99 void getCommands(ArrayRef<CompileCommandRef> CommandsRef,
100 std::vector<CompileCommand> &Commands) const;
102 // Maps file paths to the compile command lines for that file.
103 llvm::StringMap< std::vector<CompileCommandRef> > IndexByFile;
105 FileMatchTrie MatchTrie;
107 OwningPtr<llvm::MemoryBuffer> Database;
109 llvm::yaml::Stream YAMLStream;
112 } // end namespace tooling
113 } // end namespace clang
115 #endif // LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H