r341140 - Add dump() method for SourceRange

Stephen Kelly via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 30 16:10:52 PDT 2018


Author: steveire
Date: Thu Aug 30 16:10:52 2018
New Revision: 341140

URL: http://llvm.org/viewvc/llvm-project?rev=341140&view=rev
Log:
Add dump() method for SourceRange

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D50662

Modified:
    cfe/trunk/include/clang/Basic/SourceLocation.h
    cfe/trunk/lib/Basic/SourceLocation.cpp
    cfe/trunk/unittests/Basic/SourceManagerTest.cpp

Modified: cfe/trunk/include/clang/Basic/SourceLocation.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/SourceLocation.h?rev=341140&r1=341139&r2=341140&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/SourceLocation.h (original)
+++ cfe/trunk/include/clang/Basic/SourceLocation.h Thu Aug 30 16:10:52 2018
@@ -220,6 +220,10 @@ public:
   bool operator!=(const SourceRange &X) const {
     return B != X.B || E != X.E;
   }
+
+  void print(raw_ostream &OS, const SourceManager &SM) const;
+  std::string printToString(const SourceManager &SM) const;
+  void dump(const SourceManager &SM) const;
 };
 
 /// Represents a character-granular source range.

Modified: cfe/trunk/lib/Basic/SourceLocation.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/SourceLocation.cpp?rev=341140&r1=341139&r2=341140&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/SourceLocation.cpp (original)
+++ cfe/trunk/lib/Basic/SourceLocation.cpp Thu Aug 30 16:10:52 2018
@@ -80,6 +80,60 @@ LLVM_DUMP_METHOD void SourceLocation::du
   llvm::errs() << '\n';
 }
 
+LLVM_DUMP_METHOD void SourceRange::dump(const SourceManager &SM) const {
+  print(llvm::errs(), SM);
+  llvm::errs() << '\n';
+}
+
+static PresumedLoc PrintDifference(raw_ostream &OS, const SourceManager &SM,
+                                   SourceLocation Loc, PresumedLoc Previous) {
+  if (Loc.isFileID()) {
+
+    PresumedLoc PLoc = SM.getPresumedLoc(Loc);
+
+    if (PLoc.isInvalid()) {
+      OS << "<invalid sloc>";
+      return Previous;
+    }
+
+    if (Previous.isInvalid() ||
+        strcmp(PLoc.getFilename(), Previous.getFilename()) != 0) {
+      OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':'
+         << PLoc.getColumn();
+    } else if (Previous.isInvalid() || PLoc.getLine() != Previous.getLine()) {
+      OS << "line" << ':' << PLoc.getLine() << ':' << PLoc.getColumn();
+    } else {
+      OS << "col" << ':' << PLoc.getColumn();
+    }
+    return PLoc;
+  }
+  auto PrintedLoc = PrintDifference(OS, SM, SM.getExpansionLoc(Loc), Previous);
+
+  OS << " <Spelling=";
+  PrintedLoc = PrintDifference(OS, SM, SM.getSpellingLoc(Loc), PrintedLoc);
+  OS << '>';
+  return PrintedLoc;
+}
+
+void SourceRange::print(raw_ostream &OS, const SourceManager &SM) const {
+
+  OS << '<';
+  auto PrintedLoc = PrintDifference(OS, SM, B, {});
+  if (B != E) {
+    OS << ", ";
+    PrintDifference(OS, SM, E, PrintedLoc);
+  }
+  OS << '>';
+}
+
+LLVM_DUMP_METHOD std::string
+SourceRange::printToString(const SourceManager &SM) const {
+  std::string S;
+  llvm::raw_string_ostream OS(S);
+  print(OS, SM);
+  return OS.str();
+}
+
 //===----------------------------------------------------------------------===//
 // FullSourceLoc
 //===----------------------------------------------------------------------===//

Modified: cfe/trunk/unittests/Basic/SourceManagerTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Basic/SourceManagerTest.cpp?rev=341140&r1=341139&r2=341140&view=diff
==============================================================================
--- cfe/trunk/unittests/Basic/SourceManagerTest.cpp (original)
+++ cfe/trunk/unittests/Basic/SourceManagerTest.cpp Thu Aug 30 16:10:52 2018
@@ -155,6 +155,54 @@ TEST_F(SourceManagerTest, getColumnNumbe
   EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr));
 }
 
+TEST_F(SourceManagerTest, locationPrintTest) {
+  const char *header = "#define IDENTITY(x) x\n";
+
+  const char *Source = "int x;\n"
+                       "include \"test-header.h\"\n"
+                       "IDENTITY(int y);\n"
+                       "int z;";
+
+  std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
+      llvm::MemoryBuffer::getMemBuffer(header);
+  std::unique_ptr<llvm::MemoryBuffer> Buf =
+      llvm::MemoryBuffer::getMemBuffer(Source);
+
+  const FileEntry *SourceFile =
+      FileMgr.getVirtualFile("/mainFile.cpp", Buf->getBufferSize(), 0);
+  SourceMgr.overrideFileContents(SourceFile, std::move(Buf));
+
+  const FileEntry *HeaderFile =
+      FileMgr.getVirtualFile("/test-header.h", HeaderBuf->getBufferSize(), 0);
+  SourceMgr.overrideFileContents(HeaderFile, std::move(HeaderBuf));
+
+  FileID MainFileID = SourceMgr.getOrCreateFileID(SourceFile, SrcMgr::C_User);
+  FileID HeaderFileID = SourceMgr.getOrCreateFileID(HeaderFile, SrcMgr::C_User);
+  SourceMgr.setMainFileID(MainFileID);
+
+  auto BeginLoc = SourceMgr.getLocForStartOfFile(MainFileID);
+  auto EndLoc = SourceMgr.getLocForEndOfFile(MainFileID);
+
+  auto BeginEOLLoc = SourceMgr.translateLineCol(MainFileID, 1, 7);
+
+  auto HeaderLoc = SourceMgr.getLocForStartOfFile(HeaderFileID);
+
+  EXPECT_EQ(BeginLoc.printToString(SourceMgr), "/mainFile.cpp:1:1");
+  EXPECT_EQ(EndLoc.printToString(SourceMgr), "/mainFile.cpp:4:7");
+
+  EXPECT_EQ(BeginEOLLoc.printToString(SourceMgr), "/mainFile.cpp:1:7");
+  EXPECT_EQ(HeaderLoc.printToString(SourceMgr), "/test-header.h:1:1");
+
+  EXPECT_EQ(SourceRange(BeginLoc, BeginLoc).printToString(SourceMgr),
+            "</mainFile.cpp:1:1>");
+  EXPECT_EQ(SourceRange(BeginLoc, BeginEOLLoc).printToString(SourceMgr),
+            "</mainFile.cpp:1:1, col:7>");
+  EXPECT_EQ(SourceRange(BeginLoc, EndLoc).printToString(SourceMgr),
+            "</mainFile.cpp:1:1, line:4:7>");
+  EXPECT_EQ(SourceRange(BeginLoc, HeaderLoc).printToString(SourceMgr),
+            "</mainFile.cpp:1:1, /test-header.h:1:1>");
+}
+
 #if defined(LLVM_ON_UNIX)
 
 TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {




More information about the cfe-commits mailing list