aboutsummaryrefslogtreecommitdiff
path: root/clang/lib
diff options
context:
space:
mode:
authorYonghong Song <yhs@fb.com>2022-09-26 07:08:24 -0700
committerTobias Hieta <tobias@hieta.se>2022-10-04 11:28:35 +0200
commit67ac047d7f465e10f3d5e9678b458d8b0dbbbd9a (patch)
tree31bcaee931e1c50b2dfa8b0ad71b050ee4640c1d /clang/lib
parent541ea23a58e756fc381e3513f31fb6c92a5280a8 (diff)
downloadllvm-67ac047d7f465e10f3d5e9678b458d8b0dbbbd9a.zip
llvm-67ac047d7f465e10f3d5e9678b458d8b0dbbbd9a.tar.gz
llvm-67ac047d7f465e10f3d5e9678b458d8b0dbbbd9a.tar.bz2
[clang][DebugInfo] Emit debuginfo for non-constant case value
Currently, clang does not emit debuginfo for the switch stmt case value if it is an enum value. For example, $ cat test.c enum { AA = 1, BB = 2 }; int func1(int a) { switch(a) { case AA: return 10; case BB: return 11; default: break; } return 0; } $ llvm-dwarfdump test.o | grep AA $ Note that gcc does emit debuginfo for the same test case. This patch added such a support with similar implementation to CodeGenFunction::EmitDeclRefExprDbgValue(). With this patch, $ clang -g -c test.c $ llvm-dwarfdump test.o | grep AA DW_AT_name ("AA") $ Differential Revision: https://reviews.llvm.org/D134705 (cherry picked from commit 75be0482a2e2a78fae83f1ca604f4ee20d673796)
Diffstat (limited to 'clang/lib')
-rw-r--r--clang/lib/CodeGen/CGStmt.cpp15
1 files changed, 15 insertions, 0 deletions
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 481438d..9935fcc 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -1509,6 +1509,21 @@ void CodeGenFunction::EmitCaseStmt(const CaseStmt &S,
llvm::ConstantInt *CaseVal =
Builder.getInt(S.getLHS()->EvaluateKnownConstInt(getContext()));
+
+ // Emit debuginfo for the case value if it is an enum value.
+ const ConstantExpr *CE;
+ if (auto ICE = dyn_cast<ImplicitCastExpr>(S.getLHS()))
+ CE = dyn_cast<ConstantExpr>(ICE->getSubExpr());
+ else
+ CE = dyn_cast<ConstantExpr>(S.getLHS());
+ if (CE) {
+ if (auto DE = dyn_cast<DeclRefExpr>(CE->getSubExpr()))
+ if (CGDebugInfo *Dbg = getDebugInfo())
+ if (CGM.getCodeGenOpts().hasReducedDebugInfo())
+ Dbg->EmitGlobalVariable(DE->getDecl(),
+ APValue(llvm::APSInt(CaseVal->getValue())));
+ }
+
if (SwitchLikelihood)
SwitchLikelihood->push_back(Stmt::getLikelihood(Attrs));