r181048 - Serialization for captured statements

Ben Langmuir ben.langmuir at intel.com
Fri May 3 12:20:19 PDT 2013


Author: benlangmuir
Date: Fri May  3 14:20:19 2013
New Revision: 181048

URL: http://llvm.org/viewvc/llvm-project?rev=181048&view=rev
Log:
Serialization for captured statements

Add serialization for captured statements and captured decls.  Also add
a const_capture_iterator to CapturedStmt.

Test contributed by Wei Pan

Differential Revision: http://llvm-reviews.chandlerc.com/D727

Added:
    cfe/trunk/test/PCH/captured-stmt.cpp
Modified:
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/include/clang/AST/Stmt.h
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/AST/Stmt.cpp
    cfe/trunk/lib/Serialization/ASTReaderDecl.cpp
    cfe/trunk/lib/Serialization/ASTReaderStmt.cpp
    cfe/trunk/lib/Serialization/ASTWriterDecl.cpp
    cfe/trunk/lib/Serialization/ASTWriterStmt.cpp

Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=181048&r1=181047&r2=181048&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Fri May  3 14:20:19 2013
@@ -3185,10 +3185,14 @@ private:
 
 public:
   static CapturedDecl *Create(ASTContext &C, DeclContext *DC, unsigned NumParams);
+  static CapturedDecl *CreateDeserialized(ASTContext &C, unsigned ID,
+                                          unsigned NumParams);
 
   Stmt *getBody() const { return Body; }
   void setBody(Stmt *B) { Body = B; }
 
+  unsigned getNumParams() const { return NumParams; }
+
   ImplicitParamDecl *getParam(unsigned i) const {
     assert(i < NumParams);
     return getParams()[i];
@@ -3217,6 +3221,9 @@ public:
   static CapturedDecl *castFromDeclContext(const DeclContext *DC) {
     return static_cast<CapturedDecl *>(const_cast<DeclContext *>(DC));
   }
+
+  friend class ASTDeclReader;
+  friend class ASTDeclWriter;
 };
 
 /// \brief Describes a module import declaration, which makes the contents

Modified: cfe/trunk/include/clang/AST/Stmt.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Stmt.h?rev=181048&r1=181047&r2=181048&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Stmt.h (original)
+++ cfe/trunk/include/clang/AST/Stmt.h Fri May  3 14:20:19 2013
@@ -1975,6 +1975,7 @@ public:
       assert(!capturesThis() && "No variable available for 'this' capture");
       return VarAndKind.getPointer();
     }
+    friend class ASTStmtReader;
   };
 
 private:
@@ -2001,6 +2002,8 @@ private:
 
   Capture *getStoredCaptures() const;
 
+  void setCapturedStmt(Stmt *S) { getStoredStmts()[NumCaptures] = S; }
+
 public:
   static CapturedStmt *Create(ASTContext &Context, Stmt *S,
                               ArrayRef<Capture> Captures,
@@ -2026,10 +2029,12 @@ public:
   bool capturesVariable(const VarDecl *Var) const;
 
   /// \brief An iterator that walks over the captures.
-  typedef const Capture *capture_iterator;
+  typedef Capture *capture_iterator;
+  typedef const Capture *const_capture_iterator;
 
   /// \brief Retrieve an iterator pointing to the first capture.
-  capture_iterator capture_begin() const { return getStoredCaptures(); }
+  capture_iterator capture_begin() { return getStoredCaptures(); }
+  const_capture_iterator capture_begin() const { return getStoredCaptures(); }
 
   /// \brief Retrieve an iterator pointing past the end of the sequence of
   /// captures.
@@ -2069,6 +2074,8 @@ public:
   }
 
   child_range children();
+
+  friend class ASTStmtReader;
 };
 
 }  // end namespace clang

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=181048&r1=181047&r2=181048&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Fri May  3 14:20:19 2013
@@ -3249,10 +3249,17 @@ MSPropertyDecl *MSPropertyDecl::CreateDe
 
 CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC,
                                    unsigned NumParams) {
-  unsigned Size = sizeof(CapturedDecl) + NumParams *sizeof(ImplicitParamDecl*);
+  unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
   return new (C.Allocate(Size)) CapturedDecl(DC, NumParams);
 }
 
+CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID,
+                                   unsigned NumParams) {
+  unsigned Size = sizeof(CapturedDecl) + NumParams * sizeof(ImplicitParamDecl*);
+  void *Mem = AllocateDeserializedDecl(C, ID, Size);
+  return new (Mem) CapturedDecl(0, NumParams);
+}
+
 EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
                                            SourceLocation L,
                                            IdentifierInfo *Id, QualType T,

Modified: cfe/trunk/lib/AST/Stmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Stmt.cpp?rev=181048&r1=181047&r2=181048&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Stmt.cpp (original)
+++ cfe/trunk/lib/AST/Stmt.cpp Fri May  3 14:20:19 2013
@@ -1124,8 +1124,8 @@ Stmt::child_range CapturedStmt::children
 }
 
 bool CapturedStmt::capturesVariable(const VarDecl *Var) const {
-  for (capture_iterator I = capture_begin(),
-                        E = capture_end(); I != E; ++I) {
+  for (const_capture_iterator I = capture_begin(),
+                              E = capture_end(); I != E; ++I) {
     if (I->capturesThis())
       continue;
 

Modified: cfe/trunk/lib/Serialization/ASTReaderDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReaderDecl.cpp?rev=181048&r1=181047&r2=181048&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReaderDecl.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReaderDecl.cpp Fri May  3 14:20:19 2013
@@ -996,8 +996,11 @@ void ASTDeclReader::VisitBlockDecl(Block
                   captures.end(), capturesCXXThis);
 }
 
-void ASTDeclReader::VisitCapturedDecl(CapturedDecl *) {
-  llvm_unreachable("not implemented yet");
+void ASTDeclReader::VisitCapturedDecl(CapturedDecl *CD) {
+  VisitDecl(CD);
+  // Body is set by VisitCapturedStmt.
+  for (unsigned i = 0; i < CD->NumParams; ++i)
+    CD->setParam(i, ReadDeclAs<ImplicitParamDecl>(Record, Idx));
 }
 
 void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
@@ -2153,7 +2156,7 @@ Decl *ASTReader::ReadDeclRecord(DeclID I
     D = MSPropertyDecl::CreateDeserialized(Context, ID);
     break;
   case DECL_CAPTURED:
-    llvm_unreachable("not implemented yet");
+    D = CapturedDecl::CreateDeserialized(Context, ID, Record[Idx++]);
     break;
   case DECL_CXX_BASE_SPECIFIERS:
     Error("attempt to read a C++ base-specifier record as a declaration");

Modified: cfe/trunk/lib/Serialization/ASTReaderStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReaderStmt.cpp?rev=181048&r1=181047&r2=181048&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReaderStmt.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReaderStmt.cpp Fri May  3 14:20:19 2013
@@ -381,7 +381,29 @@ void ASTStmtReader::VisitMSAsmStmt(MSAsm
 }
 
 void ASTStmtReader::VisitCapturedStmt(CapturedStmt *S) {
-  llvm_unreachable("not implemented yet");
+  VisitStmt(S);
+  S->TheCapturedDecl = ReadDeclAs<CapturedDecl>(Record, Idx);
+  S->TheRecordDecl = ReadDeclAs<RecordDecl>(Record, Idx);
+
+  // Capture inits
+  for (CapturedStmt::capture_init_iterator I = S->capture_init_begin(),
+                                           E = S->capture_init_end();
+       I != E; ++I)
+    *I = Reader.ReadSubExpr();
+
+  // Body
+  S->setCapturedStmt(Reader.ReadSubStmt());
+  S->TheCapturedDecl->setBody(S->getCapturedStmt());
+
+  // Captures
+  for (CapturedStmt::capture_iterator I = S->capture_begin(),
+                                      E = S->capture_end();
+       I != E; ++I) {
+    I->VarAndKind.setPointer(ReadDeclAs<VarDecl>(Record, Idx));
+    I->VarAndKind
+        .setInt(static_cast<CapturedStmt::VariableCaptureKind>(Record[Idx++]));
+    I->Loc = ReadSourceLocation(Record, Idx);
+  }
 }
 
 void ASTStmtReader::VisitExpr(Expr *E) {
@@ -1791,7 +1813,8 @@ Stmt *ASTReader::ReadStmtFromStream(Modu
       break;
 
     case STMT_CAPTURED:
-      llvm_unreachable("not implemented yet");
+      S = CapturedStmt::CreateDeserialized(Context,
+                                           Record[ASTStmtReader::NumExprFields]);
       break;
 
     case EXPR_PREDEFINED:

Modified: cfe/trunk/lib/Serialization/ASTWriterDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriterDecl.cpp?rev=181048&r1=181047&r2=181048&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriterDecl.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriterDecl.cpp Fri May  3 14:20:19 2013
@@ -826,8 +826,13 @@ void ASTDeclWriter::VisitBlockDecl(Block
   Code = serialization::DECL_BLOCK;
 }
 
-void ASTDeclWriter::VisitCapturedDecl(CapturedDecl *) {
-  llvm_unreachable("not implemented yet");
+void ASTDeclWriter::VisitCapturedDecl(CapturedDecl *CD) {
+  Record.push_back(CD->getNumParams());
+  VisitDecl(CD);
+  // Body is stored by VisitCapturedStmt.
+  for (unsigned i = 0; i < CD->getNumParams(); ++i)
+    Writer.AddDeclRef(CD->getParam(i), Record);
+  Code = serialization::DECL_CAPTURED;
 }
 
 void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {

Modified: cfe/trunk/lib/Serialization/ASTWriterStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriterStmt.cpp?rev=181048&r1=181047&r2=181048&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriterStmt.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriterStmt.cpp Fri May  3 14:20:19 2013
@@ -287,9 +287,34 @@ void ASTStmtWriter::VisitMSAsmStmt(MSAsm
 
 void ASTStmtWriter::VisitCapturedStmt(CapturedStmt *S) {
   VisitStmt(S);
-  Code = serialization::STMT_CAPTURED;
+  // NumCaptures
+  Record.push_back(std::distance(S->capture_begin(), S->capture_end()));
+
+  Writer.AddDeclRef(S->getCapturedDecl(), Record);
+  Writer.AddDeclRef(S->getCapturedRecordDecl(), Record);
+
+  // Capture inits
+  for (CapturedStmt::capture_init_iterator I = S->capture_init_begin(),
+                                           E = S->capture_init_end();
+       I != E; ++I)
+    Writer.AddStmt(*I);
 
-  llvm_unreachable("not implemented yet");
+  // Body
+  Writer.AddStmt(S->getCapturedStmt());
+
+  // Captures
+  for (CapturedStmt::capture_iterator I = S->capture_begin(),
+                                      E = S->capture_end();
+       I != E; ++I) {
+    if (I->capturesThis())
+      Writer.AddDeclRef(0, Record);
+    else
+      Writer.AddDeclRef(I->getCapturedVar(), Record);
+    Record.push_back(I->getCaptureKind());
+    Writer.AddSourceLocation(I->getLocation(), Record);
+  }
+
+  Code = serialization::STMT_CAPTURED;
 }
 
 void ASTStmtWriter::VisitExpr(Expr *E) {

Added: cfe/trunk/test/PCH/captured-stmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/captured-stmt.cpp?rev=181048&view=auto
==============================================================================
--- cfe/trunk/test/PCH/captured-stmt.cpp (added)
+++ cfe/trunk/test/PCH/captured-stmt.cpp Fri May  3 14:20:19 2013
@@ -0,0 +1,42 @@
+// RUN: %clang_cc1 -x c++-header -emit-pch %s -o %t
+// RUN: %clang_cc1 -include-pch %t -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+#ifndef HEADER_INCLUDED
+#define HEADER_INCLUDED
+
+static inline void foo(int &x, int y) {
+  // Capturing x and y
+  #pragma clang __debug captured
+  {
+    x += y;
+  }
+}
+
+struct C {
+  int val;
+
+  explicit C(int v) : val(v) { }
+
+  void bar(int &x) {
+    // Capturing x and this
+    #pragma clang __debug captured
+    {
+      x += val;
+    }
+  }
+};
+
+#else
+
+void test_foo(int &x) {
+  foo(x, 10);
+}
+
+void test_bar(int &x) {
+  C Obj(10);
+  Obj.bar(x);
+}
+
+#endif





More information about the cfe-commits mailing list