diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -12476,6 +12476,17 @@ } if (!Var->isInvalidDecl() && RealDecl->hasAttr()) { + if (Var->getStorageClass() == SC_Extern) { + Diag(Var->getLocation(), diag::err_loader_uninitialized_extern_decl) + << Var; + Var->setInvalidDecl(); + return; + } + if (RequireCompleteType(Var->getLocation(), Var->getType(), + diag::err_typecheck_decl_incomplete_type)) { + Var->setInvalidDecl(); + return; + } if (CXXRecordDecl *RD = Var->getType()->getAsCXXRecordDecl()) { if (!RD->hasTrivialDefaultConstructor()) { Diag(Var->getLocation(), diag::err_loader_uninitialized_trivial_ctor); @@ -12483,12 +12494,6 @@ return; } } - if (Var->getStorageClass() == SC_Extern) { - Diag(Var->getLocation(), diag::err_loader_uninitialized_extern_decl) - << Var; - Var->setInvalidDecl(); - return; - } } VarDecl::DefinitionKind DefKind = Var->isThisDeclarationADefinition(); diff --git a/clang/test/CodeGenCXX/attr-loader-uninitialized.cpp b/clang/test/CodeGenCXX/attr-loader-uninitialized.cpp --- a/clang/test/CodeGenCXX/attr-loader-uninitialized.cpp +++ b/clang/test/CodeGenCXX/attr-loader-uninitialized.cpp @@ -28,3 +28,15 @@ // Defining as arr2[] [[clang..]] raises the error: attribute cannot be applied to types // CHECK: @arr2 = global [4 x double] undef double arr2 [[clang::loader_uninitialized]] [4]; + +template struct templ{T * t;}; + +// CHECK: @templ_int = global %struct.templ undef, align 8 +templ templ_int [[clang::loader_uninitialized]]; + +// CHECK: @templ_trivial = global %struct.templ.0 undef, align 8 +templ templ_trivial [[clang::loader_uninitialized]]; + +// CHECK: @templ_incomplete = global %struct.templ.1 undef, align 8 +struct incomplete; +templ templ_incomplete [[clang::loader_uninitialized]]; diff --git a/clang/test/Sema/attr-loader-uninitialized.c b/clang/test/Sema/attr-loader-uninitialized.c --- a/clang/test/Sema/attr-loader-uninitialized.c +++ b/clang/test/Sema/attr-loader-uninitialized.c @@ -10,6 +10,10 @@ extern int external_rejected __attribute__((loader_uninitialized)); // expected-error@-1 {{variable 'external_rejected' cannot be declared both 'extern' and with the 'loader_uninitialized' attribute}} +struct S; +extern struct S incomplete_external_rejected __attribute__((loader_uninitialized)); +// expected-error@-1 {{variable 'incomplete_external_rejected' cannot be declared both 'extern' and with the 'loader_uninitialized' attribute}} + int noargs __attribute__((loader_uninitialized(0))); // expected-error@-1 {{'loader_uninitialized' attribute takes no arguments}} @@ -35,3 +39,8 @@ extern __attribute__((visibility("hidden"))) int extern_hidden __attribute__((loader_uninitialized)); // expected-error@-1 {{variable 'extern_hidden' cannot be declared both 'extern' and with the 'loader_uninitialized' attribute}} + +struct Incomplete; +struct Incomplete incomplete __attribute__((loader_uninitialized)); +// expected-error@-1 {{variable has incomplete type 'struct Incomplete'}} +// expected-note@-3 {{forward declaration of 'struct Incomplete'}} diff --git a/clang/test/Sema/attr-loader-uninitialized.cpp b/clang/test/Sema/attr-loader-uninitialized.cpp --- a/clang/test/Sema/attr-loader-uninitialized.cpp +++ b/clang/test/Sema/attr-loader-uninitialized.cpp @@ -9,6 +9,10 @@ extern int external_rejected __attribute__((loader_uninitialized)); // expected-error@-1 {{variable 'external_rejected' cannot be declared both 'extern' and with the 'loader_uninitialized' attribute}} +struct S; +extern S incomplete_external_rejected __attribute__((loader_uninitialized)); +// expected-error@-1 {{variable 'incomplete_external_rejected' cannot be declared both 'extern' and with the 'loader_uninitialized' attribute}} + int noargs __attribute__((loader_uninitialized(0))); // expected-error@-1 {{'loader_uninitialized' attribute takes no arguments}} @@ -58,3 +62,12 @@ nontrivial needs_trivial_ctor __attribute__((loader_uninitialized)); // expected-error@-1 {{variable with 'loader_uninitialized' attribute must have a trivial default constructor}} + +struct Incomplete; +Incomplete incomplete __attribute__((loader_uninitialized)); +// expected-error@-1 {{variable has incomplete type 'Incomplete'}} +// expected-note@-3 {{forward declaration of 'Incomplete'}} + +struct Incomplete s_incomplete __attribute__((loader_uninitialized)); +// expected-error@-1 {{variable has incomplete type 'struct Incomplete'}} +// expected-note@-7 {{forward declaration of 'Incomplete'}}