SerializedDiagnosticReader.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SerializedDiagnosticReader.h - Reads diagnostics ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_FRONTEND_SERIALIZEDDIAGNOSTICREADER_H
  14. #define LLVM_CLANG_FRONTEND_SERIALIZEDDIAGNOSTICREADER_H
  15. #include "clang/Basic/LLVM.h"
  16. #include "llvm/Bitstream/BitstreamReader.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/Support/ErrorOr.h"
  19. #include <system_error>
  20. namespace clang {
  21. namespace serialized_diags {
  22. enum class SDError {
  23. CouldNotLoad = 1,
  24. InvalidSignature,
  25. InvalidDiagnostics,
  26. MalformedTopLevelBlock,
  27. MalformedSubBlock,
  28. MalformedBlockInfoBlock,
  29. MalformedMetadataBlock,
  30. MalformedDiagnosticBlock,
  31. MalformedDiagnosticRecord,
  32. MissingVersion,
  33. VersionMismatch,
  34. UnsupportedConstruct,
  35. /// A generic error for subclass handlers that don't want or need to define
  36. /// their own error_category.
  37. HandlerFailed
  38. };
  39. const std::error_category &SDErrorCategory();
  40. inline std::error_code make_error_code(SDError E) {
  41. return std::error_code(static_cast<int>(E), SDErrorCategory());
  42. }
  43. /// A location that is represented in the serialized diagnostics.
  44. struct Location {
  45. unsigned FileID;
  46. unsigned Line;
  47. unsigned Col;
  48. unsigned Offset;
  49. Location(unsigned FileID, unsigned Line, unsigned Col, unsigned Offset)
  50. : FileID(FileID), Line(Line), Col(Col), Offset(Offset) {}
  51. };
  52. /// A base class that handles reading serialized diagnostics from a file.
  53. ///
  54. /// Subclasses should override the visit* methods with their logic for handling
  55. /// the various constructs that are found in serialized diagnostics.
  56. class SerializedDiagnosticReader {
  57. public:
  58. SerializedDiagnosticReader() = default;
  59. virtual ~SerializedDiagnosticReader() = default;
  60. /// Read the diagnostics in \c File
  61. std::error_code readDiagnostics(StringRef File);
  62. private:
  63. enum class Cursor;
  64. /// Read to the next record or block to process.
  65. llvm::ErrorOr<Cursor> skipUntilRecordOrBlock(llvm::BitstreamCursor &Stream,
  66. unsigned &BlockOrRecordId);
  67. /// Read a metadata block from \c Stream.
  68. std::error_code readMetaBlock(llvm::BitstreamCursor &Stream);
  69. /// Read a diagnostic block from \c Stream.
  70. std::error_code readDiagnosticBlock(llvm::BitstreamCursor &Stream);
  71. protected:
  72. /// Visit the start of a diagnostic block.
  73. virtual std::error_code visitStartOfDiagnostic() { return {}; }
  74. /// Visit the end of a diagnostic block.
  75. virtual std::error_code visitEndOfDiagnostic() { return {}; }
  76. /// Visit a category. This associates the category \c ID to a \c Name.
  77. virtual std::error_code visitCategoryRecord(unsigned ID, StringRef Name) {
  78. return {};
  79. }
  80. /// Visit a flag. This associates the flag's \c ID to a \c Name.
  81. virtual std::error_code visitDiagFlagRecord(unsigned ID, StringRef Name) {
  82. return {};
  83. }
  84. /// Visit a diagnostic.
  85. virtual std::error_code
  86. visitDiagnosticRecord(unsigned Severity, const Location &Location,
  87. unsigned Category, unsigned Flag, StringRef Message) {
  88. return {};
  89. }
  90. /// Visit a filename. This associates the file's \c ID to a \c Name.
  91. virtual std::error_code visitFilenameRecord(unsigned ID, unsigned Size,
  92. unsigned Timestamp,
  93. StringRef Name) {
  94. return {};
  95. }
  96. /// Visit a fixit hint.
  97. virtual std::error_code
  98. visitFixitRecord(const Location &Start, const Location &End, StringRef Text) {
  99. return {};
  100. }
  101. /// Visit a source range.
  102. virtual std::error_code visitSourceRangeRecord(const Location &Start,
  103. const Location &End) {
  104. return {};
  105. }
  106. /// Visit the version of the set of diagnostics.
  107. virtual std::error_code visitVersionRecord(unsigned Version) { return {}; }
  108. };
  109. } // namespace serialized_diags
  110. } // namespace clang
  111. namespace std {
  112. template <>
  113. struct is_error_code_enum<clang::serialized_diags::SDError> : std::true_type {};
  114. } // namespace std
  115. #endif // LLVM_CLANG_FRONTEND_SERIALIZEDDIAGNOSTICREADER_H
  116. #ifdef __GNUC__
  117. #pragma GCC diagnostic pop
  118. #endif