diagnostic.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // The diagnostic's kind.
  69. DiagnosticKind kind;
  70. // The diagnostic's level.
  71. DiagnosticLevel level;
  72. // The calculated location of the diagnostic.
  73. DiagnosticLoc loc;
  74. // The diagnostic's format string. This, along with format_args, will be
  75. // passed to format_fn.
  76. llvm::StringLiteral format;
  77. // A list of format arguments.
  78. //
  79. // These may be used by non-standard consumers to inspect diagnostic details
  80. // without needing to parse the formatted string; however, it should be
  81. // understood that diagnostic formats are subject to change and the llvm::Any
  82. // offers limited compile-time type safety. Integration tests are required.
  83. llvm::SmallVector<llvm::Any> format_args;
  84. // Returns the formatted string. By default, this uses llvm::formatv.
  85. std::function<std::string(const DiagnosticMessage&)> format_fn;
  86. };
  87. // An instance of a single error or warning. Information about the diagnostic
  88. // can be recorded into it for more complex consumers.
  89. struct Diagnostic {
  90. // The diagnostic's level.
  91. DiagnosticLevel level;
  92. // The byte offset of the final token which is associated with the diagnostic.
  93. // This is used by `SortingDiagnosticConsumer`. This is separate from the
  94. // `DiagnosticLoc` because it must refer to a position in the primary file
  95. // being processed by a consumer, and has no use cross-file or in notes.
  96. //
  97. // This will usually be the start position (not end) of the last lexed token
  98. // processed before the diagnostic; it could also be `-1` when no source code
  99. // needs to be processed for a diagnostic, or an appropriate byte offset when
  100. // we specifically want a different diagnostic ordering than when a diagnostic
  101. // is issued.
  102. int32_t last_byte_offset = -1;
  103. // Messages related to the diagnostic. Only one should be a warning or error;
  104. // other messages provide context.
  105. llvm::SmallVector<DiagnosticMessage> messages;
  106. };
  107. // Use the DIAGNOSTIC macro to instantiate this.
  108. // This stores static information about a diagnostic category.
  109. template <typename... Args>
  110. struct DiagnosticBase {
  111. explicit constexpr DiagnosticBase(DiagnosticKind kind, DiagnosticLevel level,
  112. llvm::StringLiteral format)
  113. : Kind(kind), Level(level), Format(format) {
  114. static_assert((... && !(std::is_same_v<Args, llvm::StringRef> ||
  115. std::is_same_v<Args, llvm::StringLiteral>)),
  116. "String type disallowed in diagnostics. See "
  117. "https://github.com/carbon-language/carbon-lang/blob/trunk/"
  118. "toolchain/docs/diagnostics.md#diagnostic-parameter-types");
  119. }
  120. // The diagnostic's kind.
  121. DiagnosticKind Kind;
  122. // The diagnostic's level.
  123. DiagnosticLevel Level;
  124. // The diagnostic's format for llvm::formatv.
  125. llvm::StringLiteral Format;
  126. };
  127. } // namespace Carbon
  128. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_H_