[PATCH] Add debug method to visualize complex source locations

Manuel Klimek klimek at google.com
Fri May 17 02:48:38 PDT 2013


  Try a slightly different layout.

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

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D768?vs=1888&id=2000#toc

Files:
  include/clang/Basic/SourceLocation.h
  lib/Basic/SourceLocation.cpp

Index: include/clang/Basic/SourceLocation.h
===================================================================
--- include/clang/Basic/SourceLocation.h
+++ include/clang/Basic/SourceLocation.h
@@ -16,6 +16,7 @@
 #define LLVM_CLANG_SOURCELOCATION_H
 
 #include "clang/Basic/LLVM.h"
+#include "llvm/ADT/Twine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/PointerLikeTypeTraits.h"
 #include <cassert>
@@ -174,6 +175,8 @@
   void print(raw_ostream &OS, const SourceManager &SM) const;
   LLVM_ATTRIBUTE_USED std::string printToString(const SourceManager &SM) const;
   void dump(const SourceManager &SM) const;
+  LLVM_ATTRIBUTE_USED std::string printTreeToString(const SourceManager &SM,
+                                                    Twine IndentStr = "") const;
 };
 
 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
Index: lib/Basic/SourceLocation.cpp
===================================================================
--- lib/Basic/SourceLocation.cpp
+++ lib/Basic/SourceLocation.cpp
@@ -61,6 +61,38 @@
   OS << '>';
 }
 
+std::string SourceLocation::printTreeToString(const SourceManager &SM,
+                                              Twine IndentStr) const {
+  std::string S;
+  llvm::raw_string_ostream OS(S);
+  if (!isValid() || !isMacroID()) {
+    OS << printToString(SM);
+  } else {
+    SourceLocation SpellingLoc = SM.getImmediateSpellingLoc(*this);
+    std::pair<SourceLocation, SourceLocation> ExpansionLoc =
+        SM.getImmediateExpansionRange(*this);
+    if (IndentStr.str().empty())
+      OS << "Loc";
+    OS << "\n";
+    if (ExpansionLoc.first != ExpansionLoc.second) {
+      OS << IndentStr << "|-"
+         << "ExpansionStart "
+         << ExpansionLoc.first.printTreeToString(SM, IndentStr + "| ") << ",\n"
+         << IndentStr << "|-"
+         << "ExpansionEnd   " << ExpansionLoc.second.printTreeToString(
+                                     SM, IndentStr + "| ") << ",\n";
+    } else {
+      OS << IndentStr << "|-"
+         << "Expansion      "
+         << ExpansionLoc.first.printTreeToString(SM, IndentStr + "| ") << ",\n";
+    }
+    OS << IndentStr << "`-"
+       << "Spelling       "
+       << SpellingLoc.printTreeToString(SM, IndentStr + "  ");
+  }
+  return OS.str();
+}
+
 std::string SourceLocation::printToString(const SourceManager &SM) const {
   std::string S;
   llvm::raw_string_ostream OS(S);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D768.2.patch
Type: text/x-patch
Size: 2421 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130517/08ff1a28/attachment.bin>


More information about the cfe-commits mailing list