1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | #ifndef LLVM_CLANG_TOOLING_ASTDIFF_ASTDIFF_H |
20 | #define LLVM_CLANG_TOOLING_ASTDIFF_ASTDIFF_H |
21 | |
22 | #include "clang/Tooling/ASTDiff/ASTDiffInternal.h" |
23 | |
24 | namespace clang { |
25 | namespace diff { |
26 | |
27 | enum ChangeKind { |
28 | None, |
29 | Delete, |
30 | Update, |
31 | Insert, |
32 | Move, |
33 | UpdateMove |
34 | }; |
35 | |
36 | |
37 | struct Node { |
38 | NodeId Parent, LeftMostDescendant, RightMostDescendant; |
39 | int Depth, Height, Shift = 0; |
40 | ast_type_traits::DynTypedNode ASTNode; |
41 | SmallVector<NodeId, 4> Children; |
42 | ChangeKind Change = None; |
43 | |
44 | ast_type_traits::ASTNodeKind getType() const; |
45 | StringRef getTypeLabel() const; |
46 | bool isLeaf() const { return Children.empty(); } |
47 | llvm::Optional<StringRef> getIdentifier() const; |
48 | llvm::Optional<std::string> getQualifiedIdentifier() const; |
49 | }; |
50 | |
51 | class ASTDiff { |
52 | public: |
53 | ASTDiff(SyntaxTree &Src, SyntaxTree &Dst, const ComparisonOptions &Options); |
54 | ~ASTDiff(); |
55 | |
56 | |
57 | NodeId getMapped(const SyntaxTree &SourceTree, NodeId Id) const; |
58 | |
59 | class Impl; |
60 | |
61 | private: |
62 | std::unique_ptr<Impl> DiffImpl; |
63 | }; |
64 | |
65 | |
66 | |
67 | class SyntaxTree { |
68 | public: |
69 | |
70 | SyntaxTree(ASTContext &AST); |
71 | |
72 | template <class T> |
73 | SyntaxTree(T *Node, ASTContext &AST) |
74 | : TreeImpl(llvm::make_unique<Impl>(this, Node, AST)) {} |
75 | SyntaxTree(SyntaxTree &&Other) = default; |
76 | ~SyntaxTree(); |
77 | |
78 | const ASTContext &getASTContext() const; |
79 | StringRef getFilename() const; |
80 | |
81 | int getSize() const; |
82 | NodeId getRootId() const; |
83 | using PreorderIterator = NodeId; |
84 | PreorderIterator begin() const; |
85 | PreorderIterator end() const; |
86 | |
87 | const Node &getNode(NodeId Id) const; |
88 | int findPositionInParent(NodeId Id) const; |
89 | |
90 | |
91 | std::pair<unsigned, unsigned> getSourceRangeOffsets(const Node &N) const; |
92 | |
93 | |
94 | |
95 | |
96 | std::string getNodeValue(NodeId Id) const; |
97 | std::string getNodeValue(const Node &Node) const; |
98 | |
99 | class Impl; |
100 | std::unique_ptr<Impl> TreeImpl; |
101 | }; |
102 | |
103 | struct ComparisonOptions { |
104 | |
105 | int MinHeight = 2; |
106 | |
107 | |
108 | |
109 | double MinSimilarity = 0.5; |
110 | |
111 | |
112 | |
113 | int MaxSize = 100; |
114 | |
115 | bool StopAfterTopDown = false; |
116 | |
117 | |
118 | bool isMatchingAllowed(const Node &N1, const Node &N2) const { |
119 | return N1.getType().isSame(N2.getType()); |
120 | } |
121 | }; |
122 | |
123 | } |
124 | } |
125 | |
126 | #endif |
127 | |