From 4fb6cc95d49d8212b2d10151be33bffbf3fa8003 Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Tue, 9 Oct 2018 20:51:33 +0000 Subject: [PATCH] llvm-dwarfdump: Extend --name to also search DW_AT_linkage_name. rdar://problem/45132695 llvm-svn: 344079 --- test/tools/llvm-dwarfdump/X86/name.test | 5 +++ tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 51 ++++++++++++++++--------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/test/tools/llvm-dwarfdump/X86/name.test b/test/tools/llvm-dwarfdump/X86/name.test index e8e90abc0cb..3e46681a40d 100644 --- a/test/tools/llvm-dwarfdump/X86/name.test +++ b/test/tools/llvm-dwarfdump/X86/name.test @@ -67,3 +67,8 @@ RUN: llvm-mc %S/brief.s -filetype obj -triple x86_64-apple-darwin -o - \ RUN: | llvm-dwarfdump -name="brief.c" - | FileCheck %s -check-prefix=NOCHILDREN NOCHILDREN: DW_AT_name ("brief.c") NOCHILDREN-NOT: DW_TAG + +Test that --name also searches in DW_AT_linkage_name. +RUN: llc -O0 %S/statistics.ll -filetype obj -o - \ +RUN: | llvm-dwarfdump -name=_Z4cubei - | FileCheck %s --check-prefix=LINKAGE +LINKAGE: DW_AT_name ("cube") \ No newline at end of file diff --git a/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/tools/llvm-dwarfdump/llvm-dwarfdump.cpp index 320bcc8f0bc..42992641eb7 100644 --- a/tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ b/tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -280,6 +280,33 @@ static bool filterArch(ObjectFile &Obj) { using HandlerFn = std::function; +/// Print only DIEs that have a certain name. +static bool filterByName(const StringSet<> &Names, DWARFDie Die, + StringRef NameRef, raw_ostream &OS) { + std::string Name = + (IgnoreCase && !UseRegex) ? NameRef.lower() : NameRef.str(); + if (UseRegex) { + // Match regular expression. + for (auto Pattern : Names.keys()) { + Regex RE(Pattern, IgnoreCase ? Regex::IgnoreCase : Regex::NoFlags); + std::string Error; + if (!RE.isValid(Error)) { + errs() << "error in regular expression: " << Error << "\n"; + exit(1); + } + if (RE.match(Name)) { + Die.dump(OS, 0, getDumpOpts()); + return true; + } + } + } else if (Names.count(Name)) { + // Match full text. + Die.dump(OS, 0, getDumpOpts()); + return true; + } + return false; +} + /// Print only DIEs that have a certain name. static void filterByName(const StringSet<> &Names, DWARFContext::unit_iterator_range CUs, @@ -287,25 +314,11 @@ static void filterByName(const StringSet<> &Names, for (const auto &CU : CUs) for (const auto &Entry : CU->dies()) { DWARFDie Die = {CU.get(), &Entry}; - if (const char *NamePtr = Die.getName(DINameKind::ShortName)) { - std::string Name = - (IgnoreCase && !UseRegex) ? StringRef(NamePtr).lower() : NamePtr; - // Match regular expression. - if (UseRegex) - for (auto Pattern : Names.keys()) { - Regex RE(Pattern, IgnoreCase ? Regex::IgnoreCase : Regex::NoFlags); - std::string Error; - if (!RE.isValid(Error)) { - errs() << "error in regular expression: " << Error << "\n"; - exit(1); - } - if (RE.match(Name)) - Die.dump(OS, 0, getDumpOpts()); - } - // Match full text. - else if (Names.count(Name)) - Die.dump(OS, 0, getDumpOpts()); - } + if (const char *Name = Die.getName(DINameKind::ShortName)) + if (filterByName(Names, Die, Name, OS)) + continue; + if (const char *Name = Die.getName(DINameKind::LinkageName)) + filterByName(Names, Die, Name, OS); } }