Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13822,7 +13822,8 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
!VDecl->isInvalidDecl() && VDecl->isThisDeclarationADefinition() &&
VDecl->getFormalLinkage() == Linkage::External && !VDecl->isInline() &&
!VDecl->isTemplated() && !isa<VarTemplateSpecializationDecl>(VDecl) &&
!VDecl->getInstantiatedFromStaticDataMember()) {
!VDecl->getInstantiatedFromStaticDataMember() &&
!(VDecl->hasAttr<SelectAnyAttr>() || VDecl->hasAttr<WeakAttr>())) {
Diag(VDecl->getLocation(), diag::err_extern_def_in_header_unit);
VDecl->setInvalidDecl();
}
Expand Down Expand Up @@ -16162,7 +16163,8 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
!FD->isInvalidDecl() && !FD->isInlined() &&
BodyKind != FnBodyKind::Delete && BodyKind != FnBodyKind::Default &&
FD->getFormalLinkage() == Linkage::External && !FD->isTemplated() &&
!FD->isTemplateInstantiation()) {
!FD->isTemplateInstantiation() &&
!(FD->hasAttr<SelectAnyAttr>() || FD->hasAttr<WeakAttr>())) {
assert(FD->isThisDeclarationADefinition());
Diag(FD->getLocation(), diag::err_extern_def_in_header_unit);
FD->setInvalidDecl();
Expand Down
13 changes: 13 additions & 0 deletions clang/test/CXX/module/module.import/p6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

// RUN: %clang_cc1 -std=c++20 -x c++-header %t/bad-header-unit.h \
// RUN: -emit-header-unit -o %t/bad-header-unit.pcm -verify
// RUN: %clang_cc1 -std=c++20 -x c++-header %t/bad-header-unit-declspec.h \
// RUN: -emit-header-unit -o %t/bad-header-unit.pcm -verify \
// RUN: -fdeclspec

//--- bad-header-unit.h

Expand Down Expand Up @@ -77,3 +80,13 @@ template <typename T> bool b() {
}

inline bool B = b<int>();

__attribute__((weak)) int weak_fun_definition() { return 42; }

__attribute__((weak)) int weak_var_definition = 42;

//--- bad-header-unit-declspec.h

/* The cases below should compile without diagnostics. */

__declspec(selectany) int selectany_var_definition = 42; // expected-no-diagnostics