summaryrefslogtreecommitdiffstats
path: root/lib/Tooling/JsonCompileCommandLineDatabase.h
blob: 9e776d60010dfa9c03f269b5eea5694ae6557ebb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//===--- JsonCompileCommandLineDatabase - Simple JSON database --*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file implements reading a compile command line database, as written
//  out for example by CMake. It only supports the subset of the JSON standard
//  that is needed to parse the CMake output.
//  See http://www.json.org/ for the full standard.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLING_JSON_COMPILE_COMMAND_LINE_DATABASE_H
#define LLVM_CLANG_TOOLING_JSON_COMPILE_COMMAND_LINE_DATABASE_H

#include "llvm/ADT/StringRef.h"
#include <string>
#include <vector>

namespace clang {
namespace tooling {

/// \brief Converts a JSON escaped command line to a vector of arguments.
///
/// \param JsonEscapedCommandLine The escaped command line as a string. This
/// is assumed to be escaped as a JSON string (e.g. " and \ are escaped).
/// In addition, any arguments containing spaces are assumed to be \-escaped
///
/// For example, the input (|| denoting non C-escaped strings):
///   |./call  a  \"b \\\" c \\\\ \"  d|
/// would yield:
///   [ |./call|, |a|, |b " c \ |, |d| ].
std::vector<std::string> UnescapeJsonCommandLine(
    llvm::StringRef JsonEscapedCommandLine);

/// \brief Interface for users of the JsonCompileCommandLineParser.
class CompileCommandHandler {
 public:
  virtual ~CompileCommandHandler() {}

  /// \brief Called after all translation units are parsed.
  virtual void EndTranslationUnits() {}

  /// \brief Called at the end of a single translation unit.
  virtual void EndTranslationUnit() {}

  /// \brief Called for every (Key, Value) pair in a translation unit
  /// description.
  virtual void HandleKeyValue(llvm::StringRef Key, llvm::StringRef Value) {}
};

/// \brief A JSON parser that supports the subset of JSON needed to parse
/// JSON compile command line databases as written out by CMake.
///
/// The supported subset describes a list of compile command lines for
/// each processed translation unit. The translation units are stored in a
/// JSON array, where each translation unit is described by a JSON object
/// containing (Key, Value) pairs for the working directory the compile command
/// line was executed from, the main C/C++ input file of the translation unit
/// and the actual compile command line, for example:
/// [
///   {
///     "file":"/file.cpp",
///     "directory":"/",
///     "command":"/cc /file.cpp"
///   }
/// ]
class JsonCompileCommandLineParser {
 public:
  /// \brief Create a parser on 'Input', calling 'CommandHandler' to handle the
  /// parsed constructs. 'CommandHandler' may be NULL in order to just check
  /// the validity of 'Input'.
  JsonCompileCommandLineParser(const llvm::StringRef Input,
                               CompileCommandHandler *CommandHandler);

  /// \brief Parses the specified input. Returns true if no parsing errors were
  /// foudn.
  bool Parse();

  /// \brief Returns an error message if Parse() returned false previously.
  std::string GetErrorMessage() const;

 private:
  bool ParseTranslationUnits();
  bool ParseTranslationUnit(bool First);
  bool ParseObjectKeyValuePairs();
  bool ParseString(llvm::StringRef &String);
  bool Consume(char C);
  bool ConsumeOrError(char C, llvm::StringRef Message);
  void NextNonWhitespace();
  bool IsWhitespace();
  void SetExpectError(char C, llvm::StringRef Message);

  const llvm::StringRef Input;
  llvm::StringRef::iterator Position;
  std::string ErrorMessage;
  CompileCommandHandler * const CommandHandler;
};

} // end namespace tooling
} // end namespace clang

#endif // LLVM_CLANG_TOOLING_JSON_COMPILE_COMMAND_LINE_DATABASE_H
OpenPOWER on IntegriCloud