Skip to content

Reland [clang] Handle instantiated members to determine visibility (#136128) #136689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ Bug Fixes in This Version
- Fixed a crash when ``#embed`` appears as a part of a failed constant
evaluation. The crashes were happening during diagnostics emission due to
unimplemented statement printer. (#GH132641)
- Fixed visibility calculation for template functions. (#GH103477)

Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
13 changes: 10 additions & 3 deletions clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,9 @@ void LinkageComputer::mergeTemplateLV(
FunctionTemplateDecl *temp = specInfo->getTemplate();
// Merge information from the template declaration.
LinkageInfo tempLV = getLVForDecl(temp, computation);
// The linkage of the specialization should be consistent with the
// template declaration.
LV.setLinkage(tempLV.getLinkage());
// The linkage and visibility of the specialization should be
// consistent with the template declaration.
LV.mergeMaybeWithVisibility(tempLV, considerVisibility);

// Merge information from the template parameters.
LinkageInfo paramsLV =
Expand Down Expand Up @@ -1051,6 +1051,13 @@ LinkageComputer::getLVForClassMember(const NamedDecl *D,
if (const auto *redeclTemp = dyn_cast<RedeclarableTemplateDecl>(temp)) {
if (isExplicitMemberSpecialization(redeclTemp)) {
explicitSpecSuppressor = temp->getTemplatedDecl();
} else if (const RedeclarableTemplateDecl *from =
redeclTemp->getInstantiatedFromMemberTemplate()) {
// If no explicit visibility is specified yet, and this is an
// instantiated member of a template, look up visibility there
// as well.
LinkageInfo fromLV = from->getLinkageAndVisibility();
LV.mergeMaybeWithVisibility(fromLV, considerVisibility);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions clang/lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4787,8 +4787,12 @@ LinkageInfo LinkageComputer::computeTypeLinkageInfo(const Type *T) {
return computeTypeLinkageInfo(cast<ReferenceType>(T)->getPointeeType());
case Type::MemberPointer: {
const auto *MPT = cast<MemberPointerType>(T);
LinkageInfo LV =
getDeclLinkageAndVisibility(MPT->getMostRecentCXXRecordDecl());
LinkageInfo LV;
if (auto *D = MPT->getMostRecentCXXRecordDecl()) {
LV.merge(getDeclLinkageAndVisibility(D));
} else if (auto *Ty = MPT->getQualifier()->getAsType()) {
LV.merge(computeTypeLinkageInfo(Ty));
}
LV.merge(computeTypeLinkageInfo(MPT->getPointeeType()));
return LV;
}
Expand Down
75 changes: 74 additions & 1 deletion clang/test/CodeGenCXX/visibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1457,9 +1457,82 @@ namespace test71 {
// CHECK-LABEL: declare hidden noundef i32 @_ZN6test713fooIiE3zedEv(
// CHECK-LABEL: define linkonce_odr noundef i32 @_ZN6test713fooIiE3barIiEET_v(
// CHECK-LABEL: define linkonce_odr hidden noundef i64 @_ZN6test713fooIlE3zedEv(
// CHECK-LABEL: define linkonce_odr noundef i32 @_ZN6test713fooIlE3barIiEET_v(
// CHECK-LABEL: define linkonce_odr hidden noundef i32 @_ZN6test713fooIlE3barIiEET_v(
// CHECK-HIDDEN-LABEL: declare hidden noundef i32 @_ZN6test713fooIiE3zedEv(
// CHECK-HIDDEN-LABEL: define linkonce_odr noundef i32 @_ZN6test713fooIiE3barIiEET_v(
// CHECK-HIDDEN-LABEL: define linkonce_odr hidden noundef i64 @_ZN6test713fooIlE3zedEv(
// CHECK-HIDDEN-LABEL: define linkonce_odr hidden noundef i32 @_ZN6test713fooIlE3barIiEET_v(
}

// https://github.com/llvm/llvm-project/issues/103477
namespace test72 {
template <class a>
struct t {
template <int>
static HIDDEN void bar() {}
};

void test() {
t<char>::bar<1>();
}
// CHECK-LABEL: define linkonce_odr hidden void @_ZN6test721tIcE3barILi1EEEvv(
// CHECK-HIDDEN-LABEL: define linkonce_odr hidden void @_ZN6test721tIcE3barILi1EEEvv(
}

// https://github.com/llvm/llvm-project/issues/31462
namespace test73 {
template <class T> struct s {
template <class U>
__attribute__((__visibility__("hidden"))) U should_not_be_exported();
};

template <class T> template <class U> U s<T>::should_not_be_exported() {
return U();
}

extern template struct __attribute__((__visibility__("default"))) s<int>;

int f() {
s<int> o;
return o.should_not_be_exported<int>();
}
// CHECK-LABEL: define linkonce_odr noundef i32 @_ZN6test731sIiE22should_not_be_exportedIiEET_v(
// CHECK-HIDDEN-LABEL: define linkonce_odr noundef i32 @_ZN6test731sIiE22should_not_be_exportedIiEET_v(
}

namespace test74 {
template <typename> struct T;
template <typename R>
struct T<void (R::*)()> {
template <typename M>
static __attribute__((__visibility__("hidden"))) void Invoke(M) {
}
};

struct C;
void (C::*MM)();

void Fun() {
T<decltype(MM)>::Invoke(0);
}
// CHECK-LABEL: define linkonce_odr hidden void @_ZN6test741TIMNS_1CEFvvEE6InvokeIiEEvT_(
// CHECK-HIDDEN-LABEL: define linkonce_odr hidden void @_ZN6test741TIMNS_1CEFvvEE6InvokeIiEEvT_(
}

namespace test75 {
template <class> struct T;
template <class C, class Ret>
struct T<Ret C::*> {
template <class M>
static __attribute__((__visibility__("hidden")))
void Invoke(M) {
}
};

struct A;
void Fun() {
T<void (A::*)()>::Invoke(0);
}
// CHECK-LABEL: define linkonce_odr hidden void @_ZN6test751TIMNS_1AEFvvEE6InvokeIiEEvT_(
// CHECK-HIDDEN-LABEL: define linkonce_odr hidden void @_ZN6test751TIMNS_1AEFvvEE6InvokeIiEEvT_(
}
Loading