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