[PATCH] Add debug method to visualize complex source locations
Manuel Klimek
klimek at google.com
Wed May 22 05:26:06 PDT 2013
Insert code printing. Can be switched off via parameter for intricate locations.
http://llvm-reviews.chandlerc.com/D768
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D768?vs=2000&id=2063#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,9 @@
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,
+ bool PrintCode = true,
+ 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,53 @@
OS << '>';
}
+static void printSourceLocationContext(const SourceManager &SM,
+ SourceLocation Loc,
+ llvm::raw_ostream &OS,
+ Twine IndentStr) {
+ std::pair<FileID, unsigned> LocInfo = SM.getDecomposedSpellingLoc(Loc);
+ 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
+ << std::string(1 + LocInfo.second - First, ' ') << "^ ";
+}
+
+std::string SourceLocation::printTreeToString(const SourceManager &SM,
+ bool PrintCode,
+ Twine IndentStr) const {
+ std::string S;
+ llvm::raw_string_ostream OS(S);
+ if (!isValid() || !isMacroID()) {
+ if (PrintCode)
+ printSourceLocationContext(SM, *this, OS, IndentStr + " ");
+ 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, PrintCode, IndentStr + "| ") << ",\n"
+ << IndentStr << "|-"
+ << "ExpansionEnd " << ExpansionLoc.second.printTreeToString(
+ SM, PrintCode, IndentStr + "| ") << ",\n";
+ } else {
+ OS << IndentStr << "|-"
+ << "Expansion " << ExpansionLoc.first.printTreeToString(
+ SM, PrintCode, IndentStr + "| ") << ",\n";
+ }
+ OS << IndentStr << "`-"
+ << "Spelling "
+ << SpellingLoc.printTreeToString(SM, PrintCode, 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.3.patch
Type: text/x-patch
Size: 3433 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130522/515b5dde/attachment.bin>
More information about the cfe-commits
mailing list