From 7195f857860a8482f2841ee45c0d36d606bdd20e Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 25 Aug 2016 00:34:00 +0000 Subject: [PATCH] Lazily load the ContextDecl for a lambda's DefinitionData, to fix a deserialization cycle caused by the ContextDecl recursively importing members of the lambda's closure type. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@279694 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/DeclCXX.h | 7 ++----- lib/AST/DeclCXX.cpp | 6 ++++++ lib/Serialization/ASTReaderDecl.cpp | 2 +- lib/Serialization/ASTWriter.cpp | 2 +- test/Modules/lambda-context.cpp | 22 ++++++++++++++++++++++ test/PCH/cxx11-lambdas.mm | 7 +++++++ 6 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 test/Modules/lambda-context.cpp diff --git a/include/clang/AST/DeclCXX.h b/include/clang/AST/DeclCXX.h index 2071c1b0e1..f2b798b5a3 100644 --- a/include/clang/AST/DeclCXX.h +++ b/include/clang/AST/DeclCXX.h @@ -571,7 +571,7 @@ class CXXRecordDecl : public RecordDecl { /// actual DeclContext does not suffice. This is used for lambdas that /// occur within default arguments of function parameters within the class /// or within a data member initializer. - Decl *ContextDecl; + LazyDeclPtr ContextDecl; /// \brief The list of captures, both explicit and implicit, for this /// lambda. @@ -1673,10 +1673,7 @@ public: /// the declaration in which the lambda occurs, e.g., the function parameter /// or the non-static data member. Otherwise, it returns NULL to imply that /// the declaration context suffices. - Decl *getLambdaContextDecl() const { - assert(isLambda() && "Not a lambda closure type!"); - return getLambdaData().ContextDecl; - } + Decl *getLambdaContextDecl() const; /// \brief Set the mangling number and context declaration for a lambda /// class. diff --git a/lib/AST/DeclCXX.cpp b/lib/AST/DeclCXX.cpp index 7395db586b..5256eda24b 100644 --- a/lib/AST/DeclCXX.cpp +++ b/lib/AST/DeclCXX.cpp @@ -1107,6 +1107,12 @@ CXXRecordDecl::getGenericLambdaTemplateParameterList() const { return nullptr; } +Decl *CXXRecordDecl::getLambdaContextDecl() const { + assert(isLambda() && "Not a lambda closure type!"); + ExternalASTSource *Source = getParentASTContext().getExternalSource(); + return getLambdaData().ContextDecl.get(Source); +} + static CanQualType GetConversionType(ASTContext &Context, NamedDecl *Conv) { QualType T = cast(Conv->getUnderlyingDecl()->getAsFunction()) diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp index 04231cd8e0..e00c9561cd 100644 --- a/lib/Serialization/ASTReaderDecl.cpp +++ b/lib/Serialization/ASTReaderDecl.cpp @@ -1539,7 +1539,7 @@ void ASTDeclReader::ReadCXXDefinitionData( Lambda.NumCaptures = Record[Idx++]; Lambda.NumExplicitCaptures = Record[Idx++]; Lambda.ManglingNumber = Record[Idx++]; - Lambda.ContextDecl = ReadDecl(Record, Idx); + Lambda.ContextDecl = ReadDeclID(Record, Idx); Lambda.Captures = (Capture*)Reader.Context.Allocate(sizeof(Capture)*Lambda.NumCaptures); Capture *ToCapture = Lambda.Captures; diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp index 72b2bf6d16..7f9ae8df1c 100644 --- a/lib/Serialization/ASTWriter.cpp +++ b/lib/Serialization/ASTWriter.cpp @@ -5565,7 +5565,7 @@ void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) { Record->push_back(Lambda.NumCaptures); Record->push_back(Lambda.NumExplicitCaptures); Record->push_back(Lambda.ManglingNumber); - AddDeclRef(Lambda.ContextDecl); + AddDeclRef(D->getLambdaContextDecl()); AddTypeSourceInfo(Lambda.MethodTyInfo); for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) { const LambdaCapture &Capture = Lambda.Captures[I]; diff --git a/test/Modules/lambda-context.cpp b/test/Modules/lambda-context.cpp new file mode 100644 index 0000000000..6ce482c2b7 --- /dev/null +++ b/test/Modules/lambda-context.cpp @@ -0,0 +1,22 @@ +// RUN: %clang_cc1 -fmodules -std=c++11 -emit-pch -o %t %s +// RUN: %clang_cc1 -fmodules -std=c++11 -include-pch %t %s -verify +// +// This test checks for a bug in the deserialization code that was only +// reachable with modules enabled, but actually building and using modules is +// not necessary in order to trigger it, so we just use PCH here to make the +// test simpler. + +#ifndef HEADER_INCLUDED +#define HEADER_INCLUDED + +struct X { template X(T) {} }; +struct Y { Y(X x = [] {}); }; + +#else + +// This triggers us to load the specialization of X::X for Y's lambda. That +// lambda's context decl must not be loaded as a result of loading the lambda, +// as that would hit a deserialization cycle. +X x = [] {}; // expected-no-diagnostics + +#endif diff --git a/test/PCH/cxx11-lambdas.mm b/test/PCH/cxx11-lambdas.mm index 5d3323a02c..1f568f05e1 100644 --- a/test/PCH/cxx11-lambdas.mm +++ b/test/PCH/cxx11-lambdas.mm @@ -38,6 +38,11 @@ int init_capture(T t) { return [&, x(t)] { return sizeof(x); }; } +struct X { + template X(T); +}; +struct Y { Y(const X &x = [] {}); }; + #else // CHECK-PRINT: T add_slowly @@ -54,4 +59,6 @@ int add(int x, int y) { // CHECK-PRINT: init_capture // CHECK-PRINT: [&, x(t)] +X x = [] {}; + #endif -- 2.40.0