"default visibility for functions and variables [-fvisibility]")
ENUM_LANGOPT(TypeVisibilityMode, Visibility, 3, DefaultVisibility,
"default visibility for types [-ftype-visibility]")
+LANGOPT(SetVisibilityForExternDecls, 1, 0,
+ "apply global symbol visibility to external declarations without an explicit visibility")
ENUM_LANGOPT(StackProtector, StackProtectorMode, 2, SSPOff,
"stack protector mode")
ENUM_LANGOPT(TrivialAutoVarInit, TrivialAutoVarInitKind, 2, TrivialAutoVarInitKind::Uninitialized,
HelpText<"Default type and symbol visibility">;
def ftype_visibility : Separate<["-"], "ftype-visibility">,
HelpText<"Default type visibility">;
+def fapply_global_visibility_to_externs : Flag<["-"], "fapply-global-visibility-to-externs">,
+ HelpText<"Apply global symbol visibility to external declarations without an explicit visibility">;
def ftemplate_depth : Separate<["-"], "ftemplate-depth">,
HelpText<"Maximum depth of recursive template instantiation">;
def foperator_arrow_depth : Separate<["-"], "foperator-arrow-depth">,
}
if (!D)
return;
- // Set visibility for definitions.
+ // Set visibility for definitions, and for declarations if requested globally
+ // or set explicitly.
LinkageInfo LV = D->getLinkageAndVisibility();
- if (LV.isVisibilityExplicit() || !GV->isDeclarationForLinker())
+ if (LV.isVisibilityExplicit() || getLangOpts().SetVisibilityForExternDecls ||
+ !GV->isDeclarationForLinker())
GV->setVisibility(GetLLVMVisibility(LV.getVisibility()));
}
options::OPT_fvisibility_ms_compat)) {
CC1Args.push_back("-fvisibility");
CC1Args.push_back("hidden");
+ CC1Args.push_back("-fapply-global-visibility-to-externs");
}
}
// Default to "hidden" visibility, as object level linking will not be
// supported for the foreseeable future.
if (!DriverArgs.hasArg(options::OPT_fvisibility_EQ,
- options::OPT_fvisibility_ms_compat))
+ options::OPT_fvisibility_ms_compat)) {
CC1Args.append({"-fvisibility", "hidden"});
+ CC1Args.push_back("-fapply-global-visibility-to-externs");
+ }
}
llvm::opt::DerivedArgList *
if (Args.hasArg(OPT_fvisibility_global_new_delete_hidden))
Opts.GlobalAllocationFunctionVisibilityHidden = 1;
+ if (Args.hasArg(OPT_fapply_global_visibility_to_externs))
+ Opts.SetVisibilityForExternDecls = 1;
+
if (Args.hasArg(OPT_ftrapv)) {
Opts.setSignedOverflowBehavior(LangOptions::SOB_Trapping);
// Set the handler, if one is specified.
--- /dev/null
+// RUN: %clang_cc1 %s -std=c11 -triple=x86_64-pc-linux -fvisibility hidden -fapply-global-visibility-to-externs -emit-llvm -o - | FileCheck --check-prefix=CHECK-HIDDEN %s
+// RUN: %clang_cc1 %s -std=c11 -triple=x86_64-pc-linux -fvisibility protected -fapply-global-visibility-to-externs -emit-llvm -o - | FileCheck --check-prefix=CHECK-PROTECTED %s
+// RUN: %clang_cc1 %s -std=c11 -triple=x86_64-pc-linux -fvisibility default -fapply-global-visibility-to-externs -emit-llvm -o - | FileCheck --check-prefix=CHECK-DEFAULT %s
+
+// CHECK-HIDDEN: @var_hidden = external hidden global
+// CHECK-PROTECTED: @var_hidden = external hidden global
+// CHECK-DEFAULT: @var_hidden = external hidden global
+__attribute__((visibility("hidden"))) extern int var_hidden;
+// CHECK-HIDDEN: @var_protected = external protected global
+// CHECK-PROTECTED: @var_protected = external protected global
+// CHECK-DEFAULT: @var_protected = external protected global
+__attribute__((visibility("protected"))) extern int var_protected;
+// CHECK-HIDDEN: @var_default = external global
+// CHECK-PROTECTED: @var_default = external global
+// CHECK-DEFAULT: @var_default = external global
+__attribute__((visibility("default"))) extern int var_default;
+// CHECK-HIDDEN: @var = external hidden global
+// CHECK-PROTECTED: @var = external protected global
+// CHECK-DEFAULT: @var = external global
+extern int var;
+
+// CHECK-HIDDEN: declare hidden i32 @func_hidden()
+// CHECK-PROTECTED: declare hidden i32 @func_hidden()
+// CHECK-DEFAULT: declare hidden i32 @func_hidden()
+__attribute__((visibility("hidden"))) int func_hidden(void);
+// CHECK-HIDDEN: declare protected i32 @func_protected()
+// CHECK-PROTECTED: declare protected i32 @func_protected()
+// CHECK-DEFAULT: declare protected i32 @func_protected()
+__attribute__((visibility("protected"))) int func_protected(void);
+// CHECK-HIDDEN: declare i32 @func_default()
+// CHECK-PROTECTED: declare i32 @func_default()
+// CHECK-DEFAULT: declare i32 @func_default()
+__attribute__((visibility("default"))) int func_default(void);
+// CHECK-HIDDEN: declare hidden i32 @func()
+// CHECK-PROTECTED: declare protected i32 @func()
+// CHECK-DEFAULT: declare i32 @func()
+int func(void);
+
+int use() {
+ return var_hidden + var_protected + var_default + var +
+ func_hidden() + func_protected() + func_default() + func();
+}
// RUN: %clang -### -target amdgcn-amd-amdhsa -x cl -c -emit-llvm -fvisibility=protected %s 2>&1 | FileCheck -check-prefix=OVERRIDE-PROTECTED %s
// RUN: %clang -### -target amdgcn-amd-amdhsa -x cl -c -emit-llvm -fvisibility-ms-compat %s 2>&1 | FileCheck -check-prefix=OVERRIDE-MS %s
-// DEFAULT: "-fvisibility" "hidden"
+// DEFAULT-DAG: "-fvisibility" "hidden"
+// DEFAULT-DAG: "-fapply-global-visibility-to-externs"
+
+// OVERRIDE-PROTECTED-NOT: "-fapply-global-visibility-to-externs"
// OVERRIDE-PROTECTED: "-fvisibility" "protected"
-// OVERRIDE-MS: "-fvisibility" "hidden" "-ftype-visibility" "default"
+// OVERRIDE-PROTECTED-NOT: "-fapply-global-visibility-to-externs"
+
+// OVERRIDE-MS-NOT: "-fapply-global-visibility-to-externs"
+// OVERRIDE-MS-DAG: "-fvisibility" "hidden"
+// OVERRIDE-MS-DAG: "-ftype-visibility" "default"
+// OVERRIDE-MS-NOT: "-fapply-global-visibility-to-externs"
// CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc"
// CHECK-SAME: {{.*}} "-main-file-name" "a.cu" {{.*}} "-target-cpu" "gfx803"
// CHECK-SAME: "-fcuda-is-device" "-fvisibility" "hidden"
+// CHECK-SAME: "-fapply-global-visibility-to-externs"
// CHECK-SAME: {{.*}} "-o" [[A_BC_803:".*bc"]] "-x" "hip"
// CHECK-SAME: {{.*}} [[A_SRC:".*a.cu"]]
// CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc"
// CHECK-SAME: {{.*}} "-main-file-name" "a.cu" {{.*}} "-target-cpu" "gfx900"
// CHECK-SAME: "-fcuda-is-device" "-fvisibility" "hidden"
+// CHECK-SAME: "-fapply-global-visibility-to-externs"
// CHECK-SAME: {{.*}} "-o" [[A_BC_900:".*bc"]] "-x" "hip"
// CHECK-SAME: {{.*}} [[A_SRC]]
// CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc"
// CHECK-SAME: {{.*}} "-main-file-name" "b.hip" {{.*}} "-target-cpu" "gfx803"
// CHECK-SAME: "-fcuda-is-device" "-fvisibility" "hidden"
+// CHECK-SAME: "-fapply-global-visibility-to-externs"
// CHECK-SAME: {{.*}} "-o" [[B_BC_803:".*bc"]] "-x" "hip"
// CHECK-SAME: {{.*}} [[B_SRC:".*b.hip"]]
// CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc"
// CHECK-SAME: {{.*}} "-main-file-name" "b.hip" {{.*}} "-target-cpu" "gfx900"
// CHECK-SAME: "-fcuda-is-device" "-fvisibility" "hidden"
+// CHECK-SAME: "-fapply-global-visibility-to-externs"
// CHECK-SAME: {{.*}} "-o" [[B_BC_900:".*bc"]] "-x" "hip"
// CHECK-SAME: {{.*}} [[B_SRC]]
// RUN: %S/Inputs/hip_multiple_inputs/b.hip \
// RUN: 2>&1 | FileCheck %s
-// CHECK: [[CLANG:".*clang.*"]] "-cc1" "-triple" "amdgcn-amd-amdhsa"
+// CHECK: [[CLANG:".*clang.*"]] "-cc1" "-triple" "amdgcn-amd-amdhsa"
// CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc"
// CHECK-SAME: {{.*}} "-main-file-name" "a.cu" {{.*}} "-target-cpu" "gfx803"
// CHECK-SAME: "-fcuda-is-device" "-fgpu-rdc" "-fvisibility" "hidden"
+// CHECK-SAME: "-fapply-global-visibility-to-externs"
// CHECK-SAME: {{.*}} "-o" [[A_BC:".*bc"]] "-x" "hip"
// CHECK-SAME: {{.*}} [[A_SRC:".*a.cu"]]
// CHECK-SAME: "-aux-triple" "x86_64-unknown-linux-gnu" "-emit-llvm-bc"
// CHECK-SAME: {{.*}} "-main-file-name" "b.hip" {{.*}} "-target-cpu" "gfx803"
// CHECK-SAME: "-fcuda-is-device" "-fgpu-rdc" "-fvisibility" "hidden"
+// CHECK-SAME: "-fapply-global-visibility-to-externs"
// CHECK-SAME: {{.*}} "-o" [[B_BC:".*bc"]] "-x" "hip"
// CHECK-SAME: {{.*}} [[B_SRC:".*b.hip"]]