[llvm] e95f9a2 - [llvm-objdump] Implement --prefix option

Renato Golin via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 16 09:51:27 PDT 2020


Author: Vinicius Tinti
Date: 2020-10-16T17:50:42+01:00
New Revision: e95f9a23fad52ffa3a5b6466c7dcdf910d099956

URL: https://github.com/llvm/llvm-project/commit/e95f9a23fad52ffa3a5b6466c7dcdf910d099956
DIFF: https://github.com/llvm/llvm-project/commit/e95f9a23fad52ffa3a5b6466c7dcdf910d099956.diff

LOG: [llvm-objdump] Implement --prefix option

The prefix given to --prefix will be added to GNU absolute paths when
used with --source option (source interleaved with the disassembly).

This matches GNU's objdump behavior.

GNU and C++17 rules for absolute paths are different.

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

Fixes PR46368.

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

Added: 
    llvm/test/tools/llvm-objdump/X86/source-interleave-prefix-non-windows.test
    llvm/test/tools/llvm-objdump/X86/source-interleave-prefix-windows.test
    llvm/test/tools/llvm-objdump/X86/source-interleave-prefix.test

Modified: 
    llvm/docs/CommandGuide/llvm-objdump.rst
    llvm/docs/llvm-objdump.1
    llvm/tools/llvm-objdump/llvm-objdump.cpp
    llvm/tools/llvm-objdump/llvm-objdump.h

Removed: 
    


################################################################################
diff  --git a/llvm/docs/CommandGuide/llvm-objdump.rst b/llvm/docs/CommandGuide/llvm-objdump.rst
index e4f063b60f9f..4acb04833ab1 100644
--- a/llvm/docs/CommandGuide/llvm-objdump.rst
+++ b/llvm/docs/CommandGuide/llvm-objdump.rst
@@ -167,6 +167,11 @@ OPTIONS
 
   When disassembling, do not print the raw bytes of each instruction.
 
+.. option:: --prefix=<prefix>
+
+  When disassembling with the :option:`--source` option, prepend ``prefix`` to
+  absolute paths.
+
 .. option:: --print-imm-hex
 
   Use hex format when printing immediate values in disassembly output.

diff  --git a/llvm/docs/llvm-objdump.1 b/llvm/docs/llvm-objdump.1
index 85c40d7da682..1cf557dc8b64 100644
--- a/llvm/docs/llvm-objdump.1
+++ b/llvm/docs/llvm-objdump.1
@@ -105,6 +105,10 @@ Print no leading address.
 Print no leading headers.
 .It Fl -no-show-raw-insn
 When disassembling instructions, do not print the instruction bytes.
+.It Fl -prefix Ns = Ns Ar PREFIX
+When disassembling, add
+.Ar PREFIX
+to absolute paths.
 .It Fl -print-imm-hex
 Use hex format for immediate values.
 .It Fl -private-header

diff  --git a/llvm/test/tools/llvm-objdump/X86/source-interleave-prefix-non-windows.test b/llvm/test/tools/llvm-objdump/X86/source-interleave-prefix-non-windows.test
new file mode 100644
index 000000000000..a32d1b704736
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/X86/source-interleave-prefix-non-windows.test
@@ -0,0 +1,10 @@
+;; Test --prefix option platform specific behavior.
+; UNSUPPORTED: system-windows
+
+;; Test removal of trailing separators (only '/').
+;; The prefix 'myprefix/\' is not changed.
+
+; RUN: sed -e "s,SRC_COMPDIR,/Inputs,g" %p/Inputs/source-interleave.ll > %t.ll
+; RUN: llc -o %t.o -filetype=obj -mtriple=x86_64-pc-linux %t.ll
+; RUN: llvm-objdump --prefix 'myprefix/\' --source %t.o 2>&1 | FileCheck %s -DFILE=%t.o -DPREFIX='myprefix/\'
+; CHECK: warning: '[[FILE]]': failed to find source [[PREFIX]]/Inputs/source-interleave-x86_64.c

