From d87515fa113269e9c82ec3e60ccceb2114fd13b0 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 20 Oct 2016 00:55:15 +0000 Subject: [PATCH] PR26276: Fix detection of non-cast-expressions as operands of fold-expressions. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@284684 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/ExprCXX.h | 3 +++ lib/AST/ExprCXX.cpp | 16 ++++++++++++++++ lib/Sema/SemaTemplateVariadic.cpp | 4 +++- test/Parser/cxx1z-fold-expressions.cpp | 7 +++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/include/clang/AST/ExprCXX.h b/include/clang/AST/ExprCXX.h index af45b161f6..4778b6cc69 100644 --- a/include/clang/AST/ExprCXX.h +++ b/include/clang/AST/ExprCXX.h @@ -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 diff --git a/lib/AST/ExprCXX.cpp b/lib/AST/ExprCXX.cpp index 3efb5b1cbc..720a13a850 100644 --- a/lib/AST/ExprCXX.cpp +++ b/lib/AST/ExprCXX.cpp @@ -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; diff --git a/lib/Sema/SemaTemplateVariadic.cpp b/lib/Sema/SemaTemplateVariadic.cpp index b6bd6c03ff..c278639073 100644 --- a/lib/Sema/SemaTemplateVariadic.cpp +++ b/lib/Sema/SemaTemplateVariadic.cpp @@ -998,7 +998,9 @@ static void CheckFoldOperand(Sema &S, Expr *E) { return; E = E->IgnoreImpCasts(); - if (isa(E) || isa(E)) { + auto *OCE = dyn_cast(E); + if ((OCE && OCE->isInfixBinaryOp()) || isa(E) || + isa(E)) { S.Diag(E->getExprLoc(), diag::err_fold_expression_bad_operand) << E->getSourceRange() << FixItHint::CreateInsertion(E->getLocStart(), "(") diff --git a/test/Parser/cxx1z-fold-expressions.cpp b/test/Parser/cxx1z-fold-expressions.cpp index a908311ea3..0306385832 100644 --- a/test/Parser/cxx1z-fold-expressions.cpp +++ b/test/Parser/cxx1z-fold-expressions.cpp @@ -9,12 +9,19 @@ int k1 = (1 + ... + 2); // expected-error {{does not contain any unexpanded para 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 void bad1() { (N + ... + N); } // expected-error {{unexpanded parameter packs in both operands}} // FIXME: it would be reasonable to support this as an extension. template void bad2() { (2 * N + ... + 1); } // expected-error {{expression not permitted as operand}} template void bad3() { (2 + N * ... * 1); } // expected-error {{expression not permitted as operand}} template void bad4(int (&...x)[N]) { (N + M * ... * 1); } // expected-error {{expression not permitted as operand}} template void fixed4(int (&...x)[N]) { ((N + M) * ... * 1); } +template void bad4a(T ...t) { (t * 2 + ... + 1); } // expected-error {{expression not permitted as operand}} +template void bad4b() { (A(0) + A(N) + ...); } // expected-error {{expression not permitted as operand}} +template void bad4c() { (A(0) - A(N) + ...); } // expected-error {{expression not permitted as operand}} +template void bad4d() { (A(0)(A(0)) + ... + A(0)[A(N)]); } // Parens are mandatory. template void bad5() { N + ...; } // expected-error {{expected expression}} expected-error +{{}} -- 2.40.0