diagnostic_helpers.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_CHECK_DIAGNOSTIC_HELPERS_H_
  5. #define CARBON_TOOLCHAIN_CHECK_DIAGNOSTIC_HELPERS_H_
  6. #include <concepts>
  7. #include "llvm/ADT/APSInt.h"
  8. #include "toolchain/parse/node_ids.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. namespace Carbon::Check {
  11. // The `DiagnosticEmitterBase` is templated on this type so that
  12. // diagnostics can be passed an `InstId` as a location, without having to
  13. // explicitly construct a `LocId` from it first.
  14. class LocIdForDiagnostics {
  15. public:
  16. // Constructs a token-only location for a diagnostic.
  17. //
  18. // This means the displayed location will include only the location's specific
  19. // parse node, instead of also including its descendants.
  20. static auto TokenOnly(Parse::NodeId node_id) -> LocIdForDiagnostics {
  21. return LocIdForDiagnostics(SemIR::LocId(node_id), true);
  22. }
  23. template <class LocT>
  24. requires std::constructible_from<SemIR::LocId, LocT>
  25. // NOLINTNEXTLINE(google-explicit-constructor)
  26. LocIdForDiagnostics(LocT loc_id)
  27. : LocIdForDiagnostics(SemIR::LocId(loc_id), false) {}
  28. auto loc_id() const -> SemIR::LocId { return loc_id_; }
  29. auto is_token_only() const -> bool { return is_token_only_; }
  30. private:
  31. explicit LocIdForDiagnostics(SemIR::LocId loc_id, bool is_token_only)
  32. : loc_id_(loc_id), is_token_only_(is_token_only) {}
  33. SemIR::LocId loc_id_;
  34. bool is_token_only_;
  35. };
  36. // We define the emitter separately for dependencies, so only provide a base
  37. // here.
  38. using DiagnosticEmitterBase = Diagnostics::Emitter<LocIdForDiagnostics>;
  39. using DiagnosticBuilder = DiagnosticEmitterBase::Builder;
  40. // A function that forms a diagnostic for some kind of problem. The
  41. // DiagnosticBuilder is returned rather than emitted so that the caller
  42. // can add contextual notes as appropriate.
  43. using MakeDiagnosticBuilderFn = llvm::function_ref<auto()->DiagnosticBuilder>;
  44. // An expression with a constant value, for rendering in a diagnostic. The
  45. // diagnostic rendering will include enclosing "`"s.
  46. struct InstIdAsConstant {
  47. using DiagnosticType = Diagnostics::TypeInfo<std::string>;
  48. // NOLINTNEXTLINE(google-explicit-constructor)
  49. InstIdAsConstant(SemIR::InstId inst_id) : inst_id(inst_id) {}
  50. SemIR::InstId inst_id;
  51. };
  52. // An expression whose type should be rendered in a diagnostic. The diagnostic
  53. // rendering will include enclosing "`"s, and may also include extra information
  54. // about the type if it might otherwise be ambiguous or context-dependent, such
  55. // as the targets of aliases used in the type.
  56. //
  57. // TODO: Include such additional information where relevant. For example:
  58. // "`StdString` (aka `Cpp.std.basic_string(Char)`)".
  59. //
  60. // This should be used instead of `TypeId` as a diagnostic argument wherever
  61. // possible, because we should eventually be able to produce a sugared type name
  62. // in this case, whereas a `TypeId` will render as a canonical type.
  63. struct TypeOfInstId {
  64. using DiagnosticType = Diagnostics::TypeInfo<std::string>;
  65. // NOLINTNEXTLINE(google-explicit-constructor)
  66. TypeOfInstId(SemIR::InstId inst_id) : inst_id(inst_id) {}
  67. SemIR::InstId inst_id;
  68. };
  69. // A type expression, for rendering in a diagnostic. The diagnostic rendering
  70. // will include enclosing "`"s, and may also include extra information about the
  71. // type if it would otherwise be ambiguous.
  72. //
  73. // TODO: Include such additional information where relevant.
  74. //
  75. // This should be used when the source expression used to construct a type is
  76. // available.
  77. //
  78. // Note that this is currently an alias for InstIdAsConstant. However, using
  79. // InstIdAsType is clearer when defining CARBON_DIAGNOSTICs, and we may wish to
  80. // distinguish type arguments in diagnostics from more general constants in some
  81. // way in the future.
  82. using InstIdAsType = InstIdAsConstant;
  83. // A type expression, for rendering in a diagnostic as a raw type. When
  84. // formatting as a raw type in a diagnostic, the type will be formatted as a
  85. // simple Carbon expression, without enclosing "`"s. Once we start including
  86. // extra information about types, such annotations will also not be included for
  87. // raw types.
  88. //
  89. // This is intended for cases where the type is part of a larger syntactic
  90. // construct in a diagnostic, such as "redefinition of `impl {0} as {1}`".
  91. struct InstIdAsRawType {
  92. using DiagnosticType = Diagnostics::TypeInfo<std::string>;
  93. // NOLINTNEXTLINE(google-explicit-constructor)
  94. InstIdAsRawType(SemIR::InstId inst_id) : inst_id(inst_id) {}
  95. SemIR::InstId inst_id;
  96. };
  97. // A type value for rendering in a diagnostic without enclosing "`"s. See
  98. // `InstIdAsRawType` for details on raw type formatting.
  99. //
  100. // As with `TypeId`, this should be avoided as a diagnostic argument where
  101. // possible, because it can't be formatted with syntactic sugar such as aliases
  102. // that describe how the type was written.
  103. struct TypeIdAsRawType {
  104. using DiagnosticType = Diagnostics::TypeInfo<std::string>;
  105. // NOLINTNEXTLINE(google-explicit-constructor)
  106. TypeIdAsRawType(SemIR::TypeId type_id) : type_id(type_id) {}
  107. SemIR::TypeId type_id;
  108. };
  109. // An integer value together with its type. The type is used to determine how to
  110. // format the value in diagnostics.
  111. struct TypedInt {
  112. using DiagnosticType = Diagnostics::TypeInfo<llvm::APSInt>;
  113. SemIR::TypeId type;
  114. llvm::APInt value;
  115. };
  116. struct SpecificInterfaceIdAsRawType {
  117. using DiagnosticType = Diagnostics::TypeInfo<std::string>;
  118. // NOLINTNEXTLINE(google-explicit-constructor)
  119. SpecificInterfaceIdAsRawType(SemIR::SpecificInterfaceId specific_interface_id)
  120. : specific_interface_id(specific_interface_id) {}
  121. SemIR::SpecificInterfaceId specific_interface_id;
  122. };
  123. } // namespace Carbon::Check
  124. #endif // CARBON_TOOLCHAIN_CHECK_DIAGNOSTIC_HELPERS_H_