1 //===- SerializedDiagnosticReader.h - Reads diagnostics ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_FRONTEND_SERIALIZEDDIAGNOSTICREADER_H
11 #define LLVM_CLANG_FRONTEND_SERIALIZEDDIAGNOSTICREADER_H
13 #include "clang/Basic/LLVM.h"
14 #include "llvm/Bitcode/BitstreamReader.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/ErrorOr.h"
17 #include <system_error>
20 namespace serialized_diags {
26 MalformedTopLevelBlock,
28 MalformedBlockInfoBlock,
29 MalformedMetadataBlock,
30 MalformedDiagnosticBlock,
31 MalformedDiagnosticRecord,
35 /// A generic error for subclass handlers that don't want or need to define
36 /// their own error_category.
40 const std::error_category &SDErrorCategory();
42 inline std::error_code make_error_code(SDError E) {
43 return std::error_code(static_cast<int>(E), SDErrorCategory());
46 /// A location that is represented in the serialized diagnostics.
53 Location(unsigned FileID, unsigned Line, unsigned Col, unsigned Offset)
54 : FileID(FileID), Line(Line), Col(Col), Offset(Offset) {}
57 /// A base class that handles reading serialized diagnostics from a file.
59 /// Subclasses should override the visit* methods with their logic for handling
60 /// the various constructs that are found in serialized diagnostics.
61 class SerializedDiagnosticReader {
63 SerializedDiagnosticReader() = default;
64 virtual ~SerializedDiagnosticReader() = default;
66 /// Read the diagnostics in \c File
67 std::error_code readDiagnostics(StringRef File);
72 /// Read to the next record or block to process.
73 llvm::ErrorOr<Cursor> skipUntilRecordOrBlock(llvm::BitstreamCursor &Stream,
74 unsigned &BlockOrRecordId);
76 /// Read a metadata block from \c Stream.
77 std::error_code readMetaBlock(llvm::BitstreamCursor &Stream);
79 /// Read a diagnostic block from \c Stream.
80 std::error_code readDiagnosticBlock(llvm::BitstreamCursor &Stream);
83 /// Visit the start of a diagnostic block.
84 virtual std::error_code visitStartOfDiagnostic() { return {}; }
86 /// Visit the end of a diagnostic block.
87 virtual std::error_code visitEndOfDiagnostic() { return {}; }
89 /// Visit a category. This associates the category \c ID to a \c Name.
90 virtual std::error_code visitCategoryRecord(unsigned ID, StringRef Name) {
94 /// Visit a flag. This associates the flag's \c ID to a \c Name.
95 virtual std::error_code visitDiagFlagRecord(unsigned ID, StringRef Name) {
99 /// Visit a diagnostic.
100 virtual std::error_code
101 visitDiagnosticRecord(unsigned Severity, const Location &Location,
102 unsigned Category, unsigned Flag, StringRef Message) {
106 /// Visit a filename. This associates the file's \c ID to a \c Name.
107 virtual std::error_code visitFilenameRecord(unsigned ID, unsigned Size,
113 /// Visit a fixit hint.
114 virtual std::error_code
115 visitFixitRecord(const Location &Start, const Location &End, StringRef Text) {
119 /// Visit a source range.
120 virtual std::error_code visitSourceRangeRecord(const Location &Start,
121 const Location &End) {
125 /// Visit the version of the set of diagnostics.
126 virtual std::error_code visitVersionRecord(unsigned Version) { return {}; }
129 } // namespace serialized_diags
135 struct is_error_code_enum<clang::serialized_diags::SDError> : std::true_type {};
139 #endif // LLVM_CLANG_FRONTEND_SERIALIZEDDIAGNOSTICREADER_H