clang  14.0.0git
SerializedDiagnosticReader.h
Go to the documentation of this file.
1 //===- SerializedDiagnosticReader.h - Reads diagnostics ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_FRONTEND_SERIALIZEDDIAGNOSTICREADER_H
10 #define LLVM_CLANG_FRONTEND_SERIALIZEDDIAGNOSTICREADER_H
11 
12 #include "clang/Basic/LLVM.h"
13 #include "llvm/Bitstream/BitstreamReader.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/ErrorOr.h"
16 #include <system_error>
17 
18 namespace clang {
19 namespace serialized_diags {
20 
21 enum class SDError {
22  CouldNotLoad = 1,
34  /// A generic error for subclass handlers that don't want or need to define
35  /// their own error_category.
37 };
38 
39 const std::error_category &SDErrorCategory();
40 
41 inline std::error_code make_error_code(SDError E) {
42  return std::error_code(static_cast<int>(E), SDErrorCategory());
43 }
44 
45 /// A location that is represented in the serialized diagnostics.
46 struct Location {
47  unsigned FileID;
48  unsigned Line;
49  unsigned Col;
50  unsigned Offset;
51 
52  Location(unsigned FileID, unsigned Line, unsigned Col, unsigned Offset)
54 };
55 
56 /// A base class that handles reading serialized diagnostics from a file.
57 ///
58 /// Subclasses should override the visit* methods with their logic for handling
59 /// the various constructs that are found in serialized diagnostics.
61 public:
62  SerializedDiagnosticReader() = default;
63  virtual ~SerializedDiagnosticReader() = default;
64 
65  /// Read the diagnostics in \c File
66  std::error_code readDiagnostics(StringRef File);
67 
68 private:
69  enum class Cursor;
70 
71  /// Read to the next record or block to process.
72  llvm::ErrorOr<Cursor> skipUntilRecordOrBlock(llvm::BitstreamCursor &Stream,
73  unsigned &BlockOrRecordId);
74 
75  /// Read a metadata block from \c Stream.
76  std::error_code readMetaBlock(llvm::BitstreamCursor &Stream);
77 
78  /// Read a diagnostic block from \c Stream.
79  std::error_code readDiagnosticBlock(llvm::BitstreamCursor &Stream);
80 
81 protected:
82  /// Visit the start of a diagnostic block.
83  virtual std::error_code visitStartOfDiagnostic() { return {}; }
84 
85  /// Visit the end of a diagnostic block.
86  virtual std::error_code visitEndOfDiagnostic() { return {}; }
87 
88  /// Visit a category. This associates the category \c ID to a \c Name.
89  virtual std::error_code visitCategoryRecord(unsigned ID, StringRef Name) {
90  return {};
91  }
92 
93  /// Visit a flag. This associates the flag's \c ID to a \c Name.
94  virtual std::error_code visitDiagFlagRecord(unsigned ID, StringRef Name) {
95  return {};
96  }
97 
98  /// Visit a diagnostic.
99  virtual std::error_code
101  unsigned Category, unsigned Flag, StringRef Message) {
102  return {};
103  }
104 
105  /// Visit a filename. This associates the file's \c ID to a \c Name.
106  virtual std::error_code visitFilenameRecord(unsigned ID, unsigned Size,
107  unsigned Timestamp,
108  StringRef Name) {
109  return {};
110  }
111 
112  /// Visit a fixit hint.
113  virtual std::error_code
114  visitFixitRecord(const Location &Start, const Location &End, StringRef Text) {
115  return {};
116  }
117 
118  /// Visit a source range.
119  virtual std::error_code visitSourceRangeRecord(const Location &Start,
120  const Location &End) {
121  return {};
122  }
123 
124  /// Visit the version of the set of diagnostics.
125  virtual std::error_code visitVersionRecord(unsigned Version) { return {}; }
126 };
127 
128 } // namespace serialized_diags
129 } // namespace clang
130 
131 namespace std {
132 
133 template <>
134 struct is_error_code_enum<clang::serialized_diags::SDError> : std::true_type {};
135 
136 } // namespace std
137 
138 #endif // LLVM_CLANG_FRONTEND_SERIALIZEDDIAGNOSTICREADER_H
clang::diag::Severity
Severity
Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs to either Ignore (nothing),...
Definition: DiagnosticIDs.h:80
clang::serialized_diags::SDError::CouldNotLoad
@ CouldNotLoad
clang::serialized_diags::SDError::HandlerFailed
@ HandlerFailed
A generic error for subclass handlers that don't want or need to define their own error_category.
clang::serialized_diags::SerializedDiagnosticReader::visitCategoryRecord
virtual std::error_code visitCategoryRecord(unsigned ID, StringRef Name)
Visit a category. This associates the category ID to a Name.
Definition: SerializedDiagnosticReader.h:89
clang::serialized_diags::Location::FileID
unsigned FileID
Definition: SerializedDiagnosticReader.h:47
clang::DeclaratorContext::File
@ File
clang::serialized_diags::SerializedDiagnosticReader::visitFixitRecord
virtual std::error_code visitFixitRecord(const Location &Start, const Location &End, StringRef Text)
Visit a fixit hint.
Definition: SerializedDiagnosticReader.h:114
clang::serialized_diags::SerializedDiagnosticReader::visitFilenameRecord
virtual std::error_code visitFilenameRecord(unsigned ID, unsigned Size, unsigned Timestamp, StringRef Name)
Visit a filename. This associates the file's ID to a Name.
Definition: SerializedDiagnosticReader.h:106
clang::serialized_diags::SerializedDiagnosticReader::~SerializedDiagnosticReader
virtual ~SerializedDiagnosticReader()=default
Cursor
Cursor
Definition: SerializedDiagnosticReader.cpp:109
clang::serialized_diags::SDError::MalformedDiagnosticRecord
@ MalformedDiagnosticRecord
clang::serialized_diags::SDError::MalformedSubBlock
@ MalformedSubBlock
clang::serialized_diags::SDError::MalformedMetadataBlock
@ MalformedMetadataBlock
clang::serialized_diags::SDErrorCategory
const std::error_category & SDErrorCategory()
Definition: SerializedDiagnosticReader.cpp:369
clang::serialized_diags::SDError
SDError
Definition: SerializedDiagnosticReader.h:21
End
SourceLocation End
Definition: USRLocFinder.cpp:167
clang::serialized_diags::SDError::VersionMismatch
@ VersionMismatch
clang::serialized_diags::SerializedDiagnosticReader::visitSourceRangeRecord
virtual std::error_code visitSourceRangeRecord(const Location &Start, const Location &End)
Visit a source range.
Definition: SerializedDiagnosticReader.h:119
clang::serialized_diags::SDError::MalformedTopLevelBlock
@ MalformedTopLevelBlock
clang::serialized_diags::SDError::UnsupportedConstruct
@ UnsupportedConstruct
clang::serialized_diags::SerializedDiagnosticReader::visitStartOfDiagnostic
virtual std::error_code visitStartOfDiagnostic()
Visit the start of a diagnostic block.
Definition: SerializedDiagnosticReader.h:83
clang::serialized_diags::SDError::MalformedDiagnosticBlock
@ MalformedDiagnosticBlock
clang::serialized_diags::SDError::MalformedBlockInfoBlock
@ MalformedBlockInfoBlock
clang::serialized_diags::SerializedDiagnosticReader::SerializedDiagnosticReader
SerializedDiagnosticReader()=default
Category
int Category
Definition: Format.cpp:2269
clang::serialized_diags::SerializedDiagnosticReader::readDiagnostics
std::error_code readDiagnostics(StringRef File)
Read the diagnostics in File.
Definition: SerializedDiagnosticReader.cpp:28
clang::serialized_diags::SDError::InvalidSignature
@ InvalidSignature
clang::serialized_diags::SerializedDiagnosticReader::visitEndOfDiagnostic
virtual std::error_code visitEndOfDiagnostic()
Visit the end of a diagnostic block.
Definition: SerializedDiagnosticReader.h:86
clang::serialized_diags::SerializedDiagnosticReader::visitDiagnosticRecord
virtual std::error_code visitDiagnosticRecord(unsigned Severity, const Location &Location, unsigned Category, unsigned Flag, StringRef Message)
Visit a diagnostic.
Definition: SerializedDiagnosticReader.h:100
clang::serialized_diags::Location::Line
unsigned Line
Definition: SerializedDiagnosticReader.h:48
clang::serialized_diags::Location
A location that is represented in the serialized diagnostics.
Definition: SerializedDiagnosticReader.h:46
clang::serialized_diags::SDError::MissingVersion
@ MissingVersion
clang::serialized_diags::SDError::InvalidDiagnostics
@ InvalidDiagnostics
clang::serialized_diags::SerializedDiagnosticReader::visitDiagFlagRecord
virtual std::error_code visitDiagFlagRecord(unsigned ID, StringRef Name)
Visit a flag. This associates the flag's ID to a Name.
Definition: SerializedDiagnosticReader.h:94
LLVM.h
std
Definition: Format.h:3832
clang::Builtin::ID
ID
Definition: Builtins.h:48
clang
Definition: CalledOnceCheck.h:17
Text
StringRef Text
Definition: Format.cpp:2267
clang::serialized_diags::Location::Offset
unsigned Offset
Definition: SerializedDiagnosticReader.h:50
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:40
clang::serialized_diags::Location::Col
unsigned Col
Definition: SerializedDiagnosticReader.h:49
clang::serialized_diags::SerializedDiagnosticReader
A base class that handles reading serialized diagnostics from a file.
Definition: SerializedDiagnosticReader.h:60
clang::serialized_diags::SerializedDiagnosticReader::visitVersionRecord
virtual std::error_code visitVersionRecord(unsigned Version)
Visit the version of the set of diagnostics.
Definition: SerializedDiagnosticReader.h:125
clang::serialized_diags::Location::Location
Location(unsigned FileID, unsigned Line, unsigned Col, unsigned Offset)
Definition: SerializedDiagnosticReader.h:52
clang::serialized_diags::make_error_code
std::error_code make_error_code(SDError E)
Definition: SerializedDiagnosticReader.h:41