]> granicus.if.org Git - clang/blob - include/clang/Tooling/JSONCompilationDatabase.h
894191f30386811cbb6053ea52b2b5b7838f00e9
[clang] / include / clang / Tooling / JSONCompilationDatabase.h
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 std::unique_ptr<JSONCompilationDatabase>
57   loadFromFile(StringRef FilePath, 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 std::unique_ptr<JSONCompilationDatabase>
63   loadFromBuffer(StringRef DatabaseString, 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(std::unique_ptr<llvm::MemoryBuffer> Database)
85       : Database(std::move(Database)),
86         YAMLStream(this->Database->getBuffer(), SM) {}
87
88   /// \brief Parses the database file and creates the index.
89   ///
90   /// Returns whether parsing succeeded. Sets ErrorMessage if parsing
91   /// failed.
92   bool parse(std::string &ErrorMessage);
93
94   // Tuple (directory, commandline) where 'commandline' pointing to the
95   // corresponding nodes in the YAML stream.
96   typedef std::pair<llvm::yaml::ScalarNode*,
97                     llvm::yaml::ScalarNode*> CompileCommandRef;
98
99   /// \brief Converts the given array of CompileCommandRefs to CompileCommands.
100   void getCommands(ArrayRef<CompileCommandRef> CommandsRef,
101                    std::vector<CompileCommand> &Commands) const;
102
103   // Maps file paths to the compile command lines for that file.
104   llvm::StringMap< std::vector<CompileCommandRef> > IndexByFile;
105
106   FileMatchTrie MatchTrie;
107
108   std::unique_ptr<llvm::MemoryBuffer> Database;
109   llvm::SourceMgr SM;
110   llvm::yaml::Stream YAMLStream;
111 };
112
113 } // end namespace tooling
114 } // end namespace clang
115
116 #endif // LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H