Skip to content

Commit 559f372

Browse files
committed
[ASTMatchers] Fix hasUnaryOperand matcher for postfix operators
Differential Revision: https://reviews.llvm.org/D97095
1 parent fde55a9 commit 559f372

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

clang/include/clang/ASTMatchers/ASTMatchersInternal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2039,7 +2039,8 @@ equivalentUnaryOperator(const NodeType &Node) {
20392039
template <>
20402040
inline Optional<UnaryOperatorKind>
20412041
equivalentUnaryOperator<CXXOperatorCallExpr>(const CXXOperatorCallExpr &Node) {
2042-
if (Node.getNumArgs() != 1)
2042+
if (Node.getNumArgs() != 1 && Node.getOperator() != OO_PlusPlus &&
2043+
Node.getOperator() != OO_MinusMinus)
20432044
return None;
20442045
switch (Node.getOperator()) {
20452046
default:

clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,6 +1630,84 @@ void opFree()
16301630
cxxOperatorCallExpr(forFunction(functionDecl(hasName("opFree"))),
16311631
hasAnyOperatorName("+", "!"),
16321632
hasUnaryOperand(s1Expr)))));
1633+
1634+
Code = R"cpp(
1635+
struct HasIncOperatorsMem
1636+
{
1637+
HasIncOperatorsMem& operator++();
1638+
HasIncOperatorsMem operator++(int);
1639+
};
1640+
struct HasIncOperatorsFree
1641+
{
1642+
};
1643+
HasIncOperatorsFree& operator++(HasIncOperatorsFree&);
1644+
HasIncOperatorsFree operator++(HasIncOperatorsFree&, int);
1645+
1646+
void prefixIncOperatorMem()
1647+
{
1648+
HasIncOperatorsMem s1;
1649+
++s1;
1650+
}
1651+
void prefixIncOperatorFree()
1652+
{
1653+
HasIncOperatorsFree s1;
1654+
++s1;
1655+
}
1656+
void postfixIncOperatorMem()
1657+
{
1658+
HasIncOperatorsMem s1;
1659+
s1++;
1660+
}
1661+
void postfixIncOperatorFree()
1662+
{
1663+
HasIncOperatorsFree s1;
1664+
s1++;
1665+
}
1666+
1667+
struct HasOpPlusInt
1668+
{
1669+
HasOpPlusInt& operator+(int);
1670+
};
1671+
void plusIntOperator()
1672+
{
1673+
HasOpPlusInt s1;
1674+
s1+1;
1675+
}
1676+
)cpp";
1677+
1678+
EXPECT_TRUE(matches(
1679+
Code,
1680+
traverse(TK_IgnoreUnlessSpelledInSource,
1681+
cxxOperatorCallExpr(
1682+
forFunction(functionDecl(hasName("prefixIncOperatorMem"))),
1683+
hasOperatorName("++"), hasUnaryOperand(declRefExpr())))));
1684+
1685+
EXPECT_TRUE(matches(
1686+
Code,
1687+
traverse(TK_IgnoreUnlessSpelledInSource,
1688+
cxxOperatorCallExpr(
1689+
forFunction(functionDecl(hasName("prefixIncOperatorFree"))),
1690+
hasOperatorName("++"), hasUnaryOperand(declRefExpr())))));
1691+
1692+
EXPECT_TRUE(matches(
1693+
Code,
1694+
traverse(TK_IgnoreUnlessSpelledInSource,
1695+
cxxOperatorCallExpr(
1696+
forFunction(functionDecl(hasName("postfixIncOperatorMem"))),
1697+
hasOperatorName("++"), hasUnaryOperand(declRefExpr())))));
1698+
1699+
EXPECT_TRUE(matches(
1700+
Code,
1701+
traverse(TK_IgnoreUnlessSpelledInSource,
1702+
cxxOperatorCallExpr(
1703+
forFunction(functionDecl(hasName("postfixIncOperatorFree"))),
1704+
hasOperatorName("++"), hasUnaryOperand(declRefExpr())))));
1705+
1706+
EXPECT_FALSE(matches(
1707+
Code, traverse(TK_IgnoreUnlessSpelledInSource,
1708+
cxxOperatorCallExpr(
1709+
forFunction(functionDecl(hasName("plusIntOperator"))),
1710+
hasOperatorName("+"), hasUnaryOperand(expr())))));
16331711
}
16341712

16351713
TEST(Matcher, UnaryOperatorTypes) {

0 commit comments

Comments
 (0)