Make -fvisibility-inlines-hidden apply to static local variables in inline functions on Darwin

This effectively disables r340386 on Darwin, and provides a command line flag
to opt into/out of this behaviour. This change is needed to compile certain
Apple headers correctly.

rdar://47688592

Differential revision: https://reviews.llvm.org/D86881
This commit is contained in:
Erik Pilkington 2020-09-01 20:17:00 -04:00
parent 8ff44e644b
commit d46f2c51e4
8 changed files with 90 additions and 1 deletions

View File

@ -265,6 +265,9 @@ BENIGN_LANGOPT(DumpRecordLayoutsSimple , 1, 0, "dumping the layout of IRgen'd re
BENIGN_LANGOPT(DumpVTableLayouts , 1, 0, "dumping the layouts of emitted vtables")
LANGOPT(NoConstantCFStrings , 1, 0, "no constant CoreFoundation strings")
BENIGN_LANGOPT(InlineVisibilityHidden , 1, 0, "hidden visibility for inline C++ methods")
BENIGN_LANGOPT(VisibilityInlinesHiddenStaticLocalVar, 1, 0,
"hidden visibility for static local variables in inline C++ "
"methods when -fvisibility-inlines hidden is enabled")
LANGOPT(GlobalAllocationFunctionVisibilityHidden , 1, 0, "hidden visibility for global operator new and delete declaration")
BENIGN_LANGOPT(ParseUnknownAnytype, 1, 0, "__unknown_anytype")
BENIGN_LANGOPT(DebuggerSupport , 1, 0, "debugger support")

View File

@ -1943,6 +1943,17 @@ def fvisibility_EQ : Joined<["-"], "fvisibility=">, Group<f_Group>,
def fvisibility_inlines_hidden : Flag<["-"], "fvisibility-inlines-hidden">, Group<f_Group>,
HelpText<"Give inline C++ member functions hidden visibility by default">,
Flags<[CC1Option]>;
def fvisibility_inlines_hidden_static_local_var :
Flag<["-"], "fvisibility-inlines-hidden-static-local-var">, Group<f_Group>,
HelpText<"When -fvisibility-inlines-hidden is enabled, static variables in "
"inline C++ member functions will also be given hidden visibility "
"by default">,
Flags<[CC1Option]>;
def fno_visibility_inlines_hidden_static_local_var :
Flag<["-"], "fno-visibility-inlines-hidden-static-local-var">, Group<f_Group>,
HelpText<"Disables -fvisibility-inlines-hidden-static-local-var "
"(this is the default on non-darwin targets)">,
Flags<[CC1Option]>;
def fvisibility_ms_compat : Flag<["-"], "fvisibility-ms-compat">, Group<f_Group>,
HelpText<"Give global types 'default' visibility and global functions and "
"variables 'hidden' visibility by default">;

View File

@ -1299,7 +1299,8 @@ LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D,
// we should not make static local variables in the function hidden.
LV = getLVForDecl(FD, computation);
if (isa<VarDecl>(D) && useInlineVisibilityHidden(FD) &&
!LV.isVisibilityExplicit()) {
!LV.isVisibilityExplicit() &&
!Context.getLangOpts().VisibilityInlinesHiddenStaticLocalVar) {
assert(cast<VarDecl>(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

View File

@ -5210,6 +5210,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
}
Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden_static_local_var,
options::OPT_fno_visibility_inlines_hidden_static_local_var);
Args.AddLastArg(CmdArgs, options::OPT_fvisibility_global_new_delete_hidden);
Args.AddLastArg(CmdArgs, options::OPT_ftlsmodel_EQ);

View File

@ -2408,6 +2408,13 @@ void Darwin::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
// Enable compatibility mode for NSItemProviderCompletionHandler in
// Foundation/NSItemProvider.h.
CC1Args.push_back("-fcompatibility-qualified-id-block-type-checking");
// Give static local variables in inline functions hidden visibility when
// -fvisibility-inlines-hidden is enabled.
if (!DriverArgs.getLastArgNoClaim(
options::OPT_fvisibility_inlines_hidden_static_local_var,
options::OPT_fno_visibility_inlines_hidden_static_local_var))
CC1Args.push_back("-fvisibility-inlines-hidden-static-local-var");
}
DerivedArgList *

View File

@ -2766,6 +2766,9 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
if (Args.hasArg(OPT_fvisibility_inlines_hidden))
Opts.InlineVisibilityHidden = 1;
if (Args.hasArg(OPT_fvisibility_inlines_hidden_static_local_var))
Opts.VisibilityInlinesHiddenStaticLocalVar = 1;
if (Args.hasArg(OPT_fvisibility_global_new_delete_hidden))
Opts.GlobalAllocationFunctionVisibilityHidden = 1;

View File

@ -0,0 +1,53 @@
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fvisibility-inlines-hidden -fvisibility-inlines-hidden-static-local-var %s -emit-llvm -o - | FileCheck %s
#define used __attribute__((used))
used inline void f1() {
// CHECK: @_ZZ2f1vE6f1_var = linkonce_odr hidden global i32 0
static int f1_var = 0;
}
__attribute__((visibility("default")))
used inline void f2() {
// CHECK: @_ZZ2f2vE6f2_var = linkonce_odr global i32 0
static int f2_var = 0;
}
struct S {
used void f3() {
// CHECK: @_ZZN1S2f3EvE6f3_var = linkonce_odr hidden global i32 0
static int f3_var = 0;
}
void f6();
void f7();
};
used void f4() {
// CHECK: @_ZZ2f4vE6f4_var = internal global i32 0
static int f4_var = 0;
}
__attribute__((visibility("default")))
used void f5() {
// CHECK: @_ZZ2f5vE6f5_var = internal global i32 0
static int f5_var = 0;
}
used void S::f6() {
// CHECK: @_ZZN1S2f6EvE6f6_var = internal global i32 0
static int f6_var = 0;
}
used inline void S::f7() {
// CHECK: @_ZZN1S2f7EvE6f7_var = linkonce_odr hidden global i32 0
static int f7_var = 0;
}
struct __attribute__((visibility("default"))) S2 {
used void f8() {
// CHECK: @_ZZN2S22f8EvE6f8_var = linkonce_odr hidden global i32 0
static int f8_var = 0;
}
};

View File

@ -46,3 +46,12 @@
// RUN: %clang -target x86_64-linux-gnu -### %s 2>&1 | FileCheck --check-prefix=OTHER_COMPATIBILITY %s
// DARWIN_COMPATIBILITY: -fcompatibility-qualified-id-block-type-checking
// OTHER_COMPATIBILITY-NOT: -fcompatibility-qualified-id-block-type-checking
// Add -fvisibility-inlines-hidden-static-local-var on Darwin.
// RUN: %clang -target x86_64-apple-darwin10 -### %s 2>&1 | FileCheck --check-prefix=DARWIN_INLINES_HIDDEN %s
// RUN: %clang -target x86_64-apple-darwin10 -fno-visibility-inlines-hidden-static-local-var -### %s 2>&1 | FileCheck --check-prefix=DARWIN_INLINES_HIDDEN_EXPLICIT_NO %s
// RUN: %clang -target x86_64-linux-gnu -### %s 2>&1 | FileCheck --check-prefix=NO_DARWIN_INLINES_HIDDEN %s
// DARWIN_INLINES_HIDDEN: -fvisibility-inlines-hidden-static-local-var
// DARWIN_INLINES_HIDDEN_EXPLICIT_NO-NOT: -fvisibility-inlines-hidden-static-local-var
// DARWIN_INLINES_HIDDEN_EXPLICIT_NO: -fno-visibility-inlines-hidden-static-local-var
// NO_DARWIN_INLINES_HIDDEN-NOT: -fvisibility-inlines-hidden-static-local-var