[llvm-symbolizer] Make the output with -output-style=GNU closer to addr2line's

This patch addresses two differences in the output of llvm-symbolizer
and GNU's addr2line:

* llvm-symbolizer prints an empty line after the report for an address.

* With "-f -i=0", llvm-symbolizer replaces the name of an inlined
  function with the name from the symbol table, i. e., the top caller
  function in the inlining chain. addr2line preserves the name of the
  inlined function.

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


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@358747 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/tools/llvm-symbolizer/output-style.test b/test/tools/llvm-symbolizer/output-style-column.test
similarity index 100%
rename from test/tools/llvm-symbolizer/output-style.test
rename to test/tools/llvm-symbolizer/output-style-column.test
diff --git a/test/tools/llvm-symbolizer/output-style-empty-line.test b/test/tools/llvm-symbolizer/output-style-empty-line.test
new file mode 100644
index 0000000..8b14b4c
--- /dev/null
+++ b/test/tools/llvm-symbolizer/output-style-empty-line.test
@@ -0,0 +1,19 @@
+This test checks that with --output-style=GNU the tool does not print an empty
+line after the report for an address. The current behavior is preserved for
+--output-style=LLVM or if the option is omitted.
+
+RUN: llvm-symbolizer -e %p/Inputs/addr.exe < %p/Inputs/addr.inp \
+RUN:   | FileCheck %s --check-prefix=LLVM
+
+RUN: llvm-symbolizer --output-style=LLVM -e %p/Inputs/addr.exe < %p/Inputs/addr.inp \
+RUN:   | FileCheck %s --check-prefix=LLVM
+
+RUN: llvm-symbolizer --output-style=GNU -e %p/Inputs/addr.exe < %p/Inputs/addr.inp \
+RUN:   | FileCheck %s --check-prefix=GNU
+
+LLVM: x.c:14:0
+LLVM-EMPTY:
+LLVM-NEXT: some text2
+
+GNU: x.c:14
+GNU-NEXT: some text2
diff --git a/test/tools/llvm-symbolizer/output-style-inlined.test b/test/tools/llvm-symbolizer/output-style-inlined.test
new file mode 100644
index 0000000..e970753
--- /dev/null
+++ b/test/tools/llvm-symbolizer/output-style-inlined.test
@@ -0,0 +1,17 @@
+This test checks that when inlined frames are not shown (-i=0) and the output
+style is set to GNU (--output-style=GNU) the name of an inlined function is not
+replaced with the name of the top caller function. At the same time, the current
+behavior of llvm-symbolizer is preserved with --output-style=LLVM or when
+the option is not specified.
+
+RUN: llvm-symbolizer -i=0 -e %p/Inputs/addr.exe 0x40054d \
+RUN:   | FileCheck %s --check-prefix=LLVM --implicit-check-not=inctwo
+
+RUN: llvm-symbolizer --output-style=LLVM -i=0 -e %p/Inputs/addr.exe 0x40054d \
+RUN:   | FileCheck %s --check-prefix=LLVM --implicit-check-not=inctwo
+
+RUN: llvm-symbolizer --output-style=GNU -i=0 -e %p/Inputs/addr.exe 0x40054d \
+RUN:   | FileCheck %s --check-prefix=GNU --implicit-check-not=main
+
+LLVM: main
+GNU: inctwo
diff --git a/tools/llvm-symbolizer/llvm-symbolizer.cpp b/tools/llvm-symbolizer/llvm-symbolizer.cpp
index 359c64b..c57f5f8 100644
--- a/tools/llvm-symbolizer/llvm-symbolizer.cpp
+++ b/tools/llvm-symbolizer/llvm-symbolizer.cpp
@@ -227,13 +227,25 @@
         ModuleName, {Offset, object::SectionedAddress::UndefSection},
         ClDwpName);
     Printer << (error(ResOrErr) ? DIInliningInfo() : ResOrErr.get());
+  } else if (ClOutputStyle == DIPrinter::OutputStyle::GNU) {
+    // With ClPrintFunctions == FunctionNameKind::LinkageName (default)
+    // and ClUseSymbolTable == true (also default), Symbolizer.symbolizeCode()
+    // may override the name of an inlined function with the name of the topmost
+    // caller function in the inlining chain. This contradicts the existing
+    // behavior of addr2line. Symbolizer.symbolizeInlinedCode() overrides only
+    // the topmost function, which suits our needs better.
+    auto ResOrErr = Symbolizer.symbolizeInlinedCode(
+        ModuleName, {Offset, object::SectionedAddress::UndefSection},
+        ClDwpName);
+    Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get().getFrame(0));
   } else {
     auto ResOrErr = Symbolizer.symbolizeCode(
         ModuleName, {Offset, object::SectionedAddress::UndefSection},
         ClDwpName);
     Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get());
   }
-  outs() << "\n";
+  if (ClOutputStyle == DIPrinter::OutputStyle::LLVM)
+    outs() << "\n";
   outs().flush();
 }