[llvm] r286914 - llvm-strings: support the `-n` option
Saleem Abdulrasool via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 14 16:43:52 PST 2016
Author: compnerd
Date: Mon Nov 14 18:43:52 2016
New Revision: 286914
URL: http://llvm.org/viewvc/llvm-project?rev=286914&view=rev
Log:
llvm-strings: support the `-n` option
Permit specifying the match length (the `-n` or `--bytes` option). The
deprecated `-[length]` form is not supported as an option. This allows the
strings tool to display only the specified length strings rather than the
hardcoded default length of >= 4.
Added:
llvm/trunk/test/tools/llvm-strings/length.test
Modified:
llvm/trunk/tools/llvm-strings/llvm-strings.cpp
Added: llvm/trunk/test/tools/llvm-strings/length.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-strings/length.test?rev=286914&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-strings/length.test (added)
+++ llvm/trunk/test/tools/llvm-strings/length.test Mon Nov 14 18:43:52 2016
@@ -0,0 +1,24 @@
+RUN: sed -n 's/^STDIN: //p' %s | llvm-strings | FileCheck --check-prefix CHECK-DEFAULT %s
+RUN: sed -n 's/^STDIN: //p' %s | not llvm-strings -n 0 2>&1 | FileCheck --check-prefix CHECK-0 %s
+RUN: sed -n 's/^STDIN: //p' %s | llvm-strings -n 1 | FileCheck --check-prefix CHECK-1 %s
+RUN: sed -n 's/^STDIN: //p' %s | llvm-strings -n 2 | FileCheck --check-prefix CHECK-2 %s
+
+STDIN: a
+STDIN: ab
+STDIN: abc
+STDIN: abcd
+
+CHECK-DEFAULT: abcd
+
+CHECK-0: invalid minimum string length 0
+
+CHECK-1: a
+CHECK-1: ab
+CHECK-1: abc
+CHECK-1: abcd
+
+CHECK-2-NOT: a
+CHECK-2: ab
+CHECK-2: abc
+CHECK-2: 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=286914&r1=286913&r2=286914&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-strings/llvm-strings.cpp (original)
+++ llvm/trunk/tools/llvm-strings/llvm-strings.cpp Mon Nov 14 18:43:52 2016
@@ -35,8 +35,15 @@ static cl::opt<bool>
static cl::alias PrintFileNameShort("f", cl::desc(""),
cl::aliasopt(PrintFileName));
+static cl::opt<int>
+ MinLength("bytes", cl::desc("Print sequences of the specified length"),
+ cl::init(4));
+static cl::alias MinLengthShort("n", cl::desc(""), cl::aliasopt(MinLength));
+
static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
auto print = [&OS, FileName](StringRef L) {
+ if (L.size() < static_cast<size_t>(MinLength))
+ return;
if (PrintFileName)
OS << FileName << ": ";
OS << L << '\n';
@@ -48,12 +55,11 @@ static void strings(raw_ostream &OS, Str
if (S == nullptr)
S = P;
} else if (S) {
- if (P - S > 3)
- print(StringRef(S, P - S));
+ print(StringRef(S, P - S));
S = nullptr;
}
}
- if (S && E - S > 3)
+ if (S)
print(StringRef(S, E - S));
}
@@ -62,6 +68,10 @@ int main(int argc, char **argv) {
PrettyStackTraceProgram X(argc, argv);
cl::ParseCommandLineOptions(argc, argv, "llvm string dumper\n");
+ if (MinLength == 0) {
+ errs() << "invalid minimum string length 0\n";
+ return EXIT_FAILURE;
+ }
if (InputFileNames.empty())
InputFileNames.push_back("-");
More information about the llvm-commits
mailing list