r181048 - Serialization for captured statements

Aaron Ballman aaron at aaronballman.com
Fri Mar 14 12:41:14 PDT 2014


On Fri, Mar 14, 2014 at 3:10 PM, Ben Langmuir <blangmuir at apple.com> wrote:
>
> On Mar 14, 2014, at 11:59 AM, Aaron Ballman <aaron at aaronballman.com> wrote:
>
>> Sorry to resurrect an old commit, but...
>>
>> On Fri, May 3, 2013 at 3:20 PM, Ben Langmuir <ben.langmuir at intel.com> wrote:
>>> 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();
>>
>> While range-ifying code, I came across this, and I'm wondering: is the
>> assignment supposed to actually do anything? It appears to be spurious
>> (and indeed, removing the assignment causes no regressions in our test
>> suite).
>
> It’s been too long for my memory, but from re-reading the code I don’t see why this would be spurious.. why do you think it is?  We probably just don’t test it well :)
>
> I believe the intent is to read the initializer for this particular capture and store it in the CapturedStmt’s capture initializers.  The iterator will be valid because we allocate these when we create the CapturedStmt.

This was my mistake, so I apologize for the noise. I thought this was
doing an assignment to a *local* pointer, but I see now that it's an
assignment into the pointer stored in S due to the fact that
cpature_init_iterator is an Expr **, not an iterator class.

Phew. :-)

Thanks for letting me bug you on it.

~Aaron




More information about the cfe-commits mailing list