From 559aa046fe3260d8640791f2249d7b0d458b5700 Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Thu, 2 Mar 2017 16:47:48 +0000 Subject: Merging r296656: ------------------------------------------------------------------------ r296656 | bruno | 2017-03-01 11:18:42 -0800 (Wed, 01 Mar 2017) | 34 lines [PCH] Avoid VarDecl emission attempt if no owning module avaiable This is a stopgap fix for PR31863, a regression introduced in r276159. Consider this snippet: struct FVector; struct FVector {}; struct FBox { FVector Min; FBox(int); }; namespace { FBox InvalidBoundingBox(0); } While parsing the DECL_VAR for 'struct FBox', clang recursively read all the dep decls until it finds the DECL_CXX_RECORD forward declaration for 'struct FVector'. Then, it resumes all the way up back to DECL_VAR handling in `ReadDeclRecord`, where it checks if `isConsumerInterestedIn` for the decl. One of the condition for `isConsumerInterestedIn` to return false is if the VarDecl is imported from a module `D->getImportedOwningModule()`, because it will get emitted when we import the relevant module. However, before checking if it comes from a module, clang checks if `Ctx.DeclMustBeEmitted(D)`, which triggers the emission of 'struct FBox'. Since one of its fields is still incomplete, it crashes. Instead, check if `D->getImportedOwningModule()` is true before calling `Ctx.DeclMustBeEmitted(D)`. Differential Revision: https://reviews.llvm.org/D29753 rdar://problem/30173654 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/cfe/branches/release_40@296762 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Serialization/ASTReaderDecl.cpp | 4 ++-- test/PCH/empty-def-fwd-struct.h | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 test/PCH/empty-def-fwd-struct.h diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp index c691919339..707a9249dd 100644 --- a/lib/Serialization/ASTReaderDecl.cpp +++ b/lib/Serialization/ASTReaderDecl.cpp @@ -2513,8 +2513,8 @@ static bool isConsumerInterestedIn(ASTContext &Ctx, Decl *D, bool HasBody) { // An ImportDecl or VarDecl imported from a module will get emitted when // we import the relevant module. - if ((isa(D) || isa(D)) && Ctx.DeclMustBeEmitted(D) && - D->getImportedOwningModule()) + if ((isa(D) || isa(D)) && D->getImportedOwningModule() && + Ctx.DeclMustBeEmitted(D)) return false; if (isa(D) || diff --git a/test/PCH/empty-def-fwd-struct.h b/test/PCH/empty-def-fwd-struct.h new file mode 100644 index 0000000000..dda9827ac8 --- /dev/null +++ b/test/PCH/empty-def-fwd-struct.h @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -emit-pch -x c++-header %s -std=c++14 -o %t.pch +// RUN: %clang_cc1 -emit-llvm-only -x c++ /dev/null -std=c++14 -include-pch %t.pch -o %t.o +struct FVector; +struct FVector {}; +struct FBox { + FVector Min; + FBox(int); +}; +namespace { +FBox InvalidBoundingBox(0); +} + -- cgit v1.2.3