From: Saleem Abdulrasool Date: Sun, 13 Nov 2016 19:07:48 +0000 (+0000) Subject: llvm-strings: support printing the filename X-Git-Tag: llvmorg-4.0.0-rc1~4731 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8b9be8fd41e707f974f13d4648878bc47aa0bd34;p=platform%2Fupstream%2Fllvm.git llvm-strings: support printing the filename This adds support for the `-f` or `--print-file-name` option for strings. llvm-svn: 286767 --- diff --git a/llvm/test/tools/llvm-strings/archive-filename.test b/llvm/test/tools/llvm-strings/archive-filename.test new file mode 100644 index 0000000..52c028e --- /dev/null +++ b/llvm/test/tools/llvm-strings/archive-filename.test @@ -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: ! +CHECK: archive.a: abcd/ 0 0 0 644 4 ` +CHECK: archive.a: abcd + diff --git a/llvm/test/tools/llvm-strings/file-filename.test b/llvm/test/tools/llvm-strings/file-filename.test new file mode 100644 index 0000000..5e77aeb --- /dev/null +++ b/llvm/test/tools/llvm-strings/file-filename.test @@ -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 diff --git a/llvm/test/tools/llvm-strings/stdin-filename.test b/llvm/test/tools/llvm-strings/stdin-filename.test new file mode 100644 index 0000000..405a64a --- /dev/null +++ b/llvm/test/tools/llvm-strings/stdin-filename.test @@ -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 diff --git a/llvm/tools/llvm-strings/llvm-strings.cpp b/llvm/tools/llvm-strings/llvm-strings.cpp index 6e5e2f2..cb0fb96 100644 --- a/llvm/tools/llvm-strings/llvm-strings.cpp +++ b/llvm/tools/llvm-strings/llvm-strings.cpp @@ -29,7 +29,19 @@ static cl::list InputFileNames(cl::Positional, cl::desc(""), cl::ZeroOrMore); -static void strings(raw_ostream &OS, StringRef Contents) { +static cl::opt + 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)) { @@ -37,12 +49,12 @@ static void strings(raw_ostream &OS, StringRef Contents) { 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) { @@ -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;