1//===--- SerializedDiagnosticReader.h - Reads diagnostics -------*- 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#ifndef LLVM_CLANG_FRONTEND_SERIALIZED_DIAGNOSTIC_READER_H_
11#define LLVM_CLANG_FRONTEND_SERIALIZED_DIAGNOSTIC_READER_H_
12
13#include "clang/Basic/LLVM.h"
14#include "llvm/Bitcode/BitstreamReader.h"
15#include "llvm/Support/ErrorOr.h"
16
17namespace clang {
18namespace serialized_diags {
19
20enum class SDError {
21  CouldNotLoad = 1,
22  InvalidSignature,
23  InvalidDiagnostics,
24  MalformedTopLevelBlock,
25  MalformedSubBlock,
26  MalformedBlockInfoBlock,
27  MalformedMetadataBlock,
28  MalformedDiagnosticBlock,
29  MalformedDiagnosticRecord,
30  MissingVersion,
31  VersionMismatch,
32  UnsupportedConstruct,
33  /// A generic error for subclass handlers that don't want or need to define
34  /// their own error_category.
35  HandlerFailed
36};
37
38const std::error_category &SDErrorCategory();
39
40inline std::error_code make_error_code(SDError E) {
41  return std::error_code(static_cast<int>(E), SDErrorCategory());
42}
43
44/// \brief A location that is represented in the serialized diagnostics.
45struct Location {
46  unsigned FileID;
47  unsigned Line;
48  unsigned Col;
49  unsigned Offset;
50  Location(unsigned FileID, unsigned Line, unsigned Col, unsigned Offset)
51      : FileID(FileID), Line(Line), Col(Col), Offset(Offset) {}
52};
53
54/// \brief A base class that handles reading serialized diagnostics from a file.
55///
56/// Subclasses should override the visit* methods with their logic for handling
57/// the various constructs that are found in serialized diagnostics.
58class SerializedDiagnosticReader {
59public:
60  SerializedDiagnosticReader() {}
61  virtual ~SerializedDiagnosticReader() {}
62
63  /// \brief Read the diagnostics in \c File
64  std::error_code readDiagnostics(StringRef File);
65
66private:
67  enum class Cursor;
68
69  /// \brief Read to the next record or block to process.
70  llvm::ErrorOr<Cursor> skipUntilRecordOrBlock(llvm::BitstreamCursor &Stream,
71                                               unsigned &BlockOrRecordId);
72
73  /// \brief Read a metadata block from \c Stream.
74  std::error_code readMetaBlock(llvm::BitstreamCursor &Stream);
75
76  /// \brief Read a diagnostic block from \c Stream.
77  std::error_code readDiagnosticBlock(llvm::BitstreamCursor &Stream);
78
79protected:
80  /// \brief Visit the start of a diagnostic block.
81  virtual std::error_code visitStartOfDiagnostic() {
82    return std::error_code();
83  }
84  /// \brief Visit the end of a diagnostic block.
85  virtual std::error_code visitEndOfDiagnostic() { return std::error_code(); }
86  /// \brief Visit a category. This associates the category \c ID to a \c Name.
87  virtual std::error_code visitCategoryRecord(unsigned ID, StringRef Name) {
88    return std::error_code();
89  }
90  /// \brief Visit a flag. This associates the flag's \c ID to a \c Name.
91  virtual std::error_code visitDiagFlagRecord(unsigned ID, StringRef Name) {
92    return std::error_code();
93  }
94  /// \brief Visit a diagnostic.
95  virtual std::error_code
96  visitDiagnosticRecord(unsigned Severity, const Location &Location,
97                        unsigned Category, unsigned Flag, StringRef Message) {
98    return std::error_code();
99  }
100  /// \brief Visit a filename. This associates the file's \c ID to a \c Name.
101  virtual std::error_code visitFilenameRecord(unsigned ID, unsigned Size,
102                                              unsigned Timestamp,
103                                              StringRef Name) {
104    return std::error_code();
105  }
106  /// \brief Visit a fixit hint.
107  virtual std::error_code
108  visitFixitRecord(const Location &Start, const Location &End, StringRef Text) {
109    return std::error_code();
110  }
111  /// \brief Visit a source range.
112  virtual std::error_code visitSourceRangeRecord(const Location &Start,
113                                                 const Location &End) {
114    return std::error_code();
115  }
116  /// \brief Visit the version of the set of diagnostics.
117  virtual std::error_code visitVersionRecord(unsigned Version) {
118    return std::error_code();
119  }
120};
121
122} // end serialized_diags namespace
123} // end clang namespace
124
125namespace std {
126template <>
127struct is_error_code_enum<clang::serialized_diags::SDError> : std::true_type {};
128}
129
130#endif
131