[clang] [clang-tools-extra] [clang-tidy] Avoid processing declarations in system headers (PR #128150)
Carlos Galvez via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 21 01:41:21 PDT 2025
carlosgalvezp wrote:
So I looked into the problem, and it seems that issue is that the `ParentMapContext` class does not correctly find all the parents due to the reduced AST traversal. One quick & dirty solution that makes the CERT test pass (and keeps all the remaining tests passing) is as follows:
```cpp
ParentMapContext::ParentMap::ParentMap(ASTContext &Ctx) {
+ std::vector<Decl *> const& OldScope = Ctx.getTraversalScope2();
+ Ctx.setTraversalScope({Ctx.getTranslationUnitDecl()});
ASTVisitor(*this).TraverseAST(Ctx);
+ Ctx.setTraversalScope(OldScope);
}
```
Essentially restore the full traversal scope for the sake of fetching all the parents, and then restoring it. This pattern is not uncommon, I have seen similar usages with `TraversalKindScope` in `ASTMatchFinder.cpp`, for example:
```cpp
void visitMatch(const BoundNodes& BoundNodesView) override {
TraversalKindScope RAII(*Context, Callback->getCheckTraversalKind());
CurBoundScope RAII2(State, BoundNodesView);
Callback->run(MatchFinder::MatchResult(BoundNodesView, Context));
}
```
So I could create similar `TraversalScopeScope` RAII class.
Let me know if this is an acceptable solution. Otherwise I'm happy to add an escape hatch. I'm a bit hesitant to adding it to the config file, would a compile-time flag be preferable (similar to `CLANG_TIDY_ENABLE_STATIC_ANALYZER`)?
If none of these options are satisfactory I can of course revert and go back to the drawing board :)
https://github.com/llvm/llvm-project/pull/128150
More information about the cfe-commits
mailing list