[llvm] r286556 - llvm-strings: introduce basic strings tool
Saleem Abdulrasool via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 11 18:34:22 PST 2016
On Thu, Nov 10, 2016 at 8:02 PM, Rui Ueyama <ruiu at google.com> wrote:
> On Thu, Nov 10, 2016 at 7:59 PM, Rui Ueyama <ruiu at google.com> wrote:
>
>> I got this warning message. I was trying to add (void) there, but
>> probably we should handle errors instead of ignore them. Could you fix it?
>>
>> [1/2] Building CXX object tools/llvm-strings/CMakeFiles/
>> llvm-strings.dir/llvm-strings.cpp.o
>> /ssd/llvm/tools/llvm-strings/llvm-strings.cpp:77:5: warning: expression
>> result unused [-Wunused-value]
>> static_cast<bool>(E);
>> ^~~~~~~~~~~~~~~~~~~~
>>
>> On Thu, Nov 10, 2016 at 7:44 PM, Saleem Abdulrasool via llvm-commits <
>> llvm-commits at lists.llvm.org> wrote:
>>
>>> Author: compnerd
>>> Date: Thu Nov 10 21:44:12 2016
>>> New Revision: 286556
>>>
>>> URL: http://llvm.org/viewvc/llvm-project?rev=286556&view=rev
>>> Log:
>>> llvm-strings: introduce basic strings tool
>>>
>>> This is a replacement to binutils' string tool. It prints strings found
>>> in a
>>> binary (object file, executable, or archive library). It is rather bare
>>> and
>>> not functionally equivalent, however, it lays the groundwork necessary
>>> for the
>>> strings tool, enabling iterative development of features to reach feature
>>> parity.
>>>
>>> Added:
>>> llvm/trunk/tools/llvm-strings/
>>> llvm/trunk/tools/llvm-strings/CMakeLists.txt
>>> llvm/trunk/tools/llvm-strings/LLVMBuild.txt
>>> llvm/trunk/tools/llvm-strings/llvm-strings.cpp
>>>
>>> Added: llvm/trunk/tools/llvm-strings/CMakeLists.txt
>>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-st
>>> rings/CMakeLists.txt?rev=286556&view=auto
>>> ============================================================
>>> ==================
>>> --- llvm/trunk/tools/llvm-strings/CMakeLists.txt (added)
>>> +++ llvm/trunk/tools/llvm-strings/CMakeLists.txt Thu Nov 10 21:44:12
>>> 2016
>>> @@ -0,0 +1,8 @@
>>> +set(LLVM_LINK_COMPONENTS
>>> + Object
>>> + )
>>> +
>>> +add_llvm_tool(llvm-strings
>>> + llvm-strings.cpp
>>> + )
>>> +
>>>
>>> Added: llvm/trunk/tools/llvm-strings/LLVMBuild.txt
>>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-st
>>> rings/LLVMBuild.txt?rev=286556&view=auto
>>> ============================================================
>>> ==================
>>> --- llvm/trunk/tools/llvm-strings/LLVMBuild.txt (added)
>>> +++ llvm/trunk/tools/llvm-strings/LLVMBuild.txt Thu Nov 10 21:44:12 2016
>>> @@ -0,0 +1,22 @@
>>> +;===- ./tools/llvm-strings/LLVMBuild.txt -----------------------*-
>>> Conf -*--===;
>>> +;
>>> +; The LLVM Compiler Infrastructure
>>> +;
>>> +; This file is distributed under the University of Illinois Open Source
>>> +; License. See LICENSE.TXT for details.
>>> +;
>>> +;===-------------------------------------------------------
>>> -----------------===;
>>> +;
>>> +; This is an LLVMBuild description file for the components in this
>>> subdirectory.
>>> +;
>>> +; For more information on the LLVMBuild system, please see:
>>> +;
>>> +; http://llvm.org/docs/LLVMBuild.html
>>> +;
>>> +;===-------------------------------------------------------
>>> -----------------===;
>>> +
>>> +[component_0]
>>> +type = Tool
>>> +name = llvm-strings
>>> +parent = Tools
>>> +required_libraries = Archive Object
>>>
>>> Added: llvm/trunk/tools/llvm-strings/llvm-strings.cpp
>>> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-st
>>> rings/llvm-strings.cpp?rev=286556&view=auto
>>> ============================================================
>>> ==================
>>> --- llvm/trunk/tools/llvm-strings/llvm-strings.cpp (added)
>>> +++ llvm/trunk/tools/llvm-strings/llvm-strings.cpp Thu Nov 10 21:44:12
>>> 2016
>>> @@ -0,0 +1,120 @@
>>> +//===-- 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/IR/LLVMContext.h"
>>> +#include "llvm/Object/Archive.h"
>>> +#include "llvm/Object/Binary.h"
>>> +#include "llvm/Object/ObjectFile.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 <string>
>>> +
>>> +using namespace llvm;
>>> +using namespace llvm::object;
>>> +
>>> +static cl::list<std::string> InputFileNames(cl::Positional,
>>> + cl::desc("<input object
>>> files>"),
>>> + cl::ZeroOrMore);
>>> +
>>> +static void dump(raw_ostream &OS, StringRef Contents) {
>>> + const char *S = nullptr;
>>> + for (const char *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';
>>> + S = nullptr;
>>> + }
>>> + }
>>> +}
>>>
>>
> There's a bug here.
>
> $ echo -n abcdefg > foo
> $ bin/llvm-strings foo
> $ [nothing printed out]
>
Nice find! Ill fix this and add a test.
> +namespace {
>>> +class Strings {
>>> + LLVMContext Context;
>>> + raw_ostream &OS;
>>> +
>>> + void dump(const ObjectFile *O) {
>>> + for (const auto &S : O->sections()) {
>>> + StringRef Contents;
>>> + if (!S.getContents(Contents))
>>> + ::dump(OS, Contents);
>>> + }
>>> + }
>>> +
>>> + void dump(const Archive *A) {
>>> + Error E;
>>> + for (auto &Element : A->children(E)) {
>>> + if (Expected<std::unique_ptr<Binary>> Child =
>>> + Element.getAsBinary(&Context)) {
>>> + dump(dyn_cast<ObjectFile>(&**Child));
>>> + } else {
>>> + if (auto E = isNotObjectErrorInvalidFileType(Child.takeError()))
>>> {
>>> + errs() << A->getFileName();
>>> + if (Expected<StringRef> Name = Element.getName())
>>> + errs() << '(' << *Name << ')';
>>> + logAllUnhandledErrors(std::move(E), errs(), "");
>>> + errs() << '\n';
>>> + }
>>> + }
>>> + }
>>> + static_cast<bool>(E);
>>> + }
>>> +
>>> +public:
>>> + Strings(raw_ostream &S) : OS(S) {}
>>> +
>>> + void scan(StringRef File) {
>>> + ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
>>> + MemoryBuffer::getFileOrSTDIN(File);
>>> + if (std::error_code EC = Buffer.getError()) {
>>> + errs() << File << ": " << EC.message() << '\n';
>>> + return;
>>> + }
>>> +
>>> + if (Expected<std::unique_ptr<Binary>> B =
>>> + createBinary(Buffer.get()->getMemBufferRef(), &Context)) {
>>> + if (auto *A = dyn_cast<Archive>(&**B))
>>> + return dump(A);
>>> + if (auto *O = dyn_cast<ObjectFile>(&**B))
>>> + return dump(O);
>>> + ::dump(OS, Buffer.get()->getMemBufferRef().getBuffer());
>>> + } else {
>>> + consumeError(B.takeError());
>>> + ::dump(OS, Buffer.get()->getMemBufferRef().getBuffer());
>>> + }
>>> + }
>>> +};
>>> +}
>>> +
>>> +int main(int argc, char **argv) {
>>> + sys::PrintStackTraceOnErrorSignal(argv[0]);
>>> + PrettyStackTraceProgram X(argc, argv);
>>> +
>>> + cl::ParseCommandLineOptions(argc, argv, "llvm string dumper\n");
>>> +
>>> + if (InputFileNames.empty())
>>> + InputFileNames.push_back("-");
>>> +
>>> + Strings S(llvm::outs());
>>> + std::for_each(InputFileNames.begin(), InputFileNames.end(),
>>> + [&S](StringRef F) { S.scan(F); });
>>> + return EXIT_SUCCESS;
>>> +}
>>> +
>>>
>>>
>>> _______________________________________________
>>> llvm-commits mailing list
>>> llvm-commits at lists.llvm.org
>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>>
>>
>>
>
--
Saleem Abdulrasool
compnerd (at) compnerd (dot) org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161111/dde6aeae/attachment.html>
More information about the llvm-commits
mailing list