diagnostic.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/kind.h"
  14. namespace Carbon::Diagnostics {
  15. enum class Level : 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 `Diagnostics::Emitter::Emit` for comments about argument lifetimes.
  37. #define CARBON_DIAGNOSTIC(DiagnosticName, LevelValue, Format, ...) \
  38. static constexpr auto DiagnosticName = \
  39. ::Carbon::Diagnostics::DiagnosticBase<__VA_ARGS__>( \
  40. ::Carbon::Diagnostics::Kind::DiagnosticName, \
  41. ::Carbon::Diagnostics::Level::LevelValue, /*is_on_scope=*/false, \
  42. Format)
  43. // Similar to `CARBON_DIAGNOSTIC`, but for diagnostics that are generated on a
  44. // scope; see `Diagnostic::is_on_scope` for details.
  45. #define CARBON_DIAGNOSTIC_ON_SCOPE(DiagnosticName, LevelValue, Format, ...) \
  46. static constexpr auto DiagnosticName = \
  47. ::Carbon::Diagnostics::DiagnosticBase<__VA_ARGS__>( \
  48. ::Carbon::Diagnostics::Kind::DiagnosticName, \
  49. ::Carbon::Diagnostics::Level::LevelValue, /*is_on_scope=*/true, \
  50. Format)
  51. // A location for a diagnostic in a file. The lifetime of a Loc
  52. // is required to be less than SourceBuffer that it refers to due to the
  53. // contained filename and line references.
  54. struct Loc {
  55. // Writes the location to the given stream. It will be formatted as
  56. // `<filename>:<line_number>:<column_number>: ` with parts dropped when
  57. // unknown.
  58. auto FormatLocation(llvm::raw_ostream& out) const -> void;
  59. // Write the source snippet corresponding to this location to the given
  60. // stream.
  61. auto FormatSnippet(llvm::raw_ostream& out, int indent = 0) const -> void;
  62. // Name of the file or buffer that this diagnostic refers to.
  63. llvm::StringRef filename;
  64. // A reference to the line of the error.
  65. llvm::StringRef line;
  66. // A full snippet to print. If non-empty, this is used instead of `line` when
  67. // printing a snippet. Should contain both the quoted text and the caret line.
  68. std::string snippet;
  69. // 1-based line number. -1 indicates unknown; other values are unused.
  70. int32_t line_number = -1;
  71. // 1-based column number. -1 indicates unknown; other values are unused.
  72. int32_t column_number = -1;
  73. // The number of characters corresponding to the location in the line,
  74. // starting at column_number. Should always be at least 1.
  75. int32_t length = 1;
  76. };
  77. // A message composing a diagnostic. This may be the main message, but can also
  78. // be notes providing more information.
  79. struct Message {
  80. // Helper for calling `format_fn`.
  81. auto Format() const -> std::string { return format_fn(*this); }
  82. // The diagnostic's kind.
  83. Kind kind;
  84. // The diagnostic's level.
  85. Level level;
  86. // The calculated location of the diagnostic.
  87. Loc loc;
  88. // The diagnostic's format string. This, along with format_args, will be
  89. // passed to format_fn.
  90. llvm::StringLiteral format;
  91. // A list of format arguments.
  92. //
  93. // These may be used by non-standard consumers to inspect diagnostic details
  94. // without needing to parse the formatted string; however, it should be
  95. // understood that diagnostic formats are subject to change and the llvm::Any
  96. // offers limited compile-time type safety. Integration tests are required.
  97. llvm::SmallVector<llvm::Any> format_args;
  98. // Returns the formatted string. By default, this uses llvm::formatv.
  99. std::function<auto(const Message&)->std::string> format_fn;
  100. };
  101. // An instance of a single error or warning. Information about the diagnostic
  102. // can be recorded into it for more complex consumers.
  103. struct Diagnostic {
  104. // The diagnostic's level.
  105. Level level;
  106. // Whether a diagnostic should only sort by `last_byte_offset` (which is
  107. // normal), or if it's generated on a scope and should be sorted based on the
  108. // first message's line and column when the `last_byte_offset` is equal.
  109. // This is used by `SortingConsumer`.
  110. bool is_on_scope;
  111. // The byte offset of the final token which is associated with the diagnostic.
  112. // This is used by `SortingConsumer`. This is separate from the
  113. // `Loc` because it must refer to a position in the primary file
  114. // being processed by a consumer, and has no use cross-file or in notes.
  115. //
  116. // This will usually be the start position (not end) of the last lexed token
  117. // processed before the diagnostic; it could also be `-1` when no source code
  118. // needs to be processed for a diagnostic, or an appropriate byte offset when
  119. // we specifically want a different diagnostic ordering than when a diagnostic
  120. // is issued.
  121. int32_t last_byte_offset = -1;
  122. // Messages related to the diagnostic. Only one should be a warning or error;
  123. // other messages provide context.
  124. llvm::SmallVector<Message> messages;
  125. };
  126. // Use the DIAGNOSTIC macro to instantiate this.
  127. // This stores static information about a diagnostic category.
  128. template <typename... Args>
  129. struct DiagnosticBase {
  130. explicit constexpr DiagnosticBase(Kind kind, Level level, bool is_on_scope,
  131. llvm::StringLiteral format)
  132. : Kind(kind), Level(level), IsOnScope(is_on_scope), Format(format) {
  133. static_assert((... && !(std::is_same_v<Args, llvm::StringRef> ||
  134. std::is_same_v<Args, llvm::StringLiteral>)),
  135. "String type disallowed in diagnostics. See "
  136. "https://github.com/carbon-language/carbon-lang/blob/trunk/"
  137. "toolchain/docs/diagnostics.md#diagnostic-parameter-types");
  138. }
  139. // The diagnostic's kind.
  140. Kind Kind;
  141. // The diagnostic's level.
  142. Level Level;
  143. // See `Diagnostic::is_on_scope`.
  144. bool IsOnScope;
  145. // The diagnostic's format for llvm::formatv.
  146. llvm::StringLiteral Format;
  147. };
  148. } // namespace Carbon::Diagnostics
  149. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_H_