]> granicus.if.org Git - clang/commitdiff
Reject 'a = {0} = {0}' rather than parsing it as '(a = {0}) = {0}'. Also
authorRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 1 Mar 2012 02:59:17 +0000 (02:59 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 1 Mar 2012 02:59:17 +0000 (02:59 +0000)
improve the diagnostics for some attempts to use initializer lists in
expressions.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@151794 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticParseKinds.td
lib/Parse/ParseExpr.cpp
test/CXX/expr/expr.ass/p9-cxx11.cpp

index 95288acb86c9444613cab7da8f837cbf4bc2f720..a97e7d3a229949b417748c4dbd0d2fbeabe0a933 100644 (file)
@@ -229,6 +229,8 @@ def ext_generalized_initializer_lists : ExtWarn<
 def warn_cxx98_compat_generalized_initializer_lists : Warning<
   "generalized initializer lists are incompatible with C++98">,
   InGroup<CXX98Compat>, DefaultIgnore;
+def err_init_list_bin_op : Error<"initializer list cannot be used on the "
+  "%select{left|right}0 hand side of operator '%1'">;
 def warn_cxx98_compat_trailing_return_type : Warning<
   "trailing return types are incompatible with C++98">,
   InGroup<CXX98Compat>, DefaultIgnore;
index 2f30e6bd2b04bf9351db97e2aa6160cf1d656751..65949edf41385930b3744f9ffa43bc23996b65a1 100644 (file)
@@ -282,6 +282,12 @@ Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
     Token OpToken = Tok;
     ConsumeToken();
 
+    if (!LHS.isInvalid() && isa<InitListExpr>(LHS.get())) {
+      Diag(OpToken, diag::err_init_list_bin_op)
+        << /*LHS*/0 << PP.getSpelling(OpToken) << LHS.get()->getSourceRange();
+      LHS = ExprError();
+    }
+
     // Special case handling for the ternary operator.
     ExprResult TernaryMiddle(true);
     if (NextTokPrec == prec::Conditional) {
@@ -353,22 +359,16 @@ Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
     // Therefore we need some special-casing here.
     // Also note that the third operand of the conditional operator is
     // an assignment-expression in C++, and in C++11, we can have a
-    // braced-init-list on the RHS of an assignment.
+    // braced-init-list on the RHS of an assignment. For better diagnostics,
+    // parse as if we were allowed braced-init-lists everywhere, and check that
+    // they only appear on the RHS of assignments later.
     ExprResult RHS;
-    if (getLang().CPlusPlus0x && MinPrec == prec::Assignment &&
-        Tok.is(tok::l_brace)) {
-      Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
+    if (getLang().CPlusPlus0x && Tok.is(tok::l_brace))
       RHS = ParseBraceInitializer();
-      if (LHS.isInvalid() || RHS.isInvalid())
-        return ExprError();
-      // A braced-init-list can never be followed by more operators.
-      return Actions.ActOnBinOp(getCurScope(), OpToken.getLocation(),
-                                OpToken.getKind(), LHS.take(), RHS.take());
-    } else if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional) {
+    else if (getLang().CPlusPlus && NextTokPrec <= prec::Conditional)
       RHS = ParseAssignmentExpression();
-    } else {
+    else
       RHS = ParseCastExpression(false);
-    }
 
     if (RHS.isInvalid())
       LHS = ExprError();
@@ -387,6 +387,11 @@ Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
     // more tightly with RHS than we do, evaluate it completely first.
     if (ThisPrec < NextTokPrec ||
         (ThisPrec == NextTokPrec && isRightAssoc)) {
+      if (!LHS.isInvalid() && isa<InitListExpr>(LHS.get())) {
+        Diag(OpToken, diag::err_init_list_bin_op)
+          << /*LHS*/0 << PP.getSpelling(OpToken) << LHS.get()->getSourceRange();
+        LHS = ExprError();
+      }
       // If this is left-associative, only parse things on the RHS that bind
       // more tightly than the current operator.  If it is left-associative, it
       // is okay, to bind exactly as tightly.  For example, compile A=B=C=D as
@@ -403,6 +408,17 @@ Parser::ParseRHSOfBinaryExpression(ExprResult LHS, prec::Level MinPrec) {
     }
     assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
 
+    if (!RHS.isInvalid() && isa<InitListExpr>(RHS.get())) {
+      if (ThisPrec == prec::Assignment) {
+        Diag(OpToken, diag::warn_cxx98_compat_generalized_initializer_lists)
+          << RHS.get()->getSourceRange();
+      } else {
+        Diag(OpToken, diag::err_init_list_bin_op)
+          << /*RHS*/1 << PP.getSpelling(OpToken) << RHS.get()->getSourceRange();
+        LHS = ExprError();
+      }
+    }
+
     if (!LHS.isInvalid()) {
       // Combine the LHS and RHS into the LHS (e.g. build AST).
       if (TernaryMiddle.isInvalid()) {
index bd1603d6ab6773142403f932377b15f00d1ed700..206c82c985c74d367bf98abf6e8b59ef3aa4c5f2 100644 (file)
@@ -13,7 +13,10 @@ void std_example() {
 
   int a, b;
   a = b = { 1 };
-  a = { 1 } = b;
+  a = { 1 } = b; // expected-error {{initializer list cannot be used on the left hand side of operator '='}}
+  a = a + { 4 }; // expected-error {{initializer list cannot be used on the right hand side of operator '+'}}
+  a = { 3 } * { 4 }; // expected-error {{initializer list cannot be used on the left hand side of operator '*'}} \
+                        expected-error {{initializer list cannot be used on the right hand side of operator '*'}}
 }
 
 struct S {
@@ -27,5 +30,5 @@ struct T {
 static_assert((T() = {4, 9}) == 4, "");
 static_assert((T() += {4, 9}) == 9, "");
 
-int k1 = T() = { 1, 2 } = { 3, 4 }; // expected-error {{expected ';'}}
-int k2 = T() = { 1, 2 } + 1; // expected-error {{expected ';'}}
+int k1 = T() = { 1, 2 } = { 3, 4 }; // expected-error {{initializer list cannot be used on the left hand side of operator '='}}
+int k2 = T() = { 1, 2 } + 1; // expected-error {{initializer list cannot be used on the left hand side of operator '+'}}