================
@@ -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:
The main scenario, I guess, is to support build-time configuration without
source code modifications. E.g.
[here](https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/clangd/index/remote/CMakeLists.txt#L20)
we define macro, and this affect protobuf configuration
[here](https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/port_def.inc#L276)
https://github.com/llvm/llvm-project/pull/225198
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits