From 4d8871a898b30f11c905b27954c18d826c0953c9 Mon Sep 17 00:00:00 2001 From: Bruno De Fraine Date: Tue, 29 Jun 2021 09:46:27 +0200 Subject: [PATCH] PR50767: clear non-distinct debuginfo for function with nodebug definition after undecorated declaration Fix suggested by Yuanfang Chen: Non-distinct debuginfo is attached to the function due to the undecorated declaration. Later, when seeing the function definition and `nodebug` attribute, the non-distinct debuginfo should be cleared. Reviewed By: dblaikie Differential Revision: https://reviews.llvm.org/D104777 --- clang/lib/CodeGen/CodeGenFunction.cpp | 10 ++++++++-- clang/test/CodeGen/attr-nodebug2.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 clang/test/CodeGen/attr-nodebug2.c diff --git a/clang/lib/CodeGen/CodeGenFunction.cpp b/clang/lib/CodeGen/CodeGenFunction.cpp index 0ca9465..578b8a8 100644 --- a/clang/lib/CodeGen/CodeGenFunction.cpp +++ b/clang/lib/CodeGen/CodeGenFunction.cpp @@ -1304,8 +1304,14 @@ void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function *Fn, QualType ResTy = BuildFunctionArgList(GD, Args); // Check if we should generate debug info for this function. - if (FD->hasAttr()) - DebugInfo = nullptr; // disable debug info indefinitely for this function + if (FD->hasAttr()) { + // Clear non-distinct debug info that was possibly attached to the function + // due to an earlier declaration without the nodebug attribute + if (Fn) + Fn->setSubprogram(nullptr); + // Disable debug info indefinitely for this function + DebugInfo = nullptr; + } // The function might not have a body if we're generating thunks for a // function declaration. diff --git a/clang/test/CodeGen/attr-nodebug2.c b/clang/test/CodeGen/attr-nodebug2.c new file mode 100644 index 0000000..fd6eca1 --- /dev/null +++ b/clang/test/CodeGen/attr-nodebug2.c @@ -0,0 +1,34 @@ +// RUN: %clang_cc1 -x c -debug-info-kind=limited -debugger-tuning=gdb -dwarf-version=4 -O -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s +// RUN: %clang_cc1 -x c++ -debug-info-kind=limited -debugger-tuning=gdb -dwarf-version=4 -O -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s + +#ifdef __cplusplus +extern "C" { +#endif + +void t1(); + +void use() { t1(); } + +__attribute__((nodebug)) void t1() { + int a = 10; + a++; +} + +#ifdef __cplusplus +} +#endif + +// CHECK-LABEL: define{{.*}} void @use() +// CHECK-SAME: !dbg +// CHECK-SAME: { +// CHECK: !dbg +// CHECK: } + +// PR50767 Function __attribute__((nodebug)) inconsistency causes crash +// illegal (non-distinct) !dbg metadata was being added to _Z2t1v definition + +// CHECK-LABEL: define{{.*}} void @t1() +// CHECK-NOT: !dbg +// CHECK-SAME: { +// CHECK-NOT: !dbg +// CHECK: } -- 2.7.4