[cfe-commits] r69373 - in /cfe/trunk: include/clang/AST/Expr.h include/clang/Frontend/PCHBitCodes.h lib/Frontend/PCHReader.cpp lib/Frontend/PCHWriter.cpp test/PCH/blocks.c test/PCH/blocks.h test/PCH/stmts.c

Douglas Gregor dgregor at apple.com
Fri Apr 17 12:21:43 PDT 2009


Author: dgregor
Date: Fri Apr 17 14:21:43 2009
New Revision: 69373

URL: http://llvm.org/viewvc/llvm-project?rev=69373&view=rev
Log:
PCH support for blocks

Added:
    cfe/trunk/test/PCH/blocks.c
    cfe/trunk/test/PCH/blocks.h
Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/include/clang/Frontend/PCHBitCodes.h
    cfe/trunk/lib/Frontend/PCHReader.cpp
    cfe/trunk/lib/Frontend/PCHWriter.cpp
    cfe/trunk/test/PCH/stmts.c

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=69373&r1=69372&r2=69373&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Fri Apr 17 14:21:43 2009
@@ -2493,9 +2493,13 @@
     : Expr(BlockExprClass, ty), 
       TheBlock(BD), HasBlockDeclRefExprs(hasBlockDeclRefExprs) {}
 
+  /// \brief Build an empty block expression.
+  explicit BlockExpr(EmptyShell Empty) : Expr(BlockExprClass, Empty) { }
+
   const BlockDecl *getBlockDecl() const { return TheBlock; }
   BlockDecl *getBlockDecl() { return TheBlock; }
-  
+  void setBlockDecl(BlockDecl *BD) { TheBlock = BD; }
+
   // Convenience functions for probing the underlying BlockDecl.
   SourceLocation getCaretLocation() const;
   const Stmt *getBody() const;
@@ -2511,6 +2515,7 @@
   /// hasBlockDeclRefExprs - Return true iff the block has BlockDeclRefExpr
   /// contained inside.
   bool hasBlockDeclRefExprs() const { return HasBlockDeclRefExprs; }
+  void setHasBlockDeclRefExprs(bool BDRE) { HasBlockDeclRefExprs = BDRE; }
 
   static bool classof(const Stmt *T) { 
     return T->getStmtClass() == BlockExprClass;

Modified: cfe/trunk/include/clang/Frontend/PCHBitCodes.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/PCHBitCodes.h?rev=69373&r1=69372&r2=69373&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/PCHBitCodes.h (original)
+++ cfe/trunk/include/clang/Frontend/PCHBitCodes.h Fri Apr 17 14:21:43 2009
@@ -468,7 +468,9 @@
       EXPR_GNU_NULL,
       /// \brief A ShuffleVectorExpr record.
       EXPR_SHUFFLE_VECTOR,
-      /// FIXME: BlockExpr
+      /// \brief BlockExpr
+      EXPR_BLOCK,
+      /// \brief A BlockDeclRef record.
       EXPR_BLOCK_DECL_REF
     };
 

Modified: cfe/trunk/lib/Frontend/PCHReader.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReader.cpp?rev=69373&r1=69372&r2=69373&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReader.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReader.cpp Fri Apr 17 14:21:43 2009
@@ -197,6 +197,7 @@
 
 void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
   VisitDecl(BD);
+  BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt()));
   unsigned NumParams = Record[Idx++];
   llvm::SmallVector<ParmVarDecl *, 16> Params;
   Params.reserve(NumParams);
@@ -294,6 +295,7 @@
     unsigned VisitChooseExpr(ChooseExpr *E);
     unsigned VisitGNUNullExpr(GNUNullExpr *E);
     unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E);
+    unsigned VisitBlockExpr(BlockExpr *E);
     unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
   };
 }
@@ -792,6 +794,13 @@
   return NumExprs;
 }
 
+unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) {
+  VisitExpr(E);
+  E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++])));
+  E->setHasBlockDeclRefExprs(Record[Idx++]);
+  return 0;
+}
+
 unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
   VisitExpr(E);
   E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++])));
@@ -2388,6 +2397,10 @@
       S = new (Context) ShuffleVectorExpr(Empty);
       break;
       
