blob: 64b853fbce9f4d9d94c8c8aa92099d4f6749ce88 [file] [log] [blame]
Alex Lorenza75b2ca2017-07-21 12:49:28 +00001//===- ASTDiff.cpp - AST differencing implementation-----------*- C++ -*- -===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains definitons for the AST differencing interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Tooling/ASTDiff/ASTDiff.h"
15
16#include "clang/AST/RecursiveASTVisitor.h"
17#include "clang/Lex/Lexer.h"
18#include "llvm/ADT/PriorityQueue.h"
19
20#include <limits>
21#include <memory>
22#include <unordered_set>
23
24using namespace llvm;
25using namespace clang;
26
27namespace clang {
28namespace diff {
29
Johannes Altmanningerfa524d72017-08-18 16:34:15 +000030namespace {
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +000031/// Maps nodes of the left tree to ones on the right, and vice versa.
32class Mapping {
33public:
34 Mapping() = default;
35 Mapping(Mapping &&Other) = default;
36 Mapping &operator=(Mapping &&Other) = default;
Johannes Altmanninger51321ae2017-08-19 17:53:01 +000037
38 Mapping(size_t Size) {
39 SrcToDst = llvm::make_unique<NodeId[]>(Size);
40 DstToSrc = llvm::make_unique<NodeId[]>(Size);
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +000041 }
42
43 void link(NodeId Src, NodeId Dst) {
Johannes Altmanninger51321ae2017-08-19 17:53:01 +000044 SrcToDst[Src] = Dst, DstToSrc[Dst] = Src;
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +000045 }
46
Johannes Altmanninger51321ae2017-08-19 17:53:01 +000047 NodeId getDst(NodeId Src) const { return SrcToDst[Src]; }
48 NodeId getSrc(NodeId Dst) const { return DstToSrc[Dst]; }
49 bool hasSrc(NodeId Src) const { return getDst(Src).isValid(); }
50 bool hasDst(NodeId Dst) const { return getSrc(Dst).isValid(); }
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +000051
52private:
Johannes Altmanninger51321ae2017-08-19 17:53:01 +000053 std::unique_ptr<NodeId[]> SrcToDst, DstToSrc;
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +000054};
Johannes Altmanningerfa524d72017-08-18 16:34:15 +000055} // end anonymous namespace
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +000056
Alex Lorenza75b2ca2017-07-21 12:49:28 +000057class ASTDiff::Impl {
58public:
Johannes Altmanninger31b52d62017-08-01 20:17:46 +000059 SyntaxTree::Impl &T1, &T2;
Alex Lorenza75b2ca2017-07-21 12:49:28 +000060 Mapping TheMapping;
61
Johannes Altmanninger31b52d62017-08-01 20:17:46 +000062 Impl(SyntaxTree::Impl &T1, SyntaxTree::Impl &T2,
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +000063 const ComparisonOptions &Options);
Alex Lorenza75b2ca2017-07-21 12:49:28 +000064
65 /// Matches nodes one-by-one based on their similarity.
66 void computeMapping();
67
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +000068 // Compute Change for each node based on similarity.
69 void computeChangeKinds(Mapping &M);
Alex Lorenza75b2ca2017-07-21 12:49:28 +000070
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +000071 NodeId getMapped(const std::unique_ptr<SyntaxTree::Impl> &Tree,
72 NodeId Id) const {
73 if (&*Tree == &T1)
74 return TheMapping.getDst(Id);
75 assert(&*Tree == &T2 && "Invalid tree.");
76 return TheMapping.getSrc(Id);
77 }
Alex Lorenza75b2ca2017-07-21 12:49:28 +000078
79private:
80 // Returns true if the two subtrees are identical.
Johannes Altmanninger31b52d62017-08-01 20:17:46 +000081 bool identical(NodeId Id1, NodeId Id2) const;
Alex Lorenza75b2ca2017-07-21 12:49:28 +000082
Alex Lorenza75b2ca2017-07-21 12:49:28 +000083 // Returns false if the nodes must not be mached.
84 bool isMatchingPossible(NodeId Id1, NodeId Id2) const;
85
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +000086 // Returns true if the nodes' parents are matched.
87 bool haveSameParents(const Mapping &M, NodeId Id1, NodeId Id2) const;
88
Alex Lorenza75b2ca2017-07-21 12:49:28 +000089 // Uses an optimal albeit slow algorithm to compute a mapping between two
90 // subtrees, but only if both have fewer nodes than MaxSize.
91 void addOptimalMapping(Mapping &M, NodeId Id1, NodeId Id2) const;
92
93 // Computes the ratio of common descendants between the two nodes.
94 // Descendants are only considered to be equal when they are mapped in M.
Johannes Altmanningerd1969302017-08-20 12:09:07 +000095 double getJaccardSimilarity(const Mapping &M, NodeId Id1, NodeId Id2) const;
Alex Lorenza75b2ca2017-07-21 12:49:28 +000096
97 // Returns the node that has the highest degree of similarity.
98 NodeId findCandidate(const Mapping &M, NodeId Id1) const;
99
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000100 // Returns a mapping of identical subtrees.
101 Mapping matchTopDown() const;
102
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000103 // Tries to match any yet unmapped nodes, in a bottom-up fashion.
104 void matchBottomUp(Mapping &M) const;
105
106 const ComparisonOptions &Options;
107
108 friend class ZhangShashaMatcher;
109};
110
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000111/// Represents the AST of a TranslationUnit.
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000112class SyntaxTree::Impl {
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000113public:
114 /// Constructs a tree from the entire translation unit.
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000115 Impl(SyntaxTree *Parent, const ASTContext &AST);
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000116 /// Constructs a tree from an AST node.
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000117 Impl(SyntaxTree *Parent, Decl *N, const ASTContext &AST);
118 Impl(SyntaxTree *Parent, Stmt *N, const ASTContext &AST);
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000119 template <class T>
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000120 Impl(SyntaxTree *Parent,
121 typename std::enable_if<std::is_base_of<Stmt, T>::value, T>::type *Node,
122 const ASTContext &AST)
123 : Impl(Parent, dyn_cast<Stmt>(Node), AST) {}
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000124 template <class T>
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000125 Impl(SyntaxTree *Parent,
126 typename std::enable_if<std::is_base_of<Decl, T>::value, T>::type *Node,
127 const ASTContext &AST)
128 : Impl(Parent, dyn_cast<Decl>(Node), AST) {}
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000129
130 SyntaxTree *Parent;
131 const ASTContext &AST;
132 std::vector<NodeId> Leaves;
133 // Maps preorder indices to postorder ones.
134 std::vector<int> PostorderIds;
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000135 std::vector<NodeId> NodesBfs;
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000136
137 int getSize() const { return Nodes.size(); }
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000138 NodeId getRootId() const { return 0; }
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000139 PreorderIterator begin() const { return getRootId(); }
140 PreorderIterator end() const { return getSize(); }
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000141
142 const Node &getNode(NodeId Id) const { return Nodes[Id]; }
143 Node &getMutableNode(NodeId Id) { return Nodes[Id]; }
144 bool isValidNodeId(NodeId Id) const { return Id >= 0 && Id < getSize(); }
145 void addNode(Node &N) { Nodes.push_back(N); }
146 int getNumberOfDescendants(NodeId Id) const;
147 bool isInSubtree(NodeId Id, NodeId SubtreeRoot) const;
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000148 int findPositionInParent(NodeId Id, bool Shifted = false) const;
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000149
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000150 std::string getNodeValue(NodeId Id) const;
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000151 std::string getNodeValue(const Node &Node) const;
Johannes Altmanninger0dd86dc2017-08-20 16:18:43 +0000152 std::string getDeclValue(const Decl *D) const;
153 std::string getStmtValue(const Stmt *S) const;
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000154
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000155private:
156 /// Nodes in preorder.
157 std::vector<Node> Nodes;
158
159 void initTree();
160 void setLeftMostDescendants();
161};
162
Johannes Altmanningerd5b56a82017-08-20 10:22:32 +0000163static bool isSpecializedNodeExcluded(const Decl *D) { return D->isImplicit(); }
164static bool isSpecializedNodeExcluded(const Stmt *S) { return false; }
165
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000166template <class T>
167static bool isNodeExcluded(const SourceManager &SrcMgr, T *N) {
168 if (!N)
169 return true;
170 SourceLocation SLoc = N->getLocStart();
Johannes Altmanningerd5b56a82017-08-20 10:22:32 +0000171 if (SLoc.isValid()) {
172 // Ignore everything from other files.
173 if (!SrcMgr.isInMainFile(SLoc))
174 return true;
175 // Ignore macros.
176 if (SLoc != SrcMgr.getSpellingLoc(SLoc))
177 return true;
178 }
179 return isSpecializedNodeExcluded(N);
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000180}
181
182namespace {
183/// Counts the number of nodes that will be compared.
184struct NodeCountVisitor : public RecursiveASTVisitor<NodeCountVisitor> {
185 int Count = 0;
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000186 const SyntaxTree::Impl &Tree;
187 NodeCountVisitor(const SyntaxTree::Impl &Tree) : Tree(Tree) {}
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000188 bool TraverseDecl(Decl *D) {
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000189 if (isNodeExcluded(Tree.AST.getSourceManager(), D))
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000190 return true;
191 ++Count;
192 RecursiveASTVisitor<NodeCountVisitor>::TraverseDecl(D);
193 return true;
194 }
195 bool TraverseStmt(Stmt *S) {
Johannes Altmanningerd5b56a82017-08-20 10:22:32 +0000196 if (S)
197 S = S->IgnoreImplicit();
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000198 if (isNodeExcluded(Tree.AST.getSourceManager(), S))
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000199 return true;
200 ++Count;
201 RecursiveASTVisitor<NodeCountVisitor>::TraverseStmt(S);
202 return true;
203 }
204 bool TraverseType(QualType T) { return true; }
205};
206} // end anonymous namespace
207
208namespace {
209// Sets Height, Parent and Children for each node.
210struct PreorderVisitor : public RecursiveASTVisitor<PreorderVisitor> {
211 int Id = 0, Depth = 0;
212 NodeId Parent;
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000213 SyntaxTree::Impl &Tree;
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000214
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000215 PreorderVisitor(SyntaxTree::Impl &Tree) : Tree(Tree) {}
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000216
217 template <class T> std::tuple<NodeId, NodeId> PreTraverse(T *ASTNode) {
218 NodeId MyId = Id;
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000219 Node &N = Tree.getMutableNode(MyId);
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000220 N.Parent = Parent;
221 N.Depth = Depth;
222 N.ASTNode = DynTypedNode::create(*ASTNode);
223 assert(!N.ASTNode.getNodeKind().isNone() &&
224 "Expected nodes to have a valid kind.");
225 if (Parent.isValid()) {
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000226 Node &P = Tree.getMutableNode(Parent);
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000227 P.Children.push_back(MyId);
228 }
229 Parent = MyId;
230 ++Id;
231 ++Depth;
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000232 return std::make_tuple(MyId, Tree.getNode(MyId).Parent);
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000233 }
234 void PostTraverse(std::tuple<NodeId, NodeId> State) {
235 NodeId MyId, PreviousParent;
236 std::tie(MyId, PreviousParent) = State;
237 assert(MyId.isValid() && "Expecting to only traverse valid nodes.");
238 Parent = PreviousParent;
239 --Depth;
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000240 Node &N = Tree.getMutableNode(MyId);
Johannes Altmanningerfa524d72017-08-18 16:34:15 +0000241 N.RightMostDescendant = Id - 1;
242 assert(N.RightMostDescendant >= 0 &&
243 N.RightMostDescendant < Tree.getSize() &&
244 "Rightmost descendant must be a valid tree node.");
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000245 if (N.isLeaf())
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000246 Tree.Leaves.push_back(MyId);
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000247 N.Height = 1;
248 for (NodeId Child : N.Children)
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000249 N.Height = std::max(N.Height, 1 + Tree.getNode(Child).Height);
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000250 }
251 bool TraverseDecl(Decl *D) {
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000252 if (isNodeExcluded(Tree.AST.getSourceManager(), D))
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000253 return true;
254 auto SavedState = PreTraverse(D);
255 RecursiveASTVisitor<PreorderVisitor>::TraverseDecl(D);
256 PostTraverse(SavedState);
257 return true;
258 }
259 bool TraverseStmt(Stmt *S) {
Johannes Altmanningerd5b56a82017-08-20 10:22:32 +0000260 if (S)
261 S = S->IgnoreImplicit();
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000262 if (isNodeExcluded(Tree.AST.getSourceManager(), S))
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000263 return true;
264 auto SavedState = PreTraverse(S);
265 RecursiveASTVisitor<PreorderVisitor>::TraverseStmt(S);
266 PostTraverse(SavedState);
267 return true;
268 }
269 bool TraverseType(QualType T) { return true; }
270};
271} // end anonymous namespace
272
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000273SyntaxTree::Impl::Impl(SyntaxTree *Parent, Decl *N, const ASTContext &AST)
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000274 : Parent(Parent), AST(AST) {
275 NodeCountVisitor NodeCounter(*this);
276 NodeCounter.TraverseDecl(N);
277 Nodes.resize(NodeCounter.Count);
278 PreorderVisitor PreorderWalker(*this);
279 PreorderWalker.TraverseDecl(N);
280 initTree();
281}
282
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000283SyntaxTree::Impl::Impl(SyntaxTree *Parent, Stmt *N, const ASTContext &AST)
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000284 : Parent(Parent), AST(AST) {
285 NodeCountVisitor NodeCounter(*this);
286 NodeCounter.TraverseStmt(N);
287 Nodes.resize(NodeCounter.Count);
288 PreorderVisitor PreorderWalker(*this);
289 PreorderWalker.TraverseStmt(N);
290 initTree();
291}
292
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000293static std::vector<NodeId> getSubtreePostorder(const SyntaxTree::Impl &Tree,
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000294 NodeId Root) {
295 std::vector<NodeId> Postorder;
296 std::function<void(NodeId)> Traverse = [&](NodeId Id) {
297 const Node &N = Tree.getNode(Id);
298 for (NodeId Child : N.Children)
299 Traverse(Child);
300 Postorder.push_back(Id);
301 };
302 Traverse(Root);
303 return Postorder;
304}
305
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000306static std::vector<NodeId> getSubtreeBfs(const SyntaxTree::Impl &Tree,
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000307 NodeId Root) {
308 std::vector<NodeId> Ids;
309 size_t Expanded = 0;
310 Ids.push_back(Root);
311 while (Expanded < Ids.size())
312 for (NodeId Child : Tree.getNode(Ids[Expanded++]).Children)
313 Ids.push_back(Child);
314 return Ids;
315}
316
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000317void SyntaxTree::Impl::initTree() {
318 setLeftMostDescendants();
319 int PostorderId = 0;
320 PostorderIds.resize(getSize());
321 std::function<void(NodeId)> PostorderTraverse = [&](NodeId Id) {
322 for (NodeId Child : getNode(Id).Children)
323 PostorderTraverse(Child);
324 PostorderIds[Id] = PostorderId;
325 ++PostorderId;
326 };
327 PostorderTraverse(getRootId());
328 NodesBfs = getSubtreeBfs(*this, getRootId());
329}
330
331void SyntaxTree::Impl::setLeftMostDescendants() {
332 for (NodeId Leaf : Leaves) {
333 getMutableNode(Leaf).LeftMostDescendant = Leaf;
334 NodeId Parent, Cur = Leaf;
335 while ((Parent = getNode(Cur).Parent).isValid() &&
336 getNode(Parent).Children[0] == Cur) {
337 Cur = Parent;
338 getMutableNode(Cur).LeftMostDescendant = Leaf;
339 }
340 }
341}
342
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000343int SyntaxTree::Impl::getNumberOfDescendants(NodeId Id) const {
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000344 return getNode(Id).RightMostDescendant - Id + 1;
345}
346
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000347bool SyntaxTree::Impl::isInSubtree(NodeId Id, NodeId SubtreeRoot) const {
Johannes Altmanningerfa524d72017-08-18 16:34:15 +0000348 return Id >= SubtreeRoot && Id <= getNode(SubtreeRoot).RightMostDescendant;
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000349}
350
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000351int SyntaxTree::Impl::findPositionInParent(NodeId Id, bool Shifted) const {
352 NodeId Parent = getNode(Id).Parent;
353 if (Parent.isInvalid())
354 return 0;
355 const auto &Siblings = getNode(Parent).Children;
356 int Position = 0;
357 for (size_t I = 0, E = Siblings.size(); I < E; ++I) {
358 if (Shifted)
359 Position += getNode(Siblings[I]).Shift;
360 if (Siblings[I] == Id) {
361 Position += I;
362 return Position;
363 }
364 }
365 llvm_unreachable("Node not found in parent's children.");
Johannes Altmanninger69774d62017-08-18 21:26:34 +0000366}
367
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000368std::string SyntaxTree::Impl::getNodeValue(NodeId Id) const {
369 return getNodeValue(getNode(Id));
370}
371
372std::string SyntaxTree::Impl::getNodeValue(const Node &N) const {
373 const DynTypedNode &DTN = N.ASTNode;
Johannes Altmanninger0dd86dc2017-08-20 16:18:43 +0000374 if (auto *S = DTN.get<Stmt>())
375 return getStmtValue(S);
376 if (auto *D = DTN.get<Decl>())
377 return getDeclValue(D);
378 llvm_unreachable("Fatal: unhandled AST node.\n");
379}
380
381std::string SyntaxTree::Impl::getDeclValue(const Decl *D) const {
382 std::string Value;
383 PrintingPolicy TypePP(AST.getLangOpts());
384 TypePP.AnonymousTagLocations = false;
385
386 if (auto *V = dyn_cast<ValueDecl>(D)) {
387 Value += V->getQualifiedNameAsString() + "(" +
388 V->getType().getAsString(TypePP) + ")";
389 if (auto *C = dyn_cast<CXXConstructorDecl>(D)) {
390 for (auto *Init : C->inits()) {
391 if (!Init->isWritten())
392 continue;
393 if (Init->isBaseInitializer()) {
394 Value += Init->getBaseClass()->getCanonicalTypeInternal().getAsString(
395 TypePP);
396 } else if (Init->isDelegatingInitializer()) {
397 Value += C->getNameAsString();
398 } else {
399 assert(Init->isAnyMemberInitializer());
400 Value += Init->getMember()->getQualifiedNameAsString();
401 }
402 Value += ",";
403 }
404 }
405 return Value;
406 }
407 if (auto *N = dyn_cast<NamedDecl>(D))
408 Value += N->getQualifiedNameAsString() + ";";
409 if (auto *T = dyn_cast<TypedefNameDecl>(D))
410 return Value + T->getUnderlyingType().getAsString(TypePP) + ";";
411 if (auto *T = dyn_cast<TypeDecl>(D))
412 if (T->getTypeForDecl())
413 Value +=
414 T->getTypeForDecl()->getCanonicalTypeInternal().getAsString(TypePP) +
415 ";";
416 if (auto *U = dyn_cast<UsingDirectiveDecl>(D))
417 return U->getNominatedNamespace()->getName();
418 if (auto *A = dyn_cast<AccessSpecDecl>(D)) {
419 CharSourceRange Range(A->getSourceRange(), false);
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000420 return Lexer::getSourceText(Range, AST.getSourceManager(),
421 AST.getLangOpts());
422 }
Johannes Altmanninger0dd86dc2017-08-20 16:18:43 +0000423 return Value;
424}
425
426std::string SyntaxTree::Impl::getStmtValue(const Stmt *S) const {
427 if (auto *U = dyn_cast<UnaryOperator>(S))
428 return UnaryOperator::getOpcodeStr(U->getOpcode());
429 if (auto *B = dyn_cast<BinaryOperator>(S))
430 return B->getOpcodeStr();
431 if (auto *M = dyn_cast<MemberExpr>(S))
432 return M->getMemberDecl()->getQualifiedNameAsString();
433 if (auto *I = dyn_cast<IntegerLiteral>(S)) {
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000434 SmallString<256> Str;
Johannes Altmanninger0dd86dc2017-08-20 16:18:43 +0000435 I->getValue().toString(Str, /*Radix=*/10, /*Signed=*/false);
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000436 return Str.str();
437 }
Johannes Altmanninger0dd86dc2017-08-20 16:18:43 +0000438 if (auto *F = dyn_cast<FloatingLiteral>(S)) {
439 SmallString<256> Str;
440 F->getValue().toString(Str);
441 return Str.str();
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000442 }
Johannes Altmanninger0dd86dc2017-08-20 16:18:43 +0000443 if (auto *D = dyn_cast<DeclRefExpr>(S))
444 return D->getDecl()->getQualifiedNameAsString();
445 if (auto *String = dyn_cast<StringLiteral>(S))
446 return String->getString();
447 if (auto *B = dyn_cast<CXXBoolLiteralExpr>(S))
448 return B->getValue() ? "true" : "false";
449 return "";
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000450}
451
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000452/// Identifies a node in a subtree by its postorder offset, starting at 1.
453struct SNodeId {
454 int Id = 0;
455
456 explicit SNodeId(int Id) : Id(Id) {}
457 explicit SNodeId() = default;
458
459 operator int() const { return Id; }
460 SNodeId &operator++() { return ++Id, *this; }
461 SNodeId &operator--() { return --Id, *this; }
462 SNodeId operator+(int Other) const { return SNodeId(Id + Other); }
463};
464
465class Subtree {
466private:
467 /// The parent tree.
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000468 const SyntaxTree::Impl &Tree;
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000469 /// Maps SNodeIds to original ids.
470 std::vector<NodeId> RootIds;
471 /// Maps subtree nodes to their leftmost descendants wtihin the subtree.
472 std::vector<SNodeId> LeftMostDescendants;
473
474public:
475 std::vector<SNodeId> KeyRoots;
476
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000477 Subtree(const SyntaxTree::Impl &Tree, NodeId SubtreeRoot) : Tree(Tree) {
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000478 RootIds = getSubtreePostorder(Tree, SubtreeRoot);
479 int NumLeaves = setLeftMostDescendants();
480 computeKeyRoots(NumLeaves);
481 }
482 int getSize() const { return RootIds.size(); }
483 NodeId getIdInRoot(SNodeId Id) const {
484 assert(Id > 0 && Id <= getSize() && "Invalid subtree node index.");
485 return RootIds[Id - 1];
486 }
487 const Node &getNode(SNodeId Id) const {
488 return Tree.getNode(getIdInRoot(Id));
489 }
490 SNodeId getLeftMostDescendant(SNodeId Id) const {
491 assert(Id > 0 && Id <= getSize() && "Invalid subtree node index.");
492 return LeftMostDescendants[Id - 1];
493 }
494 /// Returns the postorder index of the leftmost descendant in the subtree.
495 NodeId getPostorderOffset() const {
496 return Tree.PostorderIds[getIdInRoot(SNodeId(1))];
497 }
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000498 std::string getNodeValue(SNodeId Id) const {
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000499 return Tree.getNodeValue(getIdInRoot(Id));
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000500 }
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000501
502private:
503 /// Returns the number of leafs in the subtree.
504 int setLeftMostDescendants() {
505 int NumLeaves = 0;
506 LeftMostDescendants.resize(getSize());
507 for (int I = 0; I < getSize(); ++I) {
508 SNodeId SI(I + 1);
509 const Node &N = getNode(SI);
510 NumLeaves += N.isLeaf();
511 assert(I == Tree.PostorderIds[getIdInRoot(SI)] - getPostorderOffset() &&
512 "Postorder traversal in subtree should correspond to traversal in "
513 "the root tree by a constant offset.");
514 LeftMostDescendants[I] = SNodeId(Tree.PostorderIds[N.LeftMostDescendant] -
515 getPostorderOffset());
516 }
517 return NumLeaves;
518 }
519 void computeKeyRoots(int Leaves) {
520 KeyRoots.resize(Leaves);
521 std::unordered_set<int> Visited;
522 int K = Leaves - 1;
523 for (SNodeId I(getSize()); I > 0; --I) {
524 SNodeId LeftDesc = getLeftMostDescendant(I);
525 if (Visited.count(LeftDesc))
526 continue;
527 assert(K >= 0 && "K should be non-negative");
528 KeyRoots[K] = I;
529 Visited.insert(LeftDesc);
530 --K;
531 }
532 }
533};
534
535/// Implementation of Zhang and Shasha's Algorithm for tree edit distance.
536/// Computes an optimal mapping between two trees using only insertion,
537/// deletion and update as edit actions (similar to the Levenshtein distance).
538class ZhangShashaMatcher {
539 const ASTDiff::Impl &DiffImpl;
540 Subtree S1;
541 Subtree S2;
542 std::unique_ptr<std::unique_ptr<double[]>[]> TreeDist, ForestDist;
543
544public:
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000545 ZhangShashaMatcher(const ASTDiff::Impl &DiffImpl, const SyntaxTree::Impl &T1,
546 const SyntaxTree::Impl &T2, NodeId Id1, NodeId Id2)
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000547 : DiffImpl(DiffImpl), S1(T1, Id1), S2(T2, Id2) {
548 TreeDist = llvm::make_unique<std::unique_ptr<double[]>[]>(
549 size_t(S1.getSize()) + 1);
550 ForestDist = llvm::make_unique<std::unique_ptr<double[]>[]>(
551 size_t(S1.getSize()) + 1);
552 for (int I = 0, E = S1.getSize() + 1; I < E; ++I) {
553 TreeDist[I] = llvm::make_unique<double[]>(size_t(S2.getSize()) + 1);
554 ForestDist[I] = llvm::make_unique<double[]>(size_t(S2.getSize()) + 1);
555 }
556 }
557
558 std::vector<std::pair<NodeId, NodeId>> getMatchingNodes() {
559 std::vector<std::pair<NodeId, NodeId>> Matches;
560 std::vector<std::pair<SNodeId, SNodeId>> TreePairs;
561
562 computeTreeDist();
563
564 bool RootNodePair = true;
565
Alex Lorenz4c0a8662017-07-21 13:04:57 +0000566 TreePairs.emplace_back(SNodeId(S1.getSize()), SNodeId(S2.getSize()));
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000567
568 while (!TreePairs.empty()) {
569 SNodeId LastRow, LastCol, FirstRow, FirstCol, Row, Col;
570 std::tie(LastRow, LastCol) = TreePairs.back();
571 TreePairs.pop_back();
572
573 if (!RootNodePair) {
574 computeForestDist(LastRow, LastCol);
575 }
576
577 RootNodePair = false;
578
579 FirstRow = S1.getLeftMostDescendant(LastRow);
580 FirstCol = S2.getLeftMostDescendant(LastCol);
581
582 Row = LastRow;
583 Col = LastCol;
584
585 while (Row > FirstRow || Col > FirstCol) {
586 if (Row > FirstRow &&
587 ForestDist[Row - 1][Col] + 1 == ForestDist[Row][Col]) {
588 --Row;
589 } else if (Col > FirstCol &&
590 ForestDist[Row][Col - 1] + 1 == ForestDist[Row][Col]) {
591 --Col;
592 } else {
593 SNodeId LMD1 = S1.getLeftMostDescendant(Row);
594 SNodeId LMD2 = S2.getLeftMostDescendant(Col);
595 if (LMD1 == S1.getLeftMostDescendant(LastRow) &&
596 LMD2 == S2.getLeftMostDescendant(LastCol)) {
597 NodeId Id1 = S1.getIdInRoot(Row);
598 NodeId Id2 = S2.getIdInRoot(Col);
599 assert(DiffImpl.isMatchingPossible(Id1, Id2) &&
600 "These nodes must not be matched.");
601 Matches.emplace_back(Id1, Id2);
602 --Row;
603 --Col;
604 } else {
605 TreePairs.emplace_back(Row, Col);
606 Row = LMD1;
607 Col = LMD2;
608 }
609 }
610 }
611 }
612 return Matches;
613 }
614
615private:
Johannes Altmanningerfa524d72017-08-18 16:34:15 +0000616 /// We use a simple cost model for edit actions, which seems good enough.
617 /// Simple cost model for edit actions. This seems to make the matching
618 /// algorithm perform reasonably well.
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000619 /// The values range between 0 and 1, or infinity if this edit action should
620 /// always be avoided.
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000621 static constexpr double DeletionCost = 1;
622 static constexpr double InsertionCost = 1;
623
624 double getUpdateCost(SNodeId Id1, SNodeId Id2) {
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000625 if (!DiffImpl.isMatchingPossible(S1.getIdInRoot(Id1), S2.getIdInRoot(Id2)))
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000626 return std::numeric_limits<double>::max();
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000627 return S1.getNodeValue(Id1) != S2.getNodeValue(Id2);
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000628 }
629
630 void computeTreeDist() {
631 for (SNodeId Id1 : S1.KeyRoots)
632 for (SNodeId Id2 : S2.KeyRoots)
633 computeForestDist(Id1, Id2);
634 }
635
636 void computeForestDist(SNodeId Id1, SNodeId Id2) {
637 assert(Id1 > 0 && Id2 > 0 && "Expecting offsets greater than 0.");
638 SNodeId LMD1 = S1.getLeftMostDescendant(Id1);
639 SNodeId LMD2 = S2.getLeftMostDescendant(Id2);
640
641 ForestDist[LMD1][LMD2] = 0;
642 for (SNodeId D1 = LMD1 + 1; D1 <= Id1; ++D1) {
643 ForestDist[D1][LMD2] = ForestDist[D1 - 1][LMD2] + DeletionCost;
644 for (SNodeId D2 = LMD2 + 1; D2 <= Id2; ++D2) {
645 ForestDist[LMD1][D2] = ForestDist[LMD1][D2 - 1] + InsertionCost;
646 SNodeId DLMD1 = S1.getLeftMostDescendant(D1);
647 SNodeId DLMD2 = S2.getLeftMostDescendant(D2);
648 if (DLMD1 == LMD1 && DLMD2 == LMD2) {
649 double UpdateCost = getUpdateCost(D1, D2);
650 ForestDist[D1][D2] =
651 std::min({ForestDist[D1 - 1][D2] + DeletionCost,
652 ForestDist[D1][D2 - 1] + InsertionCost,
653 ForestDist[D1 - 1][D2 - 1] + UpdateCost});
654 TreeDist[D1][D2] = ForestDist[D1][D2];
655 } else {
656 ForestDist[D1][D2] =
657 std::min({ForestDist[D1 - 1][D2] + DeletionCost,
658 ForestDist[D1][D2 - 1] + InsertionCost,
659 ForestDist[DLMD1][DLMD2] + TreeDist[D1][D2]});
660 }
661 }
662 }
663 }
664};
665
Johannes Altmanninger0da12c82017-08-19 00:57:38 +0000666ast_type_traits::ASTNodeKind Node::getType() const {
667 return ASTNode.getNodeKind();
668}
669
670StringRef Node::getTypeLabel() const { return getType().asStringRef(); }
671
Johannes Altmanninger0dd86dc2017-08-20 16:18:43 +0000672llvm::Optional<std::string> Node::getQualifiedIdentifier() const {
673 if (auto *ND = ASTNode.get<NamedDecl>()) {
674 if (ND->getDeclName().isIdentifier())
675 return ND->getQualifiedNameAsString();
676 }
677 return llvm::None;
678}
679
680llvm::Optional<StringRef> Node::getIdentifier() const {
681 if (auto *ND = ASTNode.get<NamedDecl>()) {
682 if (ND->getDeclName().isIdentifier())
683 return ND->getName();
684 }
685 return llvm::None;
686}
687
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000688namespace {
689// Compares nodes by their depth.
690struct HeightLess {
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000691 const SyntaxTree::Impl &Tree;
692 HeightLess(const SyntaxTree::Impl &Tree) : Tree(Tree) {}
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000693 bool operator()(NodeId Id1, NodeId Id2) const {
694 return Tree.getNode(Id1).Height < Tree.getNode(Id2).Height;
695 }
696};
697} // end anonymous namespace
698
Johannes Altmanningerfa524d72017-08-18 16:34:15 +0000699namespace {
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000700// Priority queue for nodes, sorted descendingly by their height.
701class PriorityList {
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000702 const SyntaxTree::Impl &Tree;
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000703 HeightLess Cmp;
704 std::vector<NodeId> Container;
705 PriorityQueue<NodeId, std::vector<NodeId>, HeightLess> List;
706
707public:
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000708 PriorityList(const SyntaxTree::Impl &Tree)
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000709 : Tree(Tree), Cmp(Tree), List(Cmp, Container) {}
710
711 void push(NodeId id) { List.push(id); }
712
713 std::vector<NodeId> pop() {
714 int Max = peekMax();
715 std::vector<NodeId> Result;
716 if (Max == 0)
717 return Result;
718 while (peekMax() == Max) {
719 Result.push_back(List.top());
720 List.pop();
721 }
722 // TODO this is here to get a stable output, not a good heuristic
723 std::sort(Result.begin(), Result.end());
724 return Result;
725 }
726 int peekMax() const {
727 if (List.empty())
728 return 0;
729 return Tree.getNode(List.top()).Height;
730 }
731 void open(NodeId Id) {
732 for (NodeId Child : Tree.getNode(Id).Children)
733 push(Child);
734 }
735};
Johannes Altmanningerfa524d72017-08-18 16:34:15 +0000736} // end anonymous namespace
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000737
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000738bool ASTDiff::Impl::identical(NodeId Id1, NodeId Id2) const {
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000739 const Node &N1 = T1.getNode(Id1);
740 const Node &N2 = T2.getNode(Id2);
741 if (N1.Children.size() != N2.Children.size() ||
742 !isMatchingPossible(Id1, Id2) ||
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000743 T1.getNodeValue(Id1) != T2.getNodeValue(Id2))
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000744 return false;
745 for (size_t Id = 0, E = N1.Children.size(); Id < E; ++Id)
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000746 if (!identical(N1.Children[Id], N2.Children[Id]))
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000747 return false;
748 return true;
749}
750
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000751bool ASTDiff::Impl::isMatchingPossible(NodeId Id1, NodeId Id2) const {
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000752 return Options.isMatchingAllowed(T1.getNode(Id1), T2.getNode(Id2));
753}
754
755bool ASTDiff::Impl::haveSameParents(const Mapping &M, NodeId Id1,
756 NodeId Id2) const {
757 NodeId P1 = T1.getNode(Id1).Parent;
758 NodeId P2 = T2.getNode(Id2).Parent;
759 return (P1.isInvalid() && P2.isInvalid()) ||
760 (P1.isValid() && P2.isValid() && M.getDst(P1) == P2);
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000761}
762
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000763void ASTDiff::Impl::addOptimalMapping(Mapping &M, NodeId Id1,
764 NodeId Id2) const {
Johannes Altmanningerd1969302017-08-20 12:09:07 +0000765 if (std::max(T1.getNumberOfDescendants(Id1), T2.getNumberOfDescendants(Id2)) >
766 Options.MaxSize)
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000767 return;
768 ZhangShashaMatcher Matcher(*this, T1, T2, Id1, Id2);
769 std::vector<std::pair<NodeId, NodeId>> R = Matcher.getMatchingNodes();
770 for (const auto Tuple : R) {
771 NodeId Src = Tuple.first;
772 NodeId Dst = Tuple.second;
Johannes Altmanninger51321ae2017-08-19 17:53:01 +0000773 if (!M.hasSrc(Src) && !M.hasDst(Dst))
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000774 M.link(Src, Dst);
775 }
776}
777
Johannes Altmanningerd1969302017-08-20 12:09:07 +0000778double ASTDiff::Impl::getJaccardSimilarity(const Mapping &M, NodeId Id1,
779 NodeId Id2) const {
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000780 int CommonDescendants = 0;
781 const Node &N1 = T1.getNode(Id1);
Johannes Altmanningerd1969302017-08-20 12:09:07 +0000782 // Count the common descendants, excluding the subtree root.
783 for (NodeId Src = Id1 + 1; Src <= N1.RightMostDescendant; ++Src) {
784 NodeId Dst = M.getDst(Src);
785 CommonDescendants += int(Dst.isValid() && T2.isInSubtree(Dst, Id2));
786 }
787 // We need to subtract 1 to get the number of descendants excluding the root.
788 double Denominator = T1.getNumberOfDescendants(Id1) - 1 +
789 T2.getNumberOfDescendants(Id2) - 1 - CommonDescendants;
790 // CommonDescendants is less than the size of one subtree.
791 assert(Denominator >= 0 && "Expected non-negative denominator.");
792 if (Denominator == 0)
793 return 0;
794 return CommonDescendants / Denominator;
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000795}
796
797NodeId ASTDiff::Impl::findCandidate(const Mapping &M, NodeId Id1) const {
798 NodeId Candidate;
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000799 double HighestSimilarity = 0.0;
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000800 for (NodeId Id2 : T2) {
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000801 if (!isMatchingPossible(Id1, Id2))
802 continue;
803 if (M.hasDst(Id2))
804 continue;
Johannes Altmanningerd1969302017-08-20 12:09:07 +0000805 double Similarity = getJaccardSimilarity(M, Id1, Id2);
Johannes Altmanningerfa524d72017-08-18 16:34:15 +0000806 if (Similarity >= Options.MinSimilarity && Similarity > HighestSimilarity) {
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000807 HighestSimilarity = Similarity;
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000808 Candidate = Id2;
809 }
810 }
811 return Candidate;
812}
813
814void ASTDiff::Impl::matchBottomUp(Mapping &M) const {
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000815 std::vector<NodeId> Postorder = getSubtreePostorder(T1, T1.getRootId());
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000816 for (NodeId Id1 : Postorder) {
Johannes Altmanningerfa524d72017-08-18 16:34:15 +0000817 if (Id1 == T1.getRootId() && !M.hasSrc(T1.getRootId()) &&
818 !M.hasDst(T2.getRootId())) {
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000819 if (isMatchingPossible(T1.getRootId(), T2.getRootId())) {
820 M.link(T1.getRootId(), T2.getRootId());
821 addOptimalMapping(M, T1.getRootId(), T2.getRootId());
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000822 }
823 break;
824 }
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000825 bool Matched = M.hasSrc(Id1);
Johannes Altmanningerd1969302017-08-20 12:09:07 +0000826 const Node &N1 = T1.getNode(Id1);
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000827 bool MatchedChildren =
828 std::any_of(N1.Children.begin(), N1.Children.end(),
829 [&](NodeId Child) { return M.hasSrc(Child); });
830 if (Matched || !MatchedChildren)
831 continue;
832 NodeId Id2 = findCandidate(M, Id1);
Johannes Altmanninger51321ae2017-08-19 17:53:01 +0000833 if (Id2.isValid()) {
Johannes Altmanningerfa524d72017-08-18 16:34:15 +0000834 M.link(Id1, Id2);
835 addOptimalMapping(M, Id1, Id2);
836 }
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000837 }
838}
839
840Mapping ASTDiff::Impl::matchTopDown() const {
841 PriorityList L1(T1);
842 PriorityList L2(T2);
843
Johannes Altmanninger51321ae2017-08-19 17:53:01 +0000844 Mapping M(T1.getSize() + T2.getSize());
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000845
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000846 L1.push(T1.getRootId());
847 L2.push(T2.getRootId());
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000848
849 int Max1, Max2;
850 while (std::min(Max1 = L1.peekMax(), Max2 = L2.peekMax()) >
851 Options.MinHeight) {
852 if (Max1 > Max2) {
853 for (NodeId Id : L1.pop())
854 L1.open(Id);
855 continue;
856 }
857 if (Max2 > Max1) {
858 for (NodeId Id : L2.pop())
859 L2.open(Id);
860 continue;
861 }
862 std::vector<NodeId> H1, H2;
863 H1 = L1.pop();
864 H2 = L2.pop();
865 for (NodeId Id1 : H1) {
Johannes Altmanningerfa524d72017-08-18 16:34:15 +0000866 for (NodeId Id2 : H2) {
Johannes Altmanninger51321ae2017-08-19 17:53:01 +0000867 if (identical(Id1, Id2) && !M.hasSrc(Id1) && !M.hasDst(Id2)) {
Johannes Altmanningerfa524d72017-08-18 16:34:15 +0000868 for (int I = 0, E = T1.getNumberOfDescendants(Id1); I < E; ++I)
869 M.link(Id1 + I, Id2 + I);
870 }
871 }
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000872 }
873 for (NodeId Id1 : H1) {
874 if (!M.hasSrc(Id1))
875 L1.open(Id1);
876 }
877 for (NodeId Id2 : H2) {
878 if (!M.hasDst(Id2))
879 L2.open(Id2);
880 }
881 }
882 return M;
883}
884
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000885ASTDiff::Impl::Impl(SyntaxTree::Impl &T1, SyntaxTree::Impl &T2,
886 const ComparisonOptions &Options)
887 : T1(T1), T2(T2), Options(Options) {
888 computeMapping();
889 computeChangeKinds(TheMapping);
890}
891
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000892void ASTDiff::Impl::computeMapping() {
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000893 TheMapping = matchTopDown();
Johannes Altmanningerd1969302017-08-20 12:09:07 +0000894 if (Options.StopAfterTopDown)
895 return;
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000896 matchBottomUp(TheMapping);
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000897}
898
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000899void ASTDiff::Impl::computeChangeKinds(Mapping &M) {
900 for (NodeId Id1 : T1) {
901 if (!M.hasSrc(Id1)) {
902 T1.getMutableNode(Id1).Change = Delete;
903 T1.getMutableNode(Id1).Shift -= 1;
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000904 }
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000905 }
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000906 for (NodeId Id2 : T2) {
907 if (!M.hasDst(Id2)) {
908 T2.getMutableNode(Id2).Change = Insert;
909 T2.getMutableNode(Id2).Shift -= 1;
910 }
911 }
912 for (NodeId Id1 : T1.NodesBfs) {
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000913 NodeId Id2 = M.getDst(Id1);
914 if (Id2.isInvalid())
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000915 continue;
916 if (!haveSameParents(M, Id1, Id2) ||
917 T1.findPositionInParent(Id1, true) !=
918 T2.findPositionInParent(Id2, true)) {
919 T1.getMutableNode(Id1).Shift -= 1;
920 T2.getMutableNode(Id2).Shift -= 1;
921 }
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000922 }
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000923 for (NodeId Id2 : T2.NodesBfs) {
924 NodeId Id1 = M.getSrc(Id2);
925 if (Id1.isInvalid())
926 continue;
927 Node &N1 = T1.getMutableNode(Id1);
928 Node &N2 = T2.getMutableNode(Id2);
929 if (Id1.isInvalid())
930 continue;
931 if (!haveSameParents(M, Id1, Id2) ||
932 T1.findPositionInParent(Id1, true) !=
933 T2.findPositionInParent(Id2, true)) {
934 N1.Change = N2.Change = Move;
935 }
936 if (T1.getNodeValue(Id1) != T2.getNodeValue(Id2)) {
937 N1.Change = N2.Change = (N1.Change == Move ? UpdateMove : Update);
938 }
939 }
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000940}
941
942ASTDiff::ASTDiff(SyntaxTree &T1, SyntaxTree &T2,
943 const ComparisonOptions &Options)
944 : DiffImpl(llvm::make_unique<Impl>(*T1.TreeImpl, *T2.TreeImpl, Options)) {}
945
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000946ASTDiff::~ASTDiff() = default;
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000947
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000948NodeId ASTDiff::getMapped(const SyntaxTree &SourceTree, NodeId Id) const {
949 return DiffImpl->getMapped(SourceTree.TreeImpl, Id);
950}
951
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000952SyntaxTree::SyntaxTree(const ASTContext &AST)
Johannes Altmanninger31b52d62017-08-01 20:17:46 +0000953 : TreeImpl(llvm::make_unique<SyntaxTree::Impl>(
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000954 this, AST.getTranslationUnitDecl(), AST)) {}
955
Johannes Altmanninger8b0e0662017-08-01 20:17:40 +0000956SyntaxTree::~SyntaxTree() = default;
957
Johannes Altmanninger0da12c82017-08-19 00:57:38 +0000958const ASTContext &SyntaxTree::getASTContext() const { return TreeImpl->AST; }
959
960const Node &SyntaxTree::getNode(NodeId Id) const {
961 return TreeImpl->getNode(Id);
962}
963
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000964int SyntaxTree::getSize() const { return TreeImpl->getSize(); }
Johannes Altmanninger0da12c82017-08-19 00:57:38 +0000965NodeId SyntaxTree::getRootId() const { return TreeImpl->getRootId(); }
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000966SyntaxTree::PreorderIterator SyntaxTree::begin() const {
967 return TreeImpl->begin();
968}
969SyntaxTree::PreorderIterator SyntaxTree::end() const { return TreeImpl->end(); }
Johannes Altmanninger0da12c82017-08-19 00:57:38 +0000970
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000971int SyntaxTree::findPositionInParent(NodeId Id) const {
972 return TreeImpl->findPositionInParent(Id);
973}
974
975std::pair<unsigned, unsigned>
976SyntaxTree::getSourceRangeOffsets(const Node &N) const {
Johannes Altmanninger0da12c82017-08-19 00:57:38 +0000977 const SourceManager &SrcMgr = TreeImpl->AST.getSourceManager();
978 SourceRange Range = N.ASTNode.getSourceRange();
979 SourceLocation BeginLoc = Range.getBegin();
980 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
981 Range.getEnd(), /*Offset=*/0, SrcMgr, TreeImpl->AST.getLangOpts());
982 if (auto *ThisExpr = N.ASTNode.get<CXXThisExpr>()) {
983 if (ThisExpr->isImplicit())
984 EndLoc = BeginLoc;
985 }
986 unsigned Begin = SrcMgr.getFileOffset(SrcMgr.getExpansionLoc(BeginLoc));
987 unsigned End = SrcMgr.getFileOffset(SrcMgr.getExpansionLoc(EndLoc));
988 return {Begin, End};
989}
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000990
Johannes Altmanningere0fe5cd2017-08-19 02:56:35 +0000991std::string SyntaxTree::getNodeValue(NodeId Id) const {
992 return TreeImpl->getNodeValue(Id);
993}
994
995std::string SyntaxTree::getNodeValue(const Node &N) const {
996 return TreeImpl->getNodeValue(N);
Alex Lorenza75b2ca2017-07-21 12:49:28 +0000997}
998
999} // end namespace diff
1000} // end namespace clang