[llvm-symbolizer] -print-source-context-lines option to print source code around...
[oota-llvm.git] / tools / llvm-symbolizer / llvm-symbolizer.cpp
index e45660c84c7723c5923b547bd5639c308a867f8b..950349377bf79c21ca85579188212b8442cc670c 100644 (file)
@@ -82,6 +82,10 @@ static cl::opt<bool>
     ClPrettyPrint("pretty-print", cl::init(false),
                   cl::desc("Make the output more human friendly"));
 
+static cl::opt<int> ClPrintSourceContextLines(
+    "print-source-context-lines", cl::init(0),
+    cl::desc("Print N number of source file context"));
+
 static bool error(std::error_code ec) {
   if (!ec)
     return false;
@@ -89,18 +93,14 @@ static bool error(std::error_code ec) {
   return true;
 }
 
-static bool parseCommand(bool &IsData, std::string &ModuleName,
-                         uint64_t &ModuleOffset) {
+static bool parseCommand(StringRef InputString, bool &IsData,
+                         std::string &ModuleName, uint64_t &ModuleOffset) {
   const char *kDataCmd = "DATA ";
   const char *kCodeCmd = "CODE ";
-  const int kMaxInputStringLength = 1024;
-  const char kDelimiters[] = " \n";
-  char InputString[kMaxInputStringLength];
-  if (!fgets(InputString, sizeof(InputString), stdin))
-    return false;
+  const char kDelimiters[] = " \n\r";
   IsData = false;
   ModuleName = "";
-  char *pos = InputString;
+  const char *pos = InputString.data();
   if (strncmp(pos, kDataCmd, strlen(kDataCmd)) == 0) {
     IsData = true;
     pos += strlen(kDataCmd);
@@ -117,7 +117,7 @@ static bool parseCommand(bool &IsData, std::string &ModuleName,
     if (*pos == '"' || *pos == '\'') {
       char quote = *pos;
       pos++;
-      char *end = strchr(pos, quote);
+      const char *end = strchr(pos, quote);
       if (!end)
         return false;
       ModuleName = std::string(pos, end - pos);
@@ -158,13 +158,25 @@ int main(int argc, char **argv) {
   }
   LLVMSymbolizer Symbolizer(Opts);
 
-  bool IsData = false;
-  std::string ModuleName;
-  uint64_t ModuleOffset;
   DIPrinter Printer(outs(), ClPrintFunctions != FunctionNameKind::None,
-                    ClPrettyPrint);
+                    ClPrettyPrint, ClPrintSourceContextLines);
+
+  const int kMaxInputStringLength = 1024;
+  char InputString[kMaxInputStringLength];
+
+  while (true) {
+    if (!fgets(InputString, sizeof(InputString), stdin))
+      break;
+
+    bool IsData = false;
+    std::string ModuleName;
+    uint64_t ModuleOffset = 0;
+    if (!parseCommand(StringRef(InputString), IsData, ModuleName,
+                      ModuleOffset)) {
+      outs() << InputString;
+      continue;
+    }
 
-  while (parseCommand(IsData, ModuleName, ModuleOffset)) {
     if (ClPrintAddress) {
       outs() << "0x";
       outs().write_hex(ModuleOffset);