[clang-tools-extra] [clangd] Replay macro definitions from preamble for clang-tidy checks (PR #202495)

Aleksandr Platonov via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 15 08:31:52 PDT 2026


================
@@ -107,6 +113,52 @@ MATCHER_P(rangeIs, R, "") {
   return arg.beginOffset() == R.Begin && arg.endOffset() == R.End;
 }
 
+TEST(ReplayPreambleTest, MacroDefinitions) {
+  DefinedMacros.clear();
+
+  TestTU TU;
+  TU.ClangTidyProvider = addTidyChecks(CheckName);
+  TU.Code = R"cpp(
+      #ifndef _TEST_H
+      #define _TEST_H
+      #define _TEST_MACRO
+      #endif
+  )cpp";
+
+  Config Cfg;
+  Cfg.Diagnostics.ClangTidy.FastCheckFilter = Config::FastCheckPolicy::Loose;
+  WithContextValue WithCfg(Config::Key, std::move(Cfg));
+
+  TU.build();
+
+  EXPECT_THAT(DefinedMacros, testing::Contains(std::string("_TEST_H")));
+  EXPECT_THAT(DefinedMacros, testing::Contains(std::string("_TEST_MACRO")));
+}
+
+TEST(ReplayPreambleTest, MacroDefinitionsPartialPreamble) {
+  DefinedMacros.clear();
+
+  TestTU TU;
+  TU.ClangTidyProvider = addTidyChecks(CheckName);
+  TU.Code = R"cpp(
+    #ifndef _TEST_H
+    #define _TEST_H
+    void unused(void);
+    #define _TEST_MACRO
+    #endif
+  )cpp";
+
+  Config Cfg;
+  Cfg.Diagnostics.ClangTidy.FastCheckFilter = Config::FastCheckPolicy::Loose;
+  WithContextValue WithCfg(Config::Key, std::move(Cfg));
+
+  TU.build();
+
+  // Both macros should be seen by clang-tidy
+  EXPECT_THAT(DefinedMacros, testing::Contains(std::string("_TEST_H")));
+  EXPECT_THAT(DefinedMacros, testing::Contains(std::string("_TEST_MACRO")));
+}
----------------
ArcsinX wrote:

nit: As far as both added tests are mostly the same (only `TU.Code` differs), maybe we can combine them into the one test (iterate over array and do the same for every item),  to avoid code duplication.
Like we do here: https://github.com/llvm/llvm-project/blob/main/clang-tools-extra/clangd/unittests/SemanticHighlightingTests.cpp#L108

https://github.com/llvm/llvm-project/pull/202495


More information about the cfe-commits mailing list