JSONCompilationDatabase.h revision 243830
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/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"
27#include <string>
28#include <vector>
29
30namespace clang {
31namespace 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.
50class JSONCompilationDatabase : public CompilationDatabase {
51public:
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 JSONCompilationDatabase *loadFromFile(StringRef FilePath,
57                                               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 JSONCompilationDatabase *loadFromBuffer(StringRef DatabaseString,
63                                                 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  virtual std::vector<CompileCommand> getCompileCommands(
71    StringRef FilePath) const;
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  virtual std::vector<std::string> getAllFiles() const;
77
78private:
79  /// \brief Constructs a JSON compilation database on a memory buffer.
80  JSONCompilationDatabase(llvm::MemoryBuffer *Database)
81    : Database(Database), YAMLStream(Database->getBuffer(), SM) {}
82
83  /// \brief Parses the database file and creates the index.
84  ///
85  /// Returns whether parsing succeeded. Sets ErrorMessage if parsing
86  /// failed.
87  bool parse(std::string &ErrorMessage);
88
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;
93
94  // Maps file paths to the compile command lines for that file.
95  llvm::StringMap< std::vector<CompileCommandRef> > IndexByFile;
96
97  FileMatchTrie MatchTrie;
98
99  llvm::OwningPtr<llvm::MemoryBuffer> Database;
100  llvm::SourceMgr SM;
101  llvm::yaml::Stream YAMLStream;
102};
103
104} // end namespace tooling
105} // end namespace clang
106
107#endif // LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H
108