diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -1349,19 +1349,24 @@ // If a function is hidden by -fvisibility-inlines-hidden option and // is not explicitly attributed as a hidden function, // we should not make static local variables in the function hidden. - LV = getLVForDecl(FD, computation); - if (isa(D) && useInlineVisibilityHidden(FD) && - !LV.isVisibilityExplicit() && - !Context.getLangOpts().VisibilityInlinesHiddenStaticLocalVar) { + bool mayBeVisible = + isa(D) && useInlineVisibilityHidden(FD) && + !Context.getLangOpts().VisibilityInlinesHiddenStaticLocalVar; + LVComputationKind nestedComputation = + mayBeVisible + ? LVComputationKind(computation.getExplicitVisibilityKind()) + : computation; + LV = getLVForDecl(FD, nestedComputation); + if (mayBeVisible && !LV.isVisibilityExplicit()) { assert(cast(D)->isStaticLocal()); // If this was an implicitly hidden inline method, check again for // explicit visibility on the parent class, and use that for static locals // if present. if (const auto *MD = dyn_cast(FD)) - LV = getLVForDecl(MD->getParent(), computation); + LV = getLVForDecl(MD->getParent(), nestedComputation); if (!LV.isVisibilityExplicit()) { Visibility globalVisibility = - computation.isValueVisibility() + nestedComputation.isValueVisibility() ? Context.getLangOpts().getValueVisibilityMode() : Context.getLangOpts().getTypeVisibilityMode(); return LinkageInfo(VisibleNoLinkage, globalVisibility, diff --git a/clang/test/CodeGenCXX/linkage-static-local-crash.cpp b/clang/test/CodeGenCXX/linkage-static-local-crash.cpp new file mode 100644 --- /dev/null +++ b/clang/test/CodeGenCXX/linkage-static-local-crash.cpp @@ -0,0 +1,19 @@ +// RUN: %clang_cc1 -triple powerpc-ibm-aix -mdefault-visibility-export-mapping=explicit -fvisibility-inlines-hidden -emit-llvm %s -o - | FileCheck %s + +struct C { + template + __attribute__((__visibility__("hidden"))) + static int& f() { + static int g = 42; + return g; + } +}; + +template +void foo(T i) { C::f(); } + +void bar() { + foo([]{}); +} + +// CHECK: @"_ZZN1C1fIZ3barvE3$_0EERivE1g" = internal global i32 42, align 4