[llvm] r286767 - llvm-strings: support printing the filename

Saleem Abdulrasool via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 13 11:07:48 PST 2016


Author: compnerd
Date: Sun Nov 13 13:07:48 2016
New Revision: 286767

URL: http://llvm.org/viewvc/llvm-project?rev=286767&view=rev
Log:
llvm-strings: support printing the filename

This adds support for the `-f` or `--print-file-name` option for strings.

Added:
    llvm/trunk/test/tools/llvm-strings/archive-filename.test
    llvm/trunk/test/tools/llvm-strings/file-filename.test
    llvm/trunk/test/tools/llvm-strings/stdin-filename.test
Modified:
    llvm/trunk/tools/llvm-strings/llvm-strings.cpp

Added: llvm/trunk/test/tools/llvm-strings/archive-filename.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-strings/archive-filename.test?rev=286767&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-strings/archive-filename.test (added)
+++ llvm/trunk/test/tools/llvm-strings/archive-filename.test Sun Nov 13 13:07:48 2016
@@ -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
+

Added: llvm/trunk/test/tools/llvm-strings/file-filename.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-strings/file-filename.test?rev=286767&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-strings/file-filename.test (added)
+++ llvm/trunk/test/tools/llvm-strings/file-filename.test Sun Nov 13 13:07:48 2016
@@ -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

Added: llvm/trunk/test/tools/llvm-strings/stdin-filename.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-strings/stdin-filename.test?rev=286767&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-strings/stdin-filename.test (added)
+++ llvm/trunk/test/tools/llvm-strings/stdin-filename.test Sun Nov 13 13:07:48 2016
@@ -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

Modified: llvm/trunk/tools/llvm-strings/llvm-strings.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-strings/llvm-strings.cpp?rev=286767&r1=286766&r2=286767&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-strings/llvm-strings.cpp (original)
+++ llvm/trunk/tools/llvm-strings/llvm-strings.cpp Sun Nov 13 13:07:48 2016
@@ -29,7 +29,19 @@ static cl::list<std::string> InputFileNa
                                             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)) {
@@ -37,12 +49,12 @@ static void strings(raw_ostream &OS, Str
         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;




More information about the llvm-commits mailing list