diagnostic_emitter.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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::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. // Adds a note diagnostic attached to the main diagnostic being built.
  67. // The API mirrors the main emission API: `Emitter::Emit`.
  68. // For the expected usage see the builder API: `Emitter::Build`.
  69. template <typename... Args>
  70. auto Note(LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  71. Internal::NoTypeDeduction<Args>... args) -> Builder&;
  72. // Emits the built diagnostic and its attached notes.
  73. // For the expected usage see the builder API: `Emitter::Build`.
  74. template <typename... Args>
  75. auto Emit() & -> void;
  76. // Prevent trivial uses of the builder; always `static_assert`s.
  77. template <typename... Args>
  78. auto Emit() && -> void;
  79. // Returns true if this Builder may emit a diagnostic. Can be used
  80. // to avoid excess work computing notes, etc, if no diagnostic is going to
  81. // be emitted anyway.
  82. explicit operator bool() { return emitter_; }
  83. private:
  84. friend class Emitter<LocT>;
  85. template <typename... Args>
  86. explicit Builder(Emitter<LocT>* emitter, LocT loc,
  87. const DiagnosticBase<Args...>& diagnostic_base,
  88. llvm::SmallVector<llvm::Any> args);
  89. // Create a null `Builder` that will not emit anything. Notes will
  90. // be silently ignored.
  91. Builder() : emitter_(nullptr) {}
  92. // Adds a message to the diagnostic, handling conversion of the location and
  93. // arguments.
  94. template <typename... Args>
  95. auto AddMessage(LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  96. llvm::SmallVector<llvm::Any> args) -> void;
  97. // Adds a message to the diagnostic, handling conversion of the arguments. A
  98. // Loc must be provided instead of a LocT in order to
  99. // avoid potential recursion.
  100. template <typename... Args>
  101. auto AddMessageWithLoc(Loc loc,
  102. const DiagnosticBase<Args...>& diagnostic_base,
  103. llvm::SmallVector<llvm::Any> args) -> void;
  104. // Handles the cast of llvm::Any to Args types for formatv.
  105. // TODO: Custom formatting can be provided with an format_provider, but that
  106. // affects all formatv calls. Consider replacing formatv with a custom call
  107. // that allows diagnostic-specific formatting.
  108. template <typename... Args, size_t... N>
  109. static auto FormatFn(const Message& message,
  110. std::index_sequence<N...> /*indices*/) -> std::string;
  111. Emitter<LocT>* emitter_;
  112. Diagnostic diagnostic_;
  113. };
  114. // `consumer` is required to outlive the diagnostic emitter.
  115. explicit Emitter(Consumer* consumer) : consumer_(consumer) {}
  116. virtual ~Emitter() = default;
  117. // Emits an error.
  118. //
  119. // When passing arguments, they may be buffered. As a consequence, lifetimes
  120. // may outlive the `Emit` call.
  121. template <typename... Args>
  122. auto Emit(LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  123. Internal::NoTypeDeduction<Args>... args) -> void;
  124. // A fluent interface for building a diagnostic and attaching notes for added
  125. // context or information. For example:
  126. //
  127. // emitter_.Build(loc1, MyDiagnostic)
  128. // .Note(loc2, MyDiagnosticNote)
  129. // .Emit();
  130. template <typename... Args>
  131. auto Build(LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  132. Internal::NoTypeDeduction<Args>... args) -> Builder;
  133. // Create a null `Builder` that will not emit anything. Notes will
  134. // be silently ignored.
  135. auto BuildSuppressed() -> Builder { return Builder(); }
  136. protected:
  137. // Callback type used to report context messages from ConvertLoc.
  138. // Note that the first parameter type is Loc rather than
  139. // LocT, because ConvertLoc must not recurse.
  140. using ContextFnT =
  141. llvm::function_ref<auto(Loc, const DiagnosticBase<>&)->void>;
  142. // Converts a LocT to a Loc and its `last_byte_offset` (see
  143. // `Message`). ConvertLoc may invoke context_fn to provide context
  144. // messages.
  145. virtual auto ConvertLoc(LocT loc, ContextFnT context_fn) const
  146. -> ConvertedLoc = 0;
  147. // Converts arg types as needed. Most children don't customize conversion, so
  148. // the default returns the argument unchanged.
  149. virtual auto ConvertArg(llvm::Any arg) const -> llvm::Any { return arg; }
  150. private:
  151. // Converts an argument to llvm::Any for storage, handling input to storage
  152. // type conversion when needed.
  153. template <typename Arg>
  154. auto MakeAny(Arg arg) -> llvm::Any;
  155. template <typename OtherLocT, typename AnnotateFn>
  156. friend class AnnotationScope;
  157. friend class NoLocEmitter;
  158. Consumer* consumer_;
  159. llvm::SmallVector<llvm::function_ref<auto(Builder& builder)->void>>
  160. annotate_fns_;
  161. };
  162. // This relies on `void*` location handling on `Emitter`.
  163. //
  164. // TODO: Based on how this ends up used or if we get more distinct emitters, it
  165. // might be worth considering having diagnostics specify that they don't apply
  166. // to source-location carrying emitters. For example, this might look like a
  167. // `CARBON_NO_LOC_DIAGNOSTIC` macro, or some other factoring. But it might end
  168. // up being more noise than it is worth.
  169. class NoLocEmitter : public Emitter<void*> {
  170. public:
  171. using Emitter::Emitter;
  172. template <typename LocT>
  173. explicit NoLocEmitter(const Emitter<LocT>& emitter)
  174. : Emitter(emitter.consumer_) {}
  175. // Emits an error. This specialization only applies to
  176. // `NoLocEmitter`.
  177. template <typename... Args>
  178. auto Emit(const DiagnosticBase<Args...>& diagnostic_base,
  179. Internal::NoTypeDeduction<Args>... args) -> void {
  180. Emitter::Emit(nullptr, diagnostic_base, args...);
  181. }
  182. protected:
  183. auto ConvertLoc(void* /*loc*/, ContextFnT /*context_fn*/) const
  184. -> ConvertedLoc override {
  185. return {.loc = {.filename = ""}, .last_byte_offset = -1};
  186. }
  187. };
  188. // An RAII object that denotes a scope in which any diagnostic produced should
  189. // be annotated in some way.
  190. //
  191. // This object is given a function `annotate` that will be called with a
  192. // `Builder& builder` for any diagnostic that is emitted through the
  193. // given emitter. That function can annotate the diagnostic by calling
  194. // `builder.Note` to add notes.
  195. template <typename LocT, typename AnnotateFn>
  196. class AnnotationScope {
  197. public:
  198. AnnotationScope(Emitter<LocT>* emitter, AnnotateFn annotate)
  199. : emitter_(emitter), annotate_(std::move(annotate)) {
  200. emitter_->annotate_fns_.push_back(annotate_);
  201. }
  202. ~AnnotationScope() { emitter_->annotate_fns_.pop_back(); }
  203. private:
  204. Emitter<LocT>* emitter_;
  205. // Make a copy of the annotation function to ensure that it lives long enough.
  206. AnnotateFn annotate_;
  207. };
  208. template <typename LocT, typename AnnotateFn>
  209. AnnotationScope(Emitter<LocT>* emitter, AnnotateFn annotate)
  210. -> AnnotationScope<LocT, AnnotateFn>;
  211. // ============================================================================
  212. // Only internal implementation details below this point.
  213. // ============================================================================
  214. namespace Internal {
  215. // Determines whether there's a DiagnosticType member on Arg.
  216. // Used by Emitter.
  217. template <typename Arg>
  218. concept HasDiagnosticType = requires { typename Arg::DiagnosticType; };
  219. // The default implementation with no conversion.
  220. template <typename Arg>
  221. struct DiagnosticTypeForArg : public TypeInfo<Arg> {};
  222. // Exposes a custom conversion for an argument type.
  223. template <typename Arg>
  224. requires HasDiagnosticType<Arg>
  225. struct DiagnosticTypeForArg<Arg> : public Arg::DiagnosticType {};
  226. } // namespace Internal
  227. template <typename LocT>
  228. template <typename... Args>
  229. auto Emitter<LocT>::Builder::Note(
  230. LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  231. Internal::NoTypeDeduction<Args>... args) -> Builder& {
  232. if (!emitter_) {
  233. return *this;
  234. }
  235. CARBON_CHECK(diagnostic_base.Level == Level::Note ||
  236. diagnostic_base.Level == Level::LocationInfo,
  237. "{0}", static_cast<int>(diagnostic_base.Level));
  238. AddMessage(LocT(loc), diagnostic_base, {emitter_->MakeAny<Args>(args)...});
  239. return *this;
  240. }
  241. template <typename LocT>
  242. template <typename... Args>
  243. auto Emitter<LocT>::Builder::Emit() & -> void {
  244. if (!emitter_) {
  245. return;
  246. }
  247. for (auto annotate_fn : llvm::reverse(emitter_->annotate_fns_)) {
  248. annotate_fn(*this);
  249. }
  250. emitter_->consumer_->HandleDiagnostic(std::move(diagnostic_));
  251. }
  252. namespace Internal {
  253. template <typename LocT>
  254. concept AlwaysFalse = false;
  255. } // namespace Internal
  256. template <typename LocT>
  257. template <typename... Args>
  258. auto Emitter<LocT>::Builder::Emit() && -> void {
  259. // TODO: This is required by clang-16, but `false` may work in newer clang
  260. // versions. Replace when possible.
  261. static_assert(Internal::AlwaysFalse<LocT>,
  262. "Use `emitter.Emit(...)` or "
  263. "`emitter.Build(...).Note(...).Emit(...)` "
  264. "instead of `emitter.Build(...).Emit(...)`");
  265. }
  266. template <typename LocT>
  267. template <typename... Args>
  268. Emitter<LocT>::Builder::Builder(Emitter<LocT>* emitter, LocT loc,
  269. const DiagnosticBase<Args...>& diagnostic_base,
  270. llvm::SmallVector<llvm::Any> args)
  271. : emitter_(emitter), diagnostic_({.level = diagnostic_base.Level}) {
  272. AddMessage(LocT(loc), diagnostic_base, std::move(args));
  273. CARBON_CHECK(diagnostic_base.Level != Level::Note);
  274. }
  275. template <typename LocT>
  276. template <typename... Args>
  277. auto Emitter<LocT>::Builder::AddMessage(
  278. LocT loc, const DiagnosticBase<Args...>& diagnostic_base,
  279. llvm::SmallVector<llvm::Any> args) -> void {
  280. if (!emitter_) {
  281. return;
  282. }
  283. auto converted = emitter_->ConvertLoc(
  284. loc,
  285. [&](Loc context_loc, const DiagnosticBase<>& context_diagnostic_base) {
  286. AddMessageWithLoc(context_loc, context_diagnostic_base, {});
  287. });
  288. // Use the last byte offset from the first message.
  289. if (diagnostic_.messages.empty()) {
  290. diagnostic_.last_byte_offset = converted.last_byte_offset;
  291. }
  292. AddMessageWithLoc(converted.loc, diagnostic_base, args);
  293. }
  294. template <typename LocT>
  295. template <typename... Args>
  296. auto Emitter<LocT>::Builder::AddMessageWithLoc(
  297. Loc loc, const DiagnosticBase<Args...>& diagnostic_base,
  298. llvm::SmallVector<llvm::Any> args) -> void {
  299. if (!emitter_) {
  300. return;
  301. }
  302. diagnostic_.messages.emplace_back(
  303. Message{.kind = diagnostic_base.Kind,
  304. .level = diagnostic_base.Level,
  305. .loc = loc,
  306. .format = diagnostic_base.Format,
  307. .format_args = std::move(args),
  308. .format_fn = [](const Message& message) -> std::string {
  309. return FormatFn<Args...>(
  310. message, std::make_index_sequence<sizeof...(Args)>());
  311. }});
  312. }
  313. template <typename LocT>
  314. template <typename... Args, size_t... N>
  315. auto Emitter<LocT>::Builder::FormatFn(const Message& message,
  316. std::index_sequence<N...> /*indices*/)
  317. -> std::string {
  318. static_assert(sizeof...(Args) == sizeof...(N), "Invalid template args");
  319. CARBON_CHECK(message.format_args.size() == sizeof...(Args),
  320. "Argument count mismatch on {0}: {1} != {2}", message.kind,
  321. message.format_args.size(), sizeof...(Args));
  322. return llvm::formatv(
  323. message.format.data(),
  324. llvm::any_cast<
  325. typename Internal::DiagnosticTypeForArg<Args>::StorageType>(
  326. message.format_args[N])...);
  327. }
  328. template <typename LocT>
  329. template <typename... Args>
  330. auto Emitter<LocT>::Emit(LocT loc,
  331. const DiagnosticBase<Args...>& diagnostic_base,
  332. Internal::NoTypeDeduction<Args>... args) -> void {
  333. Builder builder(this, loc, diagnostic_base, {MakeAny<Args>(args)...});
  334. builder.Emit();
  335. }
  336. template <typename LocT>
  337. template <typename... Args>
  338. auto Emitter<LocT>::Build(LocT loc,
  339. const DiagnosticBase<Args...>& diagnostic_base,
  340. Internal::NoTypeDeduction<Args>... args) -> Builder {
  341. return Builder(this, loc, diagnostic_base, {MakeAny<Args>(args)...});
  342. }
  343. template <typename LocT>
  344. template <typename Arg>
  345. auto Emitter<LocT>::MakeAny(Arg arg) -> llvm::Any {
  346. llvm::Any converted = 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::Diagnostics
  354. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_DIAGNOSTIC_EMITTER_H_