diagnostic.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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, Format)
  42. // A location for a diagnostic in a file. The lifetime of a Loc
  43. // is required to be less than SourceBuffer that it refers to due to the
  44. // contained filename and line references.
  45. struct Loc {
  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. // A full snippet to print. If non-empty, this is used instead of `line` when
  58. // printing a snippet. Should contain both the quoted text and the caret line.
  59. std::string snippet;
  60. // 1-based line number. -1 indicates unknown; other values are unused.
  61. int32_t line_number = -1;
  62. // 1-based column number. -1 indicates unknown; other values are unused.
  63. int32_t column_number = -1;
  64. // The number of characters corresponding to the location in the line,
  65. // starting at column_number. Should always be at least 1.
  66. int32_t length = 1;
  67. };
  68. // A message composing a diagnostic. This may be the main message, but can also
  69. // be notes providing more information.
  70. struct Message {
  71. // Helper for calling `format_fn`.
  72. auto Format() const -> std::string { return format_fn(*this); }
  73. // The diagnostic's kind.
  74. Kind kind;
  75. // The diagnostic's level.
  76. Level level;
  77. // The calculated location of the diagnostic.
  78. Loc loc;
  79. // The diagnostic's format string. This, along with format_args, will be
  80. // passed to format_fn.
  81. llvm::StringLiteral format;
  82. // A list of format arguments.
  83. //
  84. // These may be used by non-standard consumers to inspect diagnostic details
  85. // without needing to parse the formatted string; however, it should be
  86. // understood that diagnostic formats are subject to change and the llvm::Any
  87. // offers limited compile-time type safety. Integration tests are required.
  88. llvm::SmallVector<llvm::Any> format_args;
  89. // Returns the formatted string. By default, this uses llvm::formatv.
  90. std::function<auto(const Message&)->std::string> format_fn;
  91. };
  92. // An instance of a single error or warning. Information about the diagnostic
  93. // can be recorded into it for more complex consumers.
  94. struct Diagnostic {
  95. // The diagnostic's level.
  96. Level level;
  97. // The byte offset of the final token which is associated with the diagnostic.
  98. // This is used by `SortingConsumer`. This is separate from the
  99. // `Loc` because it must refer to a position in the primary file
  100. // being processed by a consumer, and has no use cross-file or in notes.
  101. //
  102. // This will usually be the start position (not end) of the last lexed token
  103. // processed before the diagnostic; it could also be `-1` when no source code
  104. // needs to be processed for a diagnostic, or an appropriate byte offset when
  105. // we specifically want a different diagnostic ordering than when a diagnostic
  106. // is issued.
  107. int32_t last_byte_offset = -1;
  108. // Messages related to the diagnostic. Only one should be a warning or error;
  109. // other messages provide context.
  110. llvm::SmallVector<Message> messages;
  111. };
  112. // Use the DIAGNOSTIC macro to instantiate this.
  113. // This stores static information about a diagnostic category.
  114. template <typename... Args>
  115. struct DiagnosticBase {
  116. explicit constexpr DiagnosticBase(Kind kind, Level level,
  117. llvm::StringLiteral format)
  118. : Kind(kind), Level(level), Format(format) {
  119. static_assert((... && !(std::is_same_v<Args, llvm::StringRef> ||
  120. std::is_same_v<Args, llvm::StringLiteral>)),
  121. "String type disallowed in diagnostics. See "
  122. "https://github.com/carbon-language/carbon-lang/blob/trunk/"
  123. "toolchain/docs/diagnostics.md#diagnostic-parameter-types");
  124. }
  125. // The diagnostic's kind.
  126. Kind Kind;
  127. // The diagnostic's level.
  128. Level Level;
  129. // The diagnostic's format for llvm::formatv.
  130. llvm::StringLiteral Format;
  131. };
  132. } // namespace Carbon::Diagnostics
  133. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_H_