emitter.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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_EMITTER_H_
  5. #define CARBON_TOOLCHAIN_DIAGNOSTICS_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/consumer.h"
  15. #include "toolchain/diagnostics/diagnostic.h"
  16. #include "toolchain/diagnostics/kind.h"
  17. namespace Carbon::Diagnostics {
  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 AnnotationScope;
  26. // The result of `DiagnosticConvert::ConvertLoc`. This is non-templated to allow
  27. // sharing across converters.
  28. struct ConvertedLoc {
  29. // Becomes Message::loc.
  30. Loc loc;
  31. // Becomes Diagnostic::last_byte_offset.
  32. int32_t last_byte_offset;
  33. };
  34. // Used by types to indicate a diagnostic type conversion that results in the
  35. // provided StorageType. For example, to convert NameId to a std::string, we
  36. // write:
  37. //
  38. // struct NameId {
  39. // using DiagnosticType = Diagnostics::TypeInfo<std::string>;
  40. // };
  41. template <typename StorageTypeT>
  42. struct TypeInfo {
  43. using StorageType = StorageTypeT;
  44. };
  45. // Manages the creation of reports, the testing if diagnostics are enabled, and
  46. // the collection of reports.
  47. //
  48. // This class is parameterized by a location type, allowing different
  49. // diagnostic clients to provide location information in whatever form is most
  50. // convenient for them, such as a position within a buffer when lexing, a token
  51. // when parsing, or a parse tree node when type-checking, and to allow unit
  52. // tests to be decoupled from any concrete location representation.
  53. template <typename LocT>
  54. class Emitter {
  55. public:
  56. // A builder-pattern type to provide a fluent interface for constructing
  57. // a more complex diagnostic. See `Emitter::Build` for the
  58. // expected usage.
  59. // This is nodiscard to protect against accidentally building a diagnostic
  60. // without emitting it.
  61. class [[nodiscard]] Builder {
  62. public:
  63. // Builder is move-only and cannot be copied.
  64. Builder(Builder&&) noexcept = default;
  65. auto operator=(Builder&&) noexcept -> Builder& = default;
  66. // Overrides the snippet for the most recently added diagnostic or note with
  67. // the given text. The provided override should include the caret text as
  68. // well as the source snippet. An empty snippet restores the default
  69. // behavior of printing the original source line.
  70. auto OverrideSnippet(llvm::StringRef snippet) -> Builder&;
  71. // Adds a Note about the diagnostic, attached to the main diagnostic being
  72. // built. The API mirrors the main emission API: `Emitter::Emit`. For the
  73. // expected usage see the builder API: `Emitter::Build`.
  74. template <typename... Args>
  75. auto Note(LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  76. Internal::NoTypeDeduction<Args>... args) -> Builder&;
  77. // Emits the built diagnostic and its attached notes.
  78. // For the expected usage see the builder API: `Emitter::Build`.
  79. template <typename... Args>
  80. auto Emit() & -> void;
  81. // Prevent trivial uses of the builder; always `static_assert`s.
  82. template <typename... Args>
  83. auto Emit() && -> void;
  84. // Returns true if this Builder may emit a diagnostic. Can be used
  85. // to avoid excess work computing notes, etc, if no diagnostic is going to
  86. // be emitted anyway.
  87. explicit operator bool() { return emitter_; }
  88. private:
  89. friend class Emitter<LocT>;
  90. friend class ContextBuilder;
  91. template <typename... Args>
  92. explicit Builder(Emitter<LocT>* emitter, LocT loc,
  93. const DiagnosticBase<Args...>& diagnostic_base,
  94. llvm::SmallVector<llvm::Any> args);
  95. // Adds a message to the diagnostic, handling conversion of the location and
  96. // arguments.
  97. template <typename... Args>
  98. auto AddMessage(LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  99. llvm::SmallVector<llvm::Any> args) -> void;
  100. // Adds a message to the diagnostic, handling conversion of the arguments. A
  101. // Loc must be provided instead of a LocT in order to
  102. // avoid potential recursion.
  103. template <typename... Args>
  104. auto AddMessageWithLoc(Loc loc,
  105. const DiagnosticBase<Args...>& diagnostic_base,
  106. llvm::SmallVector<llvm::Any> args) -> void;
  107. // Handles the cast of llvm::Any to Args types for formatv.
  108. // TODO: Custom formatting can be provided with an format_provider, but that
  109. // affects all formatv calls. Consider replacing formatv with a custom call
  110. // that allows diagnostic-specific formatting.
  111. template <typename... Args, size_t... N>
  112. static auto FormatFn(const Message& message,
  113. std::index_sequence<N...> /*indices*/) -> std::string;
  114. // Whether a Context or SoftContext message has been added to the Builder.
  115. auto has_context_message() const -> bool { return has_context_message_; }
  116. Emitter<LocT>* emitter_;
  117. Diagnostic diagnostic_;
  118. bool has_context_message_ = false;
  119. };
  120. class ContextBuilder {
  121. public:
  122. // Adds a Context describing a higher level operation that failed due to the
  123. // diagnostic being built. The API mirrors the main emission API:
  124. // `Emitter::Emit`. For the expected usage see the builder API:
  125. // `Emitter::Build`.
  126. template <typename... Args>
  127. auto Context(LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  128. Internal::NoTypeDeduction<Args>... args) -> ContextBuilder&;
  129. private:
  130. friend class Emitter<LocT>;
  131. explicit ContextBuilder(Emitter<LocT>* emitter, Builder* builder)
  132. : emitter_(emitter), builder_(builder) {}
  133. Emitter<LocT>* emitter_;
  134. Builder* builder_;
  135. };
  136. // `consumer` is required to outlive the diagnostic emitter.
  137. explicit Emitter(Consumer* consumer) : consumer_(consumer) {}
  138. virtual ~Emitter() = default;
  139. // Emits an error.
  140. //
  141. // When passing arguments, they may be buffered. As a consequence, lifetimes
  142. // may outlive the `Emit` call.
  143. template <typename... Args>
  144. auto Emit(LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  145. Internal::NoTypeDeduction<Args>... args) -> void;
  146. // A fluent interface for building a diagnostic and attaching notes for added
  147. // context or information. For example:
  148. //
  149. // emitter_.Build(loc1, MyDiagnostic)
  150. // .Note(loc2, MyDiagnosticNote)
  151. // .Emit();
  152. template <typename... Args>
  153. auto Build(LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  154. Internal::NoTypeDeduction<Args>... args) -> Builder;
  155. // Adds a flush function to flush pending diagnostics that might be enqueued
  156. // and not yet emitted. The flush function will be called whenever `Flush` is
  157. // called.
  158. //
  159. // No mechanism is provided to unregister a flush function, so the function
  160. // must ensure that it remains callable until the emitter is destroyed.
  161. //
  162. // This is used to register a handler to flush diagnostics from Clang.
  163. auto AddFlushFn(std::function<auto()->void> flush_fn) -> void {
  164. flush_fns_.push_back(std::move(flush_fn));
  165. }
  166. // Flush all pending diagnostics that are queued externally, such as Clang
  167. // diagnostics. This should not be called when the external source might be in
  168. // the middle of producing a diagnostic, such as between Clang producing an
  169. // error and producing the attached notes.
  170. //
  171. // This is called automatically before any diagnostic annotator is added or
  172. // removed, to flush any pending diagnostics with suitable notes attached, and
  173. // when the emitter is destroyed.
  174. auto Flush() -> void {
  175. for (auto& flush_fn : flush_fns_) {
  176. flush_fn();
  177. }
  178. }
  179. // Verifies that a callback is registered to provide context if a diagnostic
  180. // is emitted. Allows a code path to require context, which then means its
  181. // diagnostics to be framed as Notes.
  182. //
  183. // This is best effort as the registered callback can in practice do nothing,
  184. // but that would be highly unusual.
  185. auto CheckHasContext() -> void { CARBON_CHECK(!context_fns_.empty()); }
  186. protected:
  187. // Callback type used to report context messages from ConvertLoc.
  188. // Note that the first parameter type is Loc rather than
  189. // LocT, because ConvertLoc must not recurse.
  190. using ContextFnT =
  191. llvm::function_ref<auto(Loc, const DiagnosticBase<>&)->void>;
  192. // Converts a LocT to a Loc and its `last_byte_offset` (see
  193. // `Message`). ConvertLoc may invoke context_fn to provide context
  194. // messages.
  195. virtual auto ConvertLoc(LocT loc, ContextFnT context_fn) const
  196. -> ConvertedLoc = 0;
  197. // Converts arg types as needed. Most children don't customize conversion, so
  198. // the default returns the argument unchanged.
  199. virtual auto ConvertArg(llvm::Any arg) const -> llvm::Any { return arg; }
  200. private:
  201. // Converts an argument to llvm::Any for storage, handling input to storage
  202. // type conversion when needed.
  203. template <typename Arg>
  204. auto MakeAny(Arg arg) -> llvm::Any;
  205. template <typename OtherLocT, typename ContextFn>
  206. friend class ContextScope;
  207. template <typename OtherLocT, typename ContextFn>
  208. friend class AnnotationScope;
  209. friend class NoLocEmitter;
  210. Consumer* consumer_;
  211. llvm::SmallVector<std::function<auto()->void>, 1> flush_fns_;
  212. llvm::SmallVector<llvm::function_ref<auto(ContextBuilder& builder)->void>>
  213. context_fns_;
  214. llvm::SmallVector<llvm::function_ref<auto(Builder& builder)->void>>
  215. annotate_fns_;
  216. };
  217. // This relies on `void*` location handling on `Emitter`.
  218. //
  219. // TODO: Based on how this ends up used or if we get more distinct emitters, it
  220. // might be worth considering having diagnostics specify that they don't apply
  221. // to source-location carrying emitters. For example, this might look like a
  222. // `CARBON_NO_LOC_DIAGNOSTIC` macro, or some other factoring. But it might end
  223. // up being more noise than it is worth.
  224. class NoLocEmitter : public Emitter<void*> {
  225. public:
  226. using Emitter::Emitter;
  227. template <typename LocT>
  228. explicit NoLocEmitter(const Emitter<LocT>& emitter)
  229. : Emitter(emitter.consumer_) {}
  230. // Emits an error. This specialization only applies to
  231. // `NoLocEmitter`.
  232. template <typename... Args>
  233. auto Emit(const DiagnosticBase<Args...>& diagnostic_base,
  234. Internal::NoTypeDeduction<Args>... args) -> void {
  235. Emitter::Emit(nullptr, diagnostic_base, args...);
  236. }
  237. protected:
  238. auto ConvertLoc(void* /*loc*/, ContextFnT /*context_fn*/) const
  239. -> ConvertedLoc override {
  240. return {.loc = {.filename = ""}, .last_byte_offset = -1};
  241. }
  242. };
  243. // An RAII object that denotes a scope in which any diagnostic produced should
  244. // become a note attached to the higher-level operation failure described by a
  245. // Context message.
  246. //
  247. // This object is given a function `context` that will be called with a
  248. // `ContextBuilder& builder` for any diagnostic that is emitted through the
  249. // given emitter. That function can provide a context message that explains the
  250. // higher level failure caused by the diagnostic by calling `builder.Context`.
  251. template <typename LocT, typename ContextFn>
  252. class ContextScope {
  253. public:
  254. ContextScope(Emitter<LocT>* emitter, ContextFn context)
  255. requires requires(ContextFn context,
  256. Emitter<LocT>::ContextBuilder& builder) {
  257. { context(builder) } -> std::same_as<void>;
  258. }
  259. : emitter_(emitter), context_(std::move(context)) {
  260. emitter_->Flush();
  261. emitter_->context_fns_.push_back(context_);
  262. }
  263. ~ContextScope() {
  264. emitter_->Flush();
  265. emitter_->context_fns_.pop_back();
  266. }
  267. private:
  268. Emitter<LocT>* emitter_;
  269. // Make a copy of the context function to ensure that it lives long enough.
  270. ContextFn context_;
  271. };
  272. template <typename LocT, typename ContextFn>
  273. ContextScope(Emitter<LocT>* emitter, ContextFn context)
  274. -> ContextScope<LocT, ContextFn>;
  275. // An RAII object that denotes a scope in which any diagnostic produced should
  276. // be annotated in some way.
  277. //
  278. // This object is given a function `annotate` that will be called with a
  279. // `Builder& builder` for any diagnostic that is emitted through the
  280. // given emitter. That function can annotate the diagnostic by calling
  281. // `builder.Note` to add notes.
  282. template <typename LocT, typename AnnotateFn>
  283. class AnnotationScope {
  284. public:
  285. AnnotationScope(Emitter<LocT>* emitter, AnnotateFn annotate)
  286. requires requires(AnnotateFn annotate, Emitter<LocT>::Builder& builder) {
  287. { annotate(builder) } -> std::same_as<void>;
  288. }
  289. : emitter_(emitter), annotate_(std::move(annotate)) {
  290. emitter_->Flush();
  291. emitter_->annotate_fns_.push_back(annotate_);
  292. }
  293. ~AnnotationScope() {
  294. emitter_->Flush();
  295. emitter_->annotate_fns_.pop_back();
  296. }
  297. private:
  298. Emitter<LocT>* emitter_;
  299. // Make a copy of the annotation function to ensure that it lives long enough.
  300. AnnotateFn annotate_;
  301. };
  302. template <typename LocT, typename AnnotateFn>
  303. AnnotationScope(Emitter<LocT>* emitter, AnnotateFn annotate)
  304. -> AnnotationScope<LocT, AnnotateFn>;
  305. // ============================================================================
  306. // Only internal implementation details below this point.
  307. // ============================================================================
  308. namespace Internal {
  309. // Determines whether there's a DiagnosticType member on Arg.
  310. // Used by Emitter.
  311. template <typename Arg>
  312. concept HasDiagnosticType = requires { typename Arg::DiagnosticType; };
  313. // The default implementation with no conversion.
  314. template <typename Arg>
  315. struct DiagnosticTypeForArg : public TypeInfo<Arg> {};
  316. // Exposes a custom conversion for an argument type.
  317. template <typename Arg>
  318. requires HasDiagnosticType<Arg>
  319. struct DiagnosticTypeForArg<Arg> : public Arg::DiagnosticType {};
  320. } // namespace Internal
  321. template <typename LocT>
  322. auto Emitter<LocT>::Builder::OverrideSnippet(llvm::StringRef snippet)
  323. -> Builder& {
  324. diagnostic_.messages.back().loc.snippet = snippet;
  325. return *this;
  326. }
  327. template <typename LocT>
  328. template <typename... Args>
  329. auto Emitter<LocT>::Builder::Note(
  330. LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  331. Internal::NoTypeDeduction<Args>... args) -> Builder& {
  332. CARBON_CHECK(diagnostic_base.Level == Level::Note ||
  333. diagnostic_base.Level == Level::LocationInfo,
  334. "{0}", static_cast<int>(diagnostic_base.Level));
  335. AddMessage(LocT(loc), diagnostic_base, {emitter_->MakeAny<Args>(args)...});
  336. return *this;
  337. }
  338. template <typename LocT>
  339. template <typename... Args>
  340. auto Emitter<LocT>::Builder::Emit() & -> void {
  341. for (auto annotate_fn : llvm::reverse(emitter_->annotate_fns_)) {
  342. annotate_fn(*this);
  343. }
  344. emitter_->consumer_->HandleDiagnostic(std::move(diagnostic_));
  345. }
  346. template <typename LocT>
  347. template <typename... Args>
  348. auto Emitter<LocT>::Builder::Emit() && -> void {
  349. static_assert(false,
  350. "Use `emitter.Emit(...)` or "
  351. "`emitter.Build(...).Note(...).Emit(...)` "
  352. "instead of `emitter.Build(...).Emit(...)`");
  353. }
  354. template <typename LocT>
  355. template <typename... Args>
  356. Emitter<LocT>::Builder::Builder(Emitter<LocT>* emitter, LocT loc,
  357. const DiagnosticBase<Args...>& diagnostic_base,
  358. llvm::SmallVector<llvm::Any> args)
  359. : emitter_(emitter),
  360. diagnostic_({.level = diagnostic_base.Level,
  361. .is_on_scope = diagnostic_base.IsOnScope}) {
  362. CARBON_CHECK(diagnostic_.level >= Level::Warning,
  363. "building diagnostic with level {0}; expected Warning or Error",
  364. diagnostic_.level);
  365. ContextBuilder context_builder(emitter, this);
  366. for (auto context_fn : emitter_->context_fns_) {
  367. context_fn(context_builder);
  368. }
  369. AddMessage(LocT(loc), diagnostic_base, std::move(args));
  370. CARBON_CHECK(diagnostic_base.Level != Level::Note);
  371. }
  372. template <typename LocT>
  373. template <typename... Args>
  374. auto Emitter<LocT>::Builder::AddMessage(
  375. LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  376. llvm::SmallVector<llvm::Any> args) -> void {
  377. auto converted = emitter_->ConvertLoc(
  378. loc,
  379. [&](Loc context_loc, const DiagnosticBase<>& context_diagnostic_base) {
  380. AddMessageWithLoc(context_loc, context_diagnostic_base, {});
  381. });
  382. // Use the last byte offset from the first message.
  383. if (diagnostic_.messages.empty()) {
  384. diagnostic_.last_byte_offset = converted.last_byte_offset;
  385. }
  386. AddMessageWithLoc(converted.loc, diagnostic_base, args);
  387. }
  388. template <typename LocT>
  389. template <typename... Args>
  390. auto Emitter<LocT>::Builder::AddMessageWithLoc(
  391. Loc loc, const DiagnosticBase<Args...>& diagnostic_base,
  392. llvm::SmallVector<llvm::Any> args) -> void {
  393. CARBON_CHECK(
  394. diagnostic_base.Level <= diagnostic_.level,
  395. "message with level {0} is higher than the diagnostic's level {1}",
  396. diagnostic_base.Level, diagnostic_.level);
  397. if (diagnostic_base.Level == Level::SoftContext ||
  398. diagnostic_base.Level == Level::Context) {
  399. has_context_message_ = true;
  400. }
  401. diagnostic_.messages.push_back(
  402. Message{.kind = diagnostic_base.Kind,
  403. .level = diagnostic_base.Level,
  404. .loc = loc,
  405. .format = diagnostic_base.Format,
  406. .format_args = std::move(args),
  407. .format_fn = [](const Message& message) -> std::string {
  408. return FormatFn<Args...>(
  409. message, std::make_index_sequence<sizeof...(Args)>());
  410. }});
  411. }
  412. template <typename LocT>
  413. template <typename... Args, size_t... N>
  414. auto Emitter<LocT>::Builder::FormatFn(const Message& message,
  415. std::index_sequence<N...> /*indices*/)
  416. -> std::string {
  417. static_assert(sizeof...(Args) == sizeof...(N), "Invalid template args");
  418. CARBON_CHECK(message.format_args.size() == sizeof...(Args),
  419. "Argument count mismatch on {0}: {1} != {2}", message.kind,
  420. message.format_args.size(), sizeof...(Args));
  421. return llvm::formatv(
  422. message.format.data(),
  423. llvm::any_cast<
  424. typename Internal::DiagnosticTypeForArg<Args>::StorageType>(
  425. message.format_args[N])...);
  426. }
  427. template <typename LocT>
  428. template <typename... Args>
  429. auto Emitter<LocT>::Emit(LocT loc,
  430. const DiagnosticBase<Args...>& diagnostic_base,
  431. Internal::NoTypeDeduction<Args>... args) -> void {
  432. Builder builder(this, loc, diagnostic_base, {MakeAny<Args>(args)...});
  433. builder.Emit();
  434. }
  435. template <typename LocT>
  436. template <typename... Args>
  437. auto Emitter<LocT>::ContextBuilder::Context(
  438. LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  439. Internal::NoTypeDeduction<Args>... args) -> ContextBuilder& {
  440. CARBON_CHECK(diagnostic_base.Level == Level::SoftContext ||
  441. diagnostic_base.Level == Level::Context,
  442. "{0}", static_cast<int>(diagnostic_base.Level));
  443. if (builder_->has_context_message() &&
  444. diagnostic_base.Level == Level::SoftContext) {
  445. return *this;
  446. }
  447. builder_->AddMessage(LocT(loc), diagnostic_base,
  448. {emitter_->template MakeAny<Args>(args)...});
  449. return *this;
  450. }
  451. template <typename LocT>
  452. template <typename... Args>
  453. auto Emitter<LocT>::Build(LocT loc,
  454. const DiagnosticBase<Args...>& diagnostic_base,
  455. Internal::NoTypeDeduction<Args>... args) -> Builder {
  456. return Builder(this, loc, diagnostic_base, {MakeAny<Args>(args)...});
  457. }
  458. template <typename LocT>
  459. template <typename Arg>
  460. auto Emitter<LocT>::MakeAny(Arg arg) -> llvm::Any {
  461. llvm::Any converted = ConvertArg(arg);
  462. using Storage = Internal::DiagnosticTypeForArg<Arg>::StorageType;
  463. CARBON_CHECK(llvm::any_cast<Storage>(&converted),
  464. "Failed to convert argument of type {0} to its storage type {1}",
  465. typeid(Arg).name(), typeid(Storage).name());
  466. return converted;
  467. }
  468. } // namespace Carbon::Diagnostics
  469. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_EMITTER_H_