From 26ff268b80c589fd9f71c1c214af77cd972642ca Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya Date: Wed, 29 Mar 2023 14:04:45 +0200 Subject: [PATCH] [include-cleaner] Report references to operator calls as implicit Missing these references can result in false negatives in the used-ness analysis and break builds. Differential Revision: https://reviews.llvm.org/D147144 --- clang-tools-extra/include-cleaner/lib/WalkAST.cpp | 19 ++++++++++++++----- .../include-cleaner/unittests/WalkASTTest.cpp | 17 +++++++++++------ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp index b10c722b..8a1dd77 100644 --- a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp +++ b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp @@ -91,14 +91,23 @@ class ASTWalker : public RecursiveASTVisitor { public: ASTWalker(DeclCallback Callback) : Callback(Callback) {} + // Operators are almost always ADL extension points and by design references + // to them doesn't count as uses (generally the type should provide them, so + // ignore them). + // Unless we're using an operator defined as a member, in such cases treat + // this as a regular reference. bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *S) { if (!WalkUpFromCXXOperatorCallExpr(S)) return false; - - // Operators are always ADL extension points, by design references to them - // doesn't count as uses (generally the type should provide them). - // Don't traverse the callee. - + if (auto *CD = S->getCalleeDecl()) { + if (llvm::isa(CD)) { + // Treat this as a regular member reference. + report(S->getOperatorLoc(), getMemberProvider(S->getArg(0)->getType())); + } else { + report(S->getOperatorLoc(), llvm::dyn_cast(CD), + RefType::Implicit); + } + } for (auto *Arg : S->arguments()) if (!TraverseStmt(Arg)) return false; diff --git a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp index fd6c6da..fceec67 100644 --- a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp +++ b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp @@ -291,13 +291,18 @@ TEST(WalkAST, ConstructExprs) { } TEST(WalkAST, Operator) { - // References to operators are not counted as uses. - testWalk("struct string {}; int operator+(string, string);", - "int k = string() ^+ string();"); - testWalk("struct string {int operator+(string); }; ", - "int k = string() ^+ string();"); - testWalk("struct string { friend int operator+(string, string); }; ", + // Operator calls are marked as implicit references as they're ADL-used and + // type should be providing them. + testWalk( + "struct string { friend int $implicit^operator+(string, string); }; ", + "int k = string() ^+ string();"); + // Unless they're members, we treat them as regular member expr calls. + testWalk("struct $explicit^string {int operator+(string); }; ", "int k = string() ^+ string();"); + // Make sure usage is attributed to the alias. + testWalk( + "struct string {int operator+(string); }; using $explicit^foo = string;", + "int k = foo() ^+ string();"); } TEST(WalkAST, VarDecls) { -- 2.7.4