[clang] [clang-tools-extra] [clang-tidy] Avoid processing declarations in system headers (PR #128150)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 11 09:55:55 PDT 2025


================
@@ -1573,19 +1574,41 @@ bool MatchASTVisitor::TraverseAttr(Attr *AttrNode) {
 class MatchASTConsumer : public ASTConsumer {
 public:
   MatchASTConsumer(MatchFinder *Finder,
-                   MatchFinder::ParsingDoneTestCallback *ParsingDone)
-      : Finder(Finder), ParsingDone(ParsingDone) {}
+                   MatchFinder::ParsingDoneTestCallback *ParsingDone,
+                   const MatchFinderOptions &Options)
+      : Finder(Finder), ParsingDone(ParsingDone), Options(Options) {}
 
 private:
+  bool HandleTopLevelDecl(DeclGroupRef DG) override {
+    if (Options.SkipSystemHeaders) {
+      for (Decl *D : DG) {
+        if (!isInSystemHeader(D))
+          TraversalScope.push_back(D);
+      }
+    }
+    return true;
+  }
+
   void HandleTranslationUnit(ASTContext &Context) override {
+    if (!TraversalScope.empty())
+      Context.setTraversalScope(TraversalScope);
----------------
PiotrZSL wrote:

This mean that if .cpp file is empty, then we will traverse system headers includes regardless of SkipSystemHeaders.
Shouldn't this check actually Options.SkipSystemHeaders.
Maybe in such case we shoudn't even call matchAST.

By default TraversalScope in Context is set to TranslationUnit.

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


More information about the cfe-commits mailing list