Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit ee444f1

Browse files
committed
[clang-diff] Move printing of matches and changes to clang-diff
Summary: This also changes the output order of the changes. Now the matches are printed in pre-order, intertwined with insertions, updates, and moves. Deletions are printed afterwards. Reviewers: arphaman Subscribers: klimek Differential Revision: https://reviews.llvm.org/D36179 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@311200 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 5ce0ae8 commit ee444f1

File tree

5 files changed

+247
-227
lines changed

5 files changed

+247
-227
lines changed

include/clang/Tooling/ASTDiff/ASTDiff.h

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,22 @@
2525
namespace clang {
2626
namespace 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-
3428
enum 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.
5438
struct 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

include/clang/Tooling/ASTDiff/ASTDiffInternal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ struct NodeId {
3636
operator int() const { return Id; }
3737
NodeId &operator++() { return ++Id, *this; }
3838
NodeId &operator--() { return --Id, *this; }
39+
// Support defining iterators on NodeId.
40+
NodeId &operator*() { return *this; }
3941

4042
bool isValid() const { return Id != InvalidNodeId; }
4143
bool isInvalid() const { return Id == InvalidNodeId; }

0 commit comments

Comments
 (0)