diff  --git a/llvm/test/tools/llvm-objdump/X86/source-interleave-prefix-windows.test b/llvm/test/tools/llvm-objdump/X86/source-interleave-prefix-windows.test
new file mode 100644
index 000000000000..294a5671c2e6
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/X86/source-interleave-prefix-windows.test
@@ -0,0 +1,10 @@
+;; Test --prefix option platform specific behavior.
+; REQUIRES: system-windows
+
+;; Test removal of trailing separators (both '/' and '\').
+;; The prefix 'myprefix/\' is changed to 'myprefix'.
+
+; RUN: sed -e "s,SRC_COMPDIR,/Inputs,g" %p/Inputs/source-interleave.ll > %t.ll
+; RUN: llc -o %t.o -filetype=obj -mtriple=x86_64-pc-linux %t.ll
+; RUN: llvm-objdump --prefix 'myprefix/\' --source %t.o 2>&1 | FileCheck %s -DFILE=%t.o -DPREFIX='myprefix'
+; CHECK: warning: '[[FILE]]': failed to find source [[PREFIX]]/Inputs/source-interleave-x86_64.c

diff  --git a/llvm/test/tools/llvm-objdump/X86/source-interleave-prefix.test b/llvm/test/tools/llvm-objdump/X86/source-interleave-prefix.test
new file mode 100644
index 000000000000..de05c8d1cb3d
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/X86/source-interleave-prefix.test
@@ -0,0 +1,44 @@
+;; Test --prefix option.
+
+;; Test prefix option ignored for relative paths.
+
+; RUN: sed -e "s,SRC_COMPDIR,./Inputs,g" %p/Inputs/source-interleave.ll > %t-relative-path.ll
+; RUN: llc -o %t-relative-path.o -filetype=obj -mtriple=x86_64-pc-linux %t-relative-path.ll
+; RUN: llvm-objdump --prefix myprefix --source %t-relative-path.o 2>&1 | \
+; RUN:   FileCheck %s --check-prefix=CHECK-BROKEN-PREFIX -DFILE=%t-relative-path.o -DPREFIX=.
+; CHECK-BROKEN-PREFIX: warning: '[[FILE]]': failed to find source [[PREFIX]]/Inputs/source-interleave-x86_64.c
+
+;; Test invalid source interleave fixed by adding the correct prefix.
+
+; RUN: sed -e "s,SRC_COMPDIR,/Inputs,g" %p/Inputs/source-interleave.ll > %t-missing-prefix.ll
+; RUN: llc -o %t-missing-prefix.o -filetype=obj -mtriple=x86_64-pc-linux %t-missing-prefix.ll
+; RUN: llvm-objdump --prefix %p --source %t-missing-prefix.o 2>&1 | \
+; RUN:   FileCheck %s --check-prefix=CHECK-MISSING-PREFIX-FIX
+; CHECK-MISSING-PREFIX-FIX: ; int foo() {
+
+;; Test valid source interleave broken by adding an incorrect prefix.
+
+; RUN: sed -e "s,SRC_COMPDIR,%/p/Inputs,g" %p/Inputs/source-interleave.ll > %t-correct-prefix.ll
+; RUN: llc -o %t-correct-prefix.o -filetype=obj -mtriple=x86_64-pc-linux %t-correct-prefix.ll
+; RUN: llvm-objdump --prefix myprefix --source %t-correct-prefix.o 2>&1 | \
+; RUN:   FileCheck %s --check-prefix=CHECK-BROKEN-PREFIX -DFILE=%t-correct-prefix.o -DPREFIX=myprefix%/p
+
+;; Test malformed input.
+
+; RUN: sed -e "s,SRC_COMPDIR,,g" -e "s,filename: \"source-interleave-x86_64.c\",filename: \"\",g" \
+; RUN:   %p/Inputs/source-interleave.ll > %t-malformed.ll
+; RUN: llc -o %t-malformed.o -filetype=obj -mtriple=x86_64-pc-linux %t-malformed.ll
+; RUN: llvm-objdump --prefix myprefix --source %t-malformed.o 2>&1 | \
+; RUN:   FileCheck %s --check-prefix=CHECK-MALFORMED -DFILE=%t-malformed.o
+; CHECK-MALFORMED: warning: '[[FILE]]': failed to parse debug information for [[FILE]]
+
+;; Using only a prefix separator is the same as not using the `--prefix` option.
+
+; RUN: llvm-objdump --prefix / --source %t-missing-prefix.o 2>&1 | \
+; RUN:   FileCheck %s --check-prefix=CHECK-BROKEN-PREFIX -DFILE=%t-missing-prefix.o -DPREFIX=''
+
+;; All trailing separators on the prefix are discarded.
+;; The prefix 'myprefix//' is converted to 'myprefix'.
+
+; RUN: llvm-objdump --prefix myprefix// --source %t-missing-prefix.o 2>&1 | \
+; RUN:   FileCheck %s --check-prefix=CHECK-BROKEN-PREFIX -DFILE=%t-missing-prefix.o -DPREFIX=myprefix

diff  --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index d57ea8ef94e7..bc13b26b797d 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -353,6 +353,10 @@ static cl::opt<bool>
          cl::cat(ObjdumpCat));
 static cl::alias WideShort("w", cl::Grouping, cl::aliasopt(Wide));
 
+cl::opt<std::string> objdump::Prefix("prefix",
+                                     cl::desc("Add prefix to absolute paths"),
+                                     cl::cat(ObjdumpCat));
+
 enum DebugVarsFormat {
   DVDisabled,
   DVUnicode,
@@ -1031,6 +1035,13 @@ void SourcePrinter::printSourceLine(formatted_raw_ostream &OS,
     }
   }
 
+  if (!Prefix.empty() && sys::path::is_absolute_gnu(LineInfo.FileName)) {
+    SmallString<128> FilePath;
+    sys::path::append(FilePath, Prefix, LineInfo.FileName);
+
+    LineInfo.FileName = std::string(FilePath);
+  }
+
   if (PrintLines)
     printLines(OS, LineInfo, Delimiter, LVP);
   if (PrintSource)
@@ -2961,6 +2972,10 @@ int main(int argc, char **argv) {
   if (InputFilenames.empty())
     InputFilenames.push_back("a.out");
 
+  // Removes trailing separators from prefix.
+  while (!Prefix.empty() && sys::path::is_separator(Prefix.back()))
+    Prefix.pop_back();
+
   if (AllHeaders)
     ArchiveHeaders = FileHeaders = PrivateHeaders = Relocations =
         SectionHeaders = SymbolTable = true;

diff  --git a/llvm/tools/llvm-objdump/llvm-objdump.h b/llvm/tools/llvm-objdump/llvm-objdump.h
index 390fc62d09f8..4cee35484105 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.h
+++ b/llvm/tools/llvm-objdump/llvm-objdump.h
@@ -39,6 +39,7 @@ extern cl::list<std::string> MAttrs;
 extern cl::opt<std::string> MCPU;
 extern cl::opt<bool> NoShowRawInsn;
 extern cl::opt<bool> NoLeadingAddr;
+extern cl::opt<std::string> Prefix;
 extern cl::opt<bool> PrintImmHex;
 extern cl::opt<bool> PrivateHeaders;
 extern cl::opt<bool> Relocations;


        


More information about the llvm-commits mailing list