@@ -3684,6 +3684,54 @@ static void inheritDefaultTemplateArguments(ASTContext &Context,
36843684 }
36853685}
36863686
3687+ // [basic.link]/p10:
3688+ // If two declarations of an entity are attached to different modules,
3689+ // the program is ill-formed;
3690+ static void checkMultipleDefinitionInNamedModules (ASTReader &Reader, Decl *D,
3691+ Decl *Previous) {
3692+ Module *M = Previous->getOwningModule ();
3693+
3694+ // We only care about the case in named modules.
3695+ if (!M || !M->isNamedModule ())
3696+ return ;
3697+
3698+ // If it is previous implcitly introduced, it is not meaningful to
3699+ // diagnose it.
3700+ if (Previous->isImplicit ())
3701+ return ;
3702+
3703+ // FIXME: Get rid of the enumeration of decl types once we have an appropriate
3704+ // abstract for decls of an entity. e.g., the namespace decl and using decl
3705+ // doesn't introduce an entity.
3706+ if (!isa<VarDecl, FunctionDecl, TagDecl, RedeclarableTemplateDecl>(Previous))
3707+ return ;
3708+
3709+ // Skip implicit instantiations since it may give false positive diagnostic
3710+ // messages.
3711+ // FIXME: Maybe this shows the implicit instantiations may have incorrect
3712+ // module owner ships. But given we've finished the compilation of a module,
3713+ // how can we add new entities to that module?
3714+ if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Previous);
3715+ VTSD && !VTSD->isExplicitSpecialization ())
3716+ return ;
3717+ if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Previous);
3718+ CTSD && !CTSD->isExplicitSpecialization ())
3719+ return ;
3720+ if (auto *Func = dyn_cast<FunctionDecl>(Previous))
3721+ if (auto *FTSI = Func->getTemplateSpecializationInfo ();
3722+ FTSI && !FTSI->isExplicitSpecialization ())
3723+ return ;
3724+
3725+ // It is fine if they are in the same module.
3726+ if (Reader.getContext ().isInSameModule (M, D->getOwningModule ()))
3727+ return ;
3728+
3729+ Reader.Diag (Previous->getLocation (),
3730+ diag::err_multiple_decl_in_different_modules)
3731+ << cast<NamedDecl>(Previous) << M->Name ;
3732+ Reader.Diag (D->getLocation (), diag::note_also_found);
3733+ }
3734+
36873735void ASTDeclReader::attachPreviousDecl (ASTReader &Reader, Decl *D,
36883736 Decl *Previous, Decl *Canon) {
36893737 assert (D && Previous);
@@ -3697,22 +3745,7 @@ void ASTDeclReader::attachPreviousDecl(ASTReader &Reader, Decl *D,
36973745#include " clang/AST/DeclNodes.inc"
36983746 }
36993747
3700- // [basic.link]/p10:
3701- // If two declarations of an entity are attached to different modules,
3702- // the program is ill-formed;
3703- //
3704- // FIXME: Get rid of the enumeration of decl types once we have an appropriate
3705- // abstract for decls of an entity. e.g., the namespace decl and using decl
3706- // doesn't introduce an entity.
3707- if (Module *M = Previous->getOwningModule ();
3708- M && M->isNamedModule () &&
3709- isa<VarDecl, FunctionDecl, TagDecl, RedeclarableTemplateDecl>(Previous) &&
3710- !Reader.getContext ().isInSameModule (M, D->getOwningModule ())) {
3711- Reader.Diag (Previous->getLocation (),
3712- diag::err_multiple_decl_in_different_modules)
3713- << cast<NamedDecl>(Previous) << M->Name ;
3714- Reader.Diag (D->getLocation (), diag::note_also_found);
3715- }
3748+ checkMultipleDefinitionInNamedModules (Reader, D, Previous);
37163749
37173750 // If the declaration was visible in one module, a redeclaration of it in
37183751 // another module remains visible even if it wouldn't be visible by itself.
0 commit comments