Skip to content

Commit ba04861

Browse files
committedJan 21, 2017
llvm-strings: add support for -t
Allow printing the file content offset via the `-t` or `--radix` option. llvm-svn: 292707
1 parent 4c33f7f commit ba04861

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed
 
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
one
2+
two
3+
three
4+
four
5+
five
6+
six
7+
seven
8+
eight
9+
nine
10+
ten
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
RUN: llvm-strings %S/Inputs/numbers | FileCheck %s -check-prefix CHECK-NONE
2+
RUN: llvm-strings -t d %S/Inputs/numbers | FileCheck %s -check-prefix CHECK-DEC
3+
RUN: llvm-strings -t o %S/Inputs/numbers | FileCheck %s -check-prefix CHECK-OCT
4+
RUN: llvm-strings -t x %S/Inputs/numbers | FileCheck %s -check-prefix CHECK-HEX
5+
6+
CHECK-NONE: three
7+
CHECK-NONE: four
8+
CHECK-NONE: five
9+
CHECK-NONE: seven
10+
CHECK-NONE: eight
11+
CHECK-NONE: nine
12+
13+
CHECK-DEC: 8 three
14+
CHECK-DEC: 14 four
15+
CHECK-DEC: 19 five
16+
CHECK-DEC: 28 seven
17+
CHECK-DEC: 34 eight
18+
CHECK-DEC: 40 nine
19+
20+
CHECK-OCT: 10 three
21+
CHECK-OCT: 16 four
22+
CHECK-OCT: 23 five
23+
CHECK-OCT: 34 seven
24+
CHECK-OCT: 42 eight
25+
CHECK-OCT: 50 nine
26+
27+
CHECK-HEX: 8 three
28+
CHECK-HEX: e four
29+
CHECK-HEX: 13 five
30+
CHECK-HEX: 1c seven
31+
CHECK-HEX: 22 eight
32+
CHECK-HEX: 28 nine
33+

‎llvm/tools/llvm-strings/llvm-strings.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/Object/Binary.h"
1616
#include "llvm/Support/CommandLine.h"
1717
#include "llvm/Support/Error.h"
18+
#include "llvm/Support/Format.h"
1819
#include "llvm/Support/MemoryBuffer.h"
1920
#include "llvm/Support/PrettyStackTrace.h"
2021
#include "llvm/Support/Program.h"
@@ -40,27 +41,51 @@ static cl::opt<int>
4041
cl::init(4));
4142
static cl::alias MinLengthShort("n", cl::desc(""), cl::aliasopt(MinLength));
4243

44+
enum radix { none, octal, hexadecimal, decimal };
45+
static cl::opt<radix>
46+
Radix("radix", cl::desc("print the offset within the file"),
47+
cl::values(clEnumValN(octal, "o", "octal"),
48+
clEnumValN(hexadecimal, "x", "hexadecimal"),
49+
clEnumValN(decimal, "d", "decimal")),
50+
cl::init(none));
51+
static cl::alias RadixShort("t", cl::desc(""), cl::aliasopt(Radix));
52+
4353
static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
44-
auto print = [&OS, FileName](StringRef L) {
54+
auto print = [&OS, FileName](unsigned Offset, StringRef L) {
4555
if (L.size() < static_cast<size_t>(MinLength))
4656
return;
4757
if (PrintFileName)
48-
OS << FileName << ": ";
49-
OS << L << '\n';
58+
OS << FileName << ":";
59+
switch (Radix) {
60+
default:
61+
case none:
62+
break;
63+
case octal:
64+
OS << format("%8o", Offset);
65+
break;
66+
case hexadecimal:
67+
OS << format("%8x", Offset);
68+
break;
69+
case decimal:
70+
OS << format("%8u", Offset);
71+
break;
72+
}
73+
OS << " " << L << '\n';
5074
};
5175

76+
const char *B = Contents.begin();
5277
const char *P = nullptr, *E = nullptr, *S = nullptr;
5378
for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
5479
if (std::isgraph(*P) || std::isblank(*P)) {
5580
if (S == nullptr)
5681
S = P;
5782
} else if (S) {
58-
print(StringRef(S, P - S));
83+
print(S - B, StringRef(S, P - S));
5984
S = nullptr;
6085
}
6186
}
6287
if (S)
63-
print(StringRef(S, E - S));
88+
print(S - B, StringRef(S, E - S));
6489
}
6590

6691
int main(int argc, char **argv) {

0 commit comments

Comments
 (0)
Please sign in to comment.