diagnostic.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_H_
  5. #define CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_H_
  6. #include <cstdint>
  7. #include <functional>
  8. #include <string>
  9. #include "common/check.h"
  10. #include "llvm/ADT/Any.h"
  11. #include "llvm/ADT/SmallVector.h"
  12. #include "llvm/ADT/StringRef.h"
  13. #include "toolchain/diagnostics/diagnostic_kind.h"
  14. namespace Carbon {
  15. enum class DiagnosticLevel : int8_t {
  16. // Information about the location of another diagnostic, showing how we
  17. // reached that location. This is currently only used for the "in import"
  18. // message.
  19. LocationInfo,
  20. // A note, not indicating an error on its own, but possibly providing
  21. // additional information for an error or warning.
  22. Note,
  23. // A warning diagnostic, indicating a likely problem with the program.
  24. Warning,
  25. // An error diagnostic, indicating that the program is not valid.
  26. Error,
  27. };
  28. // Provides a definition of a diagnostic. For example:
  29. // CARBON_DIAGNOSTIC(MyDiagnostic, Error, "invalid code!");
  30. // CARBON_DIAGNOSTIC(MyDiagnostic, Warning, "found {0}, expected {1}",
  31. // std::string, std::string);
  32. //
  33. // Arguments are passed to llvm::formatv; see:
  34. // https://llvm.org/doxygen/FormatVariadic_8h_source.html
  35. //
  36. // See `DiagnosticEmitter::Emit` for comments about argument lifetimes.
  37. #define CARBON_DIAGNOSTIC(DiagnosticName, Level, Format, ...) \
  38. static constexpr auto DiagnosticName = \
  39. ::Carbon::DiagnosticBase<__VA_ARGS__>( \
  40. ::Carbon::DiagnosticKind::DiagnosticName, \
  41. ::Carbon::DiagnosticLevel::Level, Format)
  42. // A location for a diagnostic in a file. The lifetime of a DiagnosticLoc
  43. // is required to be less than SourceBuffer that it refers to due to the
  44. // contained filename and line references.
  45. struct DiagnosticLoc {
  46. // Writes the location to the given stream. It will be formatted as
  47. // `<filename>:<line_number>:<column_number>: ` with parts dropped when
  48. // unknown.
  49. auto FormatLocation(llvm::raw_ostream& out) const -> void;
  50. // Write the source snippet corresponding to this location to the given
  51. // stream.
  52. auto FormatSnippet(llvm::raw_ostream& out, int indent = 0) const -> void;
  53. // Name of the file or buffer that this diagnostic refers to.
  54. llvm::StringRef filename;
  55. // A reference to the line of the error.
  56. llvm::StringRef line;
  57. // 1-based line number. -1 indicates unknown; other values are unused.
  58. int32_t line_number = -1;
  59. // 1-based column number. -1 indicates unknown; other values are unused.
  60. int32_t column_number = -1;
  61. // The number of characters corresponding to the location in the line,
  62. // starting at column_number. Should always be at least 1.
  63. int32_t length = 1;
  64. };
  65. // A message composing a diagnostic. This may be the main message, but can also
  66. // be notes providing more information.
  67. struct DiagnosticMessage {
  68. // Helper for calling `format_fn`.
  69. auto Format() const -> std::string { return format_fn(*this); }
  70. // The diagnostic's kind.
  71. DiagnosticKind kind;
  72. // The diagnostic's level.
  73. DiagnosticLevel level;
  74. // The calculated location of the diagnostic.
  75. DiagnosticLoc loc;
  76. // The diagnostic's format string. This, along with format_args, will be
  77. // passed to format_fn.
  78. llvm::StringLiteral format;
  79. // A list of format arguments.
  80. //
  81. // These may be used by non-standard consumers to inspect diagnostic details
  82. // without needing to parse the formatted string; however, it should be
  83. // understood that diagnostic formats are subject to change and the llvm::Any
  84. // offers limited compile-time type safety. Integration tests are required.
  85. llvm::SmallVector<llvm::Any> format_args;
  86. // Returns the formatted string. By default, this uses llvm::formatv.
  87. std::function<auto(const DiagnosticMessage&)->std::string> format_fn;
  88. };
  89. // An instance of a single error or warning. Information about the diagnostic
  90. // can be recorded into it for more complex consumers.
  91. struct Diagnostic {
  92. // The diagnostic's level.
  93. DiagnosticLevel level;
  94. // The byte offset of the final token which is associated with the diagnostic.
  95. // This is used by `SortingDiagnosticConsumer`. This is separate from the
  96. // `DiagnosticLoc` because it must refer to a position in the primary file
  97. // being processed by a consumer, and has no use cross-file or in notes.
  98. //
  99. // This will usually be the start position (not end) of the last lexed token
  100. // processed before the diagnostic; it could also be `-1` when no source code
  101. // needs to be processed for a diagnostic, or an appropriate byte offset when
  102. // we specifically want a different diagnostic ordering than when a diagnostic
  103. // is issued.
  104. int32_t last_byte_offset = -1;
  105. // Messages related to the diagnostic. Only one should be a warning or error;
  106. // other messages provide context.
  107. llvm::SmallVector<DiagnosticMessage> messages;
  108. };
  109. // Use the DIAGNOSTIC macro to instantiate this.
  110. // This stores static information about a diagnostic category.
  111. template <typename... Args>
  112. struct DiagnosticBase {
  113. explicit constexpr DiagnosticBase(DiagnosticKind kind, DiagnosticLevel level,
  114. llvm::StringLiteral format)
  115. : Kind(kind), Level(level), Format(format) {
  116. static_assert((... && !(std::is_same_v<Args, llvm::StringRef> ||
  117. std::is_same_v<Args, llvm::StringLiteral>)),
  118. "String type disallowed in diagnostics. See "
  119. "https://github.com/carbon-language/carbon-lang/blob/trunk/"
  120. "toolchain/docs/diagnostics.md#diagnostic-parameter-types");
  121. }
  122. // The diagnostic's kind.
  123. DiagnosticKind Kind;
  124. // The diagnostic's level.
  125. DiagnosticLevel Level;
  126. // The diagnostic's format for llvm::formatv.
  127. llvm::StringLiteral Format;
  128. };
  129. } // namespace Carbon
  130. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_H_