[llvm] r365889 - [tools] [llvm-nm] Default to reading from stdin not a.out

Alex Brachet via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 12 03:20:01 PDT 2019


Author: abrachet
Date: Fri Jul 12 03:20:01 2019
New Revision: 365889

URL: http://llvm.org/viewvc/llvm-project?rev=365889&view=rev
Log:
[tools] [llvm-nm] Default to reading from stdin not a.out

Summary: This moves away from defaulting to a.out and uses stdin only if stdin has a file redirected to it. This has been discussed on the llvm-dev mailing list [[ https://lists.llvm.org/pipermail/llvm-dev/2019-July/133642.html | here ]].

Reviewers: jhenderson, rupprecht, MaskRay, chrisjackson

Reviewed By: jhenderson, MaskRay

Subscribers: llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D64290

Added:
    llvm/trunk/test/tools/llvm-nm/stdin.test
Modified:
    llvm/trunk/docs/CommandGuide/llvm-nm.rst
    llvm/trunk/tools/llvm-nm/llvm-nm.cpp

Modified: llvm/trunk/docs/CommandGuide/llvm-nm.rst
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/CommandGuide/llvm-nm.rst?rev=365889&r1=365888&r2=365889&view=diff
==============================================================================
--- llvm/trunk/docs/CommandGuide/llvm-nm.rst (original)
+++ llvm/trunk/docs/CommandGuide/llvm-nm.rst Fri Jul 12 03:20:01 2019
@@ -13,9 +13,8 @@ DESCRIPTION
 
 The :program:`llvm-nm` utility lists the names of symbols from LLVM bitcode
 files, object files, and archives. Each symbol is listed along with some simple
-information about its provenance. If no filename is specified, *a.out* is used
-as the input. If *-* is used as a filename, :program:`llvm-nm` will read a file
-from its standard input stream.
+information about its provenance. If no filename is specified, or *-* is used as
+a filename, :program:`llvm-nm` will read a file from its standard input stream.
 
 :program:`llvm-nm`'s default output format is the traditional BSD :program:`nm`
 output format. Each such output record consists of an (optional) 8-digit

Added: llvm/trunk/test/tools/llvm-nm/stdin.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-nm/stdin.test?rev=365889&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-nm/stdin.test (added)
+++ llvm/trunk/test/tools/llvm-nm/stdin.test Fri Jul 12 03:20:01 2019
@@ -0,0 +1,33 @@
+## Test llvm-nm when using stdin both explicitly (using '-' as a filename)
+## and implicitly (not specifying any filename).
+
+# RUN: yaml2obj %s -o %t.o
+
+## Pass an explicit filename to produce a baseline output. llvm-nm should
+## have the same behavior when opening a file itself and when reading that
+## file from its standard input stream.
+# RUN: llvm-nm %t.o > %t.base 2> %t.err
+
+## Make sure there is no warning message about no file redirected to stdin.
+# RUN: FileCheck %s --input-file=%t.err --allow-empty --implicit-check-not={{.}}
+
+# RUN: llvm-nm - < %t.o > %t.explicit 2> %t.err
+# RUN: FileCheck %s --input-file=%t.err --allow-empty --implicit-check-not={{.}}
+# RUN: cmp %t.base %t.explicit
+
+# RUN: llvm-nm < %t.o > %t.implicit 2> %t.err
+# RUN: FileCheck %s --input-file=%t.err --allow-empty --implicit-check-not={{.}}
+# RUN: cmp %t.base %t.implicit
+
+!ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_REL
+  Machine: EM_X86_64
+Sections:
+  - Name: .text
+    Type: SHT_PROGBITS
+Symbols:
+  - Name:    symbol_a
+    Section: .text

Modified: llvm/trunk/tools/llvm-nm/llvm-nm.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-nm/llvm-nm.cpp?rev=365889&r1=365888&r2=365889&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-nm/llvm-nm.cpp (original)
+++ llvm/trunk/tools/llvm-nm/llvm-nm.cpp Fri Jul 12 03:20:01 2019
@@ -34,6 +34,7 @@
 #include "llvm/Support/Format.h"
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/Process.h"
 #include "llvm/Support/Program.h"
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/TargetSelect.h"
@@ -1751,6 +1752,12 @@ static bool checkMachOAndArchFlags(Symbo
 }
 
 static void dumpSymbolNamesFromFile(std::string &Filename) {
+  if (Filename == "-" && sys::Process::StandardInIsUserInput()) {
+    WithColor::warning(errs(), ToolName) << "can't read from terminal\n";
+    cl::PrintHelpMessage();
+    HadError = true;
+    return;
+  }
   ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
       MemoryBuffer::getFileOrSTDIN(Filename);
   if (error(BufferOrErr.getError(), Filename))
@@ -2082,7 +2089,7 @@ int main(int argc, char **argv) {
   if (OutputFormat == sysv || SizeSort)
     PrintSize = true;
   if (InputFilenames.empty())
-    InputFilenames.push_back("a.out");
+    InputFilenames.push_back("-");
   if (InputFilenames.size() > 1)
     MultipleFiles = true;
 




More information about the llvm-commits mailing list