================
@@ -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"};
----------------
ArcsinX wrote:

Idea of this test is to issue diagnostics before a file processing. macro with 
newline inside does this.
E.g.
```
clang -fsyntax-only -x c++ $'-DMACRO=first\nsecond' /dev/null
```
Emit warning
```
warning: macro 'MACRO' contains embedded newline; text after the newline is 
ignored
1 warning generated.
```

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