[ASTMatchers] Fix hasUnaryOperand matcher for postfix operators

Differential Revision: https://reviews.llvm.org/D97095
This commit is contained in:
Stephen Kelly 2021-02-19 22:59:35 +00:00
parent fde55a9c9b
commit 559f372844
2 changed files with 80 additions and 1 deletions

View File

@ -2039,7 +2039,8 @@ equivalentUnaryOperator(const NodeType &Node) {
template <>
inline Optional<UnaryOperatorKind>
equivalentUnaryOperator<CXXOperatorCallExpr>(const CXXOperatorCallExpr &Node) {
if (Node.getNumArgs() != 1)
if (Node.getNumArgs() != 1 && Node.getOperator() != OO_PlusPlus &&
Node.getOperator() != OO_MinusMinus)
return None;
switch (Node.getOperator()) {
default:

View File

@ -1630,6 +1630,84 @@ void opFree()
cxxOperatorCallExpr(forFunction(functionDecl(hasName("opFree"))),
hasAnyOperatorName("+", "!"),
hasUnaryOperand(s1Expr)))));
Code = R"cpp(
struct HasIncOperatorsMem
{
HasIncOperatorsMem& operator++();
HasIncOperatorsMem operator++(int);
};
struct HasIncOperatorsFree
{
};
HasIncOperatorsFree& operator++(HasIncOperatorsFree&);
HasIncOperatorsFree operator++(HasIncOperatorsFree&, int);
void prefixIncOperatorMem()
{
HasIncOperatorsMem s1;
++s1;
}
void prefixIncOperatorFree()
{
HasIncOperatorsFree s1;
++s1;
}
void postfixIncOperatorMem()
{
HasIncOperatorsMem s1;
s1++;
}
void postfixIncOperatorFree()
{
HasIncOperatorsFree s1;
s1++;
}
struct HasOpPlusInt
{
HasOpPlusInt& operator+(int);
};
void plusIntOperator()
{
HasOpPlusInt s1;
s1+1;
}
)cpp";
EXPECT_TRUE(matches(
Code,
traverse(TK_IgnoreUnlessSpelledInSource,
cxxOperatorCallExpr(
forFunction(functionDecl(hasName("prefixIncOperatorMem"))),
hasOperatorName("++"), hasUnaryOperand(declRefExpr())))));
EXPECT_TRUE(matches(
Code,
traverse(TK_IgnoreUnlessSpelledInSource,
cxxOperatorCallExpr(
forFunction(functionDecl(hasName("prefixIncOperatorFree"))),
hasOperatorName("++"), hasUnaryOperand(declRefExpr())))));
EXPECT_TRUE(matches(
Code,
traverse(TK_IgnoreUnlessSpelledInSource,
cxxOperatorCallExpr(
forFunction(functionDecl(hasName("postfixIncOperatorMem"))),
hasOperatorName("++"), hasUnaryOperand(declRefExpr())))));
EXPECT_TRUE(matches(
Code,
traverse(TK_IgnoreUnlessSpelledInSource,
cxxOperatorCallExpr(
forFunction(functionDecl(hasName("postfixIncOperatorFree"))),
hasOperatorName("++"), hasUnaryOperand(declRefExpr())))));
EXPECT_FALSE(matches(
Code, traverse(TK_IgnoreUnlessSpelledInSource,
cxxOperatorCallExpr(
forFunction(functionDecl(hasName("plusIntOperator"))),
hasOperatorName("+"), hasUnaryOperand(expr())))));
}
TEST(Matcher, UnaryOperatorTypes) {