Skip to content

Commit acff429

Browse files
authored
[include-cleaner] Mark RecordDecls referenced in UsingDecls as explicit (llvm#106430)
We were reporting ambigious references from using declarations as user can be depending on different overloads of a function just because they are visible in the TU. This doesn't apply to records, or primary templates as declaration being referenced in such cases is unambigious, the ambiguity applies to specializations though. Hence this patch returns an explicit reference to record decls and primary templates of those.
1 parent 73ef397 commit acff429

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

clang-tools-extra/include-cleaner/lib/WalkAST.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,12 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
203203
bool VisitUsingDecl(UsingDecl *UD) {
204204
for (const auto *Shadow : UD->shadows()) {
205205
auto *TD = Shadow->getTargetDecl();
206-
auto IsUsed = TD->isUsed() || TD->isReferenced();
206+
// For function-decls, we might have overloads brought in due to
207+
// transitive dependencies. Hence we only want to report explicit
208+
// references for those if they're used.
209+
// But for record decls, spelling of the type always refers to primary
210+
// decl non-ambiguously. Hence spelling is already a use.
211+
auto IsUsed = TD->isUsed() || TD->isReferenced() || !TD->getAsFunction();
207212
report(UD->getLocation(), TD,
208213
IsUsed ? RefType::Explicit : RefType::Ambiguous);
209214

clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ TEST(WalkAST, TemplateSpecializationsFromUsingDecl) {
255255
// Class templates
256256
testWalk(R"cpp(
257257
namespace ns {
258-
template<class T> class $ambiguous^Z {}; // primary template
258+
template<class T> class $explicit^Z {}; // primary template
259259
template<class T> class $ambiguous^Z<T*> {}; // partial specialization
260260
template<> class $ambiguous^Z<int> {}; // full specialization
261261
}
@@ -265,7 +265,7 @@ template<> class $ambiguous^Z<int> {}; // full specialization
265265
// Var templates
266266
testWalk(R"cpp(
267267
namespace ns {
268-
template<class T> T $ambiguous^foo; // primary template
268+
template<class T> T $explicit^foo; // primary template
269269
template<class T> T $ambiguous^foo<T*>; // partial specialization
270270
template<> int* $ambiguous^foo<int>; // full specialization
271271
}
@@ -335,7 +335,12 @@ TEST(WalkAST, Using) {
335335
testWalk(R"cpp(
336336
namespace ns {
337337
template<class T>
338-
class $ambiguous^Y {};
338+
class $explicit^Y {};
339+
})cpp",
340+
"using ns::^Y;");
341+
testWalk(R"cpp(
342+
namespace ns {
343+
class $explicit^Y {};
339344
})cpp",
340345
"using ns::^Y;");
341346
testWalk(R"cpp(

0 commit comments

Comments
 (0)