[clang] 439c920 - [ASTMatchers] Fix bug in `hasUnaryOperand`

Yitzhak Mandelbaum via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 16 13:19:20 PDT 2021


Author: Yitzhak Mandelbaum
Date: 2021-06-16T20:17:56Z
New Revision: 439c9206945aba15d74d5bcaef3bf3f4d1e32b5e

URL: https://github.com/llvm/llvm-project/commit/439c9206945aba15d74d5bcaef3bf3f4d1e32b5e
DIFF: https://github.com/llvm/llvm-project/commit/439c9206945aba15d74d5bcaef3bf3f4d1e32b5e.diff

LOG: [ASTMatchers] Fix bug in `hasUnaryOperand`

Currently, `hasUnaryOperand` fails for the overloaded `operator*`. This patch fixes the bug and
adds tests for this case.

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

Added: 
    

Modified: 
    clang/include/clang/ASTMatchers/ASTMatchersInternal.h
    clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
index 64e4dcb06894f..71f4f2d17ae3f 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -2102,6 +2102,8 @@ equivalentUnaryOperator<CXXOperatorCallExpr>(const CXXOperatorCallExpr &Node) {
     return UO_Minus;
   case OO_Amp:
     return UO_AddrOf;
+  case OO_Star:
+    return UO_Deref;
   case OO_Tilde:
     return UO_Not;
   case OO_Exclaim:

diff  --git a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
index ae496d39e00cf..12012d9c699d4 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -1893,6 +1893,23 @@ void plusIntOperator()
                      cxxOperatorCallExpr(
                          forFunction(functionDecl(hasName("plusIntOperator"))),
                          hasOperatorName("+"), hasUnaryOperand(expr())))));
+
+  Code = R"cpp(
+struct HasOpArrow
+{
+    int& operator*();
+};
+void foo()
+{
+    HasOpArrow s1;
+    *s1;
+}
+)cpp";
+
+  EXPECT_TRUE(
+      matches(Code, traverse(TK_IgnoreUnlessSpelledInSource,
+                             cxxOperatorCallExpr(hasOperatorName("*"),
+                                                 hasUnaryOperand(expr())))));
 }
 
 TEST(Matcher, UnaryOperatorTypes) {


        


More information about the cfe-commits mailing list