2525namespace clang {
2626namespace diff {
2727
28+ // / This represents a match between two nodes in the source and destination
29+ // / trees, meaning that they are likely to be related.
30+ struct Match {
31+ NodeId Src, Dst;
32+ };
33+
2834enum ChangeKind {
29- None,
30- Delete, // (Src): delete node Src.
31- Update, // (Src, Dst): update the value of node Src to match Dst.
32- Insert, // (Src, Dst, Pos): insert Src as child of Dst at offset Pos.
33- Move, // (Src, Dst, Pos): move Src to be a child of Dst at offset Pos.
34- UpdateMove // Same as Move plus Update.
35+ Delete, // (Src): delete node Src.
36+ Update, // (Src, Dst): update the value of node Src to match Dst.
37+ Insert, // (Src, Dst, Pos): insert Src as child of Dst at offset Pos.
38+ Move // (Src, Dst, Pos): move Src to be a child of Dst at offset Pos.
39+ };
40+
41+ struct Change {
42+ ChangeKind Kind;
43+ NodeId Src, Dst;
44+ size_t Position;
45+
46+ Change (ChangeKind Kind, NodeId Src, NodeId Dst, size_t Position)
47+ : Kind(Kind), Src(Src), Dst(Dst), Position(Position) {}
48+ Change (ChangeKind Kind, NodeId Src) : Kind(Kind), Src(Src) {}
49+ Change (ChangeKind Kind, NodeId Src, NodeId Dst)
50+ : Kind(Kind), Src(Src), Dst(Dst) {}
3551};
3652
3753// / Represents a Clang AST node, alongside some additional information.
3854struct Node {
3955 NodeId Parent, LeftMostDescendant, RightMostDescendant;
40- int Depth, Height, Shift = 0 ;
56+ int Depth, Height;
4157 ast_type_traits::DynTypedNode ASTNode;
4258 SmallVector<NodeId, 4 > Children;
43- ChangeKind ChangeKind = None;
4459
4560 ast_type_traits::ASTNodeKind getType () const ;
4661 StringRef getTypeLabel () const ;
@@ -52,8 +67,15 @@ class ASTDiff {
5267 ASTDiff (SyntaxTree &Src, SyntaxTree &Dst, const ComparisonOptions &Options);
5368 ~ASTDiff ();
5469
55- // Returns the ID of the node that is mapped to the given node in SourceTree.
56- NodeId getMapped (const SyntaxTree &SourceTree, NodeId Id) const ;
70+ // Returns a list of matches.
71+ std::vector<Match> getMatches ();
72+ // / Returns an edit script.
73+ std::vector<Change> getChanges ();
74+
75+ // Prints an edit action.
76+ void printChange (raw_ostream &OS, const Change &Chg) const ;
77+ // Prints a match between two nodes.
78+ void printMatch (raw_ostream &OS, const Match &M) const ;
5779
5880 class Impl ;
5981
@@ -77,23 +99,16 @@ class SyntaxTree {
7799 const ASTContext &getASTContext () const ;
78100 StringRef getFilename () const ;
79101
80- int getSize () const ;
81- NodeId getRootId () const ;
82- using PreorderIterator = NodeId;
83- PreorderIterator begin () const ;
84- PreorderIterator end () const ;
85-
86102 const Node &getNode (NodeId Id) const ;
87- int findPositionInParent ( NodeId Id ) const ;
103+ NodeId getRootId ( ) const ;
88104
89105 // Returns the starting and ending offset of the node in its source file.
90106 std::pair<unsigned , unsigned > getSourceRangeOffsets (const Node &N) const ;
91107
92108 // / Serialize the node attributes to a string representation. This should
93109 // / uniquely distinguish nodes of the same kind. Note that this function just
94110 // / returns a representation of the node value, not considering descendants.
95- std::string getNodeValue (NodeId Id) const ;
96- std::string getNodeValue (const Node &Node) const ;
111+ std::string getNodeValue (const DynTypedNode &DTN) const ;
97112
98113 class Impl ;
99114 std::unique_ptr<Impl> TreeImpl;
@@ -116,8 +131,8 @@ struct ComparisonOptions {
116131 bool EnableMatchingWithUnmatchableParents = false ;
117132
118133 // / Returns false if the nodes should never be matched.
119- bool isMatchingAllowed (const Node &N1, const Node &N2) const {
120- return N1.getType ().isSame (N2.getType ());
134+ bool isMatchingAllowed (const DynTypedNode &N1, const DynTypedNode &N2) const {
135+ return N1.getNodeKind ().isSame (N2.getNodeKind ());
121136 }
122137};
123138
0 commit comments