clang API Documentation

SerializedDiagnosticReader.h
Go to the documentation of this file.
00001 //===--- SerializedDiagnosticReader.h - Reads diagnostics -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_CLANG_FRONTEND_SERIALIZED_DIAGNOSTIC_READER_H_
00011 #define LLVM_CLANG_FRONTEND_SERIALIZED_DIAGNOSTIC_READER_H_
00012 
00013 #include "clang/Basic/LLVM.h"
00014 #include "llvm/ADT/ArrayRef.h"
00015 #include "llvm/Bitcode/BitstreamReader.h"
00016 #include "llvm/Support/ErrorOr.h"
00017 
00018 namespace clang {
00019 namespace serialized_diags {
00020 
00021 enum class SDError {
00022   CouldNotLoad = 1,
00023   InvalidSignature,
00024   InvalidDiagnostics,
00025   MalformedTopLevelBlock,
00026   MalformedSubBlock,
00027   MalformedBlockInfoBlock,
00028   MalformedMetadataBlock,
00029   MalformedDiagnosticBlock,
00030   MalformedDiagnosticRecord,
00031   MissingVersion,
00032   VersionMismatch,
00033   UnsupportedConstruct,
00034   /// A generic error for subclass handlers that don't want or need to define
00035   /// their own error_category.
00036   HandlerFailed
00037 };
00038 
00039 const std::error_category &SDErrorCategory();
00040 
00041 inline std::error_code make_error_code(SDError E) {
00042   return std::error_code(static_cast<int>(E), SDErrorCategory());
00043 }
00044 
00045 /// \brief A location that is represented in the serialized diagnostics.
00046 struct Location {
00047   unsigned FileID;
00048   unsigned Line;
00049   unsigned Col;
00050   unsigned Offset;
00051   Location(unsigned FileID, unsigned Line, unsigned Col, unsigned Offset)
00052       : FileID(FileID), Line(Line), Col(Col), Offset(Offset) {}
00053 };
00054 
00055 /// \brief A base class that handles reading serialized diagnostics from a file.
00056 ///
00057 /// Subclasses should override the visit* methods with their logic for handling
00058 /// the various constructs that are found in serialized diagnostics.
00059 class SerializedDiagnosticReader {
00060 public:
00061   SerializedDiagnosticReader() {}
00062   virtual ~SerializedDiagnosticReader() {}
00063 
00064   /// \brief Read the diagnostics in \c File
00065   std::error_code readDiagnostics(StringRef File);
00066 
00067 private:
00068   enum class Cursor;
00069 
00070   /// \brief Read to the next record or block to process.
00071   llvm::ErrorOr<Cursor> skipUntilRecordOrBlock(llvm::BitstreamCursor &Stream,
00072                                                unsigned &BlockOrRecordId);
00073 
00074   /// \brief Read a metadata block from \c Stream.
00075   std::error_code readMetaBlock(llvm::BitstreamCursor &Stream);
00076 
00077   /// \brief Read a diagnostic block from \c Stream.
00078   std::error_code readDiagnosticBlock(llvm::BitstreamCursor &Stream);
00079 
00080 protected:
00081   /// \brief Visit the start of a diagnostic block.
00082   virtual std::error_code visitStartOfDiagnostic() {
00083     return std::error_code();
00084   };
00085   /// \brief Visit the end of a diagnostic block.
00086   virtual std::error_code visitEndOfDiagnostic() { return std::error_code(); };
00087   /// \brief Visit a category. This associates the category \c ID to a \c Name.
00088   virtual std::error_code visitCategoryRecord(unsigned ID, StringRef Name) {
00089     return std::error_code();
00090   };
00091   /// \brief Visit a flag. This associates the flag's \c ID to a \c Name.
00092   virtual std::error_code visitDiagFlagRecord(unsigned ID, StringRef Name) {
00093     return std::error_code();
00094   };
00095   /// \brief Visit a diagnostic.
00096   virtual std::error_code
00097   visitDiagnosticRecord(unsigned Severity, const Location &Location,
00098                         unsigned Category, unsigned Flag, StringRef Message) {
00099     return std::error_code();
00100   };
00101   /// \brief Visit a filename. This associates the file's \c ID to a \c Name.
00102   virtual std::error_code visitFilenameRecord(unsigned ID, unsigned Size,
00103                                               unsigned Timestamp,
00104                                               StringRef Name) {
00105     return std::error_code();
00106   };
00107   /// \brief Visit a fixit hint.
00108   virtual std::error_code
00109   visitFixitRecord(const Location &Start, const Location &End, StringRef Text) {
00110     return std::error_code();
00111   };
00112   /// \brief Visit a source range.
00113   virtual std::error_code visitSourceRangeRecord(const Location &Start,
00114                                                  const Location &End) {
00115     return std::error_code();
00116   };
00117   /// \brief Visit the version of the set of diagnostics.
00118   virtual std::error_code visitVersionRecord(unsigned Version) {
00119     return std::error_code();
00120   };
00121 };
00122 
00123 } // end serialized_diags namespace
00124 } // end clang namespace
00125 
00126 namespace std {
00127 template <>
00128 struct is_error_code_enum<clang::serialized_diags::SDError> : std::true_type {};
00129 }
00130 
00131 #endif