From 439c9206945aba15d74d5bcaef3bf3f4d1e32b5e Mon Sep 17 00:00:00 2001 From: Yitzhak Mandelbaum Date: Wed, 16 Jun 2021 14:47:18 +0000 Subject: [PATCH] [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 --- .../clang/ASTMatchers/ASTMatchersInternal.h | 2 ++ .../ASTMatchers/ASTMatchersTraversalTest.cpp | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h index 64e4dcb06894..71f4f2d17ae3 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchersInternal.h +++ b/clang/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -2102,6 +2102,8 @@ equivalentUnaryOperator(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 ae496d39e00c..12012d9c699d 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) {