r284684 - PR26276: Fix detection of non-cast-expressions as operands of fold-expressions.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 19 17:55:16 PDT 2016


Author: rsmith
Date: Wed Oct 19 19:55:15 2016
New Revision: 284684

URL: http://llvm.org/viewvc/llvm-project?rev=284684&view=rev
Log:
PR26276: Fix detection of non-cast-expressions as operands of fold-expressions.

Modified:
    cfe/trunk/include/clang/AST/ExprCXX.h
    cfe/trunk/lib/AST/ExprCXX.cpp
    cfe/trunk/lib/Sema/SemaTemplateVariadic.cpp
    cfe/trunk/test/Parser/cxx1z-fold-expressions.cpp

Modified: cfe/trunk/include/clang/AST/ExprCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ExprCXX.h?rev=284684&r1=284683&r2=284684&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ExprCXX.h (original)
+++ cfe/trunk/include/clang/AST/ExprCXX.h Wed Oct 19 19:55:15 2016
@@ -86,6 +86,9 @@ public:
   }
   bool isAssignmentOp() const { return isAssignmentOp(getOperator()); }
 
+  /// \brief Is this written as an infix binary operator?
+  bool isInfixBinaryOp() const;
+
   /// \brief Returns the location of the operator symbol in the expression.
   ///
   /// When \c getOperator()==OO_Call, this is the location of the right

Modified: cfe/trunk/lib/AST/ExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprCXX.cpp?rev=284684&r1=284683&r2=284684&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprCXX.cpp (original)
+++ cfe/trunk/lib/AST/ExprCXX.cpp Wed Oct 19 19:55:15 2016
@@ -25,6 +25,22 @@ using namespace clang;
 //  Child Iterators for iterating over subexpressions/substatements
 //===----------------------------------------------------------------------===//
 
+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:
+    return false;
+  default:
+    return true;
+  }
+}
+
 bool CXXTypeidExpr::isPotentiallyEvaluated() const {
   if (isTypeOperand())
     return false;

Modified: cfe/trunk/lib/Sema/SemaTemplateVariadic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateVariadic.cpp?rev=284684&r1=284683&r2=284684&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateVariadic.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateVariadic.cpp Wed Oct 19 19:55:15 2016
@@ -998,7 +998,9 @@ static void CheckFoldOperand(Sema &S, Ex
     return;
 
   E = E->IgnoreImpCasts();
-  if (isa<BinaryOperator>(E) || isa<AbstractConditionalOperator>(E)) {
+  auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
+  if ((OCE && OCE->isInfixBinaryOp()) || isa<BinaryOperator>(E) ||
+      isa<AbstractConditionalOperator>(E)) {
     S.Diag(E->getExprLoc(), diag::err_fold_expression_bad_operand)
         << E->getSourceRange()
         << FixItHint::CreateInsertion(E->getLocStart(), "(")

Modified: cfe/trunk/test/Parser/cxx1z-fold-expressions.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/cxx1z-fold-expressions.cpp?rev=284684&r1=284683&r2=284684&view=diff
==============================================================================
--- cfe/trunk/test/Parser/cxx1z-fold-expressions.cpp (original)
+++ cfe/trunk/test/Parser/cxx1z-fold-expressions.cpp Wed Oct 19 19:55:15 2016
@@ -9,12 +9,19 @@ int k1 = (1 + ... + 2); // expected-erro
 int k2 = (1 + ...); // expected-error {{does not contain any unexpanded parameter packs}}
 int k3 = (... + 2); // expected-error {{does not contain any unexpanded parameter packs}}
 
+struct A { A(int); friend A operator+(A, A); A operator-(A); A operator()(A); A operator[](A); };
+A operator*(A, A);
+
 template<int ...N> void bad1() { (N + ... + N); } // expected-error {{unexpanded parameter packs in both operands}}
 // FIXME: it would be reasonable to support this as an extension.
 template<int ...N> void bad2() { (2 * N + ... + 1); } // expected-error {{expression not permitted as operand}}
 template<int ...N> void bad3() { (2 + N * ... * 1); } // expected-error {{expression not permitted as operand}}
 template<int ...N, int ...M> void bad4(int (&...x)[N]) { (N + M * ... * 1); } // expected-error {{expression not permitted as operand}}
 template<int ...N, int ...M> void fixed4(int (&...x)[N]) { ((N + M) * ... * 1); }
+template<typename ...T> void bad4a(T ...t) { (t * 2 + ... + 1); } // expected-error {{expression not permitted as operand}}
+template<int ...N> void bad4b() { (A(0) + A(N) + ...); } // expected-error {{expression not permitted as operand}}
+template<int ...N> void bad4c() { (A(0) - A(N) + ...); } // expected-error {{expression not permitted as operand}}
+template<int ...N> void bad4d() { (A(0)(A(0)) + ... + A(0)[A(N)]); }
 
 // Parens are mandatory.
 template<int ...N> void bad5() { N + ...; } // expected-error {{expected expression}} expected-error +{{}}




More information about the cfe-commits mailing list