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;
79 /// \brief Constructs a JSON compilation database on a memory buffer.
80 JSONCompilationDatabase(llvm::MemoryBuffer *Database)
81 : Database(Database), YAMLStream(Database->getBuffer(), SM) {}
83 /// \brief Parses the database file and creates the index.
85 /// Returns whether parsing succeeded. Sets ErrorMessage if parsing
87 bool parse(std::string &ErrorMessage);
89 // Tuple (directory, commandline) where 'commandline' pointing to the
90 // corresponding nodes in the YAML stream.
91 typedef std::pair<llvm::yaml::ScalarNode*,
92 llvm::yaml::ScalarNode*> CompileCommandRef;
94 // Maps file paths to the compile command lines for that file.
95 llvm::StringMap< std::vector<CompileCommandRef> > IndexByFile;
97 FileMatchTrie MatchTrie;
99 llvm::OwningPtr<llvm::MemoryBuffer> Database;
101 llvm::yaml::Stream YAMLStream;
104 } // end namespace tooling
105 } // end namespace clang
107 #endif // LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H