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

llvm-svn: 181048
This commit is contained in:
Ben Langmuir 2013-05-03 19:20:19 +00:00
parent 637b9bebd4
commit ce914fc84b
9 changed files with 133 additions and 14 deletions

View File

@ -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

View File

@ -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

View File

@ -3249,10 +3249,17 @@ MSPropertyDecl *MSPropertyDecl::CreateDeserialized(ASTContext &C,
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,

View File

@ -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;

View File

@ -996,8 +996,11 @@ void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
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 ID) {
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");

View File

@ -381,7 +381,29 @@ void ASTStmtReader::VisitMSAsmStmt(MSAsmStmt *S) {
}
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(ModuleFile &F) {
break;
case STMT_CAPTURED:
llvm_unreachable("not implemented yet");
S = CapturedStmt::CreateDeserialized(Context,
Record[ASTStmtReader::NumExprFields]);
break;
case EXPR_PREDEFINED:

View File

@ -826,8 +826,13 @@ void ASTDeclWriter::VisitBlockDecl(BlockDecl *D) {
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) {

View File

@ -287,9 +287,34 @@ void ASTStmtWriter::VisitMSAsmStmt(MSAsmStmt *S) {
void ASTStmtWriter::VisitCapturedStmt(CapturedStmt *S) {
VisitStmt(S);
Code = serialization::STMT_CAPTURED;
// NumCaptures
Record.push_back(std::distance(S->capture_begin(), S->capture_end()));
llvm_unreachable("not implemented yet");
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);
// 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) {

View File

@ -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