diff --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp --- a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp +++ b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp @@ -66,6 +66,20 @@ public: ASTWalker(DeclCallback Callback) : Callback(Callback) {} + 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. + + for (auto *Arg : S->arguments()) + if (!TraverseStmt(Arg)) + return false; + return true; + } + bool VisitDeclRefExpr(DeclRefExpr *DRE) { report(DRE->getLocation(), DRE->getFoundDecl()); return true; diff --git a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp --- a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp +++ b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp @@ -245,6 +245,16 @@ testWalk("struct S { $implicit^S(int); };", "S t = ^42;"); } +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); }; ", + "int k = string() ^+ string();"); +} + TEST(WalkAST, Functions) { // Definition uses declaration, not the other way around. testWalk("void $explicit^foo();", "void ^foo() {}");