[PATCH] D97095: [ASTMatchers] Fix hasUnaryOperand matcher for postfix operators

2021-02-20 Thread Stephen Kelly via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG559f3728441d: [ASTMatchers] Fix hasUnaryOperand matcher for 
postfix operators (authored by stephenkelly).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D97095/new/

https://reviews.llvm.org/D97095

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

Index: clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -1630,6 +1630,84 @@
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) {
Index: clang/include/clang/ASTMatchers/ASTMatchersInternal.h
===
--- clang/include/clang/ASTMatchers/ASTMatchersInternal.h
+++ clang/include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -2039,7 +2039,8 @@
 template <>
 inline Optional
 equivalentUnaryOperator(const CXXOperatorCallExpr ) {
-  if (Node.getNumArgs() != 1)
+  if (Node.getNumArgs() != 1 && Node.getOperator() != OO_PlusPlus &&
+  Node.getOperator() != OO_MinusMinus)
 return None;
   switch (Node.getOperator()) {
   default:
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D97095: [ASTMatchers] Fix hasUnaryOperand matcher for postfix operators

2021-02-19 Thread Stephen Kelly via Phabricator via cfe-commits
steveire created this revision.
steveire added a reviewer: aaron.ballman.
steveire requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D97095

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

Index: clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
===
--- clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
+++ clang/unittests/ASTMatchers/ASTMatchersTraversalTest.cpp
@@ -1630,6 +1630,84 @@
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) {
Index: clang/include/clang/ASTMatchers/ASTMatchersInternal.h
===
--- clang/include/clang/ASTMatchers/ASTMatchersInternal.h
+++ clang/include/clang/ASTMatchers/ASTMatchersInternal.h
@@ -2039,7 +2039,8 @@
 template <>
 inline Optional
 equivalentUnaryOperator(const CXXOperatorCallExpr ) {
-  if (Node.getNumArgs() != 1)
+  if (Node.getNumArgs() != 1 && Node.getOperator() != OO_PlusPlus &&
+  Node.getOperator() != OO_MinusMinus)
 return None;
   switch (Node.getOperator()) {
   default:
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits