]> granicus.if.org Git - clang/commitdiff
Fix for PR3418: make sure to handle the RHS of expressions starting with
authorEli Friedman <eli.friedman@gmail.com>
Tue, 27 Jan 2009 08:43:38 +0000 (08:43 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Tue, 27 Jan 2009 08:43:38 +0000 (08:43 +0000)
__extension__.  This sort of construct shows up in the gcc source code.

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

include/clang/Parse/Parser.h
lib/Parse/ParseExpr.cpp
lib/Parse/ParseStmt.cpp
test/Parser/expressions.c

index df8646df796eaede28e13ecbf93cd563f4eeaaf6..cf479f000e4c3566f6cd081332edcc8cef91cb46 100644 (file)
@@ -567,6 +567,8 @@ private:
 
   OwningExprResult ParseExpressionWithLeadingAt(SourceLocation AtLoc);
 
+  OwningExprResult ParseExpressionWithLeadingExtension(SourceLocation ExtLoc);
+
   OwningExprResult ParseRHSOfBinaryExpression(OwningExprResult LHS,
                                               unsigned MinPrec);
   OwningExprResult ParseCastExpression(bool isUnaryExpression);
index 38518557a3f8e7f61efc687d3c60ba8864ed90a7..1bec279890d41c7ca6c679add06d03aed54b67ae 100644 (file)
@@ -192,6 +192,25 @@ Parser::ParseExpressionWithLeadingAt(SourceLocation AtLoc) {
   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() {
index f7014991bf8c1022cc2c5e12b128f01038284797..5ff09052f58d5edc5f1c3ebf00afd94c9e44a982 100644 (file)
@@ -366,6 +366,7 @@ Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
       // __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 @@ Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
         // 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);
index 3b47260c32f2e69d8a179210868f9de4c98a9ec9..29621f1975e6bb0ad500a1838ab998463d68af64 100644 (file)
@@ -36,4 +36,8 @@ void test_sizeof(){
         sizeof(arr)[0];
 }
 
+// PR3418
+int test_leading_extension() {
+  __extension__ (*(char*)0) = 1;
+}