[cfe-commits] r40032 - in /cfe/trunk: Parse/ParseExpr.cpp Parse/ParseInit.cpp Sema/Sema.h Sema/SemaExpr.cpp include/clang/Parse/Action.h test/Parser/compound_literal.c

Steve Naroff snaroff at apple.com
Wed Jul 18 18:06:57 PDT 2007


Author: snaroff
Date: Wed Jul 18 20:06:55 2007
New Revision: 40032

URL: http://llvm.org/viewvc/llvm-project?rev=40032&view=rev
Log:

Work towards fixing crasher with compound literals...

Before this commit, we crashed in ParseBinOp...

[dylan:~/llvm/tools/clang] admin% ../../Debug/bin/clang -parse-ast-check compound_literal.c
SemaExpr.cpp:1298: failed assertion `(rhs != 0) && "ParseBinOp(): missing right expression"'

With this commit, we still crash in the newly added action ParseCompoundLiteral (which is progress:-)

[dylan:~/llvm/tools/clang] admin% ../../Debug/bin/clang -parse-ast-check compound_literal.c
SemaExpr.cpp:478: failed assertion `(Op != 0) && "ParseCompoundLiteral(): missing expression"'

The crash go away once the actions return AST nodes. I will do this in a separate commit.


Added:
    cfe/trunk/test/Parser/compound_literal.c
Modified:
    cfe/trunk/Parse/ParseExpr.cpp
    cfe/trunk/Parse/ParseInit.cpp
    cfe/trunk/Sema/Sema.h
    cfe/trunk/Sema/SemaExpr.cpp
    cfe/trunk/include/clang/Parse/Action.h

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

==============================================================================
--- cfe/trunk/Parse/ParseExpr.cpp (original)
+++ cfe/trunk/Parse/ParseExpr.cpp Wed Jul 18 20:06:55 2007
@@ -880,7 +880,8 @@
         Diag(OpenLoc, diag::ext_c99_compound_literal);
       Result = ParseInitializer();
       ExprType = CompoundLiteral;
-      // TODO: Build AST for compound literal.
+      if (!Result.isInvalid)
+        return Actions.ParseCompoundLiteral(OpenLoc, Ty, RParenLoc, Result.Val);
     } else if (ExprType == CastExpr) {
       // Note that this doesn't parse the subsequence cast-expression, it just
       // returns the parsed type to the callee.

Modified: cfe/trunk/Parse/ParseInit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Parse/ParseInit.cpp?rev=40032&r1=40031&r2=40032&view=diff

==============================================================================
--- cfe/trunk/Parse/ParseInit.cpp (original)
+++ cfe/trunk/Parse/ParseInit.cpp Wed Jul 18 20:06:55 2007
@@ -13,6 +13,7 @@
 
 #include "clang/Parse/Parser.h"
 #include "clang/Basic/Diagnostic.h"
+#include "llvm/ADT/SmallString.h"
 using namespace clang;
 
 
@@ -152,37 +153,47 @@
   
   // We support empty initializers, but tell the user that they aren't using
   // C99-clean code.
-  if (Tok.getKind() == tok::r_brace)
+  if (Tok.getKind() == tok::r_brace) {
     Diag(LBraceLoc, diag::ext_gnu_empty_initializer);
-  else {
-    while (1) {
-      // Parse: designation[opt] initializer
-      
-      // If we know that this cannot be a designation, just parse the nested
-      // initializer directly.
-      ExprResult SubElt;
-      if (!MayBeDesignationStart(Tok.getKind()))
-        SubElt = ParseInitializer();
-      else
-        SubElt = ParseInitializerWithPotentialDesignator();
-      
-      // If we couldn't parse the subelement, bail out.
-      if (SubElt.isInvalid) {
-        SkipUntil(tok::r_brace);
-        return SubElt;
-      }
+    // Match the '}'.
+    return Actions.ParseInitList(LBraceLoc, 0, 0, ConsumeBrace());
+  }
+  llvm::SmallVector<ExprTy*, 8> InitExprs;
+  bool InitExprsOk = true;
+  
+  while (1) {
+    // Parse: designation[opt] initializer
     
-      // If we don't have a comma continued list, we're done.
-      if (Tok.getKind() != tok::comma) break;
-      ConsumeToken();
+    // If we know that this cannot be a designation, just parse the nested
+    // initializer directly.
+    ExprResult SubElt;
+    if (!MayBeDesignationStart(Tok.getKind()))
+      SubElt = ParseInitializer();
+    else
+      SubElt = ParseInitializerWithPotentialDesignator();
+
+    // If we couldn't parse the subelement, bail out.
+    if (SubElt.isInvalid) {
+      InitExprsOk = false;
+      SkipUntil(tok::r_brace);
+      break;
+    } else
+      InitExprs.push_back(SubElt.Val);
       
-      // Handle trailing comma.
-      if (Tok.getKind() == tok::r_brace) break;
-    }    
+    // If we don't have a comma continued list, we're done.
+    if (Tok.getKind() != tok::comma) break;
+    
+    // FIXME: save comma locations.
+    ConsumeToken();
+    
+    // Handle trailing comma.
+    if (Tok.getKind() == tok::r_brace) break;
   }
-  
+  if (InitExprsOk && Tok.getKind() == tok::r_brace)
+    return Actions.ParseInitList(LBraceLoc, &InitExprs[0], InitExprs.size(), 
+                                 ConsumeBrace());
   // Match the '}'.
   MatchRHSPunctuation(tok::r_brace, LBraceLoc);
-  return ExprResult(false);
+  return ExprResult(true); // an error occurred.
 }
 

Modified: cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/Sema.h?rev=40032&r1=40031&r2=40032&view=diff

==============================================================================
--- cfe/trunk/Sema/Sema.h (original)
+++ cfe/trunk/Sema/Sema.h Wed Jul 18 20:06:55 2007
@@ -245,7 +245,14 @@
   
   virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
                                    SourceLocation RParenLoc, ExprTy *Op);
