r279667 - PR29097: add an update record when we instantiate the default member
Richard Smith via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 24 14:25:37 PDT 2016
Author: rsmith
Date: Wed Aug 24 16:25:37 2016
New Revision: 279667
URL: http://llvm.org/viewvc/llvm-project?rev=279667&view=rev
Log:
PR29097: add an update record when we instantiate the default member
initializer of an imported field.
Modified:
cfe/trunk/include/clang/AST/ASTMutationListener.h
cfe/trunk/include/clang/Serialization/ASTWriter.h
cfe/trunk/lib/Frontend/MultiplexConsumer.cpp
cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp
cfe/trunk/lib/Serialization/ASTCommon.h
cfe/trunk/lib/Serialization/ASTReaderDecl.cpp
cfe/trunk/lib/Serialization/ASTWriter.cpp
cfe/trunk/test/PCH/cxx1y-default-initializer.cpp
Modified: cfe/trunk/include/clang/AST/ASTMutationListener.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTMutationListener.h?rev=279667&r1=279666&r2=279667&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTMutationListener.h (original)
+++ cfe/trunk/include/clang/AST/ASTMutationListener.h Wed Aug 24 16:25:37 2016
@@ -22,6 +22,7 @@ namespace clang {
class CXXRecordDecl;
class Decl;
class DeclContext;
+ class FieldDecl;
class FunctionDecl;
class FunctionTemplateDecl;
class Module;
@@ -93,6 +94,9 @@ public:
/// \brief A default argument was instantiated.
virtual void DefaultArgumentInstantiated(const ParmVarDecl *D) {}
+ /// \brief A default member initializer was instantiated.
+ virtual void DefaultMemberInitializerInstantiated(const FieldDecl *D) {}
+
/// \brief A new objc category class was added for an interface.
virtual void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
const ObjCInterfaceDecl *IFD) {}
Modified: cfe/trunk/include/clang/Serialization/ASTWriter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTWriter.h?rev=279667&r1=279666&r2=279667&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTWriter.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTWriter.h Wed Aug 24 16:25:37 2016
@@ -674,6 +674,7 @@ private:
void CompletedImplicitDefinition(const FunctionDecl *D) override;
void StaticDataMemberInstantiated(const VarDecl *D) override;
void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
+ void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
const ObjCInterfaceDecl *IFD) override;
Modified: cfe/trunk/lib/Frontend/MultiplexConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/MultiplexConsumer.cpp?rev=279667&r1=279666&r2=279667&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/MultiplexConsumer.cpp (original)
+++ cfe/trunk/lib/Frontend/MultiplexConsumer.cpp Wed Aug 24 16:25:37 2016
@@ -120,6 +120,7 @@ public:
void CompletedImplicitDefinition(const FunctionDecl *D) override;
void StaticDataMemberInstantiated(const VarDecl *D) override;
void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
+ void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
const ObjCInterfaceDecl *IFD) override;
void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
@@ -201,6 +202,11 @@ void MultiplexASTMutationListener::Defau
for (size_t i = 0, e = Listeners.size(); i != e; ++i)
Listeners[i]->DefaultArgumentInstantiated(D);
}
+void MultiplexASTMutationListener::DefaultMemberInitializerInstantiated(
+ const FieldDecl *D) {
+ for (size_t i = 0, e = Listeners.size(); i != e; ++i)
+ Listeners[i]->DefaultMemberInitializerInstantiated(D);
+}
void MultiplexASTMutationListener::AddedObjCCategoryToInterface(
const ObjCCategoryDecl *CatD,
const ObjCInterfaceDecl *IFD) {
Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp?rev=279667&r1=279666&r2=279667&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiate.cpp Wed Aug 24 16:25:37 2016
@@ -15,6 +15,7 @@
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/ASTLambda.h"
+#include "clang/AST/ASTMutationListener.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/Expr.h"
#include "clang/Basic/LangOptions.h"
@@ -2215,6 +2216,9 @@ bool Sema::InstantiateInClassInitializer
ActOnFinishCXXInClassMemberInitializer(
Instantiation, Init ? Init->getLocStart() : SourceLocation(), Init);
+ if (auto *L = getASTMutationListener())
+ L->DefaultMemberInitializerInstantiated(Instantiation);
+
// Exit the scope of this instantiation.
SavedContext.pop();
Modified: cfe/trunk/lib/Serialization/ASTCommon.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTCommon.h?rev=279667&r1=279666&r2=279667&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTCommon.h (original)
+++ cfe/trunk/lib/Serialization/ASTCommon.h Wed Aug 24 16:25:37 2016
@@ -30,6 +30,7 @@ enum DeclUpdateKind {
UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
UPD_CXX_INSTANTIATED_CLASS_DEFINITION,
UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT,
+ UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER,
UPD_CXX_RESOLVED_DTOR_DELETE,
UPD_CXX_RESOLVED_EXCEPTION_SPEC,
UPD_CXX_DEDUCED_RETURN_TYPE,
Modified: cfe/trunk/lib/Serialization/ASTReaderDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReaderDecl.cpp?rev=279667&r1=279666&r2=279667&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReaderDecl.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReaderDecl.cpp Wed Aug 24 16:25:37 2016
@@ -3796,6 +3796,23 @@ void ASTDeclReader::UpdateDecl(Decl *D,
break;
}
+ case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER: {
+ auto FD = cast<FieldDecl>(D);
+ auto DefaultInit = Reader.ReadExpr(F);
+
+ // Only apply the update if the field still has an uninstantiated
+ // default member initializer.
+ if (FD->hasInClassInitializer() && !FD->getInClassInitializer()) {
+ if (DefaultInit)
+ FD->setInClassInitializer(DefaultInit);
+ else
+ // Instantiation failed. We can get here if we serialized an AST for
+ // an invalid program.
+ FD->removeInClassInitializer();
+ }
+ break;
+ }
+
case UPD_CXX_ADDED_FUNCTION_DEFINITION: {
FunctionDecl *FD = cast<FunctionDecl>(D);
if (Reader.PendingBodies[FD]) {
Modified: cfe/trunk/lib/Serialization/ASTWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriter.cpp?rev=279667&r1=279666&r2=279667&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriter.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriter.cpp Wed Aug 24 16:25:37 2016
@@ -4702,6 +4702,11 @@ void ASTWriter::WriteDeclUpdatesBlocks(R
cast<ParmVarDecl>(Update.getDecl())->getDefaultArg()));
break;
+ case UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER:
+ Record.AddStmt(
+ cast<FieldDecl>(Update.getDecl())->getInClassInitializer());
+ break;
+
case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
auto *RD = cast<CXXRecordDecl>(D);
UpdatedDeclContexts.insert(RD->getPrimaryContext());
@@ -5815,6 +5820,15 @@ void ASTWriter::DefaultArgumentInstantia
DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_ARGUMENT, D));
}
+void ASTWriter::DefaultMemberInitializerInstantiated(const FieldDecl *D) {
+ assert(!WritingAST && "Already writing the AST!");
+ if (!D->isFromASTFile())
+ return;
+
+ DeclUpdates[D].push_back(
+ DeclUpdate(UPD_CXX_INSTANTIATED_DEFAULT_MEMBER_INITIALIZER, D));
+}
+
void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
const ObjCInterfaceDecl *IFD) {
if (Chain && Chain->isProcessingUpdateRecords()) return;
Modified: cfe/trunk/test/PCH/cxx1y-default-initializer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/cxx1y-default-initializer.cpp?rev=279667&r1=279666&r2=279667&view=diff
==============================================================================
--- cfe/trunk/test/PCH/cxx1y-default-initializer.cpp (original)
+++ cfe/trunk/test/PCH/cxx1y-default-initializer.cpp Wed Aug 24 16:25:37 2016
@@ -1,10 +1,10 @@
-// RUN: %clang_cc1 -pedantic -std=c++1y %s -o %t
-// RUN: %clang_cc1 -pedantic -std=c++1y -emit-pch %s -o %t
-// RUN: %clang_cc1 -pedantic -std=c++1y -include-pch %t -verify %s
+// RUN: %clang_cc1 -pedantic -std=c++1y -include %s -include %s -verify %s
+// RUN: %clang_cc1 -pedantic -std=c++1y -emit-pch -o %t.1 %s
+// RUN: %clang_cc1 -pedantic -std=c++1y -include-pch %t.1 -emit-pch -o %t.2 %s
+// RUN: %clang_cc1 -pedantic -std=c++1y -include-pch %t.2 -verify %s
-#ifndef HEADER_INCLUDED
-
-#define HEADER_INCLUDED
+#ifndef HEADER_1
+#define HEADER_1
struct A {
int x;
@@ -19,6 +19,20 @@ struct B {
constexpr B(int k) : z1(k) {}
};
+template<typename T> struct C {
+ constexpr C() {}
+ T c = T();
+ struct U {};
+};
+// Instantiate C<int> but not the default initializer.
+C<int>::U ciu;
+
+#elif !defined(HEADER_2)
+#define HEADER_2
+
+// Instantiate the default initializer now, should create an update record.
+C<int> ci;
+
#else
static_assert(A{}.z == 3, "");
@@ -27,5 +41,6 @@ static_assert(A{.y = 5}.z == 5, ""); //
static_assert(A{3, .y = 1}.z == 4, ""); // expected-warning {{C99}}
static_assert(make<int>().z == 3, "");
static_assert(make<int>(12).z == 15, "");
+static_assert(C<int>().c == 0, "");
#endif
More information about the cfe-commits
mailing list