diagnostic_emitter.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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_EMITTER_H_
  5. #define CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_EMITTER_H_
  6. #include <cstdint>
  7. #include <string>
  8. #include <type_traits>
  9. #include <utility>
  10. #include "common/check.h"
  11. #include "llvm/ADT/Any.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/Support/FormatVariadic.h"
  14. #include "toolchain/diagnostics/diagnostic.h"
  15. #include "toolchain/diagnostics/diagnostic_consumer.h"
  16. #include "toolchain/diagnostics/diagnostic_kind.h"
  17. namespace Carbon {
  18. namespace Internal {
  19. // Disable type deduction based on `args`; the type of `diagnostic_base`
  20. // determines the diagnostic's parameter types.
  21. template <typename Arg>
  22. using NoTypeDeduction = std::type_identity_t<Arg>;
  23. } // namespace Internal
  24. template <typename LocT, typename AnnotateFn>
  25. class DiagnosticAnnotationScope;
  26. // The result of `DiagnosticConvert::ConvertLoc`. This is non-templated to allow
  27. // sharing across converters.
  28. struct ConvertedDiagnosticLoc {
  29. // Becomes DiagnosticMessage::loc.
  30. DiagnosticLoc loc;
  31. // Becomes Diagnostic::last_byte_offset.
  32. int32_t last_byte_offset;
  33. };
  34. // An interface that can convert some representation of a location into a
  35. // diagnostic location.
  36. template <typename LocT>
  37. class DiagnosticConverter {
  38. public:
  39. // Callback type used to report context messages from ConvertLoc.
  40. // Note that the first parameter type is DiagnosticLoc rather than
  41. // LocT, because ConvertLoc must not recurse.
  42. using ContextFnT =
  43. llvm::function_ref<void(DiagnosticLoc, const DiagnosticBase<>&)>;
  44. virtual ~DiagnosticConverter() = default;
  45. // Converts a LocT to a DiagnosticLoc and its `last_byte_offset` (see
  46. // `DiagnosticMessage`). ConvertLoc may invoke context_fn to provide context
  47. // messages.
  48. virtual auto ConvertLoc(LocT loc, ContextFnT context_fn) const
  49. -> ConvertedDiagnosticLoc = 0;
  50. // Converts arg types as needed. Not all uses require conversion, so the
  51. // default returns the argument unchanged.
  52. virtual auto ConvertArg(llvm::Any arg) const -> llvm::Any { return arg; }
  53. };
  54. // Used by types to indicate a diagnostic type conversion that results in the
  55. // provided StorageType. For example, to convert NameId to a std::string, we
  56. // write:
  57. //
  58. // struct NameId {
  59. // using DiagnosticType = DiagnosticTypeInfo<std::string>;
  60. // };
  61. template <typename StorageTypeT>
  62. struct DiagnosticTypeInfo {
  63. using StorageType = StorageTypeT;
  64. };
  65. // Manages the creation of reports, the testing if diagnostics are enabled, and
  66. // the collection of reports.
  67. //
  68. // This class is parameterized by a location type, allowing different
  69. // diagnostic clients to provide location information in whatever form is most
  70. // convenient for them, such as a position within a buffer when lexing, a token
  71. // when parsing, or a parse tree node when type-checking, and to allow unit
  72. // tests to be decoupled from any concrete location representation.
  73. template <typename LocT>
  74. class DiagnosticEmitter {
  75. public:
  76. // A builder-pattern type to provide a fluent interface for constructing
  77. // a more complex diagnostic. See `DiagnosticEmitter::Build` for the
  78. // expected usage.
  79. // This is nodiscard to protect against accidentally building a diagnostic
  80. // without emitting it.
  81. class [[nodiscard]] DiagnosticBuilder {
  82. public:
  83. // DiagnosticBuilder is move-only and cannot be copied.
  84. DiagnosticBuilder(DiagnosticBuilder&&) noexcept = default;
  85. auto operator=(DiagnosticBuilder&&) noexcept
  86. -> DiagnosticBuilder& = default;
  87. // Adds a note diagnostic attached to the main diagnostic being built.
  88. // The API mirrors the main emission API: `DiagnosticEmitter::Emit`.
  89. // For the expected usage see the builder API: `DiagnosticEmitter::Build`.
  90. template <typename... Args>
  91. auto Note(LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  92. Internal::NoTypeDeduction<Args>... args) -> DiagnosticBuilder&;
  93. // Emits the built diagnostic and its attached notes.
  94. // For the expected usage see the builder API: `DiagnosticEmitter::Build`.
  95. template <typename... Args>
  96. auto Emit() -> void;
  97. // Returns true if this DiagnosticBuilder may emit a diagnostic. Can be used
  98. // to avoid excess work computing notes, etc, if no diagnostic is going to
  99. // be emitted anyway.
  100. explicit operator bool() { return emitter_; }
  101. private:
  102. friend class DiagnosticEmitter<LocT>;
  103. template <typename... Args>
  104. explicit DiagnosticBuilder(DiagnosticEmitter<LocT>* emitter, LocT loc,
  105. const DiagnosticBase<Args...>& diagnostic_base,
  106. llvm::SmallVector<llvm::Any> args);
  107. // Create a null `DiagnosticBuilder` that will not emit anything. Notes will
  108. // be silently ignored.
  109. DiagnosticBuilder() : emitter_(nullptr) {}
  110. // Adds a message to the diagnostic, handling conversion of the location and
  111. // arguments.
  112. template <typename... Args>
  113. auto AddMessage(LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  114. llvm::SmallVector<llvm::Any> args) -> void;
  115. // Adds a message to the diagnostic, handling conversion of the arguments. A
  116. // DiagnosticLoc must be provided instead of a LocT in order to
  117. // avoid potential recursion.
  118. template <typename... Args>
  119. auto AddMessageWithDiagnosticLoc(
  120. DiagnosticLoc loc, const DiagnosticBase<Args...>& diagnostic_base,
  121. llvm::SmallVector<llvm::Any> args) -> void;
  122. // Handles the cast of llvm::Any to Args types for formatv.
  123. // TODO: Custom formatting can be provided with an format_provider, but that
  124. // affects all formatv calls. Consider replacing formatv with a custom call
  125. // that allows diagnostic-specific formatting.
  126. template <typename... Args, size_t... N>
  127. static auto FormatFn(const DiagnosticMessage& message,
  128. std::index_sequence<N...> /*indices*/) -> std::string;
  129. DiagnosticEmitter<LocT>* emitter_;
  130. Diagnostic diagnostic_;
  131. };
  132. // The `converter` and `consumer` are required to outlive the diagnostic
  133. // emitter.
  134. // TODO: Adjust construction to take stored arguments as pointers, and
  135. // consider taking `converter` by value (move/copy) instead of reference.
  136. explicit DiagnosticEmitter(DiagnosticConverter<LocT>& converter,
  137. DiagnosticConsumer& consumer)
  138. : converter_(&converter), consumer_(&consumer) {}
  139. ~DiagnosticEmitter() = default;
  140. // Emits an error.
  141. //
  142. // When passing arguments, they may be buffered. As a consequence, lifetimes
  143. // may outlive the `Emit` call.
  144. template <typename... Args>
  145. auto Emit(LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  146. Internal::NoTypeDeduction<Args>... args) -> void;
  147. // A fluent interface for building a diagnostic and attaching notes for added
  148. // context or information. For example:
  149. //
  150. // emitter_.Build(loc1, MyDiagnostic)
  151. // .Note(loc2, MyDiagnosticNote)
  152. // .Emit();
  153. template <typename... Args>
  154. auto Build(LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  155. Internal::NoTypeDeduction<Args>... args) -> DiagnosticBuilder;
  156. // Create a null `DiagnosticBuilder` that will not emit anything. Notes will
  157. // be silently ignored.
  158. auto BuildSuppressed() -> DiagnosticBuilder { return DiagnosticBuilder(); }
  159. private:
  160. // Converts an argument to llvm::Any for storage, handling input to storage
  161. // type conversion when needed.
  162. template <typename Arg>
  163. auto MakeAny(Arg arg) -> llvm::Any;
  164. template <typename OtherLocT, typename AnnotateFn>
  165. friend class DiagnosticAnnotationScope;
  166. DiagnosticConverter<LocT>* converter_;
  167. DiagnosticConsumer* consumer_;
  168. llvm::SmallVector<llvm::function_ref<auto(DiagnosticBuilder& builder)->void>>
  169. annotate_fns_;
  170. };
  171. // This relies on `void*` location handling on `DiagnosticEmitter`.
  172. //
  173. // TODO: Based on how this ends up used or if we get more distinct emitters, it
  174. // might be worth considering having diagnostics specify that they don't apply
  175. // to source-location carrying emitters. For example, this might look like a
  176. // `CARBON_NO_LOC_DIAGNOSTIC` macro, or some other factoring. But it might end
  177. // up being more noise than it is worth.
  178. class NoLocDiagnosticEmitter : public DiagnosticEmitter<void*> {
  179. public:
  180. // This constructor only applies to NoLocDiagnosticEmitter.
  181. explicit NoLocDiagnosticEmitter(DiagnosticConsumer* consumer)
  182. : DiagnosticEmitter(converter_, *consumer) {}
  183. // Emits an error. This specialization only applies to
  184. // `NoLocDiagnosticEmitter`.
  185. template <typename... Args>
  186. auto Emit(const DiagnosticBase<Args...>& diagnostic_base,
  187. Internal::NoTypeDeduction<Args>... args) -> void {
  188. DiagnosticEmitter::Emit(nullptr, diagnostic_base, args...);
  189. }
  190. private:
  191. struct Converter : DiagnosticConverter<void*> {
  192. auto ConvertLoc(void* /*loc*/, ContextFnT /*context_fn*/) const
  193. -> ConvertedDiagnosticLoc override {
  194. return {.loc = {.filename = ""}, .last_byte_offset = -1};
  195. }
  196. };
  197. Converter converter_;
  198. };
  199. // An RAII object that denotes a scope in which any diagnostic produced should
  200. // be annotated in some way.
  201. //
  202. // This object is given a function `annotate` that will be called with a
  203. // `DiagnosticBuilder& builder` for any diagnostic that is emitted through the
  204. // given emitter. That function can annotate the diagnostic by calling
  205. // `builder.Note` to add notes.
  206. template <typename LocT, typename AnnotateFn>
  207. class DiagnosticAnnotationScope {
  208. public:
  209. DiagnosticAnnotationScope(DiagnosticEmitter<LocT>* emitter,
  210. AnnotateFn annotate)
  211. : emitter_(emitter), annotate_(std::move(annotate)) {
  212. emitter_->annotate_fns_.push_back(annotate_);
  213. }
  214. ~DiagnosticAnnotationScope() { emitter_->annotate_fns_.pop_back(); }
  215. private:
  216. DiagnosticEmitter<LocT>* emitter_;
  217. // Make a copy of the annotation function to ensure that it lives long enough.
  218. AnnotateFn annotate_;
  219. };
  220. template <typename LocT, typename AnnotateFn>
  221. DiagnosticAnnotationScope(DiagnosticEmitter<LocT>* emitter, AnnotateFn annotate)
  222. -> DiagnosticAnnotationScope<LocT, AnnotateFn>;
  223. // ============================================================================
  224. // Only internal implementation details below this point.
  225. // ============================================================================
  226. namespace Internal {
  227. // Determines whether there's a DiagnosticType member on Arg.
  228. // Used by DiagnosticEmitter.
  229. template <typename Arg>
  230. concept HasDiagnosticType = requires { typename Arg::DiagnosticType; };
  231. // The default implementation with no conversion.
  232. template <typename Arg>
  233. struct DiagnosticTypeForArg : public DiagnosticTypeInfo<Arg> {};
  234. // Exposes a custom conversion for an argument type.
  235. template <typename Arg>
  236. requires HasDiagnosticType<Arg>
  237. struct DiagnosticTypeForArg<Arg> : public Arg::DiagnosticType {};
  238. } // namespace Internal
  239. template <typename LocT>
  240. template <typename... Args>
  241. auto DiagnosticEmitter<LocT>::DiagnosticBuilder::Note(
  242. LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  243. Internal::NoTypeDeduction<Args>... args) -> DiagnosticBuilder& {
  244. if (!emitter_) {
  245. return *this;
  246. }
  247. CARBON_CHECK(diagnostic_base.Level == DiagnosticLevel::Note ||
  248. diagnostic_base.Level == DiagnosticLevel::LocationInfo,
  249. "{0}", static_cast<int>(diagnostic_base.Level));
  250. AddMessage(loc, diagnostic_base, {emitter_->MakeAny<Args>(args)...});
  251. return *this;
  252. }
  253. template <typename LocT>
  254. template <typename... Args>
  255. auto DiagnosticEmitter<LocT>::DiagnosticBuilder::Emit() -> void {
  256. if (!emitter_) {
  257. return;
  258. }
  259. for (auto annotate_fn : llvm::reverse(emitter_->annotate_fns_)) {
  260. annotate_fn(*this);
  261. }
  262. emitter_->consumer_->HandleDiagnostic(std::move(diagnostic_));
  263. }
  264. template <typename LocT>
  265. template <typename... Args>
  266. DiagnosticEmitter<LocT>::DiagnosticBuilder::DiagnosticBuilder(
  267. DiagnosticEmitter<LocT>* emitter, LocT loc,
  268. const DiagnosticBase<Args...>& diagnostic_base,
  269. llvm::SmallVector<llvm::Any> args)
  270. : emitter_(emitter), diagnostic_({.level = diagnostic_base.Level}) {
  271. AddMessage(loc, diagnostic_base, std::move(args));
  272. CARBON_CHECK(diagnostic_base.Level != DiagnosticLevel::Note);
  273. }
  274. template <typename LocT>
  275. template <typename... Args>
  276. auto DiagnosticEmitter<LocT>::DiagnosticBuilder::AddMessage(
  277. LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  278. llvm::SmallVector<llvm::Any> args) -> void {
  279. if (!emitter_) {
  280. return;
  281. }
  282. auto converted = emitter_->converter_->ConvertLoc(
  283. loc, [&](DiagnosticLoc context_loc,
  284. const DiagnosticBase<>& context_diagnostic_base) {
  285. AddMessageWithDiagnosticLoc(context_loc, context_diagnostic_base, {});
  286. });
  287. // Use the last byte offset from the first message.
  288. if (diagnostic_.messages.empty()) {
  289. diagnostic_.last_byte_offset = converted.last_byte_offset;
  290. }
  291. AddMessageWithDiagnosticLoc(converted.loc, diagnostic_base, args);
  292. }
  293. template <typename LocT>
  294. template <typename... Args>
  295. auto DiagnosticEmitter<LocT>::DiagnosticBuilder::AddMessageWithDiagnosticLoc(
  296. DiagnosticLoc loc, const DiagnosticBase<Args...>& diagnostic_base,
  297. llvm::SmallVector<llvm::Any> args) -> void {
  298. if (!emitter_) {
  299. return;
  300. }
  301. diagnostic_.messages.emplace_back(DiagnosticMessage{
  302. .kind = diagnostic_base.Kind,
  303. .level = diagnostic_base.Level,
  304. .loc = loc,
  305. .format = diagnostic_base.Format,
  306. .format_args = std::move(args),
  307. .format_fn = [](const DiagnosticMessage& message) -> std::string {
  308. return FormatFn<Args...>(message,
  309. std::make_index_sequence<sizeof...(Args)>());
  310. }});
  311. }
  312. template <typename LocT>
  313. template <typename... Args, size_t... N>
  314. auto DiagnosticEmitter<LocT>::DiagnosticBuilder::FormatFn(
  315. const DiagnosticMessage& message, std::index_sequence<N...> /*indices*/)
  316. -> std::string {
  317. static_assert(sizeof...(Args) == sizeof...(N), "Invalid template args");
  318. CARBON_CHECK(message.format_args.size() == sizeof...(Args),
  319. "Argument count mismatch on {0}: {1} != {2}", message.kind,
  320. message.format_args.size(), sizeof...(Args));
  321. return llvm::formatv(
  322. message.format.data(),
  323. llvm::any_cast<
  324. typename Internal::DiagnosticTypeForArg<Args>::StorageType>(
  325. message.format_args[N])...);
  326. }
  327. template <typename LocT>
  328. template <typename... Args>
  329. auto DiagnosticEmitter<LocT>::Emit(
  330. LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  331. Internal::NoTypeDeduction<Args>... args) -> void {
  332. DiagnosticBuilder(this, loc, diagnostic_base, {MakeAny<Args>(args)...})
  333. .Emit();
  334. }
  335. template <typename LocT>
  336. template <typename... Args>
  337. auto DiagnosticEmitter<LocT>::Build(
  338. LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  339. Internal::NoTypeDeduction<Args>... args) -> DiagnosticBuilder {
  340. return DiagnosticBuilder(this, loc, diagnostic_base,
  341. {MakeAny<Args>(args)...});
  342. }
  343. template <typename LocT>
  344. template <typename Arg>
  345. auto DiagnosticEmitter<LocT>::MakeAny(Arg arg) -> llvm::Any {
  346. llvm::Any converted = converter_->ConvertArg(arg);
  347. using Storage = Internal::DiagnosticTypeForArg<Arg>::StorageType;
  348. CARBON_CHECK(llvm::any_cast<Storage>(&converted),
  349. "Failed to convert argument of type {0} to its storage type {1}",
  350. typeid(Arg).name(), typeid(Storage).name());
  351. return converted;
  352. }
  353. } // namespace Carbon
  354. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_EMITTER_H_