diagnostic.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // Write the filename, line number, and column number corresponding to this
  47. // location to the given stream.
  48. auto FormatLocation(llvm::raw_ostream& out) const -> void;
  49. // Write the source snippet corresponding to this location to the given
  50. // stream.
  51. auto FormatSnippet(llvm::raw_ostream& out, int indent = 0) const -> void;
  52. // Name of the file or buffer that this diagnostic refers to.
  53. llvm::StringRef filename;
  54. // A reference to the line of the error.
  55. llvm::StringRef line;
  56. // 1-based line number. -1 indicates unknown; other values are unused.
  57. int32_t line_number = -1;
  58. // 1-based column number. -1 indicates unknown; other values are unused.
  59. int32_t column_number = -1;
  60. // The number of characters corresponding to the location in the line,
  61. // starting at column_number. Should always be at least 1.
  62. int32_t length = 1;
  63. };
  64. // A message composing a diagnostic. This may be the main message, but can also
  65. // be notes providing more information.
  66. struct DiagnosticMessage {
  67. // The diagnostic's kind.
  68. DiagnosticKind kind;
  69. // The diagnostic's level.
  70. DiagnosticLevel level;
  71. // The calculated location of the diagnostic.
  72. DiagnosticLoc loc;
  73. // The diagnostic's format string. This, along with format_args, will be
  74. // passed to format_fn.
  75. llvm::StringLiteral format;
  76. // A list of format arguments.
  77. //
  78. // These may be used by non-standard consumers to inspect diagnostic details
  79. // without needing to parse the formatted string; however, it should be
  80. // understood that diagnostic formats are subject to change and the llvm::Any
  81. // offers limited compile-time type safety. Integration tests are required.
  82. llvm::SmallVector<llvm::Any> format_args;
  83. // Returns the formatted string. By default, this uses llvm::formatv.
  84. std::function<std::string(const DiagnosticMessage&)> format_fn;
  85. };
  86. // An instance of a single error or warning. Information about the diagnostic
  87. // can be recorded into it for more complex consumers.
  88. struct Diagnostic {
  89. // The diagnostic's level.
  90. DiagnosticLevel level;
  91. // Messages related to the diagnostic. Only one should be a warning or error;
  92. // other messages provide context.
  93. llvm::SmallVector<DiagnosticMessage> messages;
  94. };
  95. // Use the DIAGNOSTIC macro to instantiate this.
  96. // This stores static information about a diagnostic category.
  97. template <typename... Args>
  98. struct DiagnosticBase {
  99. explicit constexpr DiagnosticBase(DiagnosticKind kind, DiagnosticLevel level,
  100. llvm::StringLiteral format)
  101. : Kind(kind), Level(level), Format(format) {
  102. static_assert((... && !(std::is_same_v<Args, llvm::StringRef> ||
  103. std::is_same_v<Args, llvm::StringLiteral>)),
  104. "String type disallowed in diagnostics. See "
  105. "https://github.com/carbon-language/carbon-lang/blob/trunk/"
  106. "toolchain/docs/diagnostics.md#diagnostic-parameter-types");
  107. }
  108. // The diagnostic's kind.
  109. DiagnosticKind Kind;
  110. // The diagnostic's level.
  111. DiagnosticLevel Level;
  112. // The diagnostic's format for llvm::formatv.
  113. llvm::StringLiteral Format;
  114. };
  115. } // namespace Carbon
  116. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_H_