10b57cec5SDimitry Andric //===- ASTDiff.cpp - AST differencing implementation-----------*- C++ -*- -===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains definitons for the AST differencing interface.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "clang/Tooling/ASTDiff/ASTDiff.h"
145ffd83dbSDimitry Andric #include "clang/AST/ParentMapContext.h"
150b57cec5SDimitry Andric #include "clang/AST/RecursiveASTVisitor.h"
165ffd83dbSDimitry Andric #include "clang/Basic/SourceManager.h"
170b57cec5SDimitry Andric #include "clang/Lex/Lexer.h"
180b57cec5SDimitry Andric #include "llvm/ADT/PriorityQueue.h"
190b57cec5SDimitry Andric
200b57cec5SDimitry Andric #include <limits>
210b57cec5SDimitry Andric #include <memory>
22bdd1243dSDimitry Andric #include <optional>
230b57cec5SDimitry Andric #include <unordered_set>
240b57cec5SDimitry Andric
250b57cec5SDimitry Andric using namespace llvm;
260b57cec5SDimitry Andric using namespace clang;
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric namespace clang {
290b57cec5SDimitry Andric namespace diff {
300b57cec5SDimitry Andric
310b57cec5SDimitry Andric namespace {
320b57cec5SDimitry Andric /// Maps nodes of the left tree to ones on the right, and vice versa.
330b57cec5SDimitry Andric class Mapping {
340b57cec5SDimitry Andric public:
350b57cec5SDimitry Andric Mapping() = default;
360b57cec5SDimitry Andric Mapping(Mapping &&Other) = default;
370b57cec5SDimitry Andric Mapping &operator=(Mapping &&Other) = default;
380b57cec5SDimitry Andric
Mapping(size_t Size)390b57cec5SDimitry Andric Mapping(size_t Size) {
40a7dea167SDimitry Andric SrcToDst = std::make_unique<NodeId[]>(Size);
41a7dea167SDimitry Andric DstToSrc = std::make_unique<NodeId[]>(Size);
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric
link(NodeId Src,NodeId Dst)440b57cec5SDimitry Andric void link(NodeId Src, NodeId Dst) {
450b57cec5SDimitry Andric SrcToDst[Src] = Dst, DstToSrc[Dst] = Src;
460b57cec5SDimitry Andric }
470b57cec5SDimitry Andric
getDst(NodeId Src) const480b57cec5SDimitry Andric NodeId getDst(NodeId Src) const { return SrcToDst[Src]; }
getSrc(NodeId Dst) const490b57cec5SDimitry Andric NodeId getSrc(NodeId Dst) const { return DstToSrc[Dst]; }
hasSrc(NodeId Src) const500b57cec5SDimitry Andric bool hasSrc(NodeId Src) const { return getDst(Src).isValid(); }
hasDst(NodeId Dst) const510b57cec5SDimitry Andric bool hasDst(NodeId Dst) const { return getSrc(Dst).isValid(); }
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric private:
540b57cec5SDimitry Andric std::unique_ptr<NodeId[]> SrcToDst, DstToSrc;
550b57cec5SDimitry Andric };
560b57cec5SDimitry Andric } // end anonymous namespace
570b57cec5SDimitry Andric
580b57cec5SDimitry Andric class ASTDiff::Impl {
590b57cec5SDimitry Andric public:
600b57cec5SDimitry Andric SyntaxTree::Impl &T1, &T2;
610b57cec5SDimitry Andric Mapping TheMapping;
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric Impl(SyntaxTree::Impl &T1, SyntaxTree::Impl &T2,
640b57cec5SDimitry Andric const ComparisonOptions &Options);
650b57cec5SDimitry Andric
660b57cec5SDimitry Andric /// Matches nodes one-by-one based on their similarity.
670b57cec5SDimitry Andric void computeMapping();
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric // Compute Change for each node based on similarity.
700b57cec5SDimitry Andric void computeChangeKinds(Mapping &M);
710b57cec5SDimitry Andric
getMapped(const std::unique_ptr<SyntaxTree::Impl> & Tree,NodeId Id) const720b57cec5SDimitry Andric NodeId getMapped(const std::unique_ptr<SyntaxTree::Impl> &Tree,
730b57cec5SDimitry Andric NodeId Id) const {
740b57cec5SDimitry Andric if (&*Tree == &T1)
750b57cec5SDimitry Andric return TheMapping.getDst(Id);
760b57cec5SDimitry Andric assert(&*Tree == &T2 && "Invalid tree.");
770b57cec5SDimitry Andric return TheMapping.getSrc(Id);
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric
800b57cec5SDimitry Andric private:
810b57cec5SDimitry Andric // Returns true if the two subtrees are identical.
820b57cec5SDimitry Andric bool identical(NodeId Id1, NodeId Id2) const;
830b57cec5SDimitry Andric
840b57cec5SDimitry Andric // Returns false if the nodes must not be mached.
850b57cec5SDimitry Andric bool isMatchingPossible(NodeId Id1, NodeId Id2) const;
860b57cec5SDimitry Andric
870b57cec5SDimitry Andric // Returns true if the nodes' parents are matched.
880b57cec5SDimitry Andric bool haveSameParents(const Mapping &M, NodeId Id1, NodeId Id2) const;
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric // Uses an optimal albeit slow algorithm to compute a mapping between two
910b57cec5SDimitry Andric // subtrees, but only if both have fewer nodes than MaxSize.
920b57cec5SDimitry Andric void addOptimalMapping(Mapping &M, NodeId Id1, NodeId Id2) const;
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric // Computes the ratio of common descendants between the two nodes.
950b57cec5SDimitry Andric // Descendants are only considered to be equal when they are mapped in M.
960b57cec5SDimitry Andric double getJaccardSimilarity(const Mapping &M, NodeId Id1, NodeId Id2) const;
970b57cec5SDimitry Andric
980b57cec5SDimitry Andric // Returns the node that has the highest degree of similarity.
990b57cec5SDimitry Andric NodeId findCandidate(const Mapping &M, NodeId Id1) const;
1000b57cec5SDimitry Andric
1010b57cec5SDimitry Andric // Returns a mapping of identical subtrees.
1020b57cec5SDimitry Andric Mapping matchTopDown() const;
1030b57cec5SDimitry Andric
1040b57cec5SDimitry Andric // Tries to match any yet unmapped nodes, in a bottom-up fashion.
1050b57cec5SDimitry Andric void matchBottomUp(Mapping &M) const;
1060b57cec5SDimitry Andric
1070b57cec5SDimitry Andric const ComparisonOptions &Options;
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric friend class ZhangShashaMatcher;
1100b57cec5SDimitry Andric };
1110b57cec5SDimitry Andric
1120b57cec5SDimitry Andric /// Represents the AST of a TranslationUnit.
1130b57cec5SDimitry Andric class SyntaxTree::Impl {
1140b57cec5SDimitry Andric public:
1150b57cec5SDimitry Andric Impl(SyntaxTree *Parent, ASTContext &AST);
1160b57cec5SDimitry Andric /// Constructs a tree from an AST node.
1170b57cec5SDimitry Andric Impl(SyntaxTree *Parent, Decl *N, ASTContext &AST);
1180b57cec5SDimitry Andric Impl(SyntaxTree *Parent, Stmt *N, ASTContext &AST);
1190b57cec5SDimitry Andric template <class T>
Impl(SyntaxTree * Parent,std::enable_if_t<std::is_base_of_v<Stmt,T>,T> * Node,ASTContext & AST)1200b57cec5SDimitry Andric Impl(SyntaxTree *Parent,
121bdd1243dSDimitry Andric std::enable_if_t<std::is_base_of_v<Stmt, T>, T> *Node, ASTContext &AST)
1220b57cec5SDimitry Andric : Impl(Parent, dyn_cast<Stmt>(Node), AST) {}
1230b57cec5SDimitry Andric template <class T>
Impl(SyntaxTree * Parent,std::enable_if_t<std::is_base_of_v<Decl,T>,T> * Node,ASTContext & AST)1240b57cec5SDimitry Andric Impl(SyntaxTree *Parent,
125bdd1243dSDimitry Andric std::enable_if_t<std::is_base_of_v<Decl, T>, T> *Node, ASTContext &AST)
1260b57cec5SDimitry Andric : Impl(Parent, dyn_cast<Decl>(Node), AST) {}
1270b57cec5SDimitry Andric
1280b57cec5SDimitry Andric SyntaxTree *Parent;
1290b57cec5SDimitry Andric ASTContext &AST;
1300b57cec5SDimitry Andric PrintingPolicy TypePP;
1310b57cec5SDimitry Andric /// Nodes in preorder.
1320b57cec5SDimitry Andric std::vector<Node> Nodes;
1330b57cec5SDimitry Andric std::vector<NodeId> Leaves;
1340b57cec5SDimitry Andric // Maps preorder indices to postorder ones.
1350b57cec5SDimitry Andric std::vector<int> PostorderIds;
1360b57cec5SDimitry Andric std::vector<NodeId> NodesBfs;
1370b57cec5SDimitry Andric
getSize() const1380b57cec5SDimitry Andric int getSize() const { return Nodes.size(); }
getRootId() const1390b57cec5SDimitry Andric NodeId getRootId() const { return 0; }
begin() const1400b57cec5SDimitry Andric PreorderIterator begin() const { return getRootId(); }
end() const1410b57cec5SDimitry Andric PreorderIterator end() const { return getSize(); }
1420b57cec5SDimitry Andric
getNode(NodeId Id) const1430b57cec5SDimitry Andric const Node &getNode(NodeId Id) const { return Nodes[Id]; }
getMutableNode(NodeId Id)1440b57cec5SDimitry Andric Node &getMutableNode(NodeId Id) { return Nodes[Id]; }
isValidNodeId(NodeId Id) const1450b57cec5SDimitry Andric bool isValidNodeId(NodeId Id) const { return Id >= 0 && Id < getSize(); }
addNode(Node & N)1460b57cec5SDimitry Andric void addNode(Node &N) { Nodes.push_back(N); }
1470b57cec5SDimitry Andric int getNumberOfDescendants(NodeId Id) const;
1480b57cec5SDimitry Andric bool isInSubtree(NodeId Id, NodeId SubtreeRoot) const;
1490b57cec5SDimitry Andric int findPositionInParent(NodeId Id, bool Shifted = false) const;
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric std::string getRelativeName(const NamedDecl *ND,
1520b57cec5SDimitry Andric const DeclContext *Context) const;
1530b57cec5SDimitry Andric std::string getRelativeName(const NamedDecl *ND) const;
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric std::string getNodeValue(NodeId Id) const;
1560b57cec5SDimitry Andric std::string getNodeValue(const Node &Node) const;
1570b57cec5SDimitry Andric std::string getDeclValue(const Decl *D) const;
1580b57cec5SDimitry Andric std::string getStmtValue(const Stmt *S) const;
1590b57cec5SDimitry Andric
1600b57cec5SDimitry Andric private:
1610b57cec5SDimitry Andric void initTree();
1620b57cec5SDimitry Andric void setLeftMostDescendants();
1630b57cec5SDimitry Andric };
1640b57cec5SDimitry Andric
isSpecializedNodeExcluded(const Decl * D)1650b57cec5SDimitry Andric static bool isSpecializedNodeExcluded(const Decl *D) { return D->isImplicit(); }
isSpecializedNodeExcluded(const Stmt * S)1660b57cec5SDimitry Andric static bool isSpecializedNodeExcluded(const Stmt *S) { return false; }
isSpecializedNodeExcluded(CXXCtorInitializer * I)1670b57cec5SDimitry Andric static bool isSpecializedNodeExcluded(CXXCtorInitializer *I) {
1680b57cec5SDimitry Andric return !I->isWritten();
1690b57cec5SDimitry Andric }
1700b57cec5SDimitry Andric
1710b57cec5SDimitry Andric template <class T>
isNodeExcluded(const SourceManager & SrcMgr,T * N)1720b57cec5SDimitry Andric static bool isNodeExcluded(const SourceManager &SrcMgr, T *N) {
1730b57cec5SDimitry Andric if (!N)
1740b57cec5SDimitry Andric return true;
1750b57cec5SDimitry Andric SourceLocation SLoc = N->getSourceRange().getBegin();
1760b57cec5SDimitry Andric if (SLoc.isValid()) {
1770b57cec5SDimitry Andric // Ignore everything from other files.
1780b57cec5SDimitry Andric if (!SrcMgr.isInMainFile(SLoc))
1790b57cec5SDimitry Andric return true;
1800b57cec5SDimitry Andric // Ignore macros.
1810b57cec5SDimitry Andric if (SLoc != SrcMgr.getSpellingLoc(SLoc))
1820b57cec5SDimitry Andric return true;
1830b57cec5SDimitry Andric }
1840b57cec5SDimitry Andric return isSpecializedNodeExcluded(N);
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric
1870b57cec5SDimitry Andric namespace {
1880b57cec5SDimitry Andric // Sets Height, Parent and Children for each node.
1890b57cec5SDimitry Andric struct PreorderVisitor : public RecursiveASTVisitor<PreorderVisitor> {
1900b57cec5SDimitry Andric int Id = 0, Depth = 0;
1910b57cec5SDimitry Andric NodeId Parent;
1920b57cec5SDimitry Andric SyntaxTree::Impl &Tree;
1930b57cec5SDimitry Andric
PreorderVisitorclang::diff::__anon36ce97a80211::PreorderVisitor1940b57cec5SDimitry Andric PreorderVisitor(SyntaxTree::Impl &Tree) : Tree(Tree) {}
1950b57cec5SDimitry Andric
PreTraverseclang::diff::__anon36ce97a80211::PreorderVisitor1960b57cec5SDimitry Andric template <class T> std::tuple<NodeId, NodeId> PreTraverse(T *ASTNode) {
1970b57cec5SDimitry Andric NodeId MyId = Id;
1980b57cec5SDimitry Andric Tree.Nodes.emplace_back();
1990b57cec5SDimitry Andric Node &N = Tree.getMutableNode(MyId);
2000b57cec5SDimitry Andric N.Parent = Parent;
2010b57cec5SDimitry Andric N.Depth = Depth;
2020b57cec5SDimitry Andric N.ASTNode = DynTypedNode::create(*ASTNode);
2030b57cec5SDimitry Andric assert(!N.ASTNode.getNodeKind().isNone() &&
2040b57cec5SDimitry Andric "Expected nodes to have a valid kind.");
2050b57cec5SDimitry Andric if (Parent.isValid()) {
2060b57cec5SDimitry Andric Node &P = Tree.getMutableNode(Parent);
2070b57cec5SDimitry Andric P.Children.push_back(MyId);
2080b57cec5SDimitry Andric }
2090b57cec5SDimitry Andric Parent = MyId;
2100b57cec5SDimitry Andric ++Id;
2110b57cec5SDimitry Andric ++Depth;
2120b57cec5SDimitry Andric return std::make_tuple(MyId, Tree.getNode(MyId).Parent);
2130b57cec5SDimitry Andric }
PostTraverseclang::diff::__anon36ce97a80211::PreorderVisitor2140b57cec5SDimitry Andric void PostTraverse(std::tuple<NodeId, NodeId> State) {
2150b57cec5SDimitry Andric NodeId MyId, PreviousParent;
2160b57cec5SDimitry Andric std::tie(MyId, PreviousParent) = State;
2170b57cec5SDimitry Andric assert(MyId.isValid() && "Expecting to only traverse valid nodes.");
2180b57cec5SDimitry Andric Parent = PreviousParent;
2190b57cec5SDimitry Andric --Depth;
2200b57cec5SDimitry Andric Node &N = Tree.getMutableNode(MyId);
2210b57cec5SDimitry Andric N.RightMostDescendant = Id - 1;
2220b57cec5SDimitry Andric assert(N.RightMostDescendant >= 0 &&
2230b57cec5SDimitry Andric N.RightMostDescendant < Tree.getSize() &&
2240b57cec5SDimitry Andric "Rightmost descendant must be a valid tree node.");
2250b57cec5SDimitry Andric if (N.isLeaf())
2260b57cec5SDimitry Andric Tree.Leaves.push_back(MyId);
2270b57cec5SDimitry Andric N.Height = 1;
2280b57cec5SDimitry Andric for (NodeId Child : N.Children)
2290b57cec5SDimitry Andric N.Height = std::max(N.Height, 1 + Tree.getNode(Child).Height);
2300b57cec5SDimitry Andric }
TraverseDeclclang::diff::__anon36ce97a80211::PreorderVisitor2310b57cec5SDimitry Andric bool TraverseDecl(Decl *D) {
2320b57cec5SDimitry Andric if (isNodeExcluded(Tree.AST.getSourceManager(), D))
2330b57cec5SDimitry Andric return true;
2340b57cec5SDimitry Andric auto SavedState = PreTraverse(D);
2350b57cec5SDimitry Andric RecursiveASTVisitor<PreorderVisitor>::TraverseDecl(D);
2360b57cec5SDimitry Andric PostTraverse(SavedState);
2370b57cec5SDimitry Andric return true;
2380b57cec5SDimitry Andric }
TraverseStmtclang::diff::__anon36ce97a80211::PreorderVisitor2390b57cec5SDimitry Andric bool TraverseStmt(Stmt *S) {
2400b57cec5SDimitry Andric if (auto *E = dyn_cast_or_null<Expr>(S))
2410b57cec5SDimitry Andric S = E->IgnoreImplicit();
2420b57cec5SDimitry Andric if (isNodeExcluded(Tree.AST.getSourceManager(), S))
2430b57cec5SDimitry Andric return true;
2440b57cec5SDimitry Andric auto SavedState = PreTraverse(S);
2450b57cec5SDimitry Andric RecursiveASTVisitor<PreorderVisitor>::TraverseStmt(S);
2460b57cec5SDimitry Andric PostTraverse(SavedState);
2470b57cec5SDimitry Andric return true;
2480b57cec5SDimitry Andric }
TraverseTypeclang::diff::__anon36ce97a80211::PreorderVisitor2490b57cec5SDimitry Andric bool TraverseType(QualType T) { return true; }
TraverseConstructorInitializerclang::diff::__anon36ce97a80211::PreorderVisitor2500b57cec5SDimitry Andric bool TraverseConstructorInitializer(CXXCtorInitializer *Init) {
2510b57cec5SDimitry Andric if (isNodeExcluded(Tree.AST.getSourceManager(), Init))
2520b57cec5SDimitry Andric return true;
2530b57cec5SDimitry Andric auto SavedState = PreTraverse(Init);
2540b57cec5SDimitry Andric RecursiveASTVisitor<PreorderVisitor>::TraverseConstructorInitializer(Init);
2550b57cec5SDimitry Andric PostTraverse(SavedState);
2560b57cec5SDimitry Andric return true;
2570b57cec5SDimitry Andric }
2580b57cec5SDimitry Andric };
2590b57cec5SDimitry Andric } // end anonymous namespace
2600b57cec5SDimitry Andric
Impl(SyntaxTree * Parent,ASTContext & AST)2610b57cec5SDimitry Andric SyntaxTree::Impl::Impl(SyntaxTree *Parent, ASTContext &AST)
2620b57cec5SDimitry Andric : Parent(Parent), AST(AST), TypePP(AST.getLangOpts()) {
2630b57cec5SDimitry Andric TypePP.AnonymousTagLocations = false;
2640b57cec5SDimitry Andric }
2650b57cec5SDimitry Andric
Impl(SyntaxTree * Parent,Decl * N,ASTContext & AST)2660b57cec5SDimitry Andric SyntaxTree::Impl::Impl(SyntaxTree *Parent, Decl *N, ASTContext &AST)
2670b57cec5SDimitry Andric : Impl(Parent, AST) {
2680b57cec5SDimitry Andric PreorderVisitor PreorderWalker(*this);
2690b57cec5SDimitry Andric PreorderWalker.TraverseDecl(N);
2700b57cec5SDimitry Andric initTree();
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric
Impl(SyntaxTree * Parent,Stmt * N,ASTContext & AST)2730b57cec5SDimitry Andric SyntaxTree::Impl::Impl(SyntaxTree *Parent, Stmt *N, ASTContext &AST)
2740b57cec5SDimitry Andric : Impl(Parent, AST) {
2750b57cec5SDimitry Andric PreorderVisitor PreorderWalker(*this);
2760b57cec5SDimitry Andric PreorderWalker.TraverseStmt(N);
2770b57cec5SDimitry Andric initTree();
2780b57cec5SDimitry Andric }
2790b57cec5SDimitry Andric
getSubtreePostorder(const SyntaxTree::Impl & Tree,NodeId Root)2800b57cec5SDimitry Andric static std::vector<NodeId> getSubtreePostorder(const SyntaxTree::Impl &Tree,
2810b57cec5SDimitry Andric NodeId Root) {
2820b57cec5SDimitry Andric std::vector<NodeId> Postorder;
2830b57cec5SDimitry Andric std::function<void(NodeId)> Traverse = [&](NodeId Id) {
2840b57cec5SDimitry Andric const Node &N = Tree.getNode(Id);
2850b57cec5SDimitry Andric for (NodeId Child : N.Children)
2860b57cec5SDimitry Andric Traverse(Child);
2870b57cec5SDimitry Andric Postorder.push_back(Id);
2880b57cec5SDimitry Andric };
2890b57cec5SDimitry Andric Traverse(Root);
2900b57cec5SDimitry Andric return Postorder;
2910b57cec5SDimitry Andric }
2920b57cec5SDimitry Andric
getSubtreeBfs(const SyntaxTree::Impl & Tree,NodeId Root)2930b57cec5SDimitry Andric static std::vector<NodeId> getSubtreeBfs(const SyntaxTree::Impl &Tree,
2940b57cec5SDimitry Andric NodeId Root) {
2950b57cec5SDimitry Andric std::vector<NodeId> Ids;
2960b57cec5SDimitry Andric size_t Expanded = 0;
2970b57cec5SDimitry Andric Ids.push_back(Root);
2980b57cec5SDimitry Andric while (Expanded < Ids.size())
2990b57cec5SDimitry Andric for (NodeId Child : Tree.getNode(Ids[Expanded++]).Children)
3000b57cec5SDimitry Andric Ids.push_back(Child);
3010b57cec5SDimitry Andric return Ids;
3020b57cec5SDimitry Andric }
3030b57cec5SDimitry Andric
initTree()3040b57cec5SDimitry Andric void SyntaxTree::Impl::initTree() {
3050b57cec5SDimitry Andric setLeftMostDescendants();
3060b57cec5SDimitry Andric int PostorderId = 0;
3070b57cec5SDimitry Andric PostorderIds.resize(getSize());
3080b57cec5SDimitry Andric std::function<void(NodeId)> PostorderTraverse = [&](NodeId Id) {
3090b57cec5SDimitry Andric for (NodeId Child : getNode(Id).Children)
3100b57cec5SDimitry Andric PostorderTraverse(Child);
3110b57cec5SDimitry Andric PostorderIds[Id] = PostorderId;
3120b57cec5SDimitry Andric ++PostorderId;
3130b57cec5SDimitry Andric };
3140b57cec5SDimitry Andric PostorderTraverse(getRootId());
3150b57cec5SDimitry Andric NodesBfs = getSubtreeBfs(*this, getRootId());
3160b57cec5SDimitry Andric }
3170b57cec5SDimitry Andric
setLeftMostDescendants()3180b57cec5SDimitry Andric void SyntaxTree::Impl::setLeftMostDescendants() {
3190b57cec5SDimitry Andric for (NodeId Leaf : Leaves) {
3200b57cec5SDimitry Andric getMutableNode(Leaf).LeftMostDescendant = Leaf;
3210b57cec5SDimitry Andric NodeId Parent, Cur = Leaf;
3220b57cec5SDimitry Andric while ((Parent = getNode(Cur).Parent).isValid() &&
3230b57cec5SDimitry Andric getNode(Parent).Children[0] == Cur) {
3240b57cec5SDimitry Andric Cur = Parent;
3250b57cec5SDimitry Andric getMutableNode(Cur).LeftMostDescendant = Leaf;
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric
getNumberOfDescendants(NodeId Id) const3300b57cec5SDimitry Andric int SyntaxTree::Impl::getNumberOfDescendants(NodeId Id) const {
3310b57cec5SDimitry Andric return getNode(Id).RightMostDescendant - Id + 1;
3320b57cec5SDimitry Andric }
3330b57cec5SDimitry Andric
isInSubtree(NodeId Id,NodeId SubtreeRoot) const3340b57cec5SDimitry Andric bool SyntaxTree::Impl::isInSubtree(NodeId Id, NodeId SubtreeRoot) const {
3350b57cec5SDimitry Andric return Id >= SubtreeRoot && Id <= getNode(SubtreeRoot).RightMostDescendant;
3360b57cec5SDimitry Andric }
3370b57cec5SDimitry Andric
findPositionInParent(NodeId Id,bool Shifted) const3380b57cec5SDimitry Andric int SyntaxTree::Impl::findPositionInParent(NodeId Id, bool Shifted) const {
3390b57cec5SDimitry Andric NodeId Parent = getNode(Id).Parent;
3400b57cec5SDimitry Andric if (Parent.isInvalid())
3410b57cec5SDimitry Andric return 0;
3420b57cec5SDimitry Andric const auto &Siblings = getNode(Parent).Children;
3430b57cec5SDimitry Andric int Position = 0;
3440b57cec5SDimitry Andric for (size_t I = 0, E = Siblings.size(); I < E; ++I) {
3450b57cec5SDimitry Andric if (Shifted)
3460b57cec5SDimitry Andric Position += getNode(Siblings[I]).Shift;
3470b57cec5SDimitry Andric if (Siblings[I] == Id) {
3480b57cec5SDimitry Andric Position += I;
3490b57cec5SDimitry Andric return Position;
3500b57cec5SDimitry Andric }
3510b57cec5SDimitry Andric }
3520b57cec5SDimitry Andric llvm_unreachable("Node not found in parent's children.");
3530b57cec5SDimitry Andric }
3540b57cec5SDimitry Andric
3550b57cec5SDimitry Andric // Returns the qualified name of ND. If it is subordinate to Context,
3560b57cec5SDimitry Andric // then the prefix of the latter is removed from the returned value.
3570b57cec5SDimitry Andric std::string
getRelativeName(const NamedDecl * ND,const DeclContext * Context) const3580b57cec5SDimitry Andric SyntaxTree::Impl::getRelativeName(const NamedDecl *ND,
3590b57cec5SDimitry Andric const DeclContext *Context) const {
3600b57cec5SDimitry Andric std::string Val = ND->getQualifiedNameAsString();
3610b57cec5SDimitry Andric std::string ContextPrefix;
3620b57cec5SDimitry Andric if (!Context)
3630b57cec5SDimitry Andric return Val;
3640b57cec5SDimitry Andric if (auto *Namespace = dyn_cast<NamespaceDecl>(Context))
3650b57cec5SDimitry Andric ContextPrefix = Namespace->getQualifiedNameAsString();
3660b57cec5SDimitry Andric else if (auto *Record = dyn_cast<RecordDecl>(Context))
3670b57cec5SDimitry Andric ContextPrefix = Record->getQualifiedNameAsString();
3680b57cec5SDimitry Andric else if (AST.getLangOpts().CPlusPlus11)
3690b57cec5SDimitry Andric if (auto *Tag = dyn_cast<TagDecl>(Context))
3700b57cec5SDimitry Andric ContextPrefix = Tag->getQualifiedNameAsString();
3710b57cec5SDimitry Andric // Strip the qualifier, if Val refers to something in the current scope.
3720b57cec5SDimitry Andric // But leave one leading ':' in place, so that we know that this is a
3730b57cec5SDimitry Andric // relative path.
3745f757f3fSDimitry Andric if (!ContextPrefix.empty() && StringRef(Val).starts_with(ContextPrefix))
3750b57cec5SDimitry Andric Val = Val.substr(ContextPrefix.size() + 1);
3760b57cec5SDimitry Andric return Val;
3770b57cec5SDimitry Andric }
3780b57cec5SDimitry Andric
getRelativeName(const NamedDecl * ND) const3790b57cec5SDimitry Andric std::string SyntaxTree::Impl::getRelativeName(const NamedDecl *ND) const {
3800b57cec5SDimitry Andric return getRelativeName(ND, ND->getDeclContext());
3810b57cec5SDimitry Andric }
3820b57cec5SDimitry Andric
getEnclosingDeclContext(ASTContext & AST,const Stmt * S)3830b57cec5SDimitry Andric static const DeclContext *getEnclosingDeclContext(ASTContext &AST,
3840b57cec5SDimitry Andric const Stmt *S) {
3850b57cec5SDimitry Andric while (S) {
3860b57cec5SDimitry Andric const auto &Parents = AST.getParents(*S);
3870b57cec5SDimitry Andric if (Parents.empty())
3880b57cec5SDimitry Andric return nullptr;
3890b57cec5SDimitry Andric const auto &P = Parents[0];
3900b57cec5SDimitry Andric if (const auto *D = P.get<Decl>())
3910b57cec5SDimitry Andric return D->getDeclContext();
3920b57cec5SDimitry Andric S = P.get<Stmt>();
3930b57cec5SDimitry Andric }
3940b57cec5SDimitry Andric return nullptr;
3950b57cec5SDimitry Andric }
3960b57cec5SDimitry Andric
getInitializerValue(const CXXCtorInitializer * Init,const PrintingPolicy & TypePP)3970b57cec5SDimitry Andric static std::string getInitializerValue(const CXXCtorInitializer *Init,
3980b57cec5SDimitry Andric const PrintingPolicy &TypePP) {
3990b57cec5SDimitry Andric if (Init->isAnyMemberInitializer())
4005ffd83dbSDimitry Andric return std::string(Init->getAnyMember()->getName());
4010b57cec5SDimitry Andric if (Init->isBaseInitializer())
4020b57cec5SDimitry Andric return QualType(Init->getBaseClass(), 0).getAsString(TypePP);
4030b57cec5SDimitry Andric if (Init->isDelegatingInitializer())
4040b57cec5SDimitry Andric return Init->getTypeSourceInfo()->getType().getAsString(TypePP);
4050b57cec5SDimitry Andric llvm_unreachable("Unknown initializer type");
4060b57cec5SDimitry Andric }
4070b57cec5SDimitry Andric
getNodeValue(NodeId Id) const4080b57cec5SDimitry Andric std::string SyntaxTree::Impl::getNodeValue(NodeId Id) const {
4090b57cec5SDimitry Andric return getNodeValue(getNode(Id));
4100b57cec5SDimitry Andric }
4110b57cec5SDimitry Andric
getNodeValue(const Node & N) const4120b57cec5SDimitry Andric std::string SyntaxTree::Impl::getNodeValue(const Node &N) const {
4130b57cec5SDimitry Andric const DynTypedNode &DTN = N.ASTNode;
4140b57cec5SDimitry Andric if (auto *S = DTN.get<Stmt>())
4150b57cec5SDimitry Andric return getStmtValue(S);
4160b57cec5SDimitry Andric if (auto *D = DTN.get<Decl>())
4170b57cec5SDimitry Andric return getDeclValue(D);
4180b57cec5SDimitry Andric if (auto *Init = DTN.get<CXXCtorInitializer>())
4190b57cec5SDimitry Andric return getInitializerValue(Init, TypePP);
4200b57cec5SDimitry Andric llvm_unreachable("Fatal: unhandled AST node.\n");
4210b57cec5SDimitry Andric }
4220b57cec5SDimitry Andric
getDeclValue(const Decl * D) const4230b57cec5SDimitry Andric std::string SyntaxTree::Impl::getDeclValue(const Decl *D) const {
4240b57cec5SDimitry Andric std::string Value;
4250b57cec5SDimitry Andric if (auto *V = dyn_cast<ValueDecl>(D))
4260b57cec5SDimitry Andric return getRelativeName(V) + "(" + V->getType().getAsString(TypePP) + ")";
4270b57cec5SDimitry Andric if (auto *N = dyn_cast<NamedDecl>(D))
4280b57cec5SDimitry Andric Value += getRelativeName(N) + ";";
4290b57cec5SDimitry Andric if (auto *T = dyn_cast<TypedefNameDecl>(D))
4300b57cec5SDimitry Andric return Value + T->getUnderlyingType().getAsString(TypePP) + ";";
4310b57cec5SDimitry Andric if (auto *T = dyn_cast<TypeDecl>(D))
4320b57cec5SDimitry Andric if (T->getTypeForDecl())
4330b57cec5SDimitry Andric Value +=
4340b57cec5SDimitry Andric T->getTypeForDecl()->getCanonicalTypeInternal().getAsString(TypePP) +
4350b57cec5SDimitry Andric ";";
4360b57cec5SDimitry Andric if (auto *U = dyn_cast<UsingDirectiveDecl>(D))
4375ffd83dbSDimitry Andric return std::string(U->getNominatedNamespace()->getName());
4380b57cec5SDimitry Andric if (auto *A = dyn_cast<AccessSpecDecl>(D)) {
4390b57cec5SDimitry Andric CharSourceRange Range(A->getSourceRange(), false);
4405ffd83dbSDimitry Andric return std::string(
4415ffd83dbSDimitry Andric Lexer::getSourceText(Range, AST.getSourceManager(), AST.getLangOpts()));
4420b57cec5SDimitry Andric }
4430b57cec5SDimitry Andric return Value;
4440b57cec5SDimitry Andric }
4450b57cec5SDimitry Andric
getStmtValue(const Stmt * S) const4460b57cec5SDimitry Andric std::string SyntaxTree::Impl::getStmtValue(const Stmt *S) const {
4470b57cec5SDimitry Andric if (auto *U = dyn_cast<UnaryOperator>(S))
4485ffd83dbSDimitry Andric return std::string(UnaryOperator::getOpcodeStr(U->getOpcode()));
4490b57cec5SDimitry Andric if (auto *B = dyn_cast<BinaryOperator>(S))
4505ffd83dbSDimitry Andric return std::string(B->getOpcodeStr());
4510b57cec5SDimitry Andric if (auto *M = dyn_cast<MemberExpr>(S))
4520b57cec5SDimitry Andric return getRelativeName(M->getMemberDecl());
4530b57cec5SDimitry Andric if (auto *I = dyn_cast<IntegerLiteral>(S)) {
4540b57cec5SDimitry Andric SmallString<256> Str;
4550b57cec5SDimitry Andric I->getValue().toString(Str, /*Radix=*/10, /*Signed=*/false);
456*7a6dacacSDimitry Andric return std::string(Str);
4570b57cec5SDimitry Andric }
4580b57cec5SDimitry Andric if (auto *F = dyn_cast<FloatingLiteral>(S)) {
4590b57cec5SDimitry Andric SmallString<256> Str;
4600b57cec5SDimitry Andric F->getValue().toString(Str);
461*7a6dacacSDimitry Andric return std::string(Str);
4620b57cec5SDimitry Andric }
4630b57cec5SDimitry Andric if (auto *D = dyn_cast<DeclRefExpr>(S))
4640b57cec5SDimitry Andric return getRelativeName(D->getDecl(), getEnclosingDeclContext(AST, S));
4650b57cec5SDimitry Andric if (auto *String = dyn_cast<StringLiteral>(S))
4665ffd83dbSDimitry Andric return std::string(String->getString());
4670b57cec5SDimitry Andric if (auto *B = dyn_cast<CXXBoolLiteralExpr>(S))
4680b57cec5SDimitry Andric return B->getValue() ? "true" : "false";
4690b57cec5SDimitry Andric return "";
4700b57cec5SDimitry Andric }
4710b57cec5SDimitry Andric
4720b57cec5SDimitry Andric /// Identifies a node in a subtree by its postorder offset, starting at 1.
4730b57cec5SDimitry Andric struct SNodeId {
4740b57cec5SDimitry Andric int Id = 0;
4750b57cec5SDimitry Andric
SNodeIdclang::diff::SNodeId4760b57cec5SDimitry Andric explicit SNodeId(int Id) : Id(Id) {}
4770b57cec5SDimitry Andric explicit SNodeId() = default;
4780b57cec5SDimitry Andric
operator intclang::diff::SNodeId4790b57cec5SDimitry Andric operator int() const { return Id; }
operator ++clang::diff::SNodeId4800b57cec5SDimitry Andric SNodeId &operator++() { return ++Id, *this; }
operator --clang::diff::SNodeId4810b57cec5SDimitry Andric SNodeId &operator--() { return --Id, *this; }
operator +clang::diff::SNodeId4820b57cec5SDimitry Andric SNodeId operator+(int Other) const { return SNodeId(Id + Other); }
4830b57cec5SDimitry Andric };
4840b57cec5SDimitry Andric
4850b57cec5SDimitry Andric class Subtree {
4860b57cec5SDimitry Andric private:
4870b57cec5SDimitry Andric /// The parent tree.
4880b57cec5SDimitry Andric const SyntaxTree::Impl &Tree;
4890b57cec5SDimitry Andric /// Maps SNodeIds to original ids.
4900b57cec5SDimitry Andric std::vector<NodeId> RootIds;
4910b57cec5SDimitry Andric /// Maps subtree nodes to their leftmost descendants wtihin the subtree.
4920b57cec5SDimitry Andric std::vector<SNodeId> LeftMostDescendants;
4930b57cec5SDimitry Andric
4940b57cec5SDimitry Andric public:
4950b57cec5SDimitry Andric std::vector<SNodeId> KeyRoots;
4960b57cec5SDimitry Andric
Subtree(const SyntaxTree::Impl & Tree,NodeId SubtreeRoot)4970b57cec5SDimitry Andric Subtree(const SyntaxTree::Impl &Tree, NodeId SubtreeRoot) : Tree(Tree) {
4980b57cec5SDimitry Andric RootIds = getSubtreePostorder(Tree, SubtreeRoot);
4990b57cec5SDimitry Andric int NumLeaves = setLeftMostDescendants();
5000b57cec5SDimitry Andric computeKeyRoots(NumLeaves);
5010b57cec5SDimitry Andric }
getSize() const5020b57cec5SDimitry Andric int getSize() const { return RootIds.size(); }
getIdInRoot(SNodeId Id) const5030b57cec5SDimitry Andric NodeId getIdInRoot(SNodeId Id) const {
5040b57cec5SDimitry Andric assert(Id > 0 && Id <= getSize() && "Invalid subtree node index.");
5050b57cec5SDimitry Andric return RootIds[Id - 1];
5060b57cec5SDimitry Andric }
getNode(SNodeId Id) const5070b57cec5SDimitry Andric const Node &getNode(SNodeId Id) const {
5080b57cec5SDimitry Andric return Tree.getNode(getIdInRoot(Id));
5090b57cec5SDimitry Andric }
getLeftMostDescendant(SNodeId Id) const5100b57cec5SDimitry Andric SNodeId getLeftMostDescendant(SNodeId Id) const {
5110b57cec5SDimitry Andric assert(Id > 0 && Id <= getSize() && "Invalid subtree node index.");
5120b57cec5SDimitry Andric return LeftMostDescendants[Id - 1];
5130b57cec5SDimitry Andric }
5140b57cec5SDimitry Andric /// Returns the postorder index of the leftmost descendant in the subtree.
getPostorderOffset() const5150b57cec5SDimitry Andric NodeId getPostorderOffset() const {
5160b57cec5SDimitry Andric return Tree.PostorderIds[getIdInRoot(SNodeId(1))];
5170b57cec5SDimitry Andric }
getNodeValue(SNodeId Id) const5180b57cec5SDimitry Andric std::string getNodeValue(SNodeId Id) const {
5190b57cec5SDimitry Andric return Tree.getNodeValue(getIdInRoot(Id));
5200b57cec5SDimitry Andric }
5210b57cec5SDimitry Andric
5220b57cec5SDimitry Andric private:
5230b57cec5SDimitry Andric /// Returns the number of leafs in the subtree.
setLeftMostDescendants()5240b57cec5SDimitry Andric int setLeftMostDescendants() {
5250b57cec5SDimitry Andric int NumLeaves = 0;
5260b57cec5SDimitry Andric LeftMostDescendants.resize(getSize());
5270b57cec5SDimitry Andric for (int I = 0; I < getSize(); ++I) {
5280b57cec5SDimitry Andric SNodeId SI(I + 1);
5290b57cec5SDimitry Andric const Node &N = getNode(SI);
5300b57cec5SDimitry Andric NumLeaves += N.isLeaf();
5310b57cec5SDimitry Andric assert(I == Tree.PostorderIds[getIdInRoot(SI)] - getPostorderOffset() &&
5320b57cec5SDimitry Andric "Postorder traversal in subtree should correspond to traversal in "
5330b57cec5SDimitry Andric "the root tree by a constant offset.");
5340b57cec5SDimitry Andric LeftMostDescendants[I] = SNodeId(Tree.PostorderIds[N.LeftMostDescendant] -
5350b57cec5SDimitry Andric getPostorderOffset());
5360b57cec5SDimitry Andric }
5370b57cec5SDimitry Andric return NumLeaves;
5380b57cec5SDimitry Andric }
computeKeyRoots(int Leaves)5390b57cec5SDimitry Andric void computeKeyRoots(int Leaves) {
5400b57cec5SDimitry Andric KeyRoots.resize(Leaves);
5410b57cec5SDimitry Andric std::unordered_set<int> Visited;
5420b57cec5SDimitry Andric int K = Leaves - 1;
5430b57cec5SDimitry Andric for (SNodeId I(getSize()); I > 0; --I) {
5440b57cec5SDimitry Andric SNodeId LeftDesc = getLeftMostDescendant(I);
5450b57cec5SDimitry Andric if (Visited.count(LeftDesc))
5460b57cec5SDimitry Andric continue;
5470b57cec5SDimitry Andric assert(K >= 0 && "K should be non-negative");
5480b57cec5SDimitry Andric KeyRoots[K] = I;
5490b57cec5SDimitry Andric Visited.insert(LeftDesc);
5500b57cec5SDimitry Andric --K;
5510b57cec5SDimitry Andric }
5520b57cec5SDimitry Andric }
5530b57cec5SDimitry Andric };
5540b57cec5SDimitry Andric
5550b57cec5SDimitry Andric /// Implementation of Zhang and Shasha's Algorithm for tree edit distance.
5560b57cec5SDimitry Andric /// Computes an optimal mapping between two trees using only insertion,
5570b57cec5SDimitry Andric /// deletion and update as edit actions (similar to the Levenshtein distance).
5580b57cec5SDimitry Andric class ZhangShashaMatcher {
5590b57cec5SDimitry Andric const ASTDiff::Impl &DiffImpl;
5600b57cec5SDimitry Andric Subtree S1;
5610b57cec5SDimitry Andric Subtree S2;
5620b57cec5SDimitry Andric std::unique_ptr<std::unique_ptr<double[]>[]> TreeDist, ForestDist;
5630b57cec5SDimitry Andric
5640b57cec5SDimitry Andric public:
ZhangShashaMatcher(const ASTDiff::Impl & DiffImpl,const SyntaxTree::Impl & T1,const SyntaxTree::Impl & T2,NodeId Id1,NodeId Id2)5650b57cec5SDimitry Andric ZhangShashaMatcher(const ASTDiff::Impl &DiffImpl, const SyntaxTree::Impl &T1,
5660b57cec5SDimitry Andric const SyntaxTree::Impl &T2, NodeId Id1, NodeId Id2)
5670b57cec5SDimitry Andric : DiffImpl(DiffImpl), S1(T1, Id1), S2(T2, Id2) {
568a7dea167SDimitry Andric TreeDist = std::make_unique<std::unique_ptr<double[]>[]>(
5690b57cec5SDimitry Andric size_t(S1.getSize()) + 1);
570a7dea167SDimitry Andric ForestDist = std::make_unique<std::unique_ptr<double[]>[]>(
5710b57cec5SDimitry Andric size_t(S1.getSize()) + 1);
5720b57cec5SDimitry Andric for (int I = 0, E = S1.getSize() + 1; I < E; ++I) {
573a7dea167SDimitry Andric TreeDist[I] = std::make_unique<double[]>(size_t(S2.getSize()) + 1);
574a7dea167SDimitry Andric ForestDist[I] = std::make_unique<double[]>(size_t(S2.getSize()) + 1);
5750b57cec5SDimitry Andric }
5760b57cec5SDimitry Andric }
5770b57cec5SDimitry Andric
getMatchingNodes()5780b57cec5SDimitry Andric std::vector<std::pair<NodeId, NodeId>> getMatchingNodes() {
5790b57cec5SDimitry Andric std::vector<std::pair<NodeId, NodeId>> Matches;
5800b57cec5SDimitry Andric std::vector<std::pair<SNodeId, SNodeId>> TreePairs;
5810b57cec5SDimitry Andric
5820b57cec5SDimitry Andric computeTreeDist();
5830b57cec5SDimitry Andric
5840b57cec5SDimitry Andric bool RootNodePair = true;
5850b57cec5SDimitry Andric
5860b57cec5SDimitry Andric TreePairs.emplace_back(SNodeId(S1.getSize()), SNodeId(S2.getSize()));
5870b57cec5SDimitry Andric
5880b57cec5SDimitry Andric while (!TreePairs.empty()) {
5890b57cec5SDimitry Andric SNodeId LastRow, LastCol, FirstRow, FirstCol, Row, Col;
5900b57cec5SDimitry Andric std::tie(LastRow, LastCol) = TreePairs.back();
5910b57cec5SDimitry Andric TreePairs.pop_back();
5920b57cec5SDimitry Andric
5930b57cec5SDimitry Andric if (!RootNodePair) {
5940b57cec5SDimitry Andric computeForestDist(LastRow, LastCol);
5950b57cec5SDimitry Andric }
5960b57cec5SDimitry Andric
5970b57cec5SDimitry Andric RootNodePair = false;
5980b57cec5SDimitry Andric
5990b57cec5SDimitry Andric FirstRow = S1.getLeftMostDescendant(LastRow);
6000b57cec5SDimitry Andric FirstCol = S2.getLeftMostDescendant(LastCol);
6010b57cec5SDimitry Andric
6020b57cec5SDimitry Andric Row = LastRow;
6030b57cec5SDimitry Andric Col = LastCol;
6040b57cec5SDimitry Andric
6050b57cec5SDimitry Andric while (Row > FirstRow || Col > FirstCol) {
6060b57cec5SDimitry Andric if (Row > FirstRow &&
6070b57cec5SDimitry Andric ForestDist[Row - 1][Col] + 1 == ForestDist[Row][Col]) {
6080b57cec5SDimitry Andric --Row;
6090b57cec5SDimitry Andric } else if (Col > FirstCol &&
6100b57cec5SDimitry Andric ForestDist[Row][Col - 1] + 1 == ForestDist[Row][Col]) {
6110b57cec5SDimitry Andric --Col;
6120b57cec5SDimitry Andric } else {
6130b57cec5SDimitry Andric SNodeId LMD1 = S1.getLeftMostDescendant(Row);
6140b57cec5SDimitry Andric SNodeId LMD2 = S2.getLeftMostDescendant(Col);
6150b57cec5SDimitry Andric if (LMD1 == S1.getLeftMostDescendant(LastRow) &&
6160b57cec5SDimitry Andric LMD2 == S2.getLeftMostDescendant(LastCol)) {
6170b57cec5SDimitry Andric NodeId Id1 = S1.getIdInRoot(Row);
6180b57cec5SDimitry Andric NodeId Id2 = S2.getIdInRoot(Col);
6190b57cec5SDimitry Andric assert(DiffImpl.isMatchingPossible(Id1, Id2) &&
6200b57cec5SDimitry Andric "These nodes must not be matched.");
6210b57cec5SDimitry Andric Matches.emplace_back(Id1, Id2);
6220b57cec5SDimitry Andric --Row;
6230b57cec5SDimitry Andric --Col;
6240b57cec5SDimitry Andric } else {
6250b57cec5SDimitry Andric TreePairs.emplace_back(Row, Col);
6260b57cec5SDimitry Andric Row = LMD1;
6270b57cec5SDimitry Andric Col = LMD2;
6280b57cec5SDimitry Andric }
6290b57cec5SDimitry Andric }
6300b57cec5SDimitry Andric }
6310b57cec5SDimitry Andric }
6320b57cec5SDimitry Andric return Matches;
6330b57cec5SDimitry Andric }
6340b57cec5SDimitry Andric
6350b57cec5SDimitry Andric private:
6360b57cec5SDimitry Andric /// We use a simple cost model for edit actions, which seems good enough.
6370b57cec5SDimitry Andric /// Simple cost model for edit actions. This seems to make the matching
6380b57cec5SDimitry Andric /// algorithm perform reasonably well.
6390b57cec5SDimitry Andric /// The values range between 0 and 1, or infinity if this edit action should
6400b57cec5SDimitry Andric /// always be avoided.
6410b57cec5SDimitry Andric static constexpr double DeletionCost = 1;
6420b57cec5SDimitry Andric static constexpr double InsertionCost = 1;
6430b57cec5SDimitry Andric
getUpdateCost(SNodeId Id1,SNodeId Id2)6440b57cec5SDimitry Andric double getUpdateCost(SNodeId Id1, SNodeId Id2) {
6450b57cec5SDimitry Andric if (!DiffImpl.isMatchingPossible(S1.getIdInRoot(Id1), S2.getIdInRoot(Id2)))
6460b57cec5SDimitry Andric return std::numeric_limits<double>::max();
6470b57cec5SDimitry Andric return S1.getNodeValue(Id1) != S2.getNodeValue(Id2);
6480b57cec5SDimitry Andric }
6490b57cec5SDimitry Andric
computeTreeDist()6500b57cec5SDimitry Andric void computeTreeDist() {
6510b57cec5SDimitry Andric for (SNodeId Id1 : S1.KeyRoots)
6520b57cec5SDimitry Andric for (SNodeId Id2 : S2.KeyRoots)
6530b57cec5SDimitry Andric computeForestDist(Id1, Id2);
6540b57cec5SDimitry Andric }
6550b57cec5SDimitry Andric
computeForestDist(SNodeId Id1,SNodeId Id2)6560b57cec5SDimitry Andric void computeForestDist(SNodeId Id1, SNodeId Id2) {
6570b57cec5SDimitry Andric assert(Id1 > 0 && Id2 > 0 && "Expecting offsets greater than 0.");
6580b57cec5SDimitry Andric SNodeId LMD1 = S1.getLeftMostDescendant(Id1);
6590b57cec5SDimitry Andric SNodeId LMD2 = S2.getLeftMostDescendant(Id2);
6600b57cec5SDimitry Andric
6610b57cec5SDimitry Andric ForestDist[LMD1][LMD2] = 0;
6620b57cec5SDimitry Andric for (SNodeId D1 = LMD1 + 1; D1 <= Id1; ++D1) {
6630b57cec5SDimitry Andric ForestDist[D1][LMD2] = ForestDist[D1 - 1][LMD2] + DeletionCost;
6640b57cec5SDimitry Andric for (SNodeId D2 = LMD2 + 1; D2 <= Id2; ++D2) {
6650b57cec5SDimitry Andric ForestDist[LMD1][D2] = ForestDist[LMD1][D2 - 1] + InsertionCost;
6660b57cec5SDimitry Andric SNodeId DLMD1 = S1.getLeftMostDescendant(D1);
6670b57cec5SDimitry Andric SNodeId DLMD2 = S2.getLeftMostDescendant(D2);
6680b57cec5SDimitry Andric if (DLMD1 == LMD1 && DLMD2 == LMD2) {
6690b57cec5SDimitry Andric double UpdateCost = getUpdateCost(D1, D2);
6700b57cec5SDimitry Andric ForestDist[D1][D2] =
6710b57cec5SDimitry Andric std::min({ForestDist[D1 - 1][D2] + DeletionCost,
6720b57cec5SDimitry Andric ForestDist[D1][D2 - 1] + InsertionCost,
6730b57cec5SDimitry Andric ForestDist[D1 - 1][D2 - 1] + UpdateCost});
6740b57cec5SDimitry Andric TreeDist[D1][D2] = ForestDist[D1][D2];
6750b57cec5SDimitry Andric } else {
6760b57cec5SDimitry Andric ForestDist[D1][D2] =
6770b57cec5SDimitry Andric std::min({ForestDist[D1 - 1][D2] + DeletionCost,
6780b57cec5SDimitry Andric ForestDist[D1][D2 - 1] + InsertionCost,
6790b57cec5SDimitry Andric ForestDist[DLMD1][DLMD2] + TreeDist[D1][D2]});
6800b57cec5SDimitry Andric }
6810b57cec5SDimitry Andric }
6820b57cec5SDimitry Andric }
6830b57cec5SDimitry Andric }
6840b57cec5SDimitry Andric };
6850b57cec5SDimitry Andric
getType() const6865ffd83dbSDimitry Andric ASTNodeKind Node::getType() const { return ASTNode.getNodeKind(); }
6870b57cec5SDimitry Andric
getTypeLabel() const6880b57cec5SDimitry Andric StringRef Node::getTypeLabel() const { return getType().asStringRef(); }
6890b57cec5SDimitry Andric
getQualifiedIdentifier() const690bdd1243dSDimitry Andric std::optional<std::string> Node::getQualifiedIdentifier() const {
6910b57cec5SDimitry Andric if (auto *ND = ASTNode.get<NamedDecl>()) {
6920b57cec5SDimitry Andric if (ND->getDeclName().isIdentifier())
6930b57cec5SDimitry Andric return ND->getQualifiedNameAsString();
6940b57cec5SDimitry Andric }
695bdd1243dSDimitry Andric return std::nullopt;
6960b57cec5SDimitry Andric }
6970b57cec5SDimitry Andric
getIdentifier() const698bdd1243dSDimitry Andric std::optional<StringRef> Node::getIdentifier() const {
6990b57cec5SDimitry Andric if (auto *ND = ASTNode.get<NamedDecl>()) {
7000b57cec5SDimitry Andric if (ND->getDeclName().isIdentifier())
7010b57cec5SDimitry Andric return ND->getName();
7020b57cec5SDimitry Andric }
703bdd1243dSDimitry Andric return std::nullopt;
7040b57cec5SDimitry Andric }
7050b57cec5SDimitry Andric
7060b57cec5SDimitry Andric namespace {
7070b57cec5SDimitry Andric // Compares nodes by their depth.
7080b57cec5SDimitry Andric struct HeightLess {
7090b57cec5SDimitry Andric const SyntaxTree::Impl &Tree;
HeightLessclang::diff::__anon36ce97a80511::HeightLess7100b57cec5SDimitry Andric HeightLess(const SyntaxTree::Impl &Tree) : Tree(Tree) {}
operator ()clang::diff::__anon36ce97a80511::HeightLess7110b57cec5SDimitry Andric bool operator()(NodeId Id1, NodeId Id2) const {
7120b57cec5SDimitry Andric return Tree.getNode(Id1).Height < Tree.getNode(Id2).Height;
7130b57cec5SDimitry Andric }
7140b57cec5SDimitry Andric };
7150b57cec5SDimitry Andric } // end anonymous namespace
7160b57cec5SDimitry Andric
7170b57cec5SDimitry Andric namespace {
7180b57cec5SDimitry Andric // Priority queue for nodes, sorted descendingly by their height.
7190b57cec5SDimitry Andric class PriorityList {
7200b57cec5SDimitry Andric const SyntaxTree::Impl &Tree;
7210b57cec5SDimitry Andric HeightLess Cmp;
7220b57cec5SDimitry Andric std::vector<NodeId> Container;
7230b57cec5SDimitry Andric PriorityQueue<NodeId, std::vector<NodeId>, HeightLess> List;
7240b57cec5SDimitry Andric
7250b57cec5SDimitry Andric public:
PriorityList(const SyntaxTree::Impl & Tree)7260b57cec5SDimitry Andric PriorityList(const SyntaxTree::Impl &Tree)
7270b57cec5SDimitry Andric : Tree(Tree), Cmp(Tree), List(Cmp, Container) {}
7280b57cec5SDimitry Andric
push(NodeId id)7290b57cec5SDimitry Andric void push(NodeId id) { List.push(id); }
7300b57cec5SDimitry Andric
pop()7310b57cec5SDimitry Andric std::vector<NodeId> pop() {
7320b57cec5SDimitry Andric int Max = peekMax();
7330b57cec5SDimitry Andric std::vector<NodeId> Result;
7340b57cec5SDimitry Andric if (Max == 0)
7350b57cec5SDimitry Andric return Result;
7360b57cec5SDimitry Andric while (peekMax() == Max) {
7370b57cec5SDimitry Andric Result.push_back(List.top());
7380b57cec5SDimitry Andric List.pop();
7390b57cec5SDimitry Andric }
7400b57cec5SDimitry Andric // TODO this is here to get a stable output, not a good heuristic
7410b57cec5SDimitry Andric llvm::sort(Result);
7420b57cec5SDimitry Andric return Result;
7430b57cec5SDimitry Andric }
peekMax() const7440b57cec5SDimitry Andric int peekMax() const {
7450b57cec5SDimitry Andric if (List.empty())
7460b57cec5SDimitry Andric return 0;
7470b57cec5SDimitry Andric return Tree.getNode(List.top()).Height;
7480b57cec5SDimitry Andric }
open(NodeId Id)7490b57cec5SDimitry Andric void open(NodeId Id) {
7500b57cec5SDimitry Andric for (NodeId Child : Tree.getNode(Id).Children)
7510b57cec5SDimitry Andric push(Child);
7520b57cec5SDimitry Andric }
7530b57cec5SDimitry Andric };
7540b57cec5SDimitry Andric } // end anonymous namespace
7550b57cec5SDimitry Andric
identical(NodeId Id1,NodeId Id2) const7560b57cec5SDimitry Andric bool ASTDiff::Impl::identical(NodeId Id1, NodeId Id2) const {
7570b57cec5SDimitry Andric const Node &N1 = T1.getNode(Id1);
7580b57cec5SDimitry Andric const Node &N2 = T2.getNode(Id2);
7590b57cec5SDimitry Andric if (N1.Children.size() != N2.Children.size() ||
7600b57cec5SDimitry Andric !isMatchingPossible(Id1, Id2) ||
7610b57cec5SDimitry Andric T1.getNodeValue(Id1) != T2.getNodeValue(Id2))
7620b57cec5SDimitry Andric return false;
7630b57cec5SDimitry Andric for (size_t Id = 0, E = N1.Children.size(); Id < E; ++Id)
7640b57cec5SDimitry Andric if (!identical(N1.Children[Id], N2.Children[Id]))
7650b57cec5SDimitry Andric return false;
7660b57cec5SDimitry Andric return true;
7670b57cec5SDimitry Andric }
7680b57cec5SDimitry Andric
isMatchingPossible(NodeId Id1,NodeId Id2) const7690b57cec5SDimitry Andric bool ASTDiff::Impl::isMatchingPossible(NodeId Id1, NodeId Id2) const {
7700b57cec5SDimitry Andric return Options.isMatchingAllowed(T1.getNode(Id1), T2.getNode(Id2));
7710b57cec5SDimitry Andric }
7720b57cec5SDimitry Andric
haveSameParents(const Mapping & M,NodeId Id1,NodeId Id2) const7730b57cec5SDimitry Andric bool ASTDiff::Impl::haveSameParents(const Mapping &M, NodeId Id1,
7740b57cec5SDimitry Andric NodeId Id2) const {
7750b57cec5SDimitry Andric NodeId P1 = T1.getNode(Id1).Parent;
7760b57cec5SDimitry Andric NodeId P2 = T2.getNode(Id2).Parent;
7770b57cec5SDimitry Andric return (P1.isInvalid() && P2.isInvalid()) ||
7780b57cec5SDimitry Andric (P1.isValid() && P2.isValid() && M.getDst(P1) == P2);
7790b57cec5SDimitry Andric }
7800b57cec5SDimitry Andric
addOptimalMapping(Mapping & M,NodeId Id1,NodeId Id2) const7810b57cec5SDimitry Andric void ASTDiff::Impl::addOptimalMapping(Mapping &M, NodeId Id1,
7820b57cec5SDimitry Andric NodeId Id2) const {
7830b57cec5SDimitry Andric if (std::max(T1.getNumberOfDescendants(Id1), T2.getNumberOfDescendants(Id2)) >
7840b57cec5SDimitry Andric Options.MaxSize)
7850b57cec5SDimitry Andric return;
7860b57cec5SDimitry Andric ZhangShashaMatcher Matcher(*this, T1, T2, Id1, Id2);
7870b57cec5SDimitry Andric std::vector<std::pair<NodeId, NodeId>> R = Matcher.getMatchingNodes();
788480093f4SDimitry Andric for (const auto &Tuple : R) {
7890b57cec5SDimitry Andric NodeId Src = Tuple.first;
7900b57cec5SDimitry Andric NodeId Dst = Tuple.second;
7910b57cec5SDimitry Andric if (!M.hasSrc(Src) && !M.hasDst(Dst))
7920b57cec5SDimitry Andric M.link(Src, Dst);
7930b57cec5SDimitry Andric }
7940b57cec5SDimitry Andric }
7950b57cec5SDimitry Andric
getJaccardSimilarity(const Mapping & M,NodeId Id1,NodeId Id2) const7960b57cec5SDimitry Andric double ASTDiff::Impl::getJaccardSimilarity(const Mapping &M, NodeId Id1,
7970b57cec5SDimitry Andric NodeId Id2) const {
7980b57cec5SDimitry Andric int CommonDescendants = 0;
7990b57cec5SDimitry Andric const Node &N1 = T1.getNode(Id1);
8000b57cec5SDimitry Andric // Count the common descendants, excluding the subtree root.
8010b57cec5SDimitry Andric for (NodeId Src = Id1 + 1; Src <= N1.RightMostDescendant; ++Src) {
8020b57cec5SDimitry Andric NodeId Dst = M.getDst(Src);
8030b57cec5SDimitry Andric CommonDescendants += int(Dst.isValid() && T2.isInSubtree(Dst, Id2));
8040b57cec5SDimitry Andric }
8050b57cec5SDimitry Andric // We need to subtract 1 to get the number of descendants excluding the root.
8060b57cec5SDimitry Andric double Denominator = T1.getNumberOfDescendants(Id1) - 1 +
8070b57cec5SDimitry Andric T2.getNumberOfDescendants(Id2) - 1 - CommonDescendants;
8080b57cec5SDimitry Andric // CommonDescendants is less than the size of one subtree.
8090b57cec5SDimitry Andric assert(Denominator >= 0 && "Expected non-negative denominator.");
8100b57cec5SDimitry Andric if (Denominator == 0)
8110b57cec5SDimitry Andric return 0;
8120b57cec5SDimitry Andric return CommonDescendants / Denominator;
8130b57cec5SDimitry Andric }
8140b57cec5SDimitry Andric
findCandidate(const Mapping & M,NodeId Id1) const8150b57cec5SDimitry Andric NodeId ASTDiff::Impl::findCandidate(const Mapping &M, NodeId Id1) const {
8160b57cec5SDimitry Andric NodeId Candidate;
8170b57cec5SDimitry Andric double HighestSimilarity = 0.0;
8180b57cec5SDimitry Andric for (NodeId Id2 : T2) {
8190b57cec5SDimitry Andric if (!isMatchingPossible(Id1, Id2))
8200b57cec5SDimitry Andric continue;
8210b57cec5SDimitry Andric if (M.hasDst(Id2))
8220b57cec5SDimitry Andric continue;
8230b57cec5SDimitry Andric double Similarity = getJaccardSimilarity(M, Id1, Id2);
8240b57cec5SDimitry Andric if (Similarity >= Options.MinSimilarity && Similarity > HighestSimilarity) {
8250b57cec5SDimitry Andric HighestSimilarity = Similarity;
8260b57cec5SDimitry Andric Candidate = Id2;
8270b57cec5SDimitry Andric }
8280b57cec5SDimitry Andric }
8290b57cec5SDimitry Andric return Candidate;
8300b57cec5SDimitry Andric }
8310b57cec5SDimitry Andric
matchBottomUp(Mapping & M) const8320b57cec5SDimitry Andric void ASTDiff::Impl::matchBottomUp(Mapping &M) const {
8330b57cec5SDimitry Andric std::vector<NodeId> Postorder = getSubtreePostorder(T1, T1.getRootId());
8340b57cec5SDimitry Andric for (NodeId Id1 : Postorder) {
8350b57cec5SDimitry Andric if (Id1 == T1.getRootId() && !M.hasSrc(T1.getRootId()) &&
8360b57cec5SDimitry Andric !M.hasDst(T2.getRootId())) {
8370b57cec5SDimitry Andric if (isMatchingPossible(T1.getRootId(), T2.getRootId())) {
8380b57cec5SDimitry Andric M.link(T1.getRootId(), T2.getRootId());
8390b57cec5SDimitry Andric addOptimalMapping(M, T1.getRootId(), T2.getRootId());
8400b57cec5SDimitry Andric }
8410b57cec5SDimitry Andric break;
8420b57cec5SDimitry Andric }
8430b57cec5SDimitry Andric bool Matched = M.hasSrc(Id1);
8440b57cec5SDimitry Andric const Node &N1 = T1.getNode(Id1);
8450b57cec5SDimitry Andric bool MatchedChildren = llvm::any_of(
8460b57cec5SDimitry Andric N1.Children, [&](NodeId Child) { return M.hasSrc(Child); });
8470b57cec5SDimitry Andric if (Matched || !MatchedChildren)
8480b57cec5SDimitry Andric continue;
8490b57cec5SDimitry Andric NodeId Id2 = findCandidate(M, Id1);
8500b57cec5SDimitry Andric if (Id2.isValid()) {
8510b57cec5SDimitry Andric M.link(Id1, Id2);
8520b57cec5SDimitry Andric addOptimalMapping(M, Id1, Id2);
8530b57cec5SDimitry Andric }
8540b57cec5SDimitry Andric }
8550b57cec5SDimitry Andric }
8560b57cec5SDimitry Andric
matchTopDown() const8570b57cec5SDimitry Andric Mapping ASTDiff::Impl::matchTopDown() const {
8580b57cec5SDimitry Andric PriorityList L1(T1);
8590b57cec5SDimitry Andric PriorityList L2(T2);
8600b57cec5SDimitry Andric
8610b57cec5SDimitry Andric Mapping M(T1.getSize() + T2.getSize());
8620b57cec5SDimitry Andric
8630b57cec5SDimitry Andric L1.push(T1.getRootId());
8640b57cec5SDimitry Andric L2.push(T2.getRootId());
8650b57cec5SDimitry Andric
8660b57cec5SDimitry Andric int Max1, Max2;
8670b57cec5SDimitry Andric while (std::min(Max1 = L1.peekMax(), Max2 = L2.peekMax()) >
8680b57cec5SDimitry Andric Options.MinHeight) {
8690b57cec5SDimitry Andric if (Max1 > Max2) {
8700b57cec5SDimitry Andric for (NodeId Id : L1.pop())
8710b57cec5SDimitry Andric L1.open(Id);
8720b57cec5SDimitry Andric continue;
8730b57cec5SDimitry Andric }
8740b57cec5SDimitry Andric if (Max2 > Max1) {
8750b57cec5SDimitry Andric for (NodeId Id : L2.pop())
8760b57cec5SDimitry Andric L2.open(Id);
8770b57cec5SDimitry Andric continue;
8780b57cec5SDimitry Andric }
8790b57cec5SDimitry Andric std::vector<NodeId> H1, H2;
8800b57cec5SDimitry Andric H1 = L1.pop();
8810b57cec5SDimitry Andric H2 = L2.pop();
8820b57cec5SDimitry Andric for (NodeId Id1 : H1) {
8830b57cec5SDimitry Andric for (NodeId Id2 : H2) {
8840b57cec5SDimitry Andric if (identical(Id1, Id2) && !M.hasSrc(Id1) && !M.hasDst(Id2)) {
8850b57cec5SDimitry Andric for (int I = 0, E = T1.getNumberOfDescendants(Id1); I < E; ++I)
8860b57cec5SDimitry Andric M.link(Id1 + I, Id2 + I);
8870b57cec5SDimitry Andric }
8880b57cec5SDimitry Andric }
8890b57cec5SDimitry Andric }
8900b57cec5SDimitry Andric for (NodeId Id1 : H1) {
8910b57cec5SDimitry Andric if (!M.hasSrc(Id1))
8920b57cec5SDimitry Andric L1.open(Id1);
8930b57cec5SDimitry Andric }
8940b57cec5SDimitry Andric for (NodeId Id2 : H2) {
8950b57cec5SDimitry Andric if (!M.hasDst(Id2))
8960b57cec5SDimitry Andric L2.open(Id2);
8970b57cec5SDimitry Andric }
8980b57cec5SDimitry Andric }
8990b57cec5SDimitry Andric return M;
9000b57cec5SDimitry Andric }
9010b57cec5SDimitry Andric
Impl(SyntaxTree::Impl & T1,SyntaxTree::Impl & T2,const ComparisonOptions & Options)9020b57cec5SDimitry Andric ASTDiff::Impl::Impl(SyntaxTree::Impl &T1, SyntaxTree::Impl &T2,
9030b57cec5SDimitry Andric const ComparisonOptions &Options)
9040b57cec5SDimitry Andric : T1(T1), T2(T2), Options(Options) {
9050b57cec5SDimitry Andric computeMapping();
9060b57cec5SDimitry Andric computeChangeKinds(TheMapping);
9070b57cec5SDimitry Andric }
9080b57cec5SDimitry Andric
computeMapping()9090b57cec5SDimitry Andric void ASTDiff::Impl::computeMapping() {
9100b57cec5SDimitry Andric TheMapping = matchTopDown();
9110b57cec5SDimitry Andric if (Options.StopAfterTopDown)
9120b57cec5SDimitry Andric return;
9130b57cec5SDimitry Andric matchBottomUp(TheMapping);
9140b57cec5SDimitry Andric }
9150b57cec5SDimitry Andric
computeChangeKinds(Mapping & M)9160b57cec5SDimitry Andric void ASTDiff::Impl::computeChangeKinds(Mapping &M) {
9170b57cec5SDimitry Andric for (NodeId Id1 : T1) {
9180b57cec5SDimitry Andric if (!M.hasSrc(Id1)) {
9190b57cec5SDimitry Andric T1.getMutableNode(Id1).Change = Delete;
9200b57cec5SDimitry Andric T1.getMutableNode(Id1).Shift -= 1;
9210b57cec5SDimitry Andric }
9220b57cec5SDimitry Andric }
9230b57cec5SDimitry Andric for (NodeId Id2 : T2) {
9240b57cec5SDimitry Andric if (!M.hasDst(Id2)) {
9250b57cec5SDimitry Andric T2.getMutableNode(Id2).Change = Insert;
9260b57cec5SDimitry Andric T2.getMutableNode(Id2).Shift -= 1;
9270b57cec5SDimitry Andric }
9280b57cec5SDimitry Andric }
9290b57cec5SDimitry Andric for (NodeId Id1 : T1.NodesBfs) {
9300b57cec5SDimitry Andric NodeId Id2 = M.getDst(Id1);
9310b57cec5SDimitry Andric if (Id2.isInvalid())
9320b57cec5SDimitry Andric continue;
9330b57cec5SDimitry Andric if (!haveSameParents(M, Id1, Id2) ||
9340b57cec5SDimitry Andric T1.findPositionInParent(Id1, true) !=
9350b57cec5SDimitry Andric T2.findPositionInParent(Id2, true)) {
9360b57cec5SDimitry Andric T1.getMutableNode(Id1).Shift -= 1;
9370b57cec5SDimitry Andric T2.getMutableNode(Id2).Shift -= 1;
9380b57cec5SDimitry Andric }
9390b57cec5SDimitry Andric }
9400b57cec5SDimitry Andric for (NodeId Id2 : T2.NodesBfs) {
9410b57cec5SDimitry Andric NodeId Id1 = M.getSrc(Id2);
9420b57cec5SDimitry Andric if (Id1.isInvalid())
9430b57cec5SDimitry Andric continue;
9440b57cec5SDimitry Andric Node &N1 = T1.getMutableNode(Id1);
9450b57cec5SDimitry Andric Node &N2 = T2.getMutableNode(Id2);
9460b57cec5SDimitry Andric if (Id1.isInvalid())
9470b57cec5SDimitry Andric continue;
9480b57cec5SDimitry Andric if (!haveSameParents(M, Id1, Id2) ||
9490b57cec5SDimitry Andric T1.findPositionInParent(Id1, true) !=
9500b57cec5SDimitry Andric T2.findPositionInParent(Id2, true)) {
9510b57cec5SDimitry Andric N1.Change = N2.Change = Move;
9520b57cec5SDimitry Andric }
9530b57cec5SDimitry Andric if (T1.getNodeValue(Id1) != T2.getNodeValue(Id2)) {
9540b57cec5SDimitry Andric N1.Change = N2.Change = (N1.Change == Move ? UpdateMove : Update);
9550b57cec5SDimitry Andric }
9560b57cec5SDimitry Andric }
9570b57cec5SDimitry Andric }
9580b57cec5SDimitry Andric
ASTDiff(SyntaxTree & T1,SyntaxTree & T2,const ComparisonOptions & Options)9590b57cec5SDimitry Andric ASTDiff::ASTDiff(SyntaxTree &T1, SyntaxTree &T2,
9600b57cec5SDimitry Andric const ComparisonOptions &Options)
961a7dea167SDimitry Andric : DiffImpl(std::make_unique<Impl>(*T1.TreeImpl, *T2.TreeImpl, Options)) {}
9620b57cec5SDimitry Andric
9630b57cec5SDimitry Andric ASTDiff::~ASTDiff() = default;
9640b57cec5SDimitry Andric
getMapped(const SyntaxTree & SourceTree,NodeId Id) const9650b57cec5SDimitry Andric NodeId ASTDiff::getMapped(const SyntaxTree &SourceTree, NodeId Id) const {
9660b57cec5SDimitry Andric return DiffImpl->getMapped(SourceTree.TreeImpl, Id);
9670b57cec5SDimitry Andric }
9680b57cec5SDimitry Andric
SyntaxTree(ASTContext & AST)9690b57cec5SDimitry Andric SyntaxTree::SyntaxTree(ASTContext &AST)
970a7dea167SDimitry Andric : TreeImpl(std::make_unique<SyntaxTree::Impl>(
9710b57cec5SDimitry Andric this, AST.getTranslationUnitDecl(), AST)) {}
9720b57cec5SDimitry Andric
9730b57cec5SDimitry Andric SyntaxTree::~SyntaxTree() = default;
9740b57cec5SDimitry Andric
getASTContext() const9750b57cec5SDimitry Andric const ASTContext &SyntaxTree::getASTContext() const { return TreeImpl->AST; }
9760b57cec5SDimitry Andric
getNode(NodeId Id) const9770b57cec5SDimitry Andric const Node &SyntaxTree::getNode(NodeId Id) const {
9780b57cec5SDimitry Andric return TreeImpl->getNode(Id);
9790b57cec5SDimitry Andric }
9800b57cec5SDimitry Andric
getSize() const9810b57cec5SDimitry Andric int SyntaxTree::getSize() const { return TreeImpl->getSize(); }
getRootId() const9820b57cec5SDimitry Andric NodeId SyntaxTree::getRootId() const { return TreeImpl->getRootId(); }
begin() const9830b57cec5SDimitry Andric SyntaxTree::PreorderIterator SyntaxTree::begin() const {
9840b57cec5SDimitry Andric return TreeImpl->begin();
9850b57cec5SDimitry Andric }
end() const9860b57cec5SDimitry Andric SyntaxTree::PreorderIterator SyntaxTree::end() const { return TreeImpl->end(); }
9870b57cec5SDimitry Andric
findPositionInParent(NodeId Id) const9880b57cec5SDimitry Andric int SyntaxTree::findPositionInParent(NodeId Id) const {
9890b57cec5SDimitry Andric return TreeImpl->findPositionInParent(Id);
9900b57cec5SDimitry Andric }
9910b57cec5SDimitry Andric
9920b57cec5SDimitry Andric std::pair<unsigned, unsigned>
getSourceRangeOffsets(const Node & N) const9930b57cec5SDimitry Andric SyntaxTree::getSourceRangeOffsets(const Node &N) const {
9940b57cec5SDimitry Andric const SourceManager &SrcMgr = TreeImpl->AST.getSourceManager();
9950b57cec5SDimitry Andric SourceRange Range = N.ASTNode.getSourceRange();
9960b57cec5SDimitry Andric SourceLocation BeginLoc = Range.getBegin();
9970b57cec5SDimitry Andric SourceLocation EndLoc = Lexer::getLocForEndOfToken(
9980b57cec5SDimitry Andric Range.getEnd(), /*Offset=*/0, SrcMgr, TreeImpl->AST.getLangOpts());
9990b57cec5SDimitry Andric if (auto *ThisExpr = N.ASTNode.get<CXXThisExpr>()) {
10000b57cec5SDimitry Andric if (ThisExpr->isImplicit())
10010b57cec5SDimitry Andric EndLoc = BeginLoc;
10020b57cec5SDimitry Andric }
10030b57cec5SDimitry Andric unsigned Begin = SrcMgr.getFileOffset(SrcMgr.getExpansionLoc(BeginLoc));
10040b57cec5SDimitry Andric unsigned End = SrcMgr.getFileOffset(SrcMgr.getExpansionLoc(EndLoc));
10050b57cec5SDimitry Andric return {Begin, End};
10060b57cec5SDimitry Andric }
10070b57cec5SDimitry Andric
getNodeValue(NodeId Id) const10080b57cec5SDimitry Andric std::string SyntaxTree::getNodeValue(NodeId Id) const {
10090b57cec5SDimitry Andric return TreeImpl->getNodeValue(Id);
10100b57cec5SDimitry Andric }
10110b57cec5SDimitry Andric
getNodeValue(const Node & N) const10120b57cec5SDimitry Andric std::string SyntaxTree::getNodeValue(const Node &N) const {
10130b57cec5SDimitry Andric return TreeImpl->getNodeValue(N);
10140b57cec5SDimitry Andric }
10150b57cec5SDimitry Andric
10160b57cec5SDimitry Andric } // end namespace diff
10170b57cec5SDimitry Andric } // end namespace clang
1018