[llvm-symbolizer] -print-source-context-lines option to print source code around...
[oota-llvm.git] / lib / DebugInfo / Symbolize / DIPrinter.cpp
index ad5f693d77e4cbfcc1a2062b6231c90f084bfb46..7d7ace0759e6d605048566b4b3b961125b4e2f0c 100644 (file)
@@ -15,6 +15,7 @@
 #include "llvm/DebugInfo/Symbolize/DIPrinter.h"
 
 #include "llvm/DebugInfo/DIContext.h"
+#include "llvm/Support/LineIterator.h"
 
 namespace llvm {
 namespace symbolize {
@@ -24,27 +25,65 @@ namespace symbolize {
 static const char kDILineInfoBadString[] = "<invalid>";
 static const char kBadString[] = "??";
 
-DIPrinter &DIPrinter::operator<<(const DILineInfo &Info) {
+// Prints source code around in the FileName the Line.
+void DIPrinter::printContext(std::string FileName, int64_t Line) {
+  if (PrintSourceContext <= 0)
+    return;
+
+  ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
+      MemoryBuffer::getFile(FileName);
+  if (!BufOrErr)
+    return;
+
+  std::unique_ptr<MemoryBuffer> Buf = std::move(BufOrErr.get());
+  int64_t FirstLine = std::max(1l, Line - PrintSourceContext / 2);
+  int64_t LastLine = FirstLine + PrintSourceContext;
+  size_t MaxLineNumberWidth = std::ceil(std::log10(LastLine));
+
+  for (line_iterator I = line_iterator(*Buf, false);
+       !I.is_at_eof() && I.line_number() <= LastLine; ++I) {
+    int64_t L = I.line_number();
+    if (L >= FirstLine && L <= LastLine) {
+      OS << format_decimal(L, MaxLineNumberWidth);
+      if (L == Line)
+        OS << " >: ";
+      else
+        OS << "  : ";
+      OS << *I << "\n";
+    }
+  }
+}
+
+void DIPrinter::print(const DILineInfo &Info, bool Inlined) {
   if (PrintFunctionNames) {
     std::string FunctionName = Info.FunctionName;
     if (FunctionName == kDILineInfoBadString)
       FunctionName = kBadString;
-    OS << FunctionName << "\n";
+
+    StringRef Delimiter = (PrintPretty == true) ? " at " : "\n";
+    StringRef Prefix = (PrintPretty && Inlined) ? " (inlined by) " : "";
+    OS << Prefix << FunctionName << Delimiter;
   }
   std::string Filename = Info.FileName;
   if (Filename == kDILineInfoBadString)
     Filename = kBadString;
   OS << Filename << ":" << Info.Line << ":" << Info.Column << "\n";
+  printContext(Filename, Info.Line);
+}
+
+DIPrinter &DIPrinter::operator<<(const DILineInfo &Info) {
+  print(Info, false);
   return *this;
 }
 
 DIPrinter &DIPrinter::operator<<(const DIInliningInfo &Info) {
   uint32_t FramesNum = Info.getNumberOfFrames();
-  if (FramesNum == 0)
-    return (*this << DILineInfo());
-  for (uint32_t i = 0; i < FramesNum; i++) {
-    *this << Info.getFrame(i);
+  if (FramesNum == 0) {
+    print(DILineInfo(), false);
+    return *this;
   }
+  for (uint32_t i = 0; i < FramesNum; i++)
+    print(Info.getFrame(i), i > 0);
   return *this;
 }