================
@@ -125,6 +138,55 @@ TEST(FeatureModulesTest, SuppressDiags) {
   }
 }
 
+TEST(FeatureModulesTest, BeforeBeginSourceFile) {
+  std::vector<frontend::ActionKind> Builds;
+  auto Module = std::make_unique<TestModule>();
+  Module->BeforeBeginSourceFile = [&](CompilerInstance &CI) {
+    Builds.push_back(CI.getFrontendOpts().ProgramAction);
+  };
+  FeatureModuleSet Modules;
+  Modules.add(std::move(Module));
+  auto TU = TestTU::withCode(R"cpp(
+    #include "header.h"
+    HeaderType value;
+  )cpp");
+  TU.AdditionalFiles["header.h"] = "struct HeaderType {};";
+  TU.FeatureModules = &Modules;
+  EXPECT_THAT(TU.build().getDiagnostics(), testing::IsEmpty());
+  // The preamble is built from header.h, but only the main-file build calls
+  // this hook.
+  EXPECT_THAT(Builds, testing::ElementsAre(frontend::ParseSyntaxOnly));
+}
+
+TEST(FeatureModulesTest, BeforeBeginSourceFileDiagnostics) {
+  unsigned SeenDiagnostics = 0;
+  auto Module = std::make_unique<TestModule>();
+  Module->BeforeBeginSourceFile = [](CompilerInstance &CI) {
+    // The newline warning is emitted while BeginSourceFile initializes macros,
+    // so beforePPCallbacks and beforeExecute would be too late to promote it.
+    CI.getDiagnostics().setSeverity(
+        diag::warn_fe_macro_contains_embedded_newline, diag::Severity::Error,
+        SourceLocation());
+  };
+  Module->SawDiagnostic = [&](const clang::Diagnostic &Info, clangd::Diag &) {
+    if (Info.getID() == diag::warn_fe_macro_contains_embedded_newline)
+      ++SeenDiagnostics;
+  };
+  FeatureModuleSet Modules;
+  Modules.add(std::move(Module));
+
+  auto TU = TestTU::withCode("int value;");
+  TU.ExtraArgs = {"-DMACRO=first\nsecond"};
+  TU.FeatureModules = &Modules;
+  auto AST = TU.build();
+  // clangd filters out this location-less diagnostic even when promoted, so
+  // check Clang's error count to verify that it was emitted as an error.
----------------
timon-ul wrote:

I am a bit lost in semantics here, we are emitting this error to the user so 
they know of it, but at the same time we do not store it because it has no 
location? Is this because we cannot tie it to a location anymore as soon as the 
macro is processed so there is nowhere to store it? But we still can tie it to 
a location for the emitting right? Or am I completly off with what is happening 
here?

https://github.com/llvm/llvm-project/pull/225198
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to