-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[clang-tidy] Avoid expensive AST traversal in RedundantTypenameCheck #170540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,9 +18,13 @@ using namespace clang::ast_matchers; | |||||||||||||||||||||||||||||||||||||||||
| namespace clang::tidy::readability { | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| void RedundantTypenameCheck::registerMatchers(MatchFinder *Finder) { | ||||||||||||||||||||||||||||||||||||||||||
| Finder->addMatcher(typeLoc(unless(hasAncestor(decl(isInstantiated())))) | ||||||||||||||||||||||||||||||||||||||||||
| .bind("nonDependentTypeLoc"), | ||||||||||||||||||||||||||||||||||||||||||
| this); | ||||||||||||||||||||||||||||||||||||||||||
| Finder->addMatcher( | ||||||||||||||||||||||||||||||||||||||||||
| typeLoc(loc(TypeMatcher(anyOf(typedefType(), tagType(), | ||||||||||||||||||||||||||||||||||||||||||
| deducedTemplateSpecializationType(), | ||||||||||||||||||||||||||||||||||||||||||
| templateSpecializationType()))), | ||||||||||||||||||||||||||||||||||||||||||
| unless(hasAncestor(decl(isInstantiated())))) | ||||||||||||||||||||||||||||||||||||||||||
| .bind("nonDependentTypeLoc"), | ||||||||||||||||||||||||||||||||||||||||||
| this); | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+21
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we skip the typeLoc(loc(qualType(unless(dependentNameType())))? Then we are not checking a type that is dependent, which eliminates the need for the if (NonDependentTypeLoc->getType()->isDependentType())
return SourceLocation();later
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that we can remove
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those are great suggestions. Thanks 😃 I’ll try to clean those up and see how our test case works after.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I originally added the llvm-project/clang-tools-extra/test/clang-tidy/checkers/readability/redundant-typename.cpp Lines 167 to 170 in 5911754
Without the unless, we get a false positive; the traversal seems to descend into the instantiation despite the traversal mode being TK_IgnoreUnlessSpelledInSource. Experimenting just now, I tried setting the traversal mode on the matcher explicitly:
Finder->addMatcher(traverse(TK_IgnoreUnlessSpelledInSource, typeLoc()
.bind("nonDependentTypeLoc")),
this);And now it does work! Is there maybe an issue causing the traversal mode to not get propagated?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, bingo. There are a bunch of llvm-project/clang/lib/ASTMatchers/ASTMatchFinder.cpp Lines 1670 to 1680 in 7220268
And all the rest (including the one we're calling) just... don't?! llvm-project/clang/lib/ASTMatchers/ASTMatchFinder.cpp Lines 1712 to 1716 in 7220268
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if (!getLangOpts().CPlusPlus20) | ||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove
TypeMatcher?