[cfe-commits] r63100 - in /cfe/trunk: include/clang/Parse/Parser.h lib/Parse/ParseExpr.cpp lib/Parse/ParseStmt.cpp test/Parser/expressions.c

Eli Friedman eli.friedman at gmail.com
Tue Jan 27 00:43:38 PST 2009


Author: efriedma
Date: Tue Jan 27 02:43:38 2009
New Revision: 63100

URL: http://llvm.org/viewvc/llvm-project?rev=63100&view=rev
Log:
Fix for PR3418: make sure to handle the RHS of expressions starting with 
__extension__.  This sort of construct shows up in the gcc source code.


Modified:
    cfe/trunk/include/clang/Parse/Parser.h
    cfe/trunk/lib/Parse/ParseExpr.cpp
    cfe/trunk/lib/Parse/ParseStmt.cpp
    cfe/trunk/test/Parser/expressions.c

Modified: cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?rev=63100&r1=63099&r2=63100&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/trunk/include/clang/Parse/Parser.h Tue Jan 27 02:43:38 2009
@@ -567,6 +567,8 @@
 
   OwningExprResult ParseExpressionWithLeadingAt(SourceLocation AtLoc);
 
+  OwningExprResult ParseExpressionWithLeadingExtension(SourceLocation ExtLoc);
+
   OwningExprResult ParseRHSOfBinaryExpression(OwningExprResult LHS,
                                               unsigned MinPrec);
   OwningExprResult ParseCastExpression(bool isUnaryExpression);

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

==============================================================================
--- cfe/trunk/lib/Parse/ParseExpr.cpp (original)
+++ cfe/trunk/lib/Parse/ParseExpr.cpp Tue Jan 27 02:43:38 2009
@@ -192,6 +192,25 @@
   return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
 }
 
+/// This routine is called when a leading '__extension__' is seen and
+/// consumed.  This is necessary because the token gets consumed in the
+/// process of disambiguating between an expression and a declaration.
+Parser::OwningExprResult
+Parser::ParseExpressionWithLeadingExtension(SourceLocation ExtLoc) {
+  // FIXME: The handling for throw is almost certainly wrong.
+  if (Tok.is(tok::kw_throw))
+    return ParseThrowExpression();
+
+  OwningExprResult LHS(ParseCastExpression(false));
+  if (LHS.isInvalid()) return move(LHS);
+
+  LHS = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
+                             move_arg(LHS));
+  if (LHS.isInvalid()) return move(LHS);
+
+  return ParseRHSOfBinaryExpression(move(LHS), prec::Comma);
+}
+
 /// ParseAssignmentExpression - Parse an expr that doesn't include commas.
 ///
 Parser::OwningExprResult Parser::ParseAssignmentExpression() {

Modified: cfe/trunk/lib/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseStmt.cpp?rev=63100&r1=63099&r2=63100&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/ParseStmt.cpp (original)
+++ cfe/trunk/lib/Parse/ParseStmt.cpp Tue Jan 27 02:43:38 2009
@@ -366,6 +366,7 @@
       // __extension__ can start declarations and it can also be a unary
       // operator for expressions.  Consume multiple __extension__ markers here
       // until we can determine which is which.
+      // FIXME: This loses extension expressions in the AST!
       SourceLocation ExtLoc = ConsumeToken();
       while (Tok.is(tok::kw___extension__))
         ConsumeToken();
@@ -381,21 +382,14 @@
         // FIXME: Pass in the right location for the end of the declstmt.
         R = Actions.ActOnDeclStmt(Res, DeclStart, DeclStart);
       } else {
-        // Otherwise this was a unary __extension__ marker.  Parse the
-        // subexpression and add the __extension__ unary op. 
-        OwningExprResult Res(ParseCastExpression(false));
+        // Otherwise this was a unary __extension__ marker.
+        OwningExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
 
         if (Res.isInvalid()) {
           SkipUntil(tok::semi);
           continue;
         }
 
-        // Add the __extension__ node to the AST.
-        Res = Actions.ActOnUnaryOp(CurScope, ExtLoc, tok::kw___extension__,
-                                   move_arg(Res));
-        if (Res.isInvalid())
-          continue;
-
         // Eat the semicolon at the end of stmt and convert the expr into a
         // statement.
         ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);

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

==============================================================================
--- cfe/trunk/test/Parser/expressions.c (original)
+++ cfe/trunk/test/Parser/expressions.c Tue Jan 27 02:43:38 2009
@@ -36,4 +36,8 @@
         sizeof(arr)[0];
 }
 
+// PR3418
+int test_leading_extension() {
+  __extension__ (*(char*)0) = 1;
+}
 





More information about the cfe-commits mailing list