Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:55

0001 //===- SerializedDiagnosticReader.h - Reads diagnostics ---------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_CLANG_FRONTEND_SERIALIZEDDIAGNOSTICREADER_H
0010 #define LLVM_CLANG_FRONTEND_SERIALIZEDDIAGNOSTICREADER_H
0011 
0012 #include "clang/Basic/LLVM.h"
0013 #include "llvm/Bitstream/BitstreamReader.h"
0014 #include "llvm/ADT/StringRef.h"
0015 #include "llvm/Support/ErrorOr.h"
0016 #include <system_error>
0017 
0018 namespace clang {
0019 namespace serialized_diags {
0020 
0021 enum class SDError {
0022   CouldNotLoad = 1,
0023   InvalidSignature,
0024   InvalidDiagnostics,
0025   MalformedTopLevelBlock,
0026   MalformedSubBlock,
0027   MalformedBlockInfoBlock,
0028   MalformedMetadataBlock,
0029   MalformedDiagnosticBlock,
0030   MalformedDiagnosticRecord,
0031   MissingVersion,
0032   VersionMismatch,
0033   UnsupportedConstruct,
0034   /// A generic error for subclass handlers that don't want or need to define
0035   /// their own error_category.
0036   HandlerFailed
0037 };
0038 
0039 const std::error_category &SDErrorCategory();
0040 
0041 inline std::error_code make_error_code(SDError E) {
0042   return std::error_code(static_cast<int>(E), SDErrorCategory());
0043 }
0044 
0045 /// A location that is represented in the serialized diagnostics.
0046 struct Location {
0047   unsigned FileID;
0048   unsigned Line;
0049   unsigned Col;
0050   unsigned Offset;
0051 
0052   Location(unsigned FileID, unsigned Line, unsigned Col, unsigned Offset)
0053       : FileID(FileID), Line(Line), Col(Col), Offset(Offset) {}
0054 };
0055 
0056 /// A base class that handles reading serialized diagnostics from a file.
0057 ///
0058 /// Subclasses should override the visit* methods with their logic for handling
0059 /// the various constructs that are found in serialized diagnostics.
0060 class SerializedDiagnosticReader {
0061 public:
0062   SerializedDiagnosticReader() = default;
0063   virtual ~SerializedDiagnosticReader() = default;
0064 
0065   /// Read the diagnostics in \c File
0066   std::error_code readDiagnostics(StringRef File);
0067 
0068 private:
0069   enum class Cursor;
0070 
0071   /// Read to the next record or block to process.
0072   llvm::ErrorOr<Cursor> skipUntilRecordOrBlock(llvm::BitstreamCursor &Stream,
0073                                                unsigned &BlockOrRecordId);
0074 
0075   /// Read a metadata block from \c Stream.
0076   std::error_code readMetaBlock(llvm::BitstreamCursor &Stream);
0077 
0078   /// Read a diagnostic block from \c Stream.
0079   std::error_code readDiagnosticBlock(llvm::BitstreamCursor &Stream);
0080 
0081 protected:
0082   /// Visit the start of a diagnostic block.
0083   virtual std::error_code visitStartOfDiagnostic() { return {}; }
0084 
0085   /// Visit the end of a diagnostic block.
0086   virtual std::error_code visitEndOfDiagnostic() { return {}; }
0087 
0088   /// Visit a category. This associates the category \c ID to a \c Name.
0089   virtual std::error_code visitCategoryRecord(unsigned ID, StringRef Name) {
0090     return {};
0091   }
0092 
0093   /// Visit a flag. This associates the flag's \c ID to a \c Name.
0094   virtual std::error_code visitDiagFlagRecord(unsigned ID, StringRef Name) {
0095     return {};
0096   }
0097 
0098   /// Visit a diagnostic.
0099   virtual std::error_code
0100   visitDiagnosticRecord(unsigned Severity, const Location &Location,
0101                         unsigned Category, unsigned Flag, StringRef Message) {
0102     return {};
0103   }
0104 
0105   /// Visit a filename. This associates the file's \c ID to a \c Name.
0106   virtual std::error_code visitFilenameRecord(unsigned ID, unsigned Size,
0107                                               unsigned Timestamp,
0108                                               StringRef Name) {
0109     return {};
0110   }
0111 
0112   /// Visit a fixit hint.
0113   virtual std::error_code
0114   visitFixitRecord(const Location &Start, const Location &End, StringRef Text) {
0115     return {};
0116   }
0117 
0118   /// Visit a source range.
0119   virtual std::error_code visitSourceRangeRecord(const Location &Start,
0120                                                  const Location &End) {
0121     return {};
0122   }
0123 
0124   /// Visit the version of the set of diagnostics.
0125   virtual std::error_code visitVersionRecord(unsigned Version) { return {}; }
0126 };
0127 
0128 } // namespace serialized_diags
0129 } // namespace clang
0130 
0131 template <>
0132 struct std::is_error_code_enum<clang::serialized_diags::SDError>
0133     : std::true_type {};
0134 
0135 #endif // LLVM_CLANG_FRONTEND_SERIALIZEDDIAGNOSTICREADER_H