From fc1812a0ad757838b66aab57e1df720ec205a16a Mon Sep 17 00:00:00 2001 From: Hongtao Yu Date: Wed, 17 Mar 2021 11:17:17 -0700 Subject: [PATCH] [UniqueLinkageName] Use consistent checks when mangling symbo linkage name and debug linkage name. C functions may be declared and defined in different prototypes like below. This patch unifies the checks for mangling names in symbol linkage name emission and debug linkage name emission so that the two names are consistent. static int go(int); static int go(a) int a; { return a; } Test Plan: Differential Revision: https://reviews.llvm.org/D98799 --- clang/lib/AST/ItaniumMangle.cpp | 2 +- clang/lib/CodeGen/CGDebugInfo.cpp | 2 +- .../CodeGen/unique-internal-linkage-names-dwarf.c | 27 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp index ba96fda..3e6e292 100644 --- a/clang/lib/AST/ItaniumMangle.cpp +++ b/clang/lib/AST/ItaniumMangle.cpp @@ -640,7 +640,7 @@ bool ItaniumMangleContextImpl::isUniqueInternalLinkageDecl( // For C functions without prototypes, return false as their // names should not be mangled. - if (!FD->getType()->getAs()) + if (!FD->hasPrototype()) return false; if (isInternalLinkageDecl(ND)) diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 468c2b7..c80249a 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -3522,7 +3522,7 @@ void CGDebugInfo::collectFunctionDeclProps(GlobalDecl GD, llvm::DIFile *Unit, llvm::DIScope *&FDContext, llvm::DINodeArray &TParamsArray, llvm::DINode::DIFlags &Flags) { - const auto *FD = cast(GD.getDecl()); + const auto *FD = cast(GD.getCanonicalDecl().getDecl()); Name = getFunctionName(FD); // Use mangled name as linkage name for C/C++ functions. if (FD->hasPrototype()) { diff --git a/clang/test/CodeGen/unique-internal-linkage-names-dwarf.c b/clang/test/CodeGen/unique-internal-linkage-names-dwarf.c index a358342..e5d507e 100644 --- a/clang/test/CodeGen/unique-internal-linkage-names-dwarf.c +++ b/clang/test/CodeGen/unique-internal-linkage-names-dwarf.c @@ -8,21 +8,48 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux -debug-info-kind=limited -dwarf-version=5 -funique-internal-linkage-names -emit-llvm -o - %s | FileCheck %s --check-prefix=UNIQUE static int glob; +// foo should be given a uniquefied name under -funique-internal-linkage-names. static int foo(void) { return glob; } +// bar should not be given a uniquefied name under -funique-internal-linkage-names, +// since it doesn't come with valid prototype. +static int bar(a) int a; +{ + return glob + a; +} + +// go should be given a uniquefied name under -funique-internal-linkage-names, even +// if its definition doesn't come with a valid prototype, but the declaration here +// has a prototype. +static int go(int); + void baz() { foo(); + bar(1); + go(2); } +static int go(a) int a; +{ + return glob + a; +} + + // PLAIN: @glob = internal global i32 // PLAIN: define internal i32 @foo() +// PLAIN: define internal i32 @bar(i32 %a) // PLAIN: distinct !DIGlobalVariable(name: "glob"{{.*}}) // PLAIN: distinct !DISubprogram(name: "foo"{{.*}}) +// PLAIN: distinct !DISubprogram(name: "bar"{{.*}}) +// PLAIN: distinct !DISubprogram(name: "go"{{.*}}) // PLAIN-NOT: linkageName: // // UNIQUE: @glob = internal global i32 // UNIQUE: define internal i32 @_ZL3foov.[[MODHASH:__uniq.[0-9]+]]() +// UNIQUE: define internal i32 @bar(i32 %a) +// UNIQUE: define internal i32 @_ZL2goi.[[MODHASH]](i32 %a) // UNIQUE: distinct !DIGlobalVariable(name: "glob"{{.*}}) // UNIQUE: distinct !DISubprogram(name: "foo", linkageName: "_ZL3foov.[[MODHASH]]"{{.*}}) +// UNIQUE: distinct !DISubprogram(name: "go", linkageName: "_ZL2goi.[[MODHASH]]"{{.*}}) -- 2.7.4