• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_JSONCOMPILATIONDATABASE_H
16 #define LLVM_CLANG_TOOLING_JSONCOMPILATIONDATABASE_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',
37 /// 'arguments' and 'file':
38 /// [
39 ///   { "directory": "<working directory of the compile>",
40 ///     "command": "<compile command line>",
41 ///     "file": "<path to source file>"
42 ///   },
43 ///   { "directory": "<working directory of the compile>",
44 ///     "arguments": ["<raw>", "<command>" "<line>" "<parameters>"],
45 ///     "file": "<path to source file>"
46 ///   },
47 ///   ...
48 /// ]
49 /// Each object entry defines one compile action. The specified file is
50 /// considered to be the main source file for the translation unit.
51 ///
52 /// 'command' is a full command line that will be unescaped.
53 ///
54 /// 'arguments' is a list of command line arguments that will not be unescaped.
55 ///
56 /// JSON compilation databases can for example be generated in CMake projects
57 /// by setting the flag -DCMAKE_EXPORT_COMPILE_COMMANDS.
58 class JSONCompilationDatabase : public CompilationDatabase {
59 public:
60   /// \brief Loads a JSON compilation database from the specified file.
61   ///
62   /// Returns NULL and sets ErrorMessage if the database could not be
63   /// loaded from the given file.
64   static std::unique_ptr<JSONCompilationDatabase>
65   loadFromFile(StringRef FilePath, std::string &ErrorMessage);
66 
67   /// \brief Loads a JSON compilation database from a data buffer.
68   ///
69   /// Returns NULL and sets ErrorMessage if the database could not be loaded.
70   static std::unique_ptr<JSONCompilationDatabase>
71   loadFromBuffer(StringRef DatabaseString, std::string &ErrorMessage);
72 
73   /// \brief Returns all compile comamnds in which the specified file was
74   /// compiled.
75   ///
76   /// FIXME: Currently FilePath must be an absolute path inside the
77   /// source directory which does not have symlinks resolved.
78   std::vector<CompileCommand>
79   getCompileCommands(StringRef FilePath) const override;
80 
81   /// \brief Returns the list of all files available in the compilation database.
82   ///
83   /// These are the 'file' entries of the JSON objects.
84   std::vector<std::string> getAllFiles() const override;
85 
86   /// \brief Returns all compile commands for all the files in the compilation
87   /// database.
88   std::vector<CompileCommand> getAllCompileCommands() const override;
89 
90 private:
91   /// \brief Constructs a JSON compilation database on a memory buffer.
JSONCompilationDatabase(std::unique_ptr<llvm::MemoryBuffer> Database)92   JSONCompilationDatabase(std::unique_ptr<llvm::MemoryBuffer> Database)
93       : Database(std::move(Database)),
94         YAMLStream(this->Database->getBuffer(), SM) {}
95 
96   /// \brief Parses the database file and creates the index.
97   ///
98   /// Returns whether parsing succeeded. Sets ErrorMessage if parsing
99   /// failed.
100   bool parse(std::string &ErrorMessage);
101 
102   // Tuple (directory, filename, commandline) where 'commandline' points to the
103   // corresponding scalar nodes in the YAML stream.
104   // If the command line contains a single argument, it is a shell-escaped
105   // command line.
106   // Otherwise, each entry in the command line vector is a literal
107   // argument to the compiler.
108   typedef std::tuple<llvm::yaml::ScalarNode *,
109                      llvm::yaml::ScalarNode *,
110                     std::vector<llvm::yaml::ScalarNode *>> CompileCommandRef;
111 
112   /// \brief Converts the given array of CompileCommandRefs to CompileCommands.
113   void getCommands(ArrayRef<CompileCommandRef> CommandsRef,
114                    std::vector<CompileCommand> &Commands) const;
115 
116   // Maps file paths to the compile command lines for that file.
117   llvm::StringMap<std::vector<CompileCommandRef>> IndexByFile;
118 
119   /// All the compile commands in the order that they were provided in the
120   /// JSON stream.
121   std::vector<CompileCommandRef> AllCommands;
122 
123   FileMatchTrie MatchTrie;
124 
125   std::unique_ptr<llvm::MemoryBuffer> Database;
126   llvm::SourceMgr SM;
127   llvm::yaml::Stream YAMLStream;
128 };
129 
130 } // end namespace tooling
131 } // end namespace clang
132 
133 #endif
134