[cfe-commits] r38881 - in /cfe/cfe/trunk: Parse/ParseExpr.cpp test/Parser/statements.c

sabre at cs.uiuc.edu sabre at cs.uiuc.edu
Wed Jul 11 09:25:27 PDT 2007


Author: sabre
Date: Wed Jul 11 11:25:27 2007
New Revision: 38881

URL: http://llvm.org/viewvc/llvm-project?rev=38881&view=rev
Log:
Fix parsing of assignment expressions and handling of right-associative
things.

Modified:
    cfe/cfe/trunk/Parse/ParseExpr.cpp
    cfe/cfe/trunk/test/Parser/statements.c

Modified: cfe/cfe/trunk/Parse/ParseExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/Parse/ParseExpr.cpp?rev=38881&r1=38880&r2=38881&view=diff

==============================================================================
--- cfe/cfe/trunk/Parse/ParseExpr.cpp (original)
+++ cfe/cfe/trunk/Parse/ParseExpr.cpp Wed Jul 11 11:25:27 2007
@@ -252,16 +252,20 @@
     // operator immediately to the right of the RHS.
     unsigned ThisPrec = NextTokPrec;
     NextTokPrec = getBinOpPrecedence(Tok.getKind());
-    
-    // FIXME: ASSIGNMENT IS RIGHT ASSOCIATIVE.
-    // FIXME: do we want to handle assignment here??
-    bool isRightAssoc = OpToken.getKind() == tok::question;
+
+    // Assignment and conditional expressions are right-associative.
+    bool isRightAssoc = NextTokPrec == prec::Conditional ||
+                        NextTokPrec == prec::Assignment;
 
     // Get the precedence of the operator to the right of the RHS.  If it binds
     // more tightly with RHS than we do, evaluate it completely first.
     if (ThisPrec < NextTokPrec ||
         (ThisPrec == NextTokPrec && isRightAssoc)) {
-      RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec+1);
+      // 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
+      // A=(B=(C=D)), where each paren is a level of recursion here.
+      RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
       if (RHS.isInvalid) return RHS;
 
       NextTokPrec = getBinOpPrecedence(Tok.getKind());

Modified: cfe/cfe/trunk/test/Parser/statements.c
URL: http://llvm.org/viewvc/llvm-project/cfe/cfe/trunk/test/Parser/statements.c?rev=38881&r1=38880&r2=38881&view=diff

==============================================================================
--- cfe/cfe/trunk/test/Parser/statements.c (original)
+++ cfe/cfe/trunk/test/Parser/statements.c Wed Jul 11 11:25:27 2007
@@ -62,3 +62,7 @@
 int test7(int a, int b) {
   return a ? a,b : a;
 }
+
+int test8(int a, int b) {
+  return a = b = c;
+}





More information about the cfe-commits mailing list