diff options
author | Ben Langmuir <ben.langmuir@intel.com> | 2013-05-03 19:20:19 +0000 |
---|---|---|
committer | Ben Langmuir <ben.langmuir@intel.com> | 2013-05-03 19:20:19 +0000 |
commit | dc5be4f54d6415cb88b2f8a7c5bc9011e332b9b8 (patch) | |
tree | c0601b4fba10524868f196d5ee845dec53db48f6 | |
parent | 0f2fc5ff49cb9abd6c6972ffd6db066295672867 (diff) |
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
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181048 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/AST/Decl.h | 7 | ||||
-rw-r--r-- | include/clang/AST/Stmt.h | 11 | ||||
-rw-r--r-- | lib/AST/Decl.cpp | 9 | ||||
-rw-r--r-- | lib/AST/Stmt.cpp | 4 | ||||
-rw-r--r-- | lib/Serialization/ASTReaderDecl.cpp | 9 | ||||
-rw-r--r-- | lib/Serialization/ASTReaderStmt.cpp | 27 | ||||
-rw-r--r-- | lib/Serialization/ASTWriterDecl.cpp | 9 | ||||
-rw-r--r-- | lib/Serialization/ASTWriterStmt.cpp | 29 | ||||
-rw-r--r-- | test/PCH/captured-stmt.cpp | 42 |
9 files changed, 133 insertions, 14 deletions
diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index 6c8c5051fe..ea6479ad28 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -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 diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index 6e5fae4f3c..da83220988 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -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 diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 01fbc3b2e0..1f4248c8e5 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -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, diff --git a/lib/AST/Stmt.cpp b/lib/AST/Stmt.cpp index 888a148b96..0db97d2967 100644 --- a/lib/AST/Stmt.cpp +++ b/lib/AST/Stmt.cpp @@ -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; diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp index 5f4c67be99..06cdeaf8a0 100644 --- a/lib/Serialization/ASTReaderDecl.cpp +++ b/lib/Serialization/ASTReaderDecl.cpp @@ -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"); diff --git a/lib/Serialization/ASTReaderStmt.cpp b/lib/Serialization/ASTReaderStmt.cpp index fd60ec4fd5..5d72614592 100644 --- a/lib/Serialization/ASTReaderStmt.cpp +++ b/lib/Serialization/ASTReaderStmt.cpp @@ -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: diff --git a/lib/Serialization/ASTWriterDecl.cpp b/lib/Serialization/ASTWriterDecl.cpp index ef50cdba55..105042fda3 100644 --- a/lib/Serialization/ASTWriterDecl.cpp +++ b/lib/Serialization/ASTWriterDecl.cpp @@ -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) { diff --git a/lib/Serialization/ASTWriterStmt.cpp b/lib/Serialization/ASTWriterStmt.cpp index 832bbf6990..7d282471bb 100644 --- a/lib/Serialization/ASTWriterStmt.cpp +++ b/lib/Serialization/ASTWriterStmt.cpp @@ -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())); + + 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) { diff --git a/test/PCH/captured-stmt.cpp b/test/PCH/captured-stmt.cpp new file mode 100644 index 0000000000..6f5ca3836e --- /dev/null +++ b/test/PCH/captured-stmt.cpp @@ -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 |