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 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 getSourceRangeOffsets() const; }; struct NodeRefIterator { Index: lib/Tooling/ASTDiff/ASTDiff.cpp =================================================================== --- lib/Tooling/ASTDiff/ASTDiff.cpp +++ lib/Tooling/ASTDiff/ASTDiff.cpp @@ -716,6 +716,21 @@ Siblings.begin(); } +std::pair 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()) { + 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 -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()) { - 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: 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);