From 75be0482a2e2a78fae83f1ca604f4ee20d673796 Mon Sep 17 00:00:00 2001 From: Yonghong Song Date: Mon, 26 Sep 2022 07:08:24 -0700 Subject: [PATCH] [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 --- clang/lib/CodeGen/CGStmt.cpp | 15 ++++++++++++++ clang/test/CodeGen/debug-info-enum-case-val.c | 30 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 clang/test/CodeGen/debug-info-enum-case-val.c 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(S.getLHS())) + CE = dyn_cast(ICE->getSubExpr()); + else + CE = dyn_cast(S.getLHS()); + if (CE) { + if (auto DE = dyn_cast(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)); diff --git a/clang/test/CodeGen/debug-info-enum-case-val.c b/clang/test/CodeGen/debug-info-enum-case-val.c new file mode 100644 index 0000000..f39de0d --- /dev/null +++ b/clang/test/CodeGen/debug-info-enum-case-val.c @@ -0,0 +1,30 @@ +// RUN: %clang_cc1 -emit-llvm -debug-info-kind=limited %s -o - | FileCheck %s + +enum { A = 1 }; +int func1(int a) { + switch(a) { + case A: return 10; + default: break; + } + return 0; +} +// CHECK: !DICompositeType(tag: DW_TAG_enumeration_type +// CHECK-SAME: elements: [[TEST1_ENUMS:![0-9]*]] +// CHECK: [[TEST1_ENUMS]] = !{[[TEST1_E:![0-9]*]]} +// CHECK: [[TEST1_E]] = !DIEnumerator(name: "A", value: 1) + +// Test ImplicitCast of switch case enum value +enum { B = 2 }; +typedef unsigned long long __t1; +typedef __t1 __t2; +int func2(__t2 a) { + switch(a) { + case B: return 10; + default: break; + } + return 0; +} +// CHECK: !DICompositeType(tag: DW_TAG_enumeration_type +// CHECK-SAME: elements: [[TEST2_ENUMS:![0-9]*]] +// CHECK: [[TEST2_ENUMS]] = !{[[TEST2_E:![0-9]*]]} +// CHECK: [[TEST2_E]] = !DIEnumerator(name: "B", value: 2) -- 2.7.4