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-
3428enum ChangeKind {
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) {}
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.
5135};
5236
5337// / Represents a Clang AST node, alongside some additional information.
5438struct Node {
5539 NodeId Parent, LeftMostDescendant, RightMostDescendant;
56- int Depth, Height;
40+ int Depth, Height, Shift = 0 ;
5741 ast_type_traits::DynTypedNode ASTNode;
5842 SmallVector<NodeId, 4 > Children;
43+ ChangeKind ChangeKind = None;
5944
6045 ast_type_traits::ASTNodeKind getType () const ;
6146 StringRef getTypeLabel () const ;
@@ -67,15 +52,8 @@ class ASTDiff {
6752 ASTDiff (SyntaxTree &Src, SyntaxTree &Dst, const ComparisonOptions &Options);
6853 ~ASTDiff ();
6954
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 ;
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 ;
7957
8058 class Impl ;
8159
@@ -99,16 +77,23 @@ class SyntaxTree {
9977 const ASTContext &getASTContext () const ;
10078 StringRef getFilename () const ;
10179
102- const Node & getNode (NodeId Id ) const ;
80+ int getSize ( ) const ;
10381 NodeId getRootId () const ;
82+ using PreorderIterator = NodeId;
83+ PreorderIterator begin () const ;
84+ PreorderIterator end () const ;
85+
86+ const Node &getNode (NodeId Id) const ;
87+ int findPositionInParent (NodeId Id) const ;
10488
10589 // Returns the starting and ending offset of the node in its source file.
10690 std::pair<unsigned , unsigned > getSourceRangeOffsets (const Node &N) const ;
10791
10892 // / Serialize the node attributes to a string representation. This should
10993 // / uniquely distinguish nodes of the same kind. Note that this function just
11094 // / returns a representation of the node value, not considering descendants.
111- std::string getNodeValue (const DynTypedNode &DTN) const ;
95+ std::string getNodeValue (NodeId Id) const ;
96+ std::string getNodeValue (const Node &Node) const ;
11297
11398 class Impl ;
11499 std::unique_ptr<Impl> TreeImpl;
@@ -131,8 +116,8 @@ struct ComparisonOptions {
131116 bool EnableMatchingWithUnmatchableParents = false ;
132117
133118 // / Returns false if the nodes should never be matched.
134- bool isMatchingAllowed (const DynTypedNode &N1, const DynTypedNode &N2) const {
135- return N1.getNodeKind ().isSame (N2.getNodeKind ());
119+ bool isMatchingAllowed (const Node &N1, const Node &N2) const {
120+ return N1.getType ().isSame (N2.getType ());
136121 }
137122};
138123
0 commit comments