diff --git a/llvm/test/ThinLTO/X86/empty-module.ll b/llvm/test/ThinLTO/X86/empty-module.ll --- a/llvm/test/ThinLTO/X86/empty-module.ll +++ b/llvm/test/ThinLTO/X86/empty-module.ll @@ -3,8 +3,7 @@ ; RUN: rm -f %t2.0 ; RUN: llvm-lto2 run %t.bc -r %t.bc,foo,pl -o %t2 -thinlto-distributed-indexes ; RUN: llvm-readobj -h %t2.0 | FileCheck %s -; RUN: llvm-nm %t2.0 2>&1 | FileCheck %s -check-prefix=NO-SYMBOLS -; NO-SYMBOLS: no symbols +; RUN: llvm-nm %t2.0 2>&1 | count 0 ; CHECK: Format: ELF64-x86-64 diff --git a/llvm/test/ThinLTO/X86/strong_non_prevailing.ll b/llvm/test/ThinLTO/X86/strong_non_prevailing.ll --- a/llvm/test/ThinLTO/X86/strong_non_prevailing.ll +++ b/llvm/test/ThinLTO/X86/strong_non_prevailing.ll @@ -3,7 +3,7 @@ ; RUN: llvm-lto -thinlto-action=run %t.bc %t2.bc -exported-symbol=__llvm_profile_filename ; RUN: llvm-nm -o - < %t.bc.thinlto.o | FileCheck %s --check-prefix=EXPORTED -; RUN: llvm-nm -o - < %t2.bc.thinlto.o 2>&1 | FileCheck %s --check-prefix=NOT_EXPORTED +; RUN: llvm-nm -o - < %t2.bc.thinlto.o 2>&1 | count 0 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" @@ -13,4 +13,3 @@ @__llvm_profile_filename = constant [19 x i8] c"default_%m.profraw\00", comdat ; EXPORTED: R __llvm_profile_filename -; NOT_EXPORTED-NOT: R __llvm_profile_filename diff --git a/llvm/test/tools/llvm-nm/X86/nm-no-symbols-local-only.yaml b/llvm/test/tools/llvm-nm/X86/nm-no-symbols-local-only.yaml new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-nm/X86/nm-no-symbols-local-only.yaml @@ -0,0 +1,15 @@ +## When a file contains only local symbols the "no symbols" error should not +## be shown, so we expect the output to be completely empty. +# RUN: yaml2obj %s > %t.o +# RUN: llvm-nm %t.o 2>&1 | count 0 + +!ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Symbols: + - Name: foo.c + Type: STT_FILE + Index: SHN_ABS diff --git a/llvm/test/tools/llvm-nm/X86/nm-no-symbols.test b/llvm/test/tools/llvm-nm/X86/nm-no-symbols.test --- a/llvm/test/tools/llvm-nm/X86/nm-no-symbols.test +++ b/llvm/test/tools/llvm-nm/X86/nm-no-symbols.test @@ -1,6 +1,5 @@ # RUN: yaml2obj %s > %t.o -# RUN: llvm-nm %t.o 2>&1 | FileCheck %s -# RUN: llvm-nm --print-file-name %t.o 2>&1 | FileCheck %s --check-prefix=CHECK-PRINT-FILE-NAME +# RUN: llvm-nm %t.o 2>&1 | FileCheck %s -DFILE=%t.o !ELF FileHeader: @@ -9,6 +8,4 @@ Type: ET_REL Machine: EM_X86_64 -# CHECK: {{^}}no symbols{{$}} - -# CHECK-PRINT-FILE-NAME: nm-no-symbols.test{{.*}}.o: no symbols{{$}} +# CHECK: [[FILE]]: no symbols{{$}} diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp --- a/llvm/tools/llvm-nm/llvm-nm.cpp +++ b/llvm/tools/llvm-nm/llvm-nm.cpp @@ -707,6 +707,19 @@ return Sym.TypeChar != 'U' && Sym.TypeChar != 'w' && Sym.TypeChar != 'v'; } +static void writeFileName(raw_ostream &S, StringRef ArchiveName, + StringRef ArchitectureName) { + if (!ArchitectureName.empty()) + S << "(for architecture " << ArchitectureName << "):"; + if (OutputFormat == posix && !ArchiveName.empty()) + S << ArchiveName << "[" << CurrentFilename << "]: "; + else { + if (!ArchiveName.empty()) + S << ArchiveName << ":"; + S << CurrentFilename << ": "; + } +} + static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName, StringRef ArchiveName, StringRef ArchitectureName) { @@ -773,24 +786,6 @@ } } - auto writeFileName = [&](raw_ostream &S) { - if (!ArchitectureName.empty()) - S << "(for architecture " << ArchitectureName << "):"; - if (OutputFormat == posix && !ArchiveName.empty()) - S << ArchiveName << "[" << CurrentFilename << "]: "; - else { - if (!ArchiveName.empty()) - S << ArchiveName << ":"; - S << CurrentFilename << ": "; - } - }; - - if (SymbolList.empty()) { - if (PrintFileName) - writeFileName(errs()); - errs() << "no symbols\n"; - } - for (const NMSymbol &S : SymbolList) { uint32_t SymFlags; std::string Name = S.Name.str(); @@ -811,7 +806,7 @@ (!Global && ExternalOnly) || (Weak && NoWeakSymbols)) continue; if (PrintFileName) - writeFileName(outs()); + writeFileName(outs(), ArchiveName, ArchitectureName); if ((JustSymbolName || (UndefinedOnly && MachO && OutputFormat != darwin)) && OutputFormat != posix) { @@ -1208,7 +1203,7 @@ if (Nsect == 0) return; } - if (!MachO || !DyldInfoOnly) { + if (!(MachO && DyldInfoOnly)) { for (BasicSymbolRef Sym : Symbols) { uint32_t SymFlags = Sym.getFlags(); if (!DebugSyms && (SymFlags & SymbolRef::SF_FormatSpecific)) @@ -1738,6 +1733,12 @@ } CurrentFilename = Obj.getFileName(); + + if (Symbols.empty() && SymbolList.empty()) { + writeFileName(errs(), ArchiveName, ArchitectureName); + errs() << "no symbols\n"; + } + sortAndPrintSymbolList(Obj, printName, ArchiveName, ArchitectureName); }