| Manuel Klimek | 9a05fa9 | 2011-04-27 16:39:14 +0000 | [diff] [blame] | 1 | //===--- JsonCompileCommandLineDatabase - Simple JSON database --*- 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 | // This file implements reading a compile command line database, as written |
| 11 | // out for example by CMake. It only supports the subset of the JSON standard |
| 12 | // that is needed to parse the CMake output. |
| 13 | // See http://www.json.org/ for the full standard. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #ifndef LLVM_CLANG_TOOLING_JSON_COMPILE_COMMAND_LINE_DATABASE_H |
| 18 | #define LLVM_CLANG_TOOLING_JSON_COMPILE_COMMAND_LINE_DATABASE_H |
| 19 | |
| 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace clang { |
| 25 | namespace tooling { |
| 26 | |
| 27 | /// \brief Converts a JSON escaped command line to a vector of arguments. |
| 28 | /// |
| 29 | /// \param JsonEscapedCommandLine The escaped command line as a string. This |
| 30 | /// is assumed to be escaped as a JSON string (e.g. " and \ are escaped). |
| 31 | /// In addition, any arguments containing spaces are assumed to be \-escaped |
| 32 | /// |
| 33 | /// For example, the input (|| denoting non C-escaped strings): |
| 34 | /// |./call a \"b \\\" c \\\\ \" d| |
| 35 | /// would yield: |
| 36 | /// [ |./call|, |a|, |b " c \ |, |d| ]. |
| 37 | std::vector<std::string> UnescapeJsonCommandLine( |
| 38 | llvm::StringRef JsonEscapedCommandLine); |
| 39 | |
| 40 | /// \brief Interface for users of the JsonCompileCommandLineParser. |
| 41 | class CompileCommandHandler { |
| 42 | public: |
| 43 | virtual ~CompileCommandHandler() {}; |
| 44 | |
| 45 | /// \brief Called after all translation units are parsed. |
| 46 | virtual void EndTranslationUnits() {} |
| 47 | |
| 48 | /// \brief Called at the end of a single translation unit. |
| 49 | virtual void EndTranslationUnit() {} |
| 50 | |
| 51 | /// \brief Called for every (Key, Value) pair in a translation unit |
| 52 | /// description. |
| 53 | virtual void HandleKeyValue(llvm::StringRef Key, llvm::StringRef Value) {} |
| 54 | }; |
| 55 | |
| 56 | /// \brief A JSON parser that supports the subset of JSON needed to parse |
| 57 | /// JSON compile command line databases as written out by CMake. |
| 58 | /// |
| 59 | /// The supported subset describes a list of compile command lines for |
| 60 | /// each processed translation unit. The translation units are stored in a |
| 61 | /// JSON array, where each translation unit is described by a JSON object |
| 62 | /// containing (Key, Value) pairs for the working directory the compile command |
| 63 | /// line was executed from, the main C/C++ input file of the translation unit |
| 64 | /// and the actual compile command line, for example: |
| 65 | /// [ |
| 66 | /// { |
| 67 | /// "file":"/file.cpp", |
| 68 | /// "directory":"/", |
| 69 | /// "command":"/cc /file.cpp" |
| 70 | /// } |
| 71 | /// ] |
| 72 | class JsonCompileCommandLineParser { |
| 73 | public: |
| 74 | /// \brief Create a parser on 'Input', calling 'CommandHandler' to handle the |
| 75 | /// parsed constructs. 'CommandHandler' may be NULL in order to just check |
| 76 | /// the validity of 'Input'. |
| 77 | JsonCompileCommandLineParser(const llvm::StringRef Input, |
| 78 | CompileCommandHandler *CommandHandler); |
| 79 | |
| 80 | /// \brief Parses the specified input. Returns true if no parsing errors were |
| 81 | /// foudn. |
| 82 | bool Parse(); |
| 83 | |
| 84 | /// \brief Returns an error message if Parse() returned false previously. |
| 85 | std::string GetErrorMessage() const; |
| 86 | |
| 87 | private: |
| 88 | bool ParseTranslationUnits(); |
| 89 | bool ParseTranslationUnit(bool First); |
| 90 | bool ParseObjectKeyValuePairs(); |
| 91 | bool ParseString(llvm::StringRef &String); |
| 92 | bool Consume(char C); |
| 93 | bool ConsumeOrError(char C, llvm::StringRef Message); |
| 94 | void NextNonWhitespace(); |
| 95 | bool IsWhitespace(); |
| 96 | void SetExpectError(char C, llvm::StringRef Message); |
| 97 | |
| 98 | const llvm::StringRef Input; |
| 99 | llvm::StringRef::iterator Position; |
| 100 | std::string ErrorMessage; |
| 101 | CompileCommandHandler * const CommandHandler; |
| 102 | }; |
| 103 | |
| 104 | } // end namespace tooling |
| 105 | } // end namespace clang |
| 106 | |
| 107 | #endif // LLVM_CLANG_TOOLING_JSON_COMPILE_COMMAND_LINE_DATABASE_H |