From 6593df241a1d119c84c0d97eec4486d2756c4ea1 Mon Sep 17 00:00:00 2001 From: Elizabeth Andrews Date: Wed, 22 Aug 2018 19:05:19 +0000 Subject: Currently clang does not emit unused static constants. GCC emits these constants by default when there is no optimization. GCC's option -fno-keep-static-consts can be used to not emit unused static constants. In Clang, since default behavior does not keep unused static constants, -fkeep-static-consts can be used to emit these if required. 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. Differential Revision: https://reviews.llvm.org/D40925 llvm-svn: 340439 --- clang/lib/CodeGen/CodeGenModule.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'clang/lib/CodeGen/CodeGenModule.cpp') diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index aaaf6c6..29692ee 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -1350,6 +1350,12 @@ void CodeGenModule::SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV) { if (D && D->hasAttr()) addUsedGlobal(GV); + + if (CodeGenOpts.KeepStaticConsts && D && isa(D)) { + const auto *VD = cast(D); + if (VD->getType().isConstQualified() && VD->getStorageClass() == SC_Static) + addUsedGlobal(GV); + } } bool CodeGenModule::GetCPUAndFeaturesAttributes(const Decl *D, @@ -1985,6 +1991,13 @@ bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) { if (LangOpts.EmitAllDecls) return true; + if (CodeGenOpts.KeepStaticConsts) { + const auto *VD = dyn_cast(Global); + if (VD && VD->getType().isConstQualified() && + VD->getStorageClass() == SC_Static) + return true; + } + return getContext().DeclMustBeEmitted(Global); } -- cgit v1.1