+    case pch::EXPR_BLOCK:
+      S = new (Context) BlockExpr(Empty);
+      break;
+
     case pch::EXPR_BLOCK_DECL_REF:
       // FIXME: untested until we have statement and block support
       S = new (Context) BlockDeclRefExpr(Empty);

Modified: cfe/trunk/lib/Frontend/PCHWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriter.cpp?rev=69373&r1=69372&r2=69373&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriter.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriter.cpp Fri Apr 17 14:21:43 2009
@@ -403,7 +403,7 @@
 
 void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
   VisitDecl(D);
-  // FIXME: emit block body
+  Writer.AddStmt(D->getBody());
   Record.push_back(D->param_size());
   for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
        P != PEnd; ++P)
@@ -496,6 +496,7 @@
     void VisitChooseExpr(ChooseExpr *E);
     void VisitGNUNullExpr(GNUNullExpr *E);
     void VisitShuffleVectorExpr(ShuffleVectorExpr *E);
+    void VisitBlockExpr(BlockExpr *E);
     void VisitBlockDeclRefExpr(BlockDeclRefExpr *E);
   };
 }
@@ -940,6 +941,13 @@
   Code = pch::EXPR_SHUFFLE_VECTOR;
 }
 
+void PCHStmtWriter::VisitBlockExpr(BlockExpr *E) {
+  VisitExpr(E);
+  Writer.AddDeclRef(E->getBlockDecl(), Record);
+  Record.push_back(E->hasBlockDeclRefExprs());
+  Code = pch::EXPR_BLOCK;
+}
+
 void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) {
   VisitExpr(E);
   Writer.AddDeclRef(E->getDecl(), Record);

Added: cfe/trunk/test/PCH/blocks.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/blocks.c?rev=69373&view=auto

==============================================================================
--- cfe/trunk/test/PCH/blocks.c (added)
+++ cfe/trunk/test/PCH/blocks.c Fri Apr 17 14:21:43 2009
@@ -0,0 +1,12 @@
+// Test this without pch.
+// RUN: clang-cc -fblocks -include %S/blocks.h -fsyntax-only -ast-print -o - %s
+
+// Test with pch.
+// RUN: clang-cc -emit-pch -fblocks -o %t %S/blocks.h &&
+// RUN: clang-cc -fblocks -include-pch %t -fsyntax-only -ast-print -o - %s 
+
+int do_add(int x, int y) { return add(x, y); }
+
+int do_scaled_add(int a, int b, int s) {
+  return scaled_add(a, b, s);
+}

Added: cfe/trunk/test/PCH/blocks.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/blocks.h?rev=69373&view=auto

==============================================================================
--- cfe/trunk/test/PCH/blocks.h (added)
+++ cfe/trunk/test/PCH/blocks.h Fri Apr 17 14:21:43 2009
@@ -0,0 +1,14 @@
+// Header for PCH test blocks.c
+
+int call_block(int (^bl)(int x, int y), int a, int b) {
+  return bl(a, b);
+}
+
+int add(int a, int b) {
+  return call_block(^(int x, int y) { return x + y; }, a, b);
+}
+
+int scaled_add(int a, int b, int s) {
+  __block int scale = s;
+  return call_block(^(int x, int y) { return x*scale + y; }, a, b);
+}

Modified: cfe/trunk/test/PCH/stmts.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/stmts.c?rev=69373&r1=69372&r2=69373&view=diff

==============================================================================
--- cfe/trunk/test/PCH/stmts.c (original)
+++ cfe/trunk/test/PCH/stmts.c Fri Apr 17 14:21:43 2009
@@ -1,9 +1,9 @@
 // Test this without pch.
-// RUN: clang-cc -fblocks -include %S/stmts.h -fsyntax-only -ast-print -o - %s
+// RUN: clang-cc -include %S/stmts.h -fsyntax-only -ast-print -o - %s
 
 // Test with pch.
-// RUN: clang-cc -emit-pch -fblocks -o %t %S/stmts.h &&
-// RUN: clang-cc -fblocks -include-pch %t -fsyntax-only -ast-print -o - %s 
+// RUN: clang-cc -emit-pch -o %t %S/stmts.h &&
+// RUN: clang-cc -include-pch %t -fsyntax-only -ast-print -o - %s 
 
 void g0(void) { f0(5); }
 int g1(int x) { return f1(x); }





More information about the cfe-commits mailing list