From: Douglas Gregor Date: Fri, 9 Sep 2011 20:53:38 +0000 (+0000) Subject: Specializations cannot be module-hidden. Diagnose attempts to do so. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d023aec8907831a18d3514a95b843a7ee06b6b5e;p=clang Specializations cannot be module-hidden. Diagnose attempts to do so. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@139406 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticParseKinds.td b/include/clang/Basic/DiagnosticParseKinds.td index 96252654dd..0f3b998c65 100644 --- a/include/clang/Basic/DiagnosticParseKinds.td +++ b/include/clang/Basic/DiagnosticParseKinds.td @@ -574,12 +574,14 @@ def err_seh___except_filter : Error< def err_seh___finally_block : Error< "%0 only allowed in __finally block">; + +} // end of Parse Issue category. -// Modules +let CategoryName = "Modules Issue" in { def err_module_expected_ident : Error< "expected a module name after '__import_module__'">; def err_module_expected_semi : Error< "expected a semicolon name after module name">; - -} // end of Parse Issue category. +} + } // end of Parser diagnostics diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 846b32ba89..49d7bed09a 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -4719,7 +4719,9 @@ def note_related_result_type_inferred : Note< let CategoryName = "Modules Issue" in { def err_module_private_follows_public : Error< "__module_private__ declaration of %0 follows public declaration">; - +def err_module_private_specialization : Error< + "%select{template|partial|member}0 specialization cannot be " + "declared __module_private__">; } } // end of sema component. diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index 3306a0f7f5..4e61fd1744 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -3836,6 +3836,7 @@ public: DeclResult ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, + SourceLocation ModulePrivateLoc, CXXScopeSpec &SS, TemplateTy Template, SourceLocation TemplateNameLoc, diff --git a/lib/Parse/ParseDeclCXX.cpp b/lib/Parse/ParseDeclCXX.cpp index dbdb184d4a..75c65481c4 100644 --- a/lib/Parse/ParseDeclCXX.cpp +++ b/lib/Parse/ParseDeclCXX.cpp @@ -1141,7 +1141,7 @@ void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind, // Build the class template specialization. TagOrTempResult = Actions.ActOnClassTemplateSpecialization(getCurScope(), TagType, TUK, - StartLoc, SS, + StartLoc, DS.getModulePrivateSpecLoc(), SS, TemplateId->Template, TemplateId->TemplateNameLoc, TemplateId->LAngleLoc, diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 003be8289f..355378eded 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -3821,8 +3821,14 @@ Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, NewVD->setThreadSpecified(true); } - if (D.getDeclSpec().isModulePrivateSpecified()) - NewVD->setModulePrivate(); + if (D.getDeclSpec().isModulePrivateSpecified()) { + if (isExplicitSpecialization) + Diag(NewVD->getLocation(), diag::err_module_private_specialization) + << 2 + << FixItHint::CreateRemoval(D.getDeclSpec().getModulePrivateSpecLoc()); + else + NewVD->setModulePrivate(); + } // Set the lexical context. If the declarator has a C++ scope specifier, the // lexical context will be different from the semantic context. @@ -4709,9 +4715,17 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, // If __module_private__ was specified, mark the function accordingly. if (D.getDeclSpec().isModulePrivateSpecified()) { - NewFD->setModulePrivate(); - if (FunctionTemplate) - FunctionTemplate->setModulePrivate(); + if (isFunctionTemplateSpecialization) { + SourceLocation ModulePrivateLoc + = D.getDeclSpec().getModulePrivateSpecLoc(); + Diag(ModulePrivateLoc, diag::err_module_private_specialization) + << 0 + << FixItHint::CreateRemoval(ModulePrivateLoc); + } else { + NewFD->setModulePrivate(); + if (FunctionTemplate) + FunctionTemplate->setModulePrivate(); + } } // Filter out previous declarations that don't match the scope. @@ -7751,7 +7765,11 @@ CreateNewDecl: if (PrevDecl && PrevDecl->isModulePrivate()) New->setModulePrivate(); else if (ModulePrivateLoc.isValid()) { - if (PrevDecl && !PrevDecl->isModulePrivate()) + if (isExplicitSpecialization) + Diag(New->getLocation(), diag::err_module_private_specialization) + << 2 + << FixItHint::CreateRemoval(ModulePrivateLoc); + else if (PrevDecl && !PrevDecl->isModulePrivate()) diagnoseModulePrivateRedeclaration(New, PrevDecl, ModulePrivateLoc); else New->setModulePrivate(); diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index 4b3f6c45d5..f613ebb36c 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -4725,6 +4725,7 @@ DeclResult Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, TagUseKind TUK, SourceLocation KWLoc, + SourceLocation ModulePrivateLoc, CXXScopeSpec &SS, TemplateTy TemplateD, SourceLocation TemplateNameLoc, @@ -5090,6 +5091,11 @@ Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec, if (Attr) ProcessDeclAttributeList(S, Specialization, Attr); + if (ModulePrivateLoc.isValid()) + Diag(Specialization->getLocation(), diag::err_module_private_specialization) + << (isPartialSpecialization? 1 : 0) + << FixItHint::CreateRemoval(ModulePrivateLoc); + // Build the fully-sugared type for this class template // specialization as the user wrote in the specialization // itself. This means that we'll pretty-print the type retrieved diff --git a/test/Modules/module-private.cpp b/test/Modules/module-private.cpp index 3ee59c8a9c..0f1d5923dc 100644 --- a/test/Modules/module-private.cpp +++ b/test/Modules/module-private.cpp @@ -88,5 +88,22 @@ __module_private__ void public_func_template(); // expected-error{{__module_priv struct public_struct; // expected-note{{previous declaration is here}} __module_private__ struct public_struct; // expected-error{{__module_private__ declaration of 'public_struct' follows public declaration}} +// Check for attempts to make specializations private +template<> __module_private__ void public_func_template(); // expected-error{{template specialization cannot be declared __module_private__}} + +template +struct public_class { + struct inner_struct; + static int static_var; +}; + +template<> __module_private__ struct public_class::inner_struct { }; // expected-error{{member specialization cannot be declared __module_private__}} +template<> __module_private__ int public_class::static_var = 17; // expected-error{{member specialization cannot be declared __module_private__}} + +template<> +__module_private__ struct public_class { }; // expected-error{{template specialization cannot be declared __module_private__}} + +template +__module_private__ struct public_class { }; // expected-error{{partial specialization cannot be declared __module_private__}} #endif