+                                   
+  virtual ExprResult ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
+                                          SourceLocation RParenLoc, ExprTy *Op);
   
+  virtual ExprResult ParseInitList(SourceLocation LParenLoc, 
+                                   ExprTy **InitList, unsigned NumInit,
+                                   SourceLocation RParenLoc);
+                                   
   virtual ExprResult ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
                                 ExprTy *LHS,ExprTy *RHS);
   

Modified: cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaExpr.cpp?rev=40032&r1=40031&r2=40032&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Wed Jul 18 20:06:55 2007
@@ -471,6 +471,28 @@
 }
 
 Action::ExprResult Sema::
+ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
+                     SourceLocation RParenLoc, ExprTy *Op) {
+  assert((Ty != 0) && "ParseCompoundLiteral(): missing type");
+  QualType literalType = QualType::getFromOpaquePtr(Ty);
+  assert((Op != 0) && "ParseCompoundLiteral(): missing expression");
+  Expr *literalExpr = static_cast<Expr*>(Op);
+  
+  // FIXME: add semantic analysis (C99 6.5.2.5).
+  return false; // FIXME: instantiate a CompoundLiteralExpr
+}
+
+Action::ExprResult Sema::
+ParseInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit,
+              SourceLocation RParenLoc) {
+  // FIXME: add semantic analysis (C99 6.7.8). This involves 
+  // knowledge of the object being intialized. As a result, the code for
+  // doing the semantic analysis will likely be located elsewhere (i.e. in 
+  // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral).
+  return false; // FIXME instantiate an InitListExpr.
+}
+
+Action::ExprResult Sema::
 ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
               SourceLocation RParenLoc, ExprTy *Op) {
   assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr");

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

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Wed Jul 18 20:06:55 2007
@@ -332,6 +332,15 @@
     return 0;
   }
   
+  virtual ExprResult ParseCompoundLiteral(SourceLocation LParen, TypeTy *Ty,
+                                          SourceLocation RParen, ExprTy *Op) {
+    return 0;
+  }
+  virtual ExprResult ParseInitList(SourceLocation LParenLoc,
+                                   ExprTy **InitList, unsigned NumInit,
+                                   SourceLocation RParenLoc) {
+    return 0;
+  }
   virtual ExprResult ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
                                    SourceLocation RParenLoc, ExprTy *Op) {
     return 0;

Added: cfe/trunk/test/Parser/compound_literal.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/compound_literal.c?rev=40032&view=auto

==============================================================================
--- cfe/trunk/test/Parser/compound_literal.c (added)
+++ cfe/trunk/test/Parser/compound_literal.c Wed Jul 18 20:06:55 2007
@@ -0,0 +1,5 @@
+// RUN: clang -parse-ast-check %s
+int main() {
+  char *s;
+  s = (char []){"whatever"}; 
+}





More information about the cfe-commits mailing list