File indexing completed on 2026-05-10 08:37:10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #ifndef LLVM_CLANG_TOOLING_ASTDIFF_ASTDIFF_H
0020 #define LLVM_CLANG_TOOLING_ASTDIFF_ASTDIFF_H
0021
0022 #include "clang/Tooling/ASTDiff/ASTDiffInternal.h"
0023 #include <optional>
0024
0025 namespace clang {
0026 namespace diff {
0027
0028 enum ChangeKind {
0029 None,
0030 Delete,
0031 Update,
0032 Insert,
0033 Move,
0034 UpdateMove
0035 };
0036
0037
0038 struct Node {
0039 NodeId Parent, LeftMostDescendant, RightMostDescendant;
0040 int Depth, Height, Shift = 0;
0041 DynTypedNode ASTNode;
0042 SmallVector<NodeId, 4> Children;
0043 ChangeKind Change = None;
0044
0045 ASTNodeKind getType() const;
0046 StringRef getTypeLabel() const;
0047 bool isLeaf() const { return Children.empty(); }
0048 std::optional<StringRef> getIdentifier() const;
0049 std::optional<std::string> getQualifiedIdentifier() const;
0050 };
0051
0052
0053
0054 class SyntaxTree {
0055 public:
0056
0057 SyntaxTree(ASTContext &AST);
0058
0059 template <class T>
0060 SyntaxTree(T *Node, ASTContext &AST)
0061 : TreeImpl(std::make_unique<Impl>(this, Node, AST)) {}
0062 SyntaxTree(SyntaxTree &&Other) = default;
0063 ~SyntaxTree();
0064
0065 const ASTContext &getASTContext() const;
0066 StringRef getFilename() const;
0067
0068 int getSize() const;
0069 NodeId getRootId() const;
0070 using PreorderIterator = NodeId;
0071 PreorderIterator begin() const;
0072 PreorderIterator end() const;
0073
0074 const Node &getNode(NodeId Id) const;
0075 int findPositionInParent(NodeId Id) const;
0076
0077
0078 std::pair<unsigned, unsigned> getSourceRangeOffsets(const Node &N) const;
0079
0080
0081
0082
0083 std::string getNodeValue(NodeId Id) const;
0084 std::string getNodeValue(const Node &Node) const;
0085
0086 class Impl;
0087 std::unique_ptr<Impl> TreeImpl;
0088 };
0089
0090 struct ComparisonOptions {
0091
0092 int MinHeight = 2;
0093
0094
0095
0096 double MinSimilarity = 0.5;
0097
0098
0099
0100 int MaxSize = 100;
0101
0102 bool StopAfterTopDown = false;
0103
0104
0105 bool isMatchingAllowed(const Node &N1, const Node &N2) const {
0106 return N1.getType().isSame(N2.getType());
0107 }
0108 };
0109
0110 class ASTDiff {
0111 public:
0112 ASTDiff(SyntaxTree &Src, SyntaxTree &Dst, const ComparisonOptions &Options);
0113 ~ASTDiff();
0114
0115
0116 NodeId getMapped(const SyntaxTree &SourceTree, NodeId Id) const;
0117
0118 class Impl;
0119
0120 private:
0121 std::unique_ptr<Impl> DiffImpl;
0122 };
0123
0124 }
0125 }
0126
0127 #endif