From ffbb88fc8abeacf4074c41f75ef1f7da2fd3cfec Mon Sep 17 00:00:00 2001 From: Adrian Prantl Date: Tue, 3 Oct 2017 22:08:22 +0000 Subject: [PATCH] llvm-dwarfdump: implement the --regex option in combination with --name. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@314855 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/tools/llvm-dwarfdump/X86/name.test | 19 ++++++++++++- test/tools/llvm-dwarfdump/cmdline.test | 1 + tools/llvm-dwarfdump/llvm-dwarfdump.cpp | 38 +++++++++++++++++++------ 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/test/tools/llvm-dwarfdump/X86/name.test b/test/tools/llvm-dwarfdump/X86/name.test index d757b7b53f5..66483179b5a 100644 --- a/test/tools/llvm-dwarfdump/X86/name.test +++ b/test/tools/llvm-dwarfdump/X86/name.test @@ -43,4 +43,21 @@ RUN: | llvm-dwarfdump -name=Main - | FileCheck %s -check-prefix=EMPTY RUN: llvm-mc %S/brief.s -filetype obj -triple x86_64-apple-darwin -o - \ RUN: | llvm-dwarfdump -name=Main -i - | FileCheck %s RUN: llvm-mc %S/brief.s -filetype obj -triple x86_64-apple-darwin -o - \ -RUN: | llvm-dwarfdump -name=MAIN -ignore-case - | FileCheck %s \ No newline at end of file +RUN: | llvm-dwarfdump -name=MAIN -ignore-case - | FileCheck %s + +Test the -regex option. +RUN: llvm-mc %S/brief.s -filetype obj -triple x86_64-apple-darwin -o - \ +RUN: | llvm-dwarfdump -regex -name=m.+n - | FileCheck %s +RUN: llvm-mc %S/brief.s -filetype obj -triple x86_64-apple-darwin -o - \ +RUN: | llvm-dwarfdump -x -name=m.+n - | FileCheck %s +RUN: llvm-mc %S/brief.s -filetype obj -triple x86_64-apple-darwin -o - \ +RUN: | llvm-dwarfdump -x -i -name=M.+n - | FileCheck %s +RUN: llvm-mc %S/brief.s -filetype obj -triple x86_64-apple-darwin -o - \ +RUN: | not llvm-dwarfdump -x -name=+ - 2>&1 | FileCheck %s --check-prefix=ERR +ERR: error +RUN: llvm-dwarfdump %S/../../dsymutil/Inputs/libfat-test.a \ +RUN: -x -name=x86_64h_var -name=i386_var \ +RUN: | FileCheck %s --check-prefix=MULTI +RUN: llvm-dwarfdump %S/../../dsymutil/Inputs/libfat-test.a \ +RUN: -x -name=.*86.*_var \ +RUN: | FileCheck %s --check-prefix=MULTI diff --git a/test/tools/llvm-dwarfdump/cmdline.test b/test/tools/llvm-dwarfdump/cmdline.test index f4c023af6ef..7a34a2d1ef7 100644 --- a/test/tools/llvm-dwarfdump/cmdline.test +++ b/test/tools/llvm-dwarfdump/cmdline.test @@ -10,6 +10,7 @@ HELP: -find HELP: -ignore-case HELP: -name HELP: -recurse-depth= +HELP: -regex HELP: -show-children HELP: -show-form HELP: -show-parents diff --git a/tools/llvm-dwarfdump/llvm-dwarfdump.cpp b/tools/llvm-dwarfdump/llvm-dwarfdump.cpp index 9abc92b7b83..9e6fc773139 100644 --- a/tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ b/tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -27,6 +27,7 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/Regex.h" #include "llvm/Support/Signals.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/ToolOutputFile.h" @@ -149,11 +150,12 @@ static opt value_desc("i"), cat(DwarfDumpCategory)); static alias IgnoreCaseAlias("i", desc("Alias for -ignore-case"), aliasopt(IgnoreCase)); -static list - Name("name", - desc("Find and print all debug info entries whose name (DW_AT_name " - "attribute) matches the exact text in ."), - value_desc("name"), cat(DwarfDumpCategory)); +static list Name( + "name", + desc("Find and print all debug info entries whose name (DW_AT_name " + "attribute) matches the exact text in . When used with the " + "the -regex option is interpreted as a regular expression."), + value_desc("pattern"), cat(DwarfDumpCategory)); static alias NameAlias("n", desc("Alias for -name"), aliasopt(Name)); static opt OutputFilename("out-file", cl::init(""), @@ -162,6 +164,12 @@ static opt static alias OutputFilenameAlias("o", desc("Alias for -out-file"), aliasopt(OutputFilename), cat(DwarfDumpCategory)); +static opt + UseRegex("regex", + desc("Treat any strings as regular expressions when " + "searching instead of just as an exact string match."), + cat(DwarfDumpCategory)); +static alias RegexAlias("x", desc("Alias for -regex"), aliasopt(UseRegex)); static opt ShowChildren("show-children", desc("Show a debug info entry's children when selectively " @@ -272,8 +280,22 @@ static void filterByName(const StringSet<> &Names, for (const auto &Entry : CU->dies()) { DWARFDie Die = {CU.get(), &Entry}; if (const char *NamePtr = Die.getName(DINameKind::ShortName)) { - std::string Name = IgnoreCase ? StringRef(NamePtr).lower() : NamePtr; - if (Names.count(Name)) + 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()); } } @@ -292,7 +314,7 @@ static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx, Twine Filename, if (!Name.empty()) { StringSet<> Names; for (auto name : Name) - Names.insert(IgnoreCase ? StringRef(name).lower() : name); + Names.insert((IgnoreCase && !UseRegex) ? StringRef(name).lower() : name); filterByName(Names, DICtx.compile_units(), OS); filterByName(Names, DICtx.dwo_compile_units(), OS); -- 2.40.0