eandrews created this revision.

Currently clang does not emit unused static constants declared globally. Local 
static constants are emitted by default. -fkeep-static-consts can be used to 
emit static constants declared globally even if they are not used.

This could be useful for producing identification strings like SVN identifiers 
inside the object file even though the string isn't used by the program.


https://reviews.llvm.org/D40925

Files:
  include/clang/Basic/LangOptions.def
  include/clang/Driver/Options.td
  lib/AST/ASTContext.cpp
  lib/Driver/ToolChains/Clang.cpp
  lib/Frontend/CompilerInvocation.cpp
  test/CodeGen/keep-static-consts.cpp


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;
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: 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/AST/ASTContext.cpp
===================================================================
--- lib/AST/ASTContext.cpp
+++ lib/AST/ASTContext.cpp
@@ -9314,6 +9314,12 @@
   if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
     return true;
 
+  // Emit static constants even if they are not used if KeepStaticConsts is 
set.
+  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
+    if (LangOpts.KeepStaticConsts && VD->getType().isConstQualified())
+      return true;
+  }
+
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
     // Forward declarations aren't required.
     if (!FD->doesThisDeclarationHaveABody())
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<f_Group>;
 def fno_jump_tables : Flag<["-"], "fno-jump-tables">, Group<f_Group>, 
Flags<[CC1Option]>,
   HelpText<"Do not use jump tables for lowering switches">;
+def fkeep_static_consts : Flag<["-"], "fkeep-static-consts">, Group<f_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: 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: 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;
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: 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/AST/ASTContext.cpp
===================================================================
--- lib/AST/ASTContext.cpp
+++ lib/AST/ASTContext.cpp
@@ -9314,6 +9314,12 @@
   if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
     return true;
 
+  // Emit static constants even if they are not used if KeepStaticConsts is set.
+  if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
+    if (LangOpts.KeepStaticConsts && VD->getType().isConstQualified())
+      return true;
+  }
+
   if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
     // Forward declarations aren't required.
     if (!FD->doesThisDeclarationHaveABody())
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<f_Group>;
 def fno_jump_tables : Flag<["-"], "fno-jump-tables">, Group<f_Group>, Flags<[CC1Option]>,
   HelpText<"Do not use jump tables for lowering switches">;
+def fkeep_static_consts : Flag<["-"], "fkeep-static-consts">, Group<f_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: 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
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to