[clang-tools-extra] 499bf67 - [include-cleaner] Don't count references to operators as uses

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 9 05:02:56 PST 2023


Author: Haojian Wu
Date: 2023-01-09T13:59:15+01:00
New Revision: 499bf67208d982948e2580b56a09944a285fee76

URL: https://github.com/llvm/llvm-project/commit/499bf67208d982948e2580b56a09944a285fee76
DIFF: https://github.com/llvm/llvm-project/commit/499bf67208d982948e2580b56a09944a285fee76.diff

LOG: [include-cleaner] Don't count references to operators as uses

Fixes https://github.com/llvm/llvm-project/issues/59655

Differential Revision: https://reviews.llvm.org/D140551

Added: 
    

Modified: 
    clang-tools-extra/include-cleaner/lib/WalkAST.cpp
    clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
index f32221018caf..18e6d5e21df0 100644
--- a/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
+++ b/clang-tools-extra/include-cleaner/lib/WalkAST.cpp
@@ -66,6 +66,20 @@ class ASTWalker : public RecursiveASTVisitor<ASTWalker> {
 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
index ca2eb25eceee..c8959e7eb673 100644
--- a/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/WalkASTTest.cpp
@@ -245,6 +245,16 @@ TEST(WalkAST, ConstructExprs) {
   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() {}");


        


More information about the cfe-commits mailing list