diagnostic.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // A note, not indicating an error on its own, but possibly providing context
  17. // for an error.
  18. Note,
  19. // A warning diagnostic, indicating a likely problem with the program.
  20. Warning,
  21. // An error diagnostic, indicating that the program is not valid.
  22. Error,
  23. };
  24. // Provides a definition of a diagnostic. For example:
  25. // CARBON_DIAGNOSTIC(MyDiagnostic, Error, "Invalid code!");
  26. // CARBON_DIAGNOSTIC(MyDiagnostic, Warning, "Found {0}, expected {1}.",
  27. // std::string, std::string);
  28. //
  29. // Arguments are passed to llvm::formatv; see:
  30. // https://llvm.org/doxygen/FormatVariadic_8h_source.html
  31. //
  32. // See `DiagnosticEmitter::Emit` for comments about argument lifetimes.
  33. #define CARBON_DIAGNOSTIC(DiagnosticName, Level, Format, ...) \
  34. static constexpr auto DiagnosticName = \
  35. ::Carbon::Internal::DiagnosticBase<__VA_ARGS__>( \
  36. ::Carbon::DiagnosticKind::DiagnosticName, \
  37. ::Carbon::DiagnosticLevel::Level, Format)
  38. // A location for a diagnostic in a file. The lifetime of a DiagnosticLoc
  39. // is required to be less than SourceBuffer that it refers to due to the
  40. // contained filename and line references.
  41. struct DiagnosticLoc {
  42. // Write the filename, line number, and column number corresponding to this
  43. // location to the given stream.
  44. auto FormatLocation(llvm::raw_ostream& out) const -> void;
  45. // Write the source snippet corresponding to this location to the given
  46. // stream.
  47. auto FormatSnippet(llvm::raw_ostream& out) const -> void;
  48. // Name of the file or buffer that this diagnostic refers to.
  49. llvm::StringRef filename;
  50. // A reference to the line of the error.
  51. llvm::StringRef line;
  52. // 1-based line number.
  53. int32_t line_number = -1;
  54. // 1-based column number.
  55. int32_t column_number = -1;
  56. // A location can represent a range of text if set to >1 value.
  57. int32_t length = 1;
  58. };
  59. // A message composing a diagnostic. This may be the main message, but can also
  60. // be notes providing more information.
  61. struct DiagnosticMessage {
  62. // The diagnostic's kind.
  63. DiagnosticKind kind;
  64. // The diagnostic's level.
  65. DiagnosticLevel level;
  66. // The calculated location of the diagnostic.
  67. DiagnosticLoc loc;
  68. // The diagnostic's format string. This, along with format_args, will be
  69. // passed to format_fn.
  70. llvm::StringLiteral format;
  71. // A list of format arguments.
  72. //
  73. // These may be used by non-standard consumers to inspect diagnostic details
  74. // without needing to parse the formatted string; however, it should be
  75. // understood that diagnostic formats are subject to change and the llvm::Any
  76. // offers limited compile-time type safety. Integration tests are required.
  77. llvm::SmallVector<llvm::Any> format_args;
  78. // Returns the formatted string. By default, this uses llvm::formatv.
  79. std::function<std::string(const DiagnosticMessage&)> format_fn;
  80. };
  81. // An instance of a single error or warning. Information about the diagnostic
  82. // can be recorded into it for more complex consumers.
  83. struct Diagnostic {
  84. // The diagnostic's level.
  85. DiagnosticLevel level;
  86. // Messages related to the diagnostic. Only one should be a warning or error;
  87. // other messages provide context.
  88. llvm::SmallVector<DiagnosticMessage> messages;
  89. };
  90. namespace Internal {
  91. // Use the DIAGNOSTIC macro to instantiate this.
  92. // This stores static information about a diagnostic category.
  93. template <typename... Args>
  94. struct DiagnosticBase {
  95. explicit constexpr DiagnosticBase(DiagnosticKind kind, DiagnosticLevel level,
  96. llvm::StringLiteral format)
  97. : Kind(kind), Level(level), Format(format) {
  98. static_assert((... && !std::is_same_v<Args, llvm::StringRef>),
  99. "Use std::string or llvm::StringLiteral for diagnostics to "
  100. "avoid lifetime issues.");
  101. }
  102. // The diagnostic's kind.
  103. DiagnosticKind Kind;
  104. // The diagnostic's level.
  105. DiagnosticLevel Level;
  106. // The diagnostic's format for llvm::formatv.
  107. llvm::StringLiteral Format;
  108. };
  109. } // namespace Internal
  110. } // namespace Carbon
  111. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_H_