[PATCH] D39650: [clang-diff] Make getSourceRangeOffsets a member of Node

Johannes Altmanninger via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 5 11:00:19 PST 2017


johannes created this revision.
Herald added a subscriber: klimek.

https://reviews.llvm.org/D39650

Files:
  include/clang/Tooling/ASTDiff/ASTDiff.h
  lib/Tooling/ASTDiff/ASTDiff.cpp
  tools/clang-diff/ClangDiff.cpp


Index: tools/clang-diff/ClangDiff.cpp
===================================================================
--- tools/clang-diff/ClangDiff.cpp
+++ tools/clang-diff/ClangDiff.cpp
@@ -284,7 +284,7 @@
     RightId = Node.getId();
   }
   unsigned Begin, End;
-  std::tie(Begin, End) = Tree.getSourceRangeOffsets(Node);
+  std::tie(Begin, End) = Node.getSourceRangeOffsets();
   const SourceManager &SM = Tree.getASTContext().getSourceManager();
   auto Code = SM.getBuffer(SM.getMainFileID())->getBuffer();
   for (; Offset < Begin; ++Offset)
@@ -347,7 +347,7 @@
                                 diff::NodeRef Node) {
   OS << R"("id":)" << int(Node.getId());
   OS << R"(,"type":")" << Node.getTypeLabel() << '"';
-  auto Offsets = Tree.getSourceRangeOffsets(Node);
+  auto Offsets = Node.getSourceRangeOffsets();
   OS << R"(,"begin":)" << Offsets.first;
   OS << R"(,"end":)" << Offsets.second;
   std::string Value = Tree.getNodeValue(Node);
Index: lib/Tooling/ASTDiff/ASTDiff.cpp
===================================================================
--- lib/Tooling/ASTDiff/ASTDiff.cpp
+++ lib/Tooling/ASTDiff/ASTDiff.cpp
@@ -716,6 +716,21 @@
          Siblings.begin();
 }
 
+std::pair<unsigned, unsigned> Node::getSourceRangeOffsets() const {
+  const SourceManager &SM = Tree.AST.getSourceManager();
+  SourceRange Range = ASTNode.getSourceRange();
+  SourceLocation BeginLoc = Range.getBegin();
+  SourceLocation EndLoc = Lexer::getLocForEndOfToken(
+      Range.getEnd(), /*Offset=*/0, SM, Tree.AST.getLangOpts());
+  if (auto *ThisExpr = ASTNode.get<CXXThisExpr>()) {
+    if (ThisExpr->isImplicit())
+      EndLoc = BeginLoc;
+  }
+  unsigned Begin = SM.getFileOffset(SM.getExpansionLoc(BeginLoc));
+  unsigned End = SM.getFileOffset(SM.getExpansionLoc(EndLoc));
+  return {Begin, End};
+}
+
 namespace {
 // Compares nodes by their depth.
 struct HeightLess {
@@ -1026,22 +1041,6 @@
 }
 SyntaxTree::PreorderIterator SyntaxTree::end() const { return TreeImpl->end(); }
 
-std::pair<unsigned, unsigned>
-SyntaxTree::getSourceRangeOffsets(NodeRef N) const {
-  const SourceManager &SM = TreeImpl->AST.getSourceManager();
-  SourceRange Range = N.ASTNode.getSourceRange();
-  SourceLocation BeginLoc = Range.getBegin();
-  SourceLocation EndLoc = Lexer::getLocForEndOfToken(
-      Range.getEnd(), /*Offset=*/0, SM, TreeImpl->AST.getLangOpts());
-  if (auto *ThisExpr = N.ASTNode.get<CXXThisExpr>()) {
-    if (ThisExpr->isImplicit())
-      EndLoc = BeginLoc;
-  }
-  unsigned Begin = SM.getFileOffset(SM.getExpansionLoc(BeginLoc));
-  unsigned End = SM.getFileOffset(SM.getExpansionLoc(EndLoc));
-  return {Begin, End};
-}
-
 std::string SyntaxTree::getNodeValue(NodeRef N) const {
   return TreeImpl->getNodeValue(N);
 }
Index: include/clang/Tooling/ASTDiff/ASTDiff.h
===================================================================
--- include/clang/Tooling/ASTDiff/ASTDiff.h
+++ include/clang/Tooling/ASTDiff/ASTDiff.h
@@ -91,9 +91,6 @@
 
   NodeRef getNode(NodeId Id) const;
 
-  // Returns the starting and ending offset of the node in its source file.
-  std::pair<unsigned, unsigned> getSourceRangeOffsets(NodeRef N) const;
-
   /// Serialize the node attributes to a string representation. This should
   /// uniquely distinguish nodes of the same kind. Note that this function
   /// just
@@ -129,6 +126,9 @@
   NodeRefIterator end() const;
 
   int findPositionInParent() const;
+
+  // Returns the starting and ending offset of the node in its source file.
+  std::pair<unsigned, unsigned> getSourceRangeOffsets() const;
 };
 
 struct NodeRefIterator {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39650.121638.patch
Type: text/x-patch
Size: 3571 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171105/adb9f14b/attachment-0001.bin>


More information about the cfe-commits mailing list