================
@@ -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.
----------------
ArcsinX wrote:
Here we have a warning diagnostic without location, because this macro comes
from command line.
In clangd we filter-out such diagnostics if these are not errors by default
[here](https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/clangd/Diagnostics.cpp#L738-L744)
The hook promotes the warning to an error, so error count increases. But this
promotion doesn't make this diagnostic to be "error by default", it's still
"warning by default", so it isn’t stored, passed to `sawDiagnostic()` or
published to the editor.
Without the early hook the diagnostic would already have been emitted as a
warning, and the error counter check would fail.
So the test proving early configuration works, but it deliberately does not
demonstrate a diagnostic reaching the user.
https://github.com/llvm/llvm-project/pull/225198
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits