emitter_test.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #include "toolchain/diagnostics/emitter.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "llvm/ADT/StringRef.h"
  8. #include "toolchain/diagnostics/mocks.h"
  9. namespace Carbon::Testing {
  10. namespace {
  11. using testing::ElementsAre;
  12. class FakeEmitter : public Diagnostics::Emitter<int> {
  13. public:
  14. using Emitter::Emitter;
  15. protected:
  16. auto ConvertLoc(int n, ContextFnT /*context_fn*/) const
  17. -> Diagnostics::ConvertedLoc override {
  18. return {.loc = {.line_number = 1, .column_number = n},
  19. .last_byte_offset = -1};
  20. }
  21. };
  22. class EmitterTest : public ::testing::Test {
  23. public:
  24. EmitterTest() : emitter_(&consumer_) {}
  25. Testing::MockDiagnosticConsumer consumer_;
  26. FakeEmitter emitter_;
  27. };
  28. TEST_F(EmitterTest, EmitSimpleError) {
  29. CARBON_DIAGNOSTIC(TestDiagnostic, Error, "simple error");
  30. EXPECT_CALL(consumer_, HandleDiagnostic(IsSingleDiagnostic(
  31. Diagnostics::Kind::TestDiagnostic,
  32. Diagnostics::Level::Error, 1, 1, "simple error")));
  33. EXPECT_CALL(consumer_, HandleDiagnostic(IsSingleDiagnostic(
  34. Diagnostics::Kind::TestDiagnostic,
  35. Diagnostics::Level::Error, 1, 2, "simple error")));
  36. emitter_.Emit(1, TestDiagnostic);
  37. emitter_.Emit(2, TestDiagnostic);
  38. }
  39. TEST_F(EmitterTest, EmitSimpleWarning) {
  40. CARBON_DIAGNOSTIC(TestDiagnostic, Warning, "simple warning");
  41. EXPECT_CALL(consumer_,
  42. HandleDiagnostic(IsSingleDiagnostic(
  43. Diagnostics::Kind::TestDiagnostic,
  44. Diagnostics::Level::Warning, 1, 1, "simple warning")));
  45. emitter_.Emit(1, TestDiagnostic);
  46. }
  47. TEST_F(EmitterTest, EmitOneArgDiagnostic) {
  48. CARBON_DIAGNOSTIC(TestDiagnostic, Error, "arg: `{0}`", std::string);
  49. EXPECT_CALL(consumer_, HandleDiagnostic(IsSingleDiagnostic(
  50. Diagnostics::Kind::TestDiagnostic,
  51. Diagnostics::Level::Error, 1, 1, "arg: `str`")));
  52. emitter_.Emit(1, TestDiagnostic, "str");
  53. }
  54. TEST_F(EmitterTest, EmitNote) {
  55. CARBON_DIAGNOSTIC(TestDiagnostic, Warning, "simple warning");
  56. CARBON_DIAGNOSTIC(TestDiagnosticNote, Note, "note");
  57. EXPECT_CALL(
  58. consumer_,
  59. HandleDiagnostic(IsDiagnostic(
  60. Diagnostics::Level::Warning,
  61. ElementsAre(
  62. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnostic,
  63. Diagnostics::Level::Warning, 1, 1,
  64. "simple warning"),
  65. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnosticNote,
  66. Diagnostics::Level::Note, 1, 2, "note")))));
  67. emitter_.Build(1, TestDiagnostic).Note(2, TestDiagnosticNote).Emit();
  68. }
  69. TEST_F(EmitterTest, EmitContext) {
  70. CARBON_DIAGNOSTIC(TestDiagnosticContext, Context, "context");
  71. CARBON_DIAGNOSTIC(TestDiagnostic, Warning, "simple warning");
  72. EXPECT_CALL(
  73. consumer_,
  74. HandleDiagnostic(IsDiagnostic(
  75. Diagnostics::Level::Warning,
  76. ElementsAre(
  77. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnosticContext,
  78. Diagnostics::Level::Context, 1, 2, "context"),
  79. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnostic,
  80. Diagnostics::Level::Warning, 1, 1,
  81. "simple warning")))));
  82. Diagnostics::ContextScope scope(&emitter_, [&](auto& builder) {
  83. builder.Context(2, TestDiagnosticContext);
  84. });
  85. emitter_.Emit(1, TestDiagnostic);
  86. }
  87. TEST_F(EmitterTest, EmitSoftContext) {
  88. CARBON_DIAGNOSTIC(TestDiagnosticSoftContext, SoftContext, "soft context");
  89. CARBON_DIAGNOSTIC(TestDiagnostic, Warning, "simple warning");
  90. EXPECT_CALL(
  91. consumer_,
  92. HandleDiagnostic(IsDiagnostic(
  93. Diagnostics::Level::Warning,
  94. ElementsAre(
  95. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnosticSoftContext,
  96. Diagnostics::Level::SoftContext, 1, 2,
  97. "soft context"),
  98. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnostic,
  99. Diagnostics::Level::Warning, 1, 1,
  100. "simple warning")))));
  101. Diagnostics::ContextScope soft_scope(&emitter_, [&](auto& builder) {
  102. builder.Context(2, TestDiagnosticSoftContext);
  103. });
  104. emitter_.Emit(1, TestDiagnostic);
  105. }
  106. TEST_F(EmitterTest, EmitSoftContextAndContext) {
  107. CARBON_DIAGNOSTIC(TestDiagnosticSoftContext, SoftContext, "soft context");
  108. CARBON_DIAGNOSTIC(TestDiagnosticContext, Context, "context");
  109. CARBON_DIAGNOSTIC(TestDiagnostic, Warning, "simple warning");
  110. EXPECT_CALL(
  111. consumer_,
  112. HandleDiagnostic(IsDiagnostic(
  113. Diagnostics::Level::Warning,
  114. ElementsAre(
  115. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnosticSoftContext,
  116. Diagnostics::Level::SoftContext, 1, 3,
  117. "soft context"),
  118. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnosticContext,
  119. Diagnostics::Level::Context, 1, 2, "context"),
  120. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnostic,
  121. Diagnostics::Level::Warning, 1, 1,
  122. "simple warning")))));
  123. Diagnostics::ContextScope soft_scope(&emitter_, [&](auto& builder) {
  124. builder.Context(3, TestDiagnosticSoftContext);
  125. });
  126. Diagnostics::ContextScope scope(&emitter_, [&](auto& builder) {
  127. builder.Context(2, TestDiagnosticContext);
  128. });
  129. emitter_.Emit(1, TestDiagnostic);
  130. }
  131. TEST_F(EmitterTest, EmitContextAndSoftContext) {
  132. CARBON_DIAGNOSTIC(TestDiagnosticContext, Context, "context");
  133. CARBON_DIAGNOSTIC(TestDiagnosticSoftContext, SoftContext, "soft context");
  134. CARBON_DIAGNOSTIC(TestDiagnostic, Warning, "simple warning");
  135. EXPECT_CALL(
  136. consumer_,
  137. HandleDiagnostic(IsDiagnostic(
  138. Diagnostics::Level::Warning,
  139. ElementsAre(
  140. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnosticContext,
  141. Diagnostics::Level::Context, 1, 3, "context"),
  142. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnostic,
  143. Diagnostics::Level::Warning, 1, 1,
  144. "simple warning")))));
  145. Diagnostics::ContextScope scope(&emitter_, [&](auto& builder) {
  146. builder.Context(3, TestDiagnosticContext);
  147. });
  148. // This SoftContext does not produce a message, since the earlire Context
  149. // supersedes it.
  150. Diagnostics::ContextScope soft_scope(&emitter_, [&](auto& builder) {
  151. builder.Context(2, TestDiagnosticSoftContext);
  152. });
  153. emitter_.Emit(1, TestDiagnostic);
  154. }
  155. TEST_F(EmitterTest, EmitTwoContext) {
  156. CARBON_DIAGNOSTIC(TestDiagnosticContext, Context, "context");
  157. CARBON_DIAGNOSTIC(TestDiagnosticContext2, Context, "context 2");
  158. CARBON_DIAGNOSTIC(TestDiagnostic, Warning, "simple warning");
  159. EXPECT_CALL(
  160. consumer_,
  161. HandleDiagnostic(IsDiagnostic(
  162. Diagnostics::Level::Warning,
  163. ElementsAre(
  164. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnosticContext,
  165. Diagnostics::Level::Context, 1, 3, "context"),
  166. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnosticContext2,
  167. Diagnostics::Level::Context, 1, 2,
  168. "context 2"),
  169. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnostic,
  170. Diagnostics::Level::Warning, 1, 1,
  171. "simple warning")))));
  172. Diagnostics::ContextScope scope(&emitter_, [&](auto& builder) {
  173. builder.Context(3, TestDiagnosticContext);
  174. });
  175. Diagnostics::ContextScope scope2(&emitter_, [&](auto& builder) {
  176. builder.Context(2, TestDiagnosticContext2);
  177. });
  178. emitter_.Emit(1, TestDiagnostic);
  179. }
  180. TEST_F(EmitterTest, EmitTwoSoftContext) {
  181. CARBON_DIAGNOSTIC(TestDiagnosticSoftContext, SoftContext, "soft context");
  182. CARBON_DIAGNOSTIC(TestDiagnosticSoftContext2, SoftContext, "soft context 2");
  183. CARBON_DIAGNOSTIC(TestDiagnostic, Warning, "simple warning");
  184. EXPECT_CALL(
  185. consumer_,
  186. HandleDiagnostic(IsDiagnostic(
  187. Diagnostics::Level::Warning,
  188. ElementsAre(
  189. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnosticSoftContext,
  190. Diagnostics::Level::SoftContext, 1, 3,
  191. "soft context"),
  192. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnostic,
  193. Diagnostics::Level::Warning, 1, 1,
  194. "simple warning")))));
  195. Diagnostics::ContextScope scope(&emitter_, [&](auto& builder) {
  196. builder.Context(3, TestDiagnosticSoftContext);
  197. });
  198. // This SoftContext does not produce a message, since the earlire Context
  199. // supersedes it.
  200. Diagnostics::ContextScope soft_scope(&emitter_, [&](auto& builder) {
  201. builder.Context(2, TestDiagnosticSoftContext2);
  202. });
  203. emitter_.Emit(1, TestDiagnostic);
  204. }
  205. TEST_F(EmitterTest, Flush) {
  206. bool flushed = false;
  207. auto flush_fn = [&]() { flushed = true; };
  208. {
  209. FakeEmitter emitter(&consumer_);
  210. emitter.AddFlushFn(flush_fn);
  211. // Registering the function does not flush.
  212. EXPECT_FALSE(flushed);
  213. // Explicit calls to `Flush` should flush.
  214. emitter.Flush();
  215. EXPECT_TRUE(flushed);
  216. flushed = false;
  217. {
  218. Diagnostics::AnnotationScope annot(&emitter, [](auto&) {});
  219. // Registering an annotation scope should flush.
  220. EXPECT_TRUE(flushed);
  221. flushed = false;
  222. }
  223. // Unregistering an annotation scope should flush.
  224. EXPECT_TRUE(flushed);
  225. flushed = false;
  226. }
  227. // Destroying the emitter should not flush, as that could call back into the
  228. // base class emitter after the derived-class emitter has been destroyed.
  229. EXPECT_FALSE(flushed);
  230. }
  231. } // namespace
  232. } // namespace Carbon::Testing