2017-07-21 20:49:28 +08:00
|
|
|
//===- ASTDiff.cpp - AST differencing implementation-----------*- C++ -*- -===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains definitons for the AST differencing interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Tooling/ASTDiff/ASTDiff.h"
|
|
|
|
|
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
|
|
|
#include "clang/Lex/Lexer.h"
|
|
|
|
#include "llvm/ADT/PriorityQueue.h"
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
#include <memory>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace diff {
|
|
|
|
|
2017-08-19 00:34:15 +08:00
|
|
|
namespace {
|
2017-08-02 04:17:40 +08:00
|
|
|
/// Maps nodes of the left tree to ones on the right, and vice versa.
|
|
|
|
class Mapping {
|
|
|
|
public:
|
|
|
|
Mapping() = default;
|
|
|
|
Mapping(Mapping &&Other) = default;
|
|
|
|
Mapping &operator=(Mapping &&Other) = default;
|
|
|
|
Mapping(int Size1, int Size2) {
|
|
|
|
// Maximum possible size after patching one tree.
|
|
|
|
int Size = Size1 + Size2;
|
|
|
|
SrcToDst = llvm::make_unique<SmallVector<NodeId, 2>[]>(Size);
|
|
|
|
DstToSrc = llvm::make_unique<SmallVector<NodeId, 2>[]>(Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void link(NodeId Src, NodeId Dst) {
|
|
|
|
SrcToDst[Src].push_back(Dst);
|
|
|
|
DstToSrc[Dst].push_back(Src);
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeId getDst(NodeId Src) const {
|
|
|
|
if (hasSrc(Src))
|
|
|
|
return SrcToDst[Src][0];
|
|
|
|
return NodeId();
|
|
|
|
}
|
|
|
|
NodeId getSrc(NodeId Dst) const {
|
|
|
|
if (hasDst(Dst))
|
|
|
|
return DstToSrc[Dst][0];
|
|
|
|
return NodeId();
|
|
|
|
}
|
|
|
|
const SmallVector<NodeId, 2> &getAllDsts(NodeId Src) const {
|
|
|
|
return SrcToDst[Src];
|
|
|
|
}
|
|
|
|
const SmallVector<NodeId, 2> &getAllSrcs(NodeId Dst) const {
|
|
|
|
return DstToSrc[Dst];
|
|
|
|
}
|
|
|
|
bool hasSrc(NodeId Src) const { return !SrcToDst[Src].empty(); }
|
|
|
|
bool hasDst(NodeId Dst) const { return !DstToSrc[Dst].empty(); }
|
|
|
|
bool hasSrcDst(NodeId Src, NodeId Dst) const {
|
|
|
|
for (NodeId DstId : SrcToDst[Src])
|
|
|
|
if (DstId == Dst)
|
|
|
|
return true;
|
|
|
|
for (NodeId SrcId : DstToSrc[Dst])
|
|
|
|
if (SrcId == Src)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<SmallVector<NodeId, 2>[]> SrcToDst, DstToSrc;
|
|
|
|
};
|
2017-08-19 00:34:15 +08:00
|
|
|
} // end anonymous namespace
|
2017-08-02 04:17:40 +08:00
|
|
|
|
2017-07-21 20:49:28 +08:00
|
|
|
class ASTDiff::Impl {
|
|
|
|
public:
|
2017-08-02 04:17:46 +08:00
|
|
|
SyntaxTree::Impl &T1, &T2;
|
2017-07-21 20:49:28 +08:00
|
|
|
Mapping TheMapping;
|
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
Impl(SyntaxTree::Impl &T1, SyntaxTree::Impl &T2,
|
2017-08-19 05:26:34 +08:00
|
|
|
const ComparisonOptions &Options);
|
2017-07-21 20:49:28 +08:00
|
|
|
|
|
|
|
/// Matches nodes one-by-one based on their similarity.
|
|
|
|
void computeMapping();
|
|
|
|
|
2017-08-19 05:26:34 +08:00
|
|
|
// Compute ChangeKind for each node based on similarity.
|
|
|
|
void computeChangeKinds(Mapping &M);
|
2017-07-21 20:49:28 +08:00
|
|
|
|
2017-08-19 05:26:34 +08:00
|
|
|
NodeId getMapped(const std::unique_ptr<SyntaxTree::Impl> &Tree,
|
|
|
|
NodeId Id) const {
|
|
|
|
if (&*Tree == &T1)
|
|
|
|
return TheMapping.getDst(Id);
|
|
|
|
assert(&*Tree == &T2 && "Invalid tree.");
|
|
|
|
return TheMapping.getSrc(Id);
|
|
|
|
}
|
2017-07-21 20:49:28 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Returns true if the two subtrees are identical.
|
2017-08-02 04:17:46 +08:00
|
|
|
bool identical(NodeId Id1, NodeId Id2) const;
|
2017-07-21 20:49:28 +08:00
|
|
|
|
|
|
|
bool canBeAddedToMapping(const Mapping &M, NodeId Id1, NodeId Id2) const;
|
|
|
|
|
|
|
|
// Returns false if the nodes must not be mached.
|
|
|
|
bool isMatchingPossible(NodeId Id1, NodeId Id2) const;
|
|
|
|
|
2017-08-19 05:26:34 +08:00
|
|
|
// Returns true if the nodes' parents are matched.
|
|
|
|
bool haveSameParents(const Mapping &M, NodeId Id1, NodeId Id2) const;
|
|
|
|
|
2017-07-21 20:49:28 +08:00
|
|
|
// Uses an optimal albeit slow algorithm to compute a mapping between two
|
|
|
|
// subtrees, but only if both have fewer nodes than MaxSize.
|
|
|
|
void addOptimalMapping(Mapping &M, NodeId Id1, NodeId Id2) const;
|
|
|
|
|
|
|
|
// Computes the ratio of common descendants between the two nodes.
|
|
|
|
// Descendants are only considered to be equal when they are mapped in M.
|
|
|
|
double getSimilarity(const Mapping &M, NodeId Id1, NodeId Id2) const;
|
|
|
|
|
|
|
|
// Returns the node that has the highest degree of similarity.
|
|
|
|
NodeId findCandidate(const Mapping &M, NodeId Id1) const;
|
|
|
|
|
2017-08-19 05:26:34 +08:00
|
|
|
// Returns a mapping of identical subtrees.
|
|
|
|
Mapping matchTopDown() const;
|
|
|
|
|
2017-07-21 20:49:28 +08:00
|
|
|
// Tries to match any yet unmapped nodes, in a bottom-up fashion.
|
|
|
|
void matchBottomUp(Mapping &M) const;
|
|
|
|
|
|
|
|
const ComparisonOptions &Options;
|
|
|
|
|
|
|
|
friend class ZhangShashaMatcher;
|
|
|
|
};
|
|
|
|
|
2017-08-02 04:17:40 +08:00
|
|
|
/// Represents the AST of a TranslationUnit.
|
2017-08-02 04:17:46 +08:00
|
|
|
class SyntaxTree::Impl {
|
2017-08-02 04:17:40 +08:00
|
|
|
public:
|
|
|
|
/// Constructs a tree from the entire translation unit.
|
2017-08-02 04:17:46 +08:00
|
|
|
Impl(SyntaxTree *Parent, const ASTContext &AST);
|
2017-08-02 04:17:40 +08:00
|
|
|
/// Constructs a tree from an AST node.
|
2017-08-02 04:17:46 +08:00
|
|
|
Impl(SyntaxTree *Parent, Decl *N, const ASTContext &AST);
|
|
|
|
Impl(SyntaxTree *Parent, Stmt *N, const ASTContext &AST);
|
2017-08-02 04:17:40 +08:00
|
|
|
template <class T>
|
2017-08-02 04:17:46 +08:00
|
|
|
Impl(SyntaxTree *Parent,
|
|
|
|
typename std::enable_if<std::is_base_of<Stmt, T>::value, T>::type *Node,
|
|
|
|
const ASTContext &AST)
|
|
|
|
: Impl(Parent, dyn_cast<Stmt>(Node), AST) {}
|
2017-08-02 04:17:40 +08:00
|
|
|
template <class T>
|
2017-08-02 04:17:46 +08:00
|
|
|
Impl(SyntaxTree *Parent,
|
|
|
|
typename std::enable_if<std::is_base_of<Decl, T>::value, T>::type *Node,
|
|
|
|
const ASTContext &AST)
|
|
|
|
: Impl(Parent, dyn_cast<Decl>(Node), AST) {}
|
2017-08-02 04:17:40 +08:00
|
|
|
|
|
|
|
SyntaxTree *Parent;
|
|
|
|
const ASTContext &AST;
|
|
|
|
std::vector<NodeId> Leaves;
|
|
|
|
// Maps preorder indices to postorder ones.
|
|
|
|
std::vector<int> PostorderIds;
|
2017-08-19 05:26:34 +08:00
|
|
|
std::vector<NodeId> NodesBfs;
|
2017-08-02 04:17:40 +08:00
|
|
|
|
|
|
|
int getSize() const { return Nodes.size(); }
|
2017-08-02 04:17:46 +08:00
|
|
|
NodeId getRootId() const { return 0; }
|
2017-08-19 05:26:34 +08:00
|
|
|
PreorderIterator begin() const { return getRootId(); }
|
|
|
|
PreorderIterator end() const { return getSize(); }
|
2017-08-02 04:17:40 +08:00
|
|
|
|
|
|
|
const Node &getNode(NodeId Id) const { return Nodes[Id]; }
|
|
|
|
Node &getMutableNode(NodeId Id) { return Nodes[Id]; }
|
|
|
|
bool isValidNodeId(NodeId Id) const { return Id >= 0 && Id < getSize(); }
|
|
|
|
void addNode(Node &N) { Nodes.push_back(N); }
|
|
|
|
int getNumberOfDescendants(NodeId Id) const;
|
|
|
|
bool isInSubtree(NodeId Id, NodeId SubtreeRoot) const;
|
2017-08-19 05:26:34 +08:00
|
|
|
int findPositionInParent(NodeId Id, bool Shifted = false) const;
|
2017-08-02 04:17:40 +08:00
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
std::string getNodeValue(NodeId Id) const;
|
2017-08-19 05:26:34 +08:00
|
|
|
std::string getNodeValue(const Node &Node) const;
|
2017-08-02 04:17:40 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Nodes in preorder.
|
|
|
|
std::vector<Node> Nodes;
|
|
|
|
|
|
|
|
void initTree();
|
|
|
|
void setLeftMostDescendants();
|
|
|
|
};
|
|
|
|
|
2017-07-21 20:49:28 +08:00
|
|
|
template <class T>
|
|
|
|
static bool isNodeExcluded(const SourceManager &SrcMgr, T *N) {
|
|
|
|
if (!N)
|
|
|
|
return true;
|
|
|
|
SourceLocation SLoc = N->getLocStart();
|
|
|
|
return SLoc.isValid() && SrcMgr.isInSystemHeader(SLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
/// Counts the number of nodes that will be compared.
|
|
|
|
struct NodeCountVisitor : public RecursiveASTVisitor<NodeCountVisitor> {
|
|
|
|
int Count = 0;
|
2017-08-02 04:17:46 +08:00
|
|
|
const SyntaxTree::Impl &Tree;
|
|
|
|
NodeCountVisitor(const SyntaxTree::Impl &Tree) : Tree(Tree) {}
|
2017-07-21 20:49:28 +08:00
|
|
|
bool TraverseDecl(Decl *D) {
|
2017-08-02 04:17:46 +08:00
|
|
|
if (isNodeExcluded(Tree.AST.getSourceManager(), D))
|
2017-07-21 20:49:28 +08:00
|
|
|
return true;
|
|
|
|
++Count;
|
|
|
|
RecursiveASTVisitor<NodeCountVisitor>::TraverseDecl(D);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool TraverseStmt(Stmt *S) {
|
2017-08-02 04:17:46 +08:00
|
|
|
if (isNodeExcluded(Tree.AST.getSourceManager(), S))
|
2017-07-21 20:49:28 +08:00
|
|
|
return true;
|
|
|
|
++Count;
|
|
|
|
RecursiveASTVisitor<NodeCountVisitor>::TraverseStmt(S);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool TraverseType(QualType T) { return true; }
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
// Sets Height, Parent and Children for each node.
|
|
|
|
struct PreorderVisitor : public RecursiveASTVisitor<PreorderVisitor> {
|
|
|
|
int Id = 0, Depth = 0;
|
|
|
|
NodeId Parent;
|
2017-08-02 04:17:46 +08:00
|
|
|
SyntaxTree::Impl &Tree;
|
2017-07-21 20:49:28 +08:00
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
PreorderVisitor(SyntaxTree::Impl &Tree) : Tree(Tree) {}
|
2017-07-21 20:49:28 +08:00
|
|
|
|
|
|
|
template <class T> std::tuple<NodeId, NodeId> PreTraverse(T *ASTNode) {
|
|
|
|
NodeId MyId = Id;
|
2017-08-02 04:17:46 +08:00
|
|
|
Node &N = Tree.getMutableNode(MyId);
|
2017-07-21 20:49:28 +08:00
|
|
|
N.Parent = Parent;
|
|
|
|
N.Depth = Depth;
|
|
|
|
N.ASTNode = DynTypedNode::create(*ASTNode);
|
|
|
|
assert(!N.ASTNode.getNodeKind().isNone() &&
|
|
|
|
"Expected nodes to have a valid kind.");
|
|
|
|
if (Parent.isValid()) {
|
2017-08-02 04:17:46 +08:00
|
|
|
Node &P = Tree.getMutableNode(Parent);
|
2017-07-21 20:49:28 +08:00
|
|
|
P.Children.push_back(MyId);
|
|
|
|
}
|
|
|
|
Parent = MyId;
|
|
|
|
++Id;
|
|
|
|
++Depth;
|
2017-08-02 04:17:46 +08:00
|
|
|
return std::make_tuple(MyId, Tree.getNode(MyId).Parent);
|
2017-07-21 20:49:28 +08:00
|
|
|
}
|
|
|
|
void PostTraverse(std::tuple<NodeId, NodeId> State) {
|
|
|
|
NodeId MyId, PreviousParent;
|
|
|
|
std::tie(MyId, PreviousParent) = State;
|
|
|
|
assert(MyId.isValid() && "Expecting to only traverse valid nodes.");
|
|
|
|
Parent = PreviousParent;
|
|
|
|
--Depth;
|
2017-08-02 04:17:46 +08:00
|
|
|
Node &N = Tree.getMutableNode(MyId);
|
2017-08-19 00:34:15 +08:00
|
|
|
N.RightMostDescendant = Id - 1;
|
|
|
|
assert(N.RightMostDescendant >= 0 &&
|
|
|
|
N.RightMostDescendant < Tree.getSize() &&
|
|
|
|
"Rightmost descendant must be a valid tree node.");
|
2017-07-21 20:49:28 +08:00
|
|
|
if (N.isLeaf())
|
2017-08-02 04:17:46 +08:00
|
|
|
Tree.Leaves.push_back(MyId);
|
2017-07-21 20:49:28 +08:00
|
|
|
N.Height = 1;
|
|
|
|
for (NodeId Child : N.Children)
|
2017-08-02 04:17:46 +08:00
|
|
|
N.Height = std::max(N.Height, 1 + Tree.getNode(Child).Height);
|
2017-07-21 20:49:28 +08:00
|
|
|
}
|
|
|
|
bool TraverseDecl(Decl *D) {
|
2017-08-02 04:17:46 +08:00
|
|
|
if (isNodeExcluded(Tree.AST.getSourceManager(), D))
|
2017-07-21 20:49:28 +08:00
|
|
|
return true;
|
|
|
|
auto SavedState = PreTraverse(D);
|
|
|
|
RecursiveASTVisitor<PreorderVisitor>::TraverseDecl(D);
|
|
|
|
PostTraverse(SavedState);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool TraverseStmt(Stmt *S) {
|
2017-08-02 04:17:46 +08:00
|
|
|
if (isNodeExcluded(Tree.AST.getSourceManager(), S))
|
2017-07-21 20:49:28 +08:00
|
|
|
return true;
|
|
|
|
auto SavedState = PreTraverse(S);
|
|
|
|
RecursiveASTVisitor<PreorderVisitor>::TraverseStmt(S);
|
|
|
|
PostTraverse(SavedState);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool TraverseType(QualType T) { return true; }
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
SyntaxTree::Impl::Impl(SyntaxTree *Parent, const ASTContext &AST)
|
|
|
|
: Impl(Parent, AST.getTranslationUnitDecl(), AST) {}
|
2017-07-21 20:49:28 +08:00
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
SyntaxTree::Impl::Impl(SyntaxTree *Parent, Decl *N, const ASTContext &AST)
|
2017-07-21 20:49:28 +08:00
|
|
|
: Parent(Parent), AST(AST) {
|
|
|
|
NodeCountVisitor NodeCounter(*this);
|
|
|
|
NodeCounter.TraverseDecl(N);
|
|
|
|
Nodes.resize(NodeCounter.Count);
|
|
|
|
PreorderVisitor PreorderWalker(*this);
|
|
|
|
PreorderWalker.TraverseDecl(N);
|
|
|
|
initTree();
|
|
|
|
}
|
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
SyntaxTree::Impl::Impl(SyntaxTree *Parent, Stmt *N, const ASTContext &AST)
|
2017-07-21 20:49:28 +08:00
|
|
|
: Parent(Parent), AST(AST) {
|
|
|
|
NodeCountVisitor NodeCounter(*this);
|
|
|
|
NodeCounter.TraverseStmt(N);
|
|
|
|
Nodes.resize(NodeCounter.Count);
|
|
|
|
PreorderVisitor PreorderWalker(*this);
|
|
|
|
PreorderWalker.TraverseStmt(N);
|
|
|
|
initTree();
|
|
|
|
}
|
|
|
|
|
2017-08-19 05:26:34 +08:00
|
|
|
static std::vector<NodeId> getSubtreePostorder(const SyntaxTree::Impl &Tree,
|
|
|
|
NodeId Root) {
|
|
|
|
std::vector<NodeId> Postorder;
|
|
|
|
std::function<void(NodeId)> Traverse = [&](NodeId Id) {
|
|
|
|
const Node &N = Tree.getNode(Id);
|
|
|
|
for (NodeId Child : N.Children)
|
|
|
|
Traverse(Child);
|
|
|
|
Postorder.push_back(Id);
|
|
|
|
};
|
|
|
|
Traverse(Root);
|
|
|
|
return Postorder;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::vector<NodeId> getSubtreeBfs(const SyntaxTree::Impl &Tree,
|
|
|
|
NodeId Root) {
|
|
|
|
std::vector<NodeId> Ids;
|
|
|
|
size_t Expanded = 0;
|
|
|
|
Ids.push_back(Root);
|
|
|
|
while (Expanded < Ids.size())
|
|
|
|
for (NodeId Child : Tree.getNode(Ids[Expanded++]).Children)
|
|
|
|
Ids.push_back(Child);
|
|
|
|
return Ids;
|
|
|
|
}
|
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
void SyntaxTree::Impl::initTree() {
|
2017-07-21 20:49:28 +08:00
|
|
|
setLeftMostDescendants();
|
|
|
|
int PostorderId = 0;
|
|
|
|
PostorderIds.resize(getSize());
|
|
|
|
std::function<void(NodeId)> PostorderTraverse = [&](NodeId Id) {
|
|
|
|
for (NodeId Child : getNode(Id).Children)
|
|
|
|
PostorderTraverse(Child);
|
|
|
|
PostorderIds[Id] = PostorderId;
|
|
|
|
++PostorderId;
|
|
|
|
};
|
2017-08-02 04:17:46 +08:00
|
|
|
PostorderTraverse(getRootId());
|
2017-08-19 05:26:34 +08:00
|
|
|
NodesBfs = getSubtreeBfs(*this, getRootId());
|
2017-07-21 20:49:28 +08:00
|
|
|
}
|
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
void SyntaxTree::Impl::setLeftMostDescendants() {
|
2017-07-21 20:49:28 +08:00
|
|
|
for (NodeId Leaf : Leaves) {
|
|
|
|
getMutableNode(Leaf).LeftMostDescendant = Leaf;
|
|
|
|
NodeId Parent, Cur = Leaf;
|
|
|
|
while ((Parent = getNode(Cur).Parent).isValid() &&
|
|
|
|
getNode(Parent).Children[0] == Cur) {
|
|
|
|
Cur = Parent;
|
|
|
|
getMutableNode(Cur).LeftMostDescendant = Leaf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
int SyntaxTree::Impl::getNumberOfDescendants(NodeId Id) const {
|
2017-07-21 20:49:28 +08:00
|
|
|
return getNode(Id).RightMostDescendant - Id + 1;
|
|
|
|
}
|
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
bool SyntaxTree::Impl::isInSubtree(NodeId Id, NodeId SubtreeRoot) const {
|
2017-08-19 00:34:15 +08:00
|
|
|
return Id >= SubtreeRoot && Id <= getNode(SubtreeRoot).RightMostDescendant;
|
2017-07-21 20:49:28 +08:00
|
|
|
}
|
|
|
|
|
2017-08-19 05:26:34 +08:00
|
|
|
int SyntaxTree::Impl::findPositionInParent(NodeId Id, bool Shifted) const {
|
|
|
|
NodeId Parent = getNode(Id).Parent;
|
|
|
|
if (Parent.isInvalid())
|
|
|
|
return 0;
|
|
|
|
const auto &Siblings = getNode(Parent).Children;
|
|
|
|
int Position = 0;
|
|
|
|
for (size_t I = 0, E = Siblings.size(); I < E; ++I) {
|
|
|
|
if (Shifted)
|
|
|
|
Position += getNode(Siblings[I]).Shift;
|
|
|
|
if (Siblings[I] == Id) {
|
|
|
|
Position += I;
|
|
|
|
return Position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm_unreachable("Node not found in parent's children.");
|
|
|
|
}
|
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
std::string SyntaxTree::Impl::getNodeValue(NodeId Id) const {
|
2017-08-19 05:26:34 +08:00
|
|
|
return getNodeValue(getNode(Id));
|
2017-07-21 20:49:28 +08:00
|
|
|
}
|
|
|
|
|
2017-08-19 05:26:34 +08:00
|
|
|
std::string SyntaxTree::Impl::getNodeValue(const Node &N) const {
|
|
|
|
const DynTypedNode &DTN = N.ASTNode;
|
2017-07-21 20:49:28 +08:00
|
|
|
if (auto *X = DTN.get<BinaryOperator>())
|
|
|
|
return X->getOpcodeStr();
|
|
|
|
if (auto *X = DTN.get<AccessSpecDecl>()) {
|
|
|
|
CharSourceRange Range(X->getSourceRange(), false);
|
|
|
|
return Lexer::getSourceText(Range, AST.getSourceManager(),
|
|
|
|
AST.getLangOpts());
|
|
|
|
}
|
|
|
|
if (auto *X = DTN.get<IntegerLiteral>()) {
|
|
|
|
SmallString<256> Str;
|
|
|
|
X->getValue().toString(Str, /*Radix=*/10, /*Signed=*/false);
|
|
|
|
return Str.str();
|
|
|
|
}
|
|
|
|
if (auto *X = DTN.get<StringLiteral>())
|
|
|
|
return X->getString();
|
|
|
|
if (auto *X = DTN.get<ValueDecl>())
|
|
|
|
return X->getNameAsString() + "(" + X->getType().getAsString() + ")";
|
2017-07-21 21:18:51 +08:00
|
|
|
if (DTN.get<DeclStmt>() || DTN.get<TranslationUnitDecl>())
|
2017-07-21 20:49:28 +08:00
|
|
|
return "";
|
|
|
|
std::string Value;
|
|
|
|
if (auto *X = DTN.get<DeclRefExpr>()) {
|
|
|
|
if (X->hasQualifier()) {
|
|
|
|
llvm::raw_string_ostream OS(Value);
|
|
|
|
PrintingPolicy PP(AST.getLangOpts());
|
|
|
|
X->getQualifier()->print(OS, PP);
|
|
|
|
}
|
|
|
|
Value += X->getDecl()->getNameAsString();
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
if (auto *X = DTN.get<NamedDecl>())
|
|
|
|
Value += X->getNameAsString() + ";";
|
|
|
|
if (auto *X = DTN.get<TypedefNameDecl>())
|
|
|
|
return Value + X->getUnderlyingType().getAsString() + ";";
|
2017-07-21 21:18:51 +08:00
|
|
|
if (DTN.get<NamespaceDecl>())
|
2017-07-21 20:49:28 +08:00
|
|
|
return Value;
|
|
|
|
if (auto *X = DTN.get<TypeDecl>())
|
|
|
|
if (X->getTypeForDecl())
|
|
|
|
Value +=
|
|
|
|
X->getTypeForDecl()->getCanonicalTypeInternal().getAsString() + ";";
|
2017-07-21 21:18:51 +08:00
|
|
|
if (DTN.get<Decl>())
|
2017-07-21 20:49:28 +08:00
|
|
|
return Value;
|
2017-07-21 21:18:51 +08:00
|
|
|
if (DTN.get<Stmt>())
|
2017-07-21 20:49:28 +08:00
|
|
|
return "";
|
|
|
|
llvm_unreachable("Fatal: unhandled AST node.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Identifies a node in a subtree by its postorder offset, starting at 1.
|
|
|
|
struct SNodeId {
|
|
|
|
int Id = 0;
|
|
|
|
|
|
|
|
explicit SNodeId(int Id) : Id(Id) {}
|
|
|
|
explicit SNodeId() = default;
|
|
|
|
|
|
|
|
operator int() const { return Id; }
|
|
|
|
SNodeId &operator++() { return ++Id, *this; }
|
|
|
|
SNodeId &operator--() { return --Id, *this; }
|
|
|
|
SNodeId operator+(int Other) const { return SNodeId(Id + Other); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class Subtree {
|
|
|
|
private:
|
|
|
|
/// The parent tree.
|
2017-08-02 04:17:46 +08:00
|
|
|
const SyntaxTree::Impl &Tree;
|
2017-07-21 20:49:28 +08:00
|
|
|
/// Maps SNodeIds to original ids.
|
|
|
|
std::vector<NodeId> RootIds;
|
|
|
|
/// Maps subtree nodes to their leftmost descendants wtihin the subtree.
|
|
|
|
std::vector<SNodeId> LeftMostDescendants;
|
|
|
|
|
|
|
|
public:
|
|
|
|
std::vector<SNodeId> KeyRoots;
|
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
Subtree(const SyntaxTree::Impl &Tree, NodeId SubtreeRoot) : Tree(Tree) {
|
2017-07-21 20:49:28 +08:00
|
|
|
RootIds = getSubtreePostorder(Tree, SubtreeRoot);
|
|
|
|
int NumLeaves = setLeftMostDescendants();
|
|
|
|
computeKeyRoots(NumLeaves);
|
|
|
|
}
|
|
|
|
int getSize() const { return RootIds.size(); }
|
|
|
|
NodeId getIdInRoot(SNodeId Id) const {
|
|
|
|
assert(Id > 0 && Id <= getSize() && "Invalid subtree node index.");
|
|
|
|
return RootIds[Id - 1];
|
|
|
|
}
|
|
|
|
const Node &getNode(SNodeId Id) const {
|
|
|
|
return Tree.getNode(getIdInRoot(Id));
|
|
|
|
}
|
|
|
|
SNodeId getLeftMostDescendant(SNodeId Id) const {
|
|
|
|
assert(Id > 0 && Id <= getSize() && "Invalid subtree node index.");
|
|
|
|
return LeftMostDescendants[Id - 1];
|
|
|
|
}
|
|
|
|
/// Returns the postorder index of the leftmost descendant in the subtree.
|
|
|
|
NodeId getPostorderOffset() const {
|
|
|
|
return Tree.PostorderIds[getIdInRoot(SNodeId(1))];
|
|
|
|
}
|
2017-08-02 04:17:40 +08:00
|
|
|
std::string getNodeValue(SNodeId Id) const {
|
2017-08-02 04:17:46 +08:00
|
|
|
return Tree.getNodeValue(getIdInRoot(Id));
|
2017-08-02 04:17:40 +08:00
|
|
|
}
|
2017-07-21 20:49:28 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Returns the number of leafs in the subtree.
|
|
|
|
int setLeftMostDescendants() {
|
|
|
|
int NumLeaves = 0;
|
|
|
|
LeftMostDescendants.resize(getSize());
|
|
|
|
for (int I = 0; I < getSize(); ++I) {
|
|
|
|
SNodeId SI(I + 1);
|
|
|
|
const Node &N = getNode(SI);
|
|
|
|
NumLeaves += N.isLeaf();
|
|
|
|
assert(I == Tree.PostorderIds[getIdInRoot(SI)] - getPostorderOffset() &&
|
|
|
|
"Postorder traversal in subtree should correspond to traversal in "
|
|
|
|
"the root tree by a constant offset.");
|
|
|
|
LeftMostDescendants[I] = SNodeId(Tree.PostorderIds[N.LeftMostDescendant] -
|
|
|
|
getPostorderOffset());
|
|
|
|
}
|
|
|
|
return NumLeaves;
|
|
|
|
}
|
|
|
|
void computeKeyRoots(int Leaves) {
|
|
|
|
KeyRoots.resize(Leaves);
|
|
|
|
std::unordered_set<int> Visited;
|
|
|
|
int K = Leaves - 1;
|
|
|
|
for (SNodeId I(getSize()); I > 0; --I) {
|
|
|
|
SNodeId LeftDesc = getLeftMostDescendant(I);
|
|
|
|
if (Visited.count(LeftDesc))
|
|
|
|
continue;
|
|
|
|
assert(K >= 0 && "K should be non-negative");
|
|
|
|
KeyRoots[K] = I;
|
|
|
|
Visited.insert(LeftDesc);
|
|
|
|
--K;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Implementation of Zhang and Shasha's Algorithm for tree edit distance.
|
|
|
|
/// Computes an optimal mapping between two trees using only insertion,
|
|
|
|
/// deletion and update as edit actions (similar to the Levenshtein distance).
|
|
|
|
class ZhangShashaMatcher {
|
|
|
|
const ASTDiff::Impl &DiffImpl;
|
|
|
|
Subtree S1;
|
|
|
|
Subtree S2;
|
|
|
|
std::unique_ptr<std::unique_ptr<double[]>[]> TreeDist, ForestDist;
|
|
|
|
|
|
|
|
public:
|
2017-08-02 04:17:46 +08:00
|
|
|
ZhangShashaMatcher(const ASTDiff::Impl &DiffImpl, const SyntaxTree::Impl &T1,
|
|
|
|
const SyntaxTree::Impl &T2, NodeId Id1, NodeId Id2)
|
2017-07-21 20:49:28 +08:00
|
|
|
: DiffImpl(DiffImpl), S1(T1, Id1), S2(T2, Id2) {
|
|
|
|
TreeDist = llvm::make_unique<std::unique_ptr<double[]>[]>(
|
|
|
|
size_t(S1.getSize()) + 1);
|
|
|
|
ForestDist = llvm::make_unique<std::unique_ptr<double[]>[]>(
|
|
|
|
size_t(S1.getSize()) + 1);
|
|
|
|
for (int I = 0, E = S1.getSize() + 1; I < E; ++I) {
|
|
|
|
TreeDist[I] = llvm::make_unique<double[]>(size_t(S2.getSize()) + 1);
|
|
|
|
ForestDist[I] = llvm::make_unique<double[]>(size_t(S2.getSize()) + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<NodeId, NodeId>> getMatchingNodes() {
|
|
|
|
std::vector<std::pair<NodeId, NodeId>> Matches;
|
|
|
|
std::vector<std::pair<SNodeId, SNodeId>> TreePairs;
|
|
|
|
|
|
|
|
computeTreeDist();
|
|
|
|
|
|
|
|
bool RootNodePair = true;
|
|
|
|
|
2017-07-21 21:04:57 +08:00
|
|
|
TreePairs.emplace_back(SNodeId(S1.getSize()), SNodeId(S2.getSize()));
|
2017-07-21 20:49:28 +08:00
|
|
|
|
|
|
|
while (!TreePairs.empty()) {
|
|
|
|
SNodeId LastRow, LastCol, FirstRow, FirstCol, Row, Col;
|
|
|
|
std::tie(LastRow, LastCol) = TreePairs.back();
|
|
|
|
TreePairs.pop_back();
|
|
|
|
|
|
|
|
if (!RootNodePair) {
|
|
|
|
computeForestDist(LastRow, LastCol);
|
|
|
|
}
|
|
|
|
|
|
|
|
RootNodePair = false;
|
|
|
|
|
|
|
|
FirstRow = S1.getLeftMostDescendant(LastRow);
|
|
|
|
FirstCol = S2.getLeftMostDescendant(LastCol);
|
|
|
|
|
|
|
|
Row = LastRow;
|
|
|
|
Col = LastCol;
|
|
|
|
|
|
|
|
while (Row > FirstRow || Col > FirstCol) {
|
|
|
|
if (Row > FirstRow &&
|
|
|
|
ForestDist[Row - 1][Col] + 1 == ForestDist[Row][Col]) {
|
|
|
|
--Row;
|
|
|
|
} else if (Col > FirstCol &&
|
|
|
|
ForestDist[Row][Col - 1] + 1 == ForestDist[Row][Col]) {
|
|
|
|
--Col;
|
|
|
|
} else {
|
|
|
|
SNodeId LMD1 = S1.getLeftMostDescendant(Row);
|
|
|
|
SNodeId LMD2 = S2.getLeftMostDescendant(Col);
|
|
|
|
if (LMD1 == S1.getLeftMostDescendant(LastRow) &&
|
|
|
|
LMD2 == S2.getLeftMostDescendant(LastCol)) {
|
|
|
|
NodeId Id1 = S1.getIdInRoot(Row);
|
|
|
|
NodeId Id2 = S2.getIdInRoot(Col);
|
|
|
|
assert(DiffImpl.isMatchingPossible(Id1, Id2) &&
|
|
|
|
"These nodes must not be matched.");
|
|
|
|
Matches.emplace_back(Id1, Id2);
|
|
|
|
--Row;
|
|
|
|
--Col;
|
|
|
|
} else {
|
|
|
|
TreePairs.emplace_back(Row, Col);
|
|
|
|
Row = LMD1;
|
|
|
|
Col = LMD2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-08-19 00:34:15 +08:00
|
|
|
/// We use a simple cost model for edit actions, which seems good enough.
|
|
|
|
/// Simple cost model for edit actions. This seems to make the matching
|
|
|
|
/// algorithm perform reasonably well.
|
2017-07-21 20:49:28 +08:00
|
|
|
/// The values range between 0 and 1, or infinity if this edit action should
|
|
|
|
/// always be avoided.
|
|
|
|
static constexpr double DeletionCost = 1;
|
|
|
|
static constexpr double InsertionCost = 1;
|
|
|
|
|
|
|
|
double getUpdateCost(SNodeId Id1, SNodeId Id2) {
|
2017-08-02 04:17:40 +08:00
|
|
|
if (!DiffImpl.isMatchingPossible(S1.getIdInRoot(Id1), S2.getIdInRoot(Id2)))
|
2017-07-21 20:49:28 +08:00
|
|
|
return std::numeric_limits<double>::max();
|
2017-08-02 04:17:40 +08:00
|
|
|
return S1.getNodeValue(Id1) != S2.getNodeValue(Id2);
|
2017-07-21 20:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void computeTreeDist() {
|
|
|
|
for (SNodeId Id1 : S1.KeyRoots)
|
|
|
|
for (SNodeId Id2 : S2.KeyRoots)
|
|
|
|
computeForestDist(Id1, Id2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void computeForestDist(SNodeId Id1, SNodeId Id2) {
|
|
|
|
assert(Id1 > 0 && Id2 > 0 && "Expecting offsets greater than 0.");
|
|
|
|
SNodeId LMD1 = S1.getLeftMostDescendant(Id1);
|
|
|
|
SNodeId LMD2 = S2.getLeftMostDescendant(Id2);
|
|
|
|
|
|
|
|
ForestDist[LMD1][LMD2] = 0;
|
|
|
|
for (SNodeId D1 = LMD1 + 1; D1 <= Id1; ++D1) {
|
|
|
|
ForestDist[D1][LMD2] = ForestDist[D1 - 1][LMD2] + DeletionCost;
|
|
|
|
for (SNodeId D2 = LMD2 + 1; D2 <= Id2; ++D2) {
|
|
|
|
ForestDist[LMD1][D2] = ForestDist[LMD1][D2 - 1] + InsertionCost;
|
|
|
|
SNodeId DLMD1 = S1.getLeftMostDescendant(D1);
|
|
|
|
SNodeId DLMD2 = S2.getLeftMostDescendant(D2);
|
|
|
|
if (DLMD1 == LMD1 && DLMD2 == LMD2) {
|
|
|
|
double UpdateCost = getUpdateCost(D1, D2);
|
|
|
|
ForestDist[D1][D2] =
|
|
|
|
std::min({ForestDist[D1 - 1][D2] + DeletionCost,
|
|
|
|
ForestDist[D1][D2 - 1] + InsertionCost,
|
|
|
|
ForestDist[D1 - 1][D2 - 1] + UpdateCost});
|
|
|
|
TreeDist[D1][D2] = ForestDist[D1][D2];
|
|
|
|
} else {
|
|
|
|
ForestDist[D1][D2] =
|
|
|
|
std::min({ForestDist[D1 - 1][D2] + DeletionCost,
|
|
|
|
ForestDist[D1][D2 - 1] + InsertionCost,
|
|
|
|
ForestDist[DLMD1][DLMD2] + TreeDist[D1][D2]});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-19 05:26:13 +08:00
|
|
|
ast_type_traits::ASTNodeKind Node::getType() const {
|
|
|
|
return ASTNode.getNodeKind();
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef Node::getTypeLabel() const { return getType().asStringRef(); }
|
|
|
|
|
2017-07-21 20:49:28 +08:00
|
|
|
namespace {
|
|
|
|
// Compares nodes by their depth.
|
|
|
|
struct HeightLess {
|
2017-08-02 04:17:46 +08:00
|
|
|
const SyntaxTree::Impl &Tree;
|
|
|
|
HeightLess(const SyntaxTree::Impl &Tree) : Tree(Tree) {}
|
2017-07-21 20:49:28 +08:00
|
|
|
bool operator()(NodeId Id1, NodeId Id2) const {
|
|
|
|
return Tree.getNode(Id1).Height < Tree.getNode(Id2).Height;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2017-08-19 00:34:15 +08:00
|
|
|
namespace {
|
2017-07-21 20:49:28 +08:00
|
|
|
// Priority queue for nodes, sorted descendingly by their height.
|
|
|
|
class PriorityList {
|
2017-08-02 04:17:46 +08:00
|
|
|
const SyntaxTree::Impl &Tree;
|
2017-07-21 20:49:28 +08:00
|
|
|
HeightLess Cmp;
|
|
|
|
std::vector<NodeId> Container;
|
|
|
|
PriorityQueue<NodeId, std::vector<NodeId>, HeightLess> List;
|
|
|
|
|
|
|
|
public:
|
2017-08-02 04:17:46 +08:00
|
|
|
PriorityList(const SyntaxTree::Impl &Tree)
|
2017-07-21 20:49:28 +08:00
|
|
|
: Tree(Tree), Cmp(Tree), List(Cmp, Container) {}
|
|
|
|
|
|
|
|
void push(NodeId id) { List.push(id); }
|
|
|
|
|
|
|
|
std::vector<NodeId> pop() {
|
|
|
|
int Max = peekMax();
|
|
|
|
std::vector<NodeId> Result;
|
|
|
|
if (Max == 0)
|
|
|
|
return Result;
|
|
|
|
while (peekMax() == Max) {
|
|
|
|
Result.push_back(List.top());
|
|
|
|
List.pop();
|
|
|
|
}
|
|
|
|
// TODO this is here to get a stable output, not a good heuristic
|
|
|
|
std::sort(Result.begin(), Result.end());
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
int peekMax() const {
|
|
|
|
if (List.empty())
|
|
|
|
return 0;
|
|
|
|
return Tree.getNode(List.top()).Height;
|
|
|
|
}
|
|
|
|
void open(NodeId Id) {
|
|
|
|
for (NodeId Child : Tree.getNode(Id).Children)
|
|
|
|
push(Child);
|
|
|
|
}
|
|
|
|
};
|
2017-08-19 00:34:15 +08:00
|
|
|
} // end anonymous namespace
|
2017-07-21 20:49:28 +08:00
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
bool ASTDiff::Impl::identical(NodeId Id1, NodeId Id2) const {
|
2017-07-21 20:49:28 +08:00
|
|
|
const Node &N1 = T1.getNode(Id1);
|
|
|
|
const Node &N2 = T2.getNode(Id2);
|
|
|
|
if (N1.Children.size() != N2.Children.size() ||
|
|
|
|
!isMatchingPossible(Id1, Id2) ||
|
2017-08-02 04:17:46 +08:00
|
|
|
T1.getNodeValue(Id1) != T2.getNodeValue(Id2))
|
2017-07-21 20:49:28 +08:00
|
|
|
return false;
|
|
|
|
for (size_t Id = 0, E = N1.Children.size(); Id < E; ++Id)
|
2017-08-02 04:17:46 +08:00
|
|
|
if (!identical(N1.Children[Id], N2.Children[Id]))
|
2017-07-21 20:49:28 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ASTDiff::Impl::canBeAddedToMapping(const Mapping &M, NodeId Id1,
|
|
|
|
NodeId Id2) const {
|
|
|
|
assert(isMatchingPossible(Id1, Id2) &&
|
|
|
|
"Matching must be possible in the first place.");
|
|
|
|
if (M.hasSrcDst(Id1, Id2))
|
|
|
|
return false;
|
|
|
|
if (Options.EnableMatchingWithUnmatchableParents)
|
|
|
|
return true;
|
|
|
|
const Node &N1 = T1.getNode(Id1);
|
|
|
|
const Node &N2 = T2.getNode(Id2);
|
|
|
|
NodeId P1 = N1.Parent;
|
|
|
|
NodeId P2 = N2.Parent;
|
|
|
|
// Only allow matching if parents can be matched.
|
|
|
|
return (P1.isInvalid() && P2.isInvalid()) ||
|
|
|
|
(P1.isValid() && P2.isValid() && isMatchingPossible(P1, P2));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ASTDiff::Impl::isMatchingPossible(NodeId Id1, NodeId Id2) const {
|
2017-08-19 05:26:34 +08:00
|
|
|
return Options.isMatchingAllowed(T1.getNode(Id1), T2.getNode(Id2));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ASTDiff::Impl::haveSameParents(const Mapping &M, NodeId Id1,
|
|
|
|
NodeId Id2) const {
|
|
|
|
NodeId P1 = T1.getNode(Id1).Parent;
|
|
|
|
NodeId P2 = T2.getNode(Id2).Parent;
|
|
|
|
return (P1.isInvalid() && P2.isInvalid()) ||
|
|
|
|
(P1.isValid() && P2.isValid() && M.getDst(P1) == P2);
|
2017-07-21 20:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTDiff::Impl::addOptimalMapping(Mapping &M, NodeId Id1,
|
|
|
|
NodeId Id2) const {
|
|
|
|
if (std::max(T1.getNumberOfDescendants(Id1),
|
|
|
|
T2.getNumberOfDescendants(Id2)) >= Options.MaxSize)
|
|
|
|
return;
|
|
|
|
ZhangShashaMatcher Matcher(*this, T1, T2, Id1, Id2);
|
|
|
|
std::vector<std::pair<NodeId, NodeId>> R = Matcher.getMatchingNodes();
|
|
|
|
for (const auto Tuple : R) {
|
|
|
|
NodeId Src = Tuple.first;
|
|
|
|
NodeId Dst = Tuple.second;
|
|
|
|
if (canBeAddedToMapping(M, Src, Dst))
|
|
|
|
M.link(Src, Dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double ASTDiff::Impl::getSimilarity(const Mapping &M, NodeId Id1,
|
|
|
|
NodeId Id2) const {
|
|
|
|
if (Id1.isInvalid() || Id2.isInvalid())
|
|
|
|
return 0.0;
|
|
|
|
int CommonDescendants = 0;
|
|
|
|
const Node &N1 = T1.getNode(Id1);
|
|
|
|
for (NodeId Id = Id1 + 1; Id <= N1.RightMostDescendant; ++Id)
|
|
|
|
CommonDescendants += int(T2.isInSubtree(M.getDst(Id), Id2));
|
|
|
|
return 2.0 * CommonDescendants /
|
|
|
|
(T1.getNumberOfDescendants(Id1) + T2.getNumberOfDescendants(Id2));
|
|
|
|
}
|
|
|
|
|
|
|
|
NodeId ASTDiff::Impl::findCandidate(const Mapping &M, NodeId Id1) const {
|
|
|
|
NodeId Candidate;
|
2017-08-02 04:17:46 +08:00
|
|
|
double HighestSimilarity = 0.0;
|
2017-08-19 05:26:34 +08:00
|
|
|
for (NodeId Id2 : T2) {
|
2017-07-21 20:49:28 +08:00
|
|
|
if (!isMatchingPossible(Id1, Id2))
|
|
|
|
continue;
|
|
|
|
if (M.hasDst(Id2))
|
|
|
|
continue;
|
|
|
|
double Similarity = getSimilarity(M, Id1, Id2);
|
2017-08-19 00:34:15 +08:00
|
|
|
if (Similarity >= Options.MinSimilarity && Similarity > HighestSimilarity) {
|
2017-08-02 04:17:46 +08:00
|
|
|
HighestSimilarity = Similarity;
|
2017-07-21 20:49:28 +08:00
|
|
|
Candidate = Id2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Candidate;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ASTDiff::Impl::matchBottomUp(Mapping &M) const {
|
2017-08-02 04:17:46 +08:00
|
|
|
std::vector<NodeId> Postorder = getSubtreePostorder(T1, T1.getRootId());
|
2017-07-21 20:49:28 +08:00
|
|
|
for (NodeId Id1 : Postorder) {
|
2017-08-19 00:34:15 +08:00
|
|
|
if (Id1 == T1.getRootId() && !M.hasSrc(T1.getRootId()) &&
|
|
|
|
!M.hasDst(T2.getRootId())) {
|
2017-08-02 04:17:46 +08:00
|
|
|
if (isMatchingPossible(T1.getRootId(), T2.getRootId())) {
|
|
|
|
M.link(T1.getRootId(), T2.getRootId());
|
|
|
|
addOptimalMapping(M, T1.getRootId(), T2.getRootId());
|
2017-07-21 20:49:28 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const Node &N1 = T1.getNode(Id1);
|
|
|
|
bool Matched = M.hasSrc(Id1);
|
|
|
|
bool MatchedChildren =
|
|
|
|
std::any_of(N1.Children.begin(), N1.Children.end(),
|
|
|
|
[&](NodeId Child) { return M.hasSrc(Child); });
|
|
|
|
if (Matched || !MatchedChildren)
|
|
|
|
continue;
|
|
|
|
NodeId Id2 = findCandidate(M, Id1);
|
2017-08-19 00:34:15 +08:00
|
|
|
if (Id2.isValid() && canBeAddedToMapping(M, Id1, Id2)) {
|
|
|
|
M.link(Id1, Id2);
|
|
|
|
addOptimalMapping(M, Id1, Id2);
|
|
|
|
}
|
2017-07-21 20:49:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Mapping ASTDiff::Impl::matchTopDown() const {
|
|
|
|
PriorityList L1(T1);
|
|
|
|
PriorityList L2(T2);
|
|
|
|
|
|
|
|
Mapping M(T1.getSize(), T2.getSize());
|
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
L1.push(T1.getRootId());
|
|
|
|
L2.push(T2.getRootId());
|
2017-07-21 20:49:28 +08:00
|
|
|
|
|
|
|
int Max1, Max2;
|
|
|
|
while (std::min(Max1 = L1.peekMax(), Max2 = L2.peekMax()) >
|
|
|
|
Options.MinHeight) {
|
|
|
|
if (Max1 > Max2) {
|
|
|
|
for (NodeId Id : L1.pop())
|
|
|
|
L1.open(Id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Max2 > Max1) {
|
|
|
|
for (NodeId Id : L2.pop())
|
|
|
|
L2.open(Id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::vector<NodeId> H1, H2;
|
|
|
|
H1 = L1.pop();
|
|
|
|
H2 = L2.pop();
|
|
|
|
for (NodeId Id1 : H1) {
|
2017-08-19 00:34:15 +08:00
|
|
|
for (NodeId Id2 : H2) {
|
|
|
|
if (identical(Id1, Id2) && canBeAddedToMapping(M, Id1, Id2)) {
|
|
|
|
for (int I = 0, E = T1.getNumberOfDescendants(Id1); I < E; ++I)
|
|
|
|
M.link(Id1 + I, Id2 + I);
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 20:49:28 +08:00
|
|
|
}
|
|
|
|
for (NodeId Id1 : H1) {
|
|
|
|
if (!M.hasSrc(Id1))
|
|
|
|
L1.open(Id1);
|
|
|
|
}
|
|
|
|
for (NodeId Id2 : H2) {
|
|
|
|
if (!M.hasDst(Id2))
|
|
|
|
L2.open(Id2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return M;
|
|
|
|
}
|
|
|
|
|
2017-08-19 05:26:34 +08:00
|
|
|
ASTDiff::Impl::Impl(SyntaxTree::Impl &T1, SyntaxTree::Impl &T2,
|
|
|
|
const ComparisonOptions &Options)
|
|
|
|
: T1(T1), T2(T2), Options(Options) {
|
|
|
|
computeMapping();
|
|
|
|
computeChangeKinds(TheMapping);
|
|
|
|
}
|
|
|
|
|
2017-07-21 20:49:28 +08:00
|
|
|
void ASTDiff::Impl::computeMapping() {
|
|
|
|
TheMapping = matchTopDown();
|
|
|
|
matchBottomUp(TheMapping);
|
|
|
|
}
|
|
|
|
|
2017-08-19 05:26:34 +08:00
|
|
|
void ASTDiff::Impl::computeChangeKinds(Mapping &M) {
|
|
|
|
for (NodeId Id1 : T1) {
|
|
|
|
if (!M.hasSrc(Id1)) {
|
|
|
|
T1.getMutableNode(Id1).ChangeKind = Delete;
|
|
|
|
T1.getMutableNode(Id1).Shift -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (NodeId Id2 : T2) {
|
|
|
|
if (!M.hasDst(Id2)) {
|
|
|
|
T2.getMutableNode(Id2).ChangeKind = Insert;
|
|
|
|
T2.getMutableNode(Id2).Shift -= 1;
|
2017-07-21 20:49:28 +08:00
|
|
|
}
|
2017-08-19 05:26:34 +08:00
|
|
|
}
|
|
|
|
for (NodeId Id1 : T1.NodesBfs) {
|
2017-07-21 20:49:28 +08:00
|
|
|
NodeId Id2 = M.getDst(Id1);
|
|
|
|
if (Id2.isInvalid())
|
2017-08-19 05:26:34 +08:00
|
|
|
continue;
|
|
|
|
if (!haveSameParents(M, Id1, Id2) ||
|
|
|
|
T1.findPositionInParent(Id1, true) !=
|
|
|
|
T2.findPositionInParent(Id2, true)) {
|
|
|
|
T1.getMutableNode(Id1).Shift -= 1;
|
|
|
|
T2.getMutableNode(Id2).Shift -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (NodeId Id2 : T2.NodesBfs) {
|
|
|
|
NodeId Id1 = M.getSrc(Id2);
|
|
|
|
if (Id1.isInvalid())
|
|
|
|
continue;
|
|
|
|
Node &N1 = T1.getMutableNode(Id1);
|
|
|
|
Node &N2 = T2.getMutableNode(Id2);
|
|
|
|
if (Id1.isInvalid())
|
|
|
|
continue;
|
|
|
|
if (!haveSameParents(M, Id1, Id2) ||
|
|
|
|
T1.findPositionInParent(Id1, true) !=
|
|
|
|
T2.findPositionInParent(Id2, true)) {
|
|
|
|
N1.ChangeKind = N2.ChangeKind = Move;
|
|
|
|
}
|
|
|
|
if (T1.getNodeValue(Id1) != T2.getNodeValue(Id2)) {
|
|
|
|
N1.ChangeKind = N2.ChangeKind =
|
|
|
|
(N1.ChangeKind == Move ? UpdateMove : Update);
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 20:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ASTDiff::ASTDiff(SyntaxTree &T1, SyntaxTree &T2,
|
|
|
|
const ComparisonOptions &Options)
|
|
|
|
: DiffImpl(llvm::make_unique<Impl>(*T1.TreeImpl, *T2.TreeImpl, Options)) {}
|
|
|
|
|
2017-08-02 04:17:46 +08:00
|
|
|
ASTDiff::~ASTDiff() = default;
|
2017-07-21 20:49:28 +08:00
|
|
|
|
2017-08-19 05:26:34 +08:00
|
|
|
NodeId ASTDiff::getMapped(const SyntaxTree &SourceTree, NodeId Id) const {
|
|
|
|
return DiffImpl->getMapped(SourceTree.TreeImpl, Id);
|
|
|
|
}
|
|
|
|
|
2017-07-21 20:49:28 +08:00
|
|
|
SyntaxTree::SyntaxTree(const ASTContext &AST)
|
2017-08-02 04:17:46 +08:00
|
|
|
: TreeImpl(llvm::make_unique<SyntaxTree::Impl>(
|
2017-07-21 20:49:28 +08:00
|
|
|
this, AST.getTranslationUnitDecl(), AST)) {}
|
|
|
|
|
2017-08-02 04:17:40 +08:00
|
|
|
SyntaxTree::~SyntaxTree() = default;
|
|
|
|
|
2017-08-19 05:26:13 +08:00
|
|
|
const ASTContext &SyntaxTree::getASTContext() const { return TreeImpl->AST; }
|
|
|
|
|
|
|
|
const Node &SyntaxTree::getNode(NodeId Id) const {
|
|
|
|
return TreeImpl->getNode(Id);
|
|
|
|
}
|
|
|
|
|
2017-08-19 05:26:34 +08:00
|
|
|
int SyntaxTree::getSize() const { return TreeImpl->getSize(); }
|
2017-08-19 05:26:13 +08:00
|
|
|
NodeId SyntaxTree::getRootId() const { return TreeImpl->getRootId(); }
|
2017-08-19 05:26:34 +08:00
|
|
|
SyntaxTree::PreorderIterator SyntaxTree::begin() const {
|
|
|
|
return TreeImpl->begin();
|
|
|
|
}
|
|
|
|
SyntaxTree::PreorderIterator SyntaxTree::end() const { return TreeImpl->end(); }
|
2017-08-19 05:26:13 +08:00
|
|
|
|
2017-08-19 05:26:34 +08:00
|
|
|
int SyntaxTree::findPositionInParent(NodeId Id) const {
|
|
|
|
return TreeImpl->findPositionInParent(Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<unsigned, unsigned>
|
|
|
|
SyntaxTree::getSourceRangeOffsets(const Node &N) const {
|
2017-08-19 05:26:13 +08:00
|
|
|
const SourceManager &SrcMgr = TreeImpl->AST.getSourceManager();
|
|
|
|
SourceRange Range = N.ASTNode.getSourceRange();
|
|
|
|
SourceLocation BeginLoc = Range.getBegin();
|
|
|
|
SourceLocation EndLoc = Lexer::getLocForEndOfToken(
|
|
|
|
Range.getEnd(), /*Offset=*/0, SrcMgr, TreeImpl->AST.getLangOpts());
|
|
|
|
if (auto *ThisExpr = N.ASTNode.get<CXXThisExpr>()) {
|
|
|
|
if (ThisExpr->isImplicit())
|
|
|
|
EndLoc = BeginLoc;
|
|
|
|
}
|
|
|
|
unsigned Begin = SrcMgr.getFileOffset(SrcMgr.getExpansionLoc(BeginLoc));
|
|
|
|
unsigned End = SrcMgr.getFileOffset(SrcMgr.getExpansionLoc(EndLoc));
|
|
|
|
return {Begin, End};
|
|
|
|
}
|
2017-07-21 20:49:28 +08:00
|
|
|
|
2017-08-19 05:26:34 +08:00
|
|
|
std::string SyntaxTree::getNodeValue(NodeId Id) const {
|
|
|
|
return TreeImpl->getNodeValue(Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string SyntaxTree::getNodeValue(const Node &N) const {
|
|
|
|
return TreeImpl->getNodeValue(N);
|
2017-07-21 20:49:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace diff
|
|
|
|
} // end namespace clang
|