Skip to content

Commit

Permalink
llvm-strings: support printing the filename
Browse files Browse the repository at this point in the history
This adds support for the `-f` or `--print-file-name` option for strings.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286767 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
compnerd committed Nov 13, 2016
1 parent 4404d0d commit c99852c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
9 changes: 9 additions & 0 deletions test/tools/llvm-strings/archive-filename.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
RUN: echo -n abcd > %T/abcd
RUN: llvm-ar -format gnu crs %T/archive.a %T/abcd
RUN: llvm-strings -f %T/archive.a | FileCheck %s
RUN: llvm-strings --print-file-name %T/archive.a | FileCheck %s

CHECK: archive.a: !<arch>
CHECK: archive.a: abcd/ 0 0 0 644 4 `
CHECK: archive.a: abcd

4 changes: 4 additions & 0 deletions test/tools/llvm-strings/file-filename.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RUN: echo -n abcd > %T/abcd
RUN: llvm-strings -f %T/abcd | FileCheck %s
RUN: llvm-strings --print-file-name %T/abcd | FileCheck %s
CHECK: {{[\\/]}}abcd: abcd
3 changes: 3 additions & 0 deletions test/tools/llvm-strings/stdin-filename.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RUN: echo -n abcd | llvm-strings -f - | FileCheck %s
RUN: echo -n abcd | llvm-strings --print-file-name - | FileCheck %s
CHECK: {standard input}: abcd
21 changes: 17 additions & 4 deletions tools/llvm-strings/llvm-strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,32 @@ static cl::list<std::string> InputFileNames(cl::Positional,
cl::desc("<input object files>"),
cl::ZeroOrMore);

static void strings(raw_ostream &OS, StringRef Contents) {
static cl::opt<bool>
PrintFileName("print-file-name",
cl::desc("Print the name of the file before each string"));
static cl::alias PrintFileNameShort("f", cl::desc(""),
cl::aliasopt(PrintFileName));

static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
auto print = [&OS, FileName](StringRef L) {
if (PrintFileName)
OS << FileName << ": ";
OS << L << '\n';
};

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) {
if (P - S > 3)
OS << StringRef(S, P - S) << '\n';
print(StringRef(S, P - S));
S = nullptr;
}
}
if (S && E - S > 3)
OS << StringRef(S, E - S) << '\n';
print(StringRef(S, E - S));
}

int main(int argc, char **argv) {
Expand All @@ -60,7 +72,8 @@ int main(int argc, char **argv) {
if (std::error_code EC = Buffer.getError())
errs() << File << ": " << EC.message() << '\n';
else
strings(llvm::outs(), Buffer.get()->getMemBufferRef().getBuffer());
strings(llvm::outs(), File == "-" ? "{standard input}" : File,
Buffer.get()->getMemBufferRef().getBuffer());
}

return EXIT_SUCCESS;
Expand Down

0 comments on commit c99852c

Please sign in to comment.