From 53d771aafdbe5b919f264f53cba3788e2c4cffd2 Mon Sep 17 00:00:00 2001 From: patrick Date: Tue, 24 Jan 2017 08:32:59 +0000 Subject: Import LLVM 4.0.0 rc1 including clang and lld to help the current development effort on OpenBSD/arm64. --- gnu/llvm/tools/llvm-strings/llvm-strings.cpp | 90 ++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 gnu/llvm/tools/llvm-strings/llvm-strings.cpp (limited to 'gnu/llvm/tools/llvm-strings/llvm-strings.cpp') diff --git a/gnu/llvm/tools/llvm-strings/llvm-strings.cpp b/gnu/llvm/tools/llvm-strings/llvm-strings.cpp new file mode 100644 index 00000000000..e750995331e --- /dev/null +++ b/gnu/llvm/tools/llvm-strings/llvm-strings.cpp @@ -0,0 +1,90 @@ +//===-- llvm-strings.cpp - Printable String dumping utility ---------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This program is a utility that works like binutils "strings", that is, it +// prints out printable strings in a binary, objdump, or archive file. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Object/Binary.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/Program.h" +#include "llvm/Support/Signals.h" +#include +#include + +using namespace llvm; +using namespace llvm::object; + +static cl::list InputFileNames(cl::Positional, + cl::desc(""), + cl::ZeroOrMore); + +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 cl::opt + 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(MinLength)) + return; + 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) { + print(StringRef(S, P - S)); + S = nullptr; + } + } + if (S) + print(StringRef(S, E - S)); +} + +int main(int argc, char **argv) { + sys::PrintStackTraceOnErrorSignal(argv[0]); + 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("-"); + + for (const auto &File : InputFileNames) { + ErrorOr> Buffer = + MemoryBuffer::getFileOrSTDIN(File); + if (std::error_code EC = Buffer.getError()) + errs() << File << ": " << EC.message() << '\n'; + else + strings(llvm::outs(), File == "-" ? "{standard input}" : File, + Buffer.get()->getMemBufferRef().getBuffer()); + } + + return EXIT_SUCCESS; +} -- cgit v1.2.3-59-g8ed1b