[PATCH] Add debug method to visualize complex source locations

Jordan Rose jordan_rose at apple.com
Wed May 22 09:20:43 PDT 2013


  You can tokenize without a lexer, but the line/caret combo might actually be better. Nice work!


================
Comment at: lib/Basic/SourceLocation.cpp:70-71
@@ +69,4 @@
+  StringRef Buffer = SM.getBufferData(LocInfo.first);
+  int First = std::max<int>(Buffer.rfind('\n', LocInfo.second) + 1, 0);
+  int Last = std::min<int>(Buffer.find('\n', LocInfo.second), Buffer.size());
+  OS << "|" << Buffer.substr(First, Last - First) << "|\n" << IndentStr
----------------
Not super-happy about relying on the value of StringRef::npos here, but okay.

================
Comment at: lib/Basic/SourceLocation.cpp:73
@@ +72,3 @@
+  OS << "|" << Buffer.substr(First, Last - First) << "|\n" << IndentStr
+     << std::string(1 + LocInfo.second - First, ' ') << "^ ";
+}
----------------
This is all nice and inline, but it does make an extra temporary std::string. raw_ostream::indent instead?


http://llvm-reviews.chandlerc.com/D768



More information about the cfe-commits mailing list