[llvm-commits] [llvm] r139945 - in /llvm/trunk: docs/CommandGuide/llvm-extract.pod tools/llvm-extract/llvm-extract.cpp

Chad Rosier mcrosier at apple.com
Fri Sep 16 14:09:17 PDT 2011


Author: mcrosier
Date: Fri Sep 16 16:09:17 2011
New Revision: 139945

URL: http://llvm.org/viewvc/llvm-project?rev=139945&view=rev
Log:
Add -rfunc and -rglob options to llvm-extract to support regular
expression matching.

Modified:
    llvm/trunk/docs/CommandGuide/llvm-extract.pod
    llvm/trunk/tools/llvm-extract/llvm-extract.cpp

Modified: llvm/trunk/docs/CommandGuide/llvm-extract.pod
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/CommandGuide/llvm-extract.pod?rev=139945&r1=139944&r2=139945&view=diff
==============================================================================
--- llvm/trunk/docs/CommandGuide/llvm-extract.pod (original)
+++ llvm/trunk/docs/CommandGuide/llvm-extract.pod Fri Sep 16 16:09:17 2011
@@ -37,11 +37,23 @@
 Extract the function named I<function-name> from the LLVM bitcode. May be
 specified multiple times to extract multiple functions at once.
 
+=item B<--rfunc> I<function-regular-expr>
+
+Extract the function(s) matching I<function-regular-expr> from the LLVM bitcode.
+All functions matching the regular expression will be extracted.  May be 
+specified multiple times.
+
 =item B<--glob> I<global-name>
 
 Extract the global variable named I<global-name> from the LLVM bitcode. May be
 specified multiple times to extract multiple global variables at once.
 
+=item B<--rglob> I<glob-regular-expr>
+
+Extract the global variable(s) matching I<global-regular-expr> from the LLVM
+bitcode. All global variables matching the regular expression will be extracted.
+May be specified multiple times.
+
 =item B<-help>
 
 Print a summary of command line options.

Modified: llvm/trunk/tools/llvm-extract/llvm-extract.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-extract/llvm-extract.cpp?rev=139945&r1=139944&r2=139945&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-extract/llvm-extract.cpp (original)
+++ llvm/trunk/tools/llvm-extract/llvm-extract.cpp Fri Sep 16 16:09:17 2011
@@ -26,7 +26,9 @@
 #include "llvm/Support/ToolOutputFile.h"
 #include "llvm/Support/SystemUtils.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/Regex.h"
 #include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/SetVector.h"
 #include <memory>
 using namespace llvm;
 
@@ -45,16 +47,30 @@
 static cl::opt<bool>
 DeleteFn("delete", cl::desc("Delete specified Globals from Module"));
 
-// ExtractFuncs - The functions to extract from the module... 
+// ExtractFuncs - The functions to extract from the module.
 static cl::list<std::string>
 ExtractFuncs("func", cl::desc("Specify function to extract"),
              cl::ZeroOrMore, cl::value_desc("function"));
 
-// ExtractGlobals - The globals to extract from the module...
+// ExtractRegExpFuncs - The functions, matched via regular expression, to 
+// extract from the module.
+static cl::list<std::string>
+ExtractRegExpFuncs("rfunc", cl::desc("Specify function(s) to extract using a "
+                                     "regular expression"),
+                   cl::ZeroOrMore, cl::value_desc("rfunction"));
+
+// ExtractGlobals - The globals to extract from the module.
 static cl::list<std::string>
 ExtractGlobals("glob", cl::desc("Specify global to extract"),
                cl::ZeroOrMore, cl::value_desc("global"));
 
+// ExtractRegExpGlobals - The globals, matched via regular expression, to
+// extract from the module...
+static cl::list<std::string>
+ExtractRegExpGlobals("rglob", cl::desc("Specify global(s) to extract using a "
+                                       "regular expression"),
+                     cl::ZeroOrMore, cl::value_desc("rglobal"));
+
 static cl::opt<bool>
 OutputAssembly("S",
                cl::desc("Write output as LLVM assembly"), cl::Hidden);
@@ -78,7 +94,8 @@
     return 1;
   }
 
-  std::vector<GlobalValue *> GVs;
+  // Use SetVector to avoid duplicates.
+  SetVector<GlobalValue *> GVs;
 
   // Figure out which globals we should extract.
   for (size_t i = 0, e = ExtractGlobals.size(); i != e; ++i) {
@@ -88,7 +105,30 @@
              << ExtractGlobals[i] << "'!\n";
       return 1;
     }
-    GVs.push_back(GV);
+    GVs.insert(GV);
+  }
+
+  // Extract globals via regular expression matching.
+  for (size_t i = 0, e = ExtractRegExpGlobals.size(); i != e; ++i) {
+    std::string Error;
+    Regex RegEx(ExtractRegExpGlobals[i]);
+    if (!RegEx.isValid(Error)) {
+      errs() << argv[0] << ": '" << ExtractRegExpGlobals[i] << "' "
+        "invalid regex: " << Error;
+    }
+    bool match = false;
+    for (Module::global_iterator GV = M.get()->global_begin(), 
+           E = M.get()->global_end(); GV != E; GV++) {
+      if (RegEx.match(GV->getName())) {
+        GVs.insert(&*GV);
+        match = true;
+      }
+    }
+    if (!match) {
+      errs() << argv[0] << ": program doesn't contain global named '"
+             << ExtractRegExpGlobals[i] << "'!\n";
+      return 1;
+    }
   }
 
   // Figure out which functions we should extract.
@@ -99,7 +139,30 @@
              << ExtractFuncs[i] << "'!\n";
       return 1;
     }
-    GVs.push_back(GV);
+    GVs.insert(GV);
+  }
+  // Extract functions via regular expression matching.
+  for (size_t i = 0, e = ExtractRegExpFuncs.size(); i != e; ++i) {
+    std::string Error;
+    StringRef RegExStr = ExtractRegExpFuncs[i];
+    Regex RegEx(RegExStr);
+    if (!RegEx.isValid(Error)) {
+      errs() << argv[0] << ": '" << ExtractRegExpFuncs[i] << "' "
+        "invalid regex: " << Error;
+    }
+    bool match = false;
+    for (Module::iterator F = M.get()->begin(), E = M.get()->end(); F != E; 
+         F++) {
+      if (RegEx.match(F->getName())) {
+        GVs.insert(&*F);
+        match = true;
+      }
+    }
+    if (!match) {
+      errs() << argv[0] << ": program doesn't contain global named '"
+             << ExtractRegExpFuncs[i] << "'!\n";
+      return 1;
+    }
   }
 
   // Materialize requisite global values.
@@ -145,7 +208,9 @@
   PassManager Passes;
   Passes.add(new TargetData(M.get())); // Use correct TargetData
 
-  Passes.add(createGVExtractionPass(GVs, DeleteFn));
+  std::vector<GlobalValue*> Gvs(GVs.begin(), GVs.end());
+
+  Passes.add(createGVExtractionPass(Gvs, DeleteFn));
   if (!DeleteFn)
     Passes.add(createGlobalDCEPass());           // Delete unreachable globals
   Passes.add(createStripDeadDebugInfoPass());    // Remove dead debug info





More information about the llvm-commits mailing list