]> granicus.if.org Git - llvm/commitdiff
llvm-strings: add support for `-t`
authorSaleem Abdulrasool <compnerd@compnerd.org>
Sat, 21 Jan 2017 02:36:28 +0000 (02:36 +0000)
committerSaleem Abdulrasool <compnerd@compnerd.org>
Sat, 21 Jan 2017 02:36:28 +0000 (02:36 +0000)
Allow printing the file content offset via the `-t` or `--radix` option.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@292707 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-strings/Inputs/numbers [new file with mode: 0644]
test/tools/llvm-strings/radix.test [new file with mode: 0644]
tools/llvm-strings/llvm-strings.cpp

diff --git a/test/tools/llvm-strings/Inputs/numbers b/test/tools/llvm-strings/Inputs/numbers
new file mode 100644 (file)
index 0000000..c9e9e05
--- /dev/null
@@ -0,0 +1,10 @@
+one
+two
+three
+four
+five
+six
+seven
+eight
+nine
+ten
diff --git a/test/tools/llvm-strings/radix.test b/test/tools/llvm-strings/radix.test
new file mode 100644 (file)
index 0000000..c81d9fe
--- /dev/null
@@ -0,0 +1,33 @@
+RUN: llvm-strings %S/Inputs/numbers | FileCheck %s -check-prefix CHECK-NONE
+RUN: llvm-strings -t d %S/Inputs/numbers | FileCheck %s -check-prefix CHECK-DEC
+RUN: llvm-strings -t o %S/Inputs/numbers | FileCheck %s -check-prefix CHECK-OCT
+RUN: llvm-strings -t x %S/Inputs/numbers | FileCheck %s -check-prefix CHECK-HEX
+
+CHECK-NONE: three
+CHECK-NONE: four
+CHECK-NONE: five
+CHECK-NONE: seven
+CHECK-NONE: eight
+CHECK-NONE: nine
+
+CHECK-DEC:      8 three
+CHECK-DEC:     14 four
+CHECK-DEC:     19 five
+CHECK-DEC:     28 seven
+CHECK-DEC:     34 eight
+CHECK-DEC:     40 nine
+
+CHECK-OCT:     10 three
+CHECK-OCT:     16 four
+CHECK-OCT:     23 five
+CHECK-OCT:     34 seven
+CHECK-OCT:     42 eight
+CHECK-OCT:     50 nine
+
+CHECK-HEX:      8 three
+CHECK-HEX:      e four
+CHECK-HEX:     13 five
+CHECK-HEX:     1c seven
+CHECK-HEX:     22 eight
+CHECK-HEX:     28 nine
+
index e750995331e333a681f2cd9fb2b48edafdbdd271..4157b201e3525632aa84801e24f6c80845e41cec 100644 (file)
@@ -15,6 +15,7 @@
 #include "llvm/Object/Binary.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Error.h"
+#include "llvm/Support/Format.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/Program.h"
@@ -40,27 +41,51 @@ static cl::opt<int>
               cl::init(4));
 static cl::alias MinLengthShort("n", cl::desc(""), cl::aliasopt(MinLength));
 
+enum radix { none, octal, hexadecimal, decimal };
+static cl::opt<radix>
+    Radix("radix", cl::desc("print the offset within the file"),
+          cl::values(clEnumValN(octal, "o", "octal"),
+                     clEnumValN(hexadecimal, "x", "hexadecimal"),
+                     clEnumValN(decimal, "d", "decimal")),
+          cl::init(none));
+static cl::alias RadixShort("t", cl::desc(""), cl::aliasopt(Radix));
+
 static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
-  auto print = [&OS, FileName](StringRef L) {
+  auto print = [&OS, FileName](unsigned Offset, StringRef L) {
     if (L.size() < static_cast<size_t>(MinLength))
       return;
     if (PrintFileName)
-      OS << FileName << ": ";
-    OS << L << '\n';
+      OS << FileName << ":";
+    switch (Radix) {
+    default:
+    case none:
+      break;
+    case octal:
+      OS << format("%8o", Offset);
+      break;
+    case hexadecimal:
+      OS << format("%8x", Offset);
+      break;
+    case decimal:
+      OS << format("%8u", Offset);
+      break;
+    }
+    OS << " " << L << '\n';
   };
 
+  const char *B = Contents.begin();
   const char *P = nullptr, *E = nullptr, *S = nullptr;
   for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
     if (std::isgraph(*P) || std::isblank(*P)) {
       if (S == nullptr)
         S = P;
     } else if (S) {
-      print(StringRef(S, P - S));
+      print(S - B, StringRef(S, P - S));
       S = nullptr;
     }
   }
   if (S)
-    print(StringRef(S, E - S));
+    print(S - B, StringRef(S, E - S));
 }
 
 int main(int argc, char **argv) {