Skip to content

[include-cleaner] Mark RecordDecls referenced in UsingDecls as explicit #106430

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

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion clang-tools-extra/include-cleaner/lib/WalkAST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,12 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
bool VisitUsingDecl(UsingDecl *UD) {
for (const auto *Shadow : UD->shadows()) {
auto *TD = Shadow->getTargetDecl();
auto IsUsed = TD->isUsed() || TD->isReferenced();
// For function-decls, we might have overloads brought in due to
// transitive dependencies. Hence we only want to report explicit
// references for those if they're used.
// But for record decls, spelling of the type always refers to primary
// decl non-ambiguously. Hence spelling is already a use.
auto IsUsed = TD->isUsed() || TD->isReferenced() || !TD->getAsFunction();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the name IsUsed no longer reflects its purpose. Consider renaming it to something more suitable or inlining it.

Additionally, it would be helpful to add comments to document the intention behind this code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I was actually thinking that usedness is still the right concept here, spelling of the type name in the using declaration is enough to trigger that use for records.

I added comments also along those lines, LMK if it still doesn't resonate with you.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The connection between usedness and !TD->getAsFunction() wasn’t immediately obvious to me. With the newly-added comment, it's much clearer now, everything looks good, thanks.

report(UD->getLocation(), TD,
IsUsed ? RefType::Explicit : RefType::Ambiguous);

Expand Down
11 changes: 8 additions & 3 deletions clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ TEST(WalkAST, TemplateSpecializationsFromUsingDecl) {
// Class templates
testWalk(R"cpp(
namespace ns {
template<class T> class $ambiguous^Z {}; // primary template
template<class T> class $explicit^Z {}; // primary template
template<class T> class $ambiguous^Z<T*> {}; // partial specialization
template<> class $ambiguous^Z<int> {}; // full specialization
}
Expand All @@ -265,7 +265,7 @@ template<> class $ambiguous^Z<int> {}; // full specialization
// Var templates
testWalk(R"cpp(
namespace ns {
template<class T> T $ambiguous^foo; // primary template
template<class T> T $explicit^foo; // primary template
template<class T> T $ambiguous^foo<T*>; // partial specialization
template<> int* $ambiguous^foo<int>; // full specialization
}
Expand Down Expand Up @@ -335,7 +335,12 @@ TEST(WalkAST, Using) {
testWalk(R"cpp(
namespace ns {
template<class T>
class $ambiguous^Y {};
class $explicit^Y {};
})cpp",
"using ns::^Y;");
testWalk(R"cpp(
namespace ns {
class $explicit^Y {};
})cpp",
"using ns::^Y;");
testWalk(R"cpp(
Expand Down
Loading