eduucaldas updated this revision to Diff 275316.
eduucaldas added a comment.
Herald added a subscriber: mgorny.

Nits


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D82954

Files:
  clang/include/clang/AST/ExprCXX.h
  clang/lib/AST/ExprCXX.cpp
  clang/unittests/Tooling/CMakeLists.txt
  clang/unittests/Tooling/CXXOperatorCallExprTest.cpp

Index: clang/unittests/Tooling/CXXOperatorCallExprTest.cpp
===================================================================
--- /dev/null
+++ clang/unittests/Tooling/CXXOperatorCallExprTest.cpp
@@ -0,0 +1,77 @@
+//===- unittests/Tooling/CXXOperatorCallExprTest.cpp ----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Unit tests for the predicates in CXXOperatorCallExpr.
+//
+//===----------------------------------------------------------------------===//
+
+#include "TestVisitor.h"
+#include "gtest/gtest.h"
+
+namespace clang {
+namespace {
+
+TEST(CXXOperatorPredicatesTest, InfixBinaryOp) {
+  const std::string Code = R"cpp(
+  struct X{
+    friend X operator+(X, X);
+  };
+  void test(X x){
+    x + x;
+  }
+  )cpp";
+
+  struct Visitor : TestVisitor<Visitor> {
+    bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
+      EXPECT_TRUE(E->isInfixBinaryOp());
+      return true;
+    }
+  } visitor;
+  visitor.runOver(Code);
+}
+
+TEST(CXXOperatorPredicatesTest, CallLikeOp) {
+  const std::string Code = R"cpp(
+  struct X{
+    int operator[](int idx);
+  };
+  void test(X x){
+    x[1];
+  }
+  )cpp";
+  struct Visitor : TestVisitor<Visitor> {
+    bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
+      EXPECT_FALSE(E->isInfixBinaryOp());
+      return true;
+    }
+  } visitor;
+
+  visitor.runOver(Code);
+}
+
+TEST(CXXOperatorPredicatesTest, PostfixUnaryOp) {
+  const std::string Code = R"cpp(
+  struct X{
+    X operator++(int);
+  };
+  void test(X x){
+    x++;
+  }
+  )cpp";
+
+  struct Visitor : TestVisitor<Visitor> {
+    bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
+      EXPECT_FALSE(E->isInfixBinaryOp());
+      return true;
+    }
+  } visitor;
+
+  visitor.runOver(Code);
+}
+} // namespace
+} // namespace clang
Index: clang/unittests/Tooling/CMakeLists.txt
===================================================================
--- clang/unittests/Tooling/CMakeLists.txt
+++ clang/unittests/Tooling/CMakeLists.txt
@@ -18,6 +18,7 @@
   CastExprTest.cpp
   CommentHandlerTest.cpp
   CompilationDatabaseTest.cpp
+  CXXOperatorCallExprTest.cpp
   DependencyScannerTest.cpp
   DiagnosticsYamlTest.cpp
   ExecutionTest.cpp
Index: clang/lib/AST/ExprCXX.cpp
===================================================================
--- clang/lib/AST/ExprCXX.cpp
+++ clang/lib/AST/ExprCXX.cpp
@@ -46,15 +46,17 @@
 //===----------------------------------------------------------------------===//
 
 bool CXXOperatorCallExpr::isInfixBinaryOp() const {
-  // An infix binary operator is any operator with two arguments other than
-  // operator() and operator[]. Note that none of these operators can have
-  // default arguments, so it suffices to check the number of argument
-  // expressions.
   if (getNumArgs() != 2)
     return false;
-
   switch (getOperator()) {
-  case OO_Call: case OO_Subscript:
+  // operator() may have two arguments, but it's not a binary operator
+  case OO_Call:
+  // operator[] takes two arguments but it's not infix
+  case OO_Subscript:
+  // Postfix unary operators (++ and --) take 2 arguments to differ from their
+  // prefix counterparts
+  case OO_PlusPlus:
+  case OO_MinusMinus:
     return false;
   default:
     return true;
Index: clang/include/clang/AST/ExprCXX.h
===================================================================
--- clang/include/clang/AST/ExprCXX.h
+++ clang/include/clang/AST/ExprCXX.h
@@ -121,6 +121,7 @@
            Opc == OO_GreaterGreaterEqual || Opc == OO_AmpEqual ||
            Opc == OO_CaretEqual || Opc == OO_PipeEqual;
   }
+
   bool isAssignmentOp() const { return isAssignmentOp(getOperator()); }
 
   static bool isComparisonOp(OverloadedOperatorKind Opc) {
@@ -137,9 +138,9 @@
       return false;
     }
   }
+
   bool isComparisonOp() const { return isComparisonOp(getOperator()); }
 
-  /// Is this written as an infix binary operator?
   bool isInfixBinaryOp() const;
 
   /// Returns the location of the operator symbol in the expression.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to