Index: include/clang/Basic/LangOptions.def =================================================================== --- include/clang/Basic/LangOptions.def +++ include/clang/Basic/LangOptions.def @@ -319,6 +319,8 @@ BENIGN_LANGOPT(AllowEditorPlaceholders, 1, 0, "allow editor placeholders in source") +BENIGN_LANGOPT(KeepStaticConsts , 1, 0, "keep static const variables even if unused") + #undef LANGOPT #undef COMPATIBLE_LANGOPT #undef BENIGN_LANGOPT Index: include/clang/Driver/Options.td =================================================================== --- include/clang/Driver/Options.td +++ include/clang/Driver/Options.td @@ -835,6 +835,8 @@ def fjump_tables : Flag<["-"], "fjump-tables">, Group; def fno_jump_tables : Flag<["-"], "fno-jump-tables">, Group, Flags<[CC1Option]>, HelpText<"Do not use jump tables for lowering switches">; +def fkeep_static_consts : Flag<["-"], "fkeep-static-consts">, Group, Flags<[CC1Option]>, + HelpText<"Keep static const variables even if unused">; // Begin sanitizer flags. These should all be core options exposed in all driver // modes. Index: lib/AST/ASTContext.cpp =================================================================== --- lib/AST/ASTContext.cpp +++ lib/AST/ASTContext.cpp @@ -9314,6 +9314,12 @@ if (D->hasAttr() || D->hasAttr()) return true; + // Emit static constants even if they are not used if KeepStaticConsts is set. + if (const VarDecl *VD = dyn_cast(D)) { + if (LangOpts.KeepStaticConsts && VD->getType().isConstQualified()) + return true; + } + if (const FunctionDecl *FD = dyn_cast(D)) { // Forward declarations aren't required. if (!FD->doesThisDeclarationHaveABody()) Index: lib/Driver/ToolChains/Clang.cpp =================================================================== --- lib/Driver/ToolChains/Clang.cpp +++ lib/Driver/ToolChains/Clang.cpp @@ -3803,6 +3803,7 @@ Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls); Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions); Args.AddLastArg(CmdArgs, options::OPT_fno_operator_names); + Args.AddLastArg(CmdArgs, options::OPT_fkeep_static_consts); // Emulated TLS is enabled by default on Android and OpenBSD, and can be enabled // manually with -femulated-tls. bool EmulatedTLSDefault = Triple.isAndroid() || Triple.isOSOpenBSD() || Index: lib/Frontend/CompilerInvocation.cpp =================================================================== --- lib/Frontend/CompilerInvocation.cpp +++ lib/Frontend/CompilerInvocation.cpp @@ -2509,6 +2509,7 @@ Opts.HalfArgsAndReturns = Args.hasArg(OPT_fallow_half_arguments_and_returns) | Opts.NativeHalfArgsAndReturns; Opts.GNUAsm = !Args.hasArg(OPT_fno_gnu_inline_asm); + Opts.KeepStaticConsts = Args.hasArg(OPT_fkeep_static_consts); // __declspec is enabled by default for the PS4 by the driver, and also // enabled for Microsoft Extensions or Borland Extensions, here. Index: test/CodeGen/keep-static-consts.cpp =================================================================== --- /dev/null +++ test/CodeGen/keep-static-consts.cpp @@ -0,0 +1,4 @@ +// RUN: %clang_cc1 -fkeep-static-consts -emit-llvm %s -o - -triple=x86_64-unknown-linux-gnu | FileCheck %s + +// CHECK: @_ZL3var = internal constant i32 10, align 4 +static const int var = 10;