OSDN Git Service

llvm-dwarfdump: implement the --regex option in combination with --name.
authorAdrian Prantl <aprantl@apple.com>
Tue, 3 Oct 2017 22:08:22 +0000 (22:08 +0000)
committerAdrian Prantl <aprantl@apple.com>
Tue, 3 Oct 2017 22:08:22 +0000 (22:08 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@314855 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-dwarfdump/X86/name.test
test/tools/llvm-dwarfdump/cmdline.test
tools/llvm-dwarfdump/llvm-dwarfdump.cpp

index d757b7b..6648317 100644 (file)
@@ -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
index f4c023a..7a34a2d 100644 (file)
@@ -10,6 +10,7 @@ HELP: -find
 HELP: -ignore-case
 HELP: -name
 HELP: -recurse-depth=<N>
+HELP: -regex
 HELP: -show-children
 HELP: -show-form
 HELP: -show-parents
index 9abc92b..9e6fc77 100644 (file)
@@ -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<bool>
                value_desc("i"), cat(DwarfDumpCategory));
 static alias IgnoreCaseAlias("i", desc("Alias for -ignore-case"),
                              aliasopt(IgnoreCase));
-static list<std::string>
-    Name("name",
-         desc("Find and print all debug info entries whose name (DW_AT_name "
-              "attribute) matches the exact text in <name>."),
-         value_desc("name"), cat(DwarfDumpCategory));
+static list<std::string> Name(
+    "name",
+    desc("Find and print all debug info entries whose name (DW_AT_name "
+         "attribute) matches the exact text in <pattern>.  When used with the "
+         "the -regex option <pattern> is interpreted as a regular expression."),
+    value_desc("pattern"), cat(DwarfDumpCategory));
 static alias NameAlias("n", desc("Alias for -name"), aliasopt(Name));
 static opt<std::string>
     OutputFilename("out-file", cl::init(""),
@@ -163,6 +165,12 @@ static alias OutputFilenameAlias("o", desc("Alias for -out-file"),
                                  aliasopt(OutputFilename),
                                  cat(DwarfDumpCategory));
 static opt<bool>
+    UseRegex("regex",
+             desc("Treat any <pattern> 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<bool>
     ShowChildren("show-children",
                  desc("Show a debug info entry's children when selectively "
                       "printing with the =<offset> option"),
@@ -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);