[PATCH] D36179: [clang-diff] Move printing of matches and changes to clang-diff
This revision was automatically updated to reflect the committed changes. Closed by commit rL311200: [clang-diff] Move printing of matches and changes to clang-diff (authored by krobelus). Repository: rL LLVM https://reviews.llvm.org/D36179 Files: cfe/trunk/include/clang/Tooling/ASTDiff/ASTDiff.h cfe/trunk/include/clang/Tooling/ASTDiff/ASTDiffInternal.h cfe/trunk/lib/Tooling/ASTDiff/ASTDiff.cpp cfe/trunk/test/Tooling/clang-diff-basic.cpp cfe/trunk/tools/clang-diff/ClangDiff.cpp Index: cfe/trunk/tools/clang-diff/ClangDiff.cpp === --- cfe/trunk/tools/clang-diff/ClangDiff.cpp +++ cfe/trunk/tools/clang-diff/ClangDiff.cpp @@ -129,7 +129,7 @@ auto Offsets = Tree.getSourceRangeOffsets(N); OS << R"(,"begin":)" << Offsets.first; OS << R"(,"end":)" << Offsets.second; - std::string Value = Tree.getNodeValue(N.ASTNode); + std::string Value = Tree.getNodeValue(N); if (!Value.empty()) { OS << R"(,"value":")"; printJsonString(OS, Value); @@ -153,6 +153,52 @@ OS << "]}"; } +static void printNode(raw_ostream &OS, diff::SyntaxTree &Tree, + diff::NodeId Id) { + if (Id.isInvalid()) { +OS << "None"; +return; + } + OS << Tree.getNode(Id).getTypeLabel(); + std::string Value = Tree.getNodeValue(Id); + if (!Value.empty()) +OS << ": " << Value; + OS << "(" << Id << ")"; +} + +static void printDstChange(raw_ostream &OS, diff::ASTDiff &Diff, + diff::SyntaxTree &SrcTree, diff::SyntaxTree &DstTree, + diff::NodeId Dst) { + const diff::Node &DstNode = DstTree.getNode(Dst); + diff::NodeId Src = Diff.getMapped(DstTree, Dst); + switch (DstNode.ChangeKind) { + case diff::None: +break; + case diff::Delete: +llvm_unreachable("The destination tree can't have deletions."); + case diff::Update: +OS << "Update "; +printNode(OS, SrcTree, Src); +OS << " to " << DstTree.getNodeValue(Dst) << "\n"; +break; + case diff::Insert: + case diff::Move: + case diff::UpdateMove: +if (DstNode.ChangeKind == diff::Insert) + OS << "Insert"; +else if (DstNode.ChangeKind == diff::Move) + OS << "Move"; +else if (DstNode.ChangeKind == diff::UpdateMove) + OS << "Update and Move"; +OS << " "; +printNode(OS, DstTree, Dst); +OS << " into "; +printNode(OS, DstTree, DstNode.Parent); +OS << " at " << DstTree.findPositionInParent(Dst) << "\n"; +break; + } +} + int main(int argc, const char **argv) { std::string ErrorMessage; std::unique_ptr CommonCompilations = @@ -199,11 +245,26 @@ Options.MaxSize = MaxSize; diff::SyntaxTree SrcTree(Src->getASTContext()); diff::SyntaxTree DstTree(Dst->getASTContext()); - diff::ASTDiff DiffTool(SrcTree, DstTree, Options); - for (const auto &Match : DiffTool.getMatches()) -DiffTool.printMatch(llvm::outs(), Match); - for (const auto &Change : DiffTool.getChanges()) -DiffTool.printChange(llvm::outs(), Change); + diff::ASTDiff Diff(SrcTree, DstTree, Options); + + for (diff::NodeId Dst : DstTree) { +diff::NodeId Src = Diff.getMapped(DstTree, Dst); +if (Src.isValid()) { + llvm::outs() << "Match "; + printNode(llvm::outs(), SrcTree, Src); + llvm::outs() << " to "; + printNode(llvm::outs(), DstTree, Dst); + llvm::outs() << "\n"; +} +printDstChange(llvm::outs(), Diff, SrcTree, DstTree, Dst); + } + for (diff::NodeId Src : SrcTree) { +if (Diff.getMapped(SrcTree, Src).isInvalid()) { + llvm::outs() << "Delete "; + printNode(llvm::outs(), SrcTree, Src); + llvm::outs() << "\n"; +} + } return 0; } Index: cfe/trunk/include/clang/Tooling/ASTDiff/ASTDiff.h === --- cfe/trunk/include/clang/Tooling/ASTDiff/ASTDiff.h +++ cfe/trunk/include/clang/Tooling/ASTDiff/ASTDiff.h @@ -25,37 +25,22 @@ namespace clang { namespace diff { -/// This represents a match between two nodes in the source and destination -/// trees, meaning that they are likely to be related. -struct Match { - NodeId Src, Dst; -}; - enum ChangeKind { - Delete, // (Src): delete node Src. - Update, // (Src, Dst): update the value of node Src to match Dst. - Insert, // (Src, Dst, Pos): insert Src as child of Dst at offset Pos. - Move// (Src, Dst, Pos): move Src to be a child of Dst at offset Pos. -}; - -struct Change { - ChangeKind Kind; - NodeId Src, Dst; - size_t Position; - - Change(ChangeKind Kind, NodeId Src, NodeId Dst, size_t Position) - : Kind(Kind), Src(Src), Dst(Dst), Position(Position) {} - Change(ChangeKind Kind, NodeId Src) : Kind(Kind), Src(Src) {} - Change(ChangeKind Kind, NodeId Src, NodeId Dst) - : Kind(Kind), Src(Src), Dst(Dst) {} + None, + Delete,// (Src): delete node Src. + Update,// (Src, Dst): update the value of node Src to match Dst. + Insert,// (Src, Dst, Pos): insert Src as child of Dst at
[PATCH] D36179: [clang-diff] Move printing of matches and changes to clang-diff
johannes updated this revision to Diff 110945. johannes added a comment. format https://reviews.llvm.org/D36179 Files: include/clang/Tooling/ASTDiff/ASTDiff.h include/clang/Tooling/ASTDiff/ASTDiffInternal.h lib/Tooling/ASTDiff/ASTDiff.cpp test/Tooling/clang-diff-basic.cpp tools/clang-diff/ClangDiff.cpp Index: tools/clang-diff/ClangDiff.cpp === --- tools/clang-diff/ClangDiff.cpp +++ tools/clang-diff/ClangDiff.cpp @@ -129,7 +129,7 @@ auto Offsets = Tree.getSourceRangeOffsets(N); OS << R"(,"begin":)" << Offsets.first; OS << R"(,"end":)" << Offsets.second; - std::string Value = Tree.getNodeValue(N.ASTNode); + std::string Value = Tree.getNodeValue(N); if (!Value.empty()) { OS << R"(,"value":")"; printJsonString(OS, Value); @@ -153,6 +153,52 @@ OS << "]}"; } +static void printNode(raw_ostream &OS, diff::SyntaxTree &Tree, + diff::NodeId Id) { + if (Id.isInvalid()) { +OS << "None"; +return; + } + OS << Tree.getNode(Id).getTypeLabel(); + std::string Value = Tree.getNodeValue(Id); + if (!Value.empty()) +OS << ": " << Value; + OS << "(" << Id << ")"; +} + +static void printDstChange(raw_ostream &OS, diff::ASTDiff &Diff, + diff::SyntaxTree &SrcTree, diff::SyntaxTree &DstTree, + diff::NodeId Dst) { + const diff::Node &DstNode = DstTree.getNode(Dst); + diff::NodeId Src = Diff.getMapped(DstTree, Dst); + switch (DstNode.ChangeKind) { + case diff::None: +break; + case diff::Delete: +llvm_unreachable("The destination tree can't have deletions."); + case diff::Update: +OS << "Update "; +printNode(OS, SrcTree, Src); +OS << " to " << DstTree.getNodeValue(Dst) << "\n"; +break; + case diff::Insert: + case diff::Move: + case diff::UpdateMove: +if (DstNode.ChangeKind == diff::Insert) + OS << "Insert"; +else if (DstNode.ChangeKind == diff::Move) + OS << "Move"; +else if (DstNode.ChangeKind == diff::UpdateMove) + OS << "Update and Move"; +OS << " "; +printNode(OS, DstTree, Dst); +OS << " into "; +printNode(OS, DstTree, DstNode.Parent); +OS << " at " << DstTree.findPositionInParent(Dst) << "\n"; +break; + } +} + int main(int argc, const char **argv) { std::string ErrorMessage; std::unique_ptr CommonCompilations = @@ -199,11 +245,26 @@ Options.MaxSize = MaxSize; diff::SyntaxTree SrcTree(Src->getASTContext()); diff::SyntaxTree DstTree(Dst->getASTContext()); - diff::ASTDiff DiffTool(SrcTree, DstTree, Options); - for (const auto &Match : DiffTool.getMatches()) -DiffTool.printMatch(llvm::outs(), Match); - for (const auto &Change : DiffTool.getChanges()) -DiffTool.printChange(llvm::outs(), Change); + diff::ASTDiff Diff(SrcTree, DstTree, Options); + + for (diff::NodeId Dst : DstTree) { +diff::NodeId Src = Diff.getMapped(DstTree, Dst); +if (Src.isValid()) { + llvm::outs() << "Match "; + printNode(llvm::outs(), SrcTree, Src); + llvm::outs() << " to "; + printNode(llvm::outs(), DstTree, Dst); + llvm::outs() << "\n"; +} +printDstChange(llvm::outs(), Diff, SrcTree, DstTree, Dst); + } + for (diff::NodeId Src : SrcTree) { +if (Diff.getMapped(SrcTree, Src).isInvalid()) { + llvm::outs() << "Delete "; + printNode(llvm::outs(), SrcTree, Src); + llvm::outs() << "\n"; +} + } return 0; } Index: test/Tooling/clang-diff-basic.cpp === --- test/Tooling/clang-diff-basic.cpp +++ test/Tooling/clang-diff-basic.cpp @@ -31,6 +31,10 @@ int id(int i) { return i; } }; } + +void m() { int x = 0 + 0 + 0; } +int um = 1 + 2 + 3; + #else // CHECK: Match TranslationUnitDecl{{.*}} to TranslationUnitDecl // CHECK: Match NamespaceDecl: src{{.*}} to NamespaceDecl: dst @@ -54,8 +58,8 @@ typedef unsigned nat; // CHECK: Match VarDecl: p(int){{.*}} to VarDecl: prod(double) -// CHECK: Match BinaryOperator: *{{.*}} to BinaryOperator: * // CHECK: Update VarDecl: p(int){{.*}} to prod(double) +// CHECK: Match BinaryOperator: *{{.*}} to BinaryOperator: * double prod = 1 * 2 * 10; // CHECK: Update DeclRefExpr int squared = prod * prod; @@ -70,9 +74,15 @@ return "foo"; return 0; } - // CHECK: Delete AccessSpecDecl: public - X(){}; - // CHECK: Delete CXXMethodDecl + X(){} }; } + +// CHECK: Move DeclStmt{{.*}} into CompoundStmt +void m() { { int x = 0 + 0 + 0; } } +// CHECK: Update and Move IntegerLiteral: 7{{.*}} into BinaryOperator: +({{.*}}) at 1 +int um = 1 + 7; #endif + +// CHECK: Delete AccessSpecDecl: public +// CHECK: Delete CXXMethodDecl Index: lib/Tooling/ASTDiff/ASTDiff.cpp === --- lib/Tooling/ASTDiff/ASTDiff.cpp +++ lib/Tooling/ASTDiff/ASTDiff.cpp @@ -82,26 +82,24 @@ class ASTDiff::Impl { public: SyntaxTree::Impl &T1, &T2; - boo
[PATCH] D36179: [clang-diff] Move printing of matches and changes to clang-diff
arphaman accepted this revision. arphaman added a comment. This revision is now accepted and ready to land. LGTM, with one request below: Comment at: lib/Tooling/ASTDiff/ASTDiff.cpp:96 - /// Finds an edit script that converts T1 to T2. - std::vector computeChanges(Mapping &M); - - void printChangeImpl(raw_ostream &OS, const Change &Chg) const; - void printMatchImpl(raw_ostream &OS, const Match &M) const; - - // Returns a mapping of identical subtrees. - Mapping matchTopDown() const; + NodeId getMapped(const std::unique_ptr &Tree, NodeId Id) const { +if (&*Tree == &T1) 80 column violation, please re-format. https://reviews.llvm.org/D36179 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D36179: [clang-diff] Move printing of matches and changes to clang-diff
johannes updated this revision to Diff 110608. johannes added a comment. initialize Node::Shift https://reviews.llvm.org/D36179 Files: include/clang/Tooling/ASTDiff/ASTDiff.h include/clang/Tooling/ASTDiff/ASTDiffInternal.h lib/Tooling/ASTDiff/ASTDiff.cpp test/Tooling/clang-diff-basic.cpp tools/clang-diff/ClangDiff.cpp Index: tools/clang-diff/ClangDiff.cpp === --- tools/clang-diff/ClangDiff.cpp +++ tools/clang-diff/ClangDiff.cpp @@ -129,7 +129,7 @@ auto Offsets = Tree.getSourceRangeOffsets(N); OS << R"(,"begin":)" << Offsets.first; OS << R"(,"end":)" << Offsets.second; - std::string Value = Tree.getNodeValue(N.ASTNode); + std::string Value = Tree.getNodeValue(N); if (!Value.empty()) { OS << R"(,"value":")"; printJsonString(OS, Value); @@ -153,6 +153,52 @@ OS << "]}"; } +static void printNode(raw_ostream &OS, diff::SyntaxTree &Tree, + diff::NodeId Id) { + if (Id.isInvalid()) { +OS << "None"; +return; + } + OS << Tree.getNode(Id).getTypeLabel(); + std::string Value = Tree.getNodeValue(Id); + if (!Value.empty()) +OS << ": " << Value; + OS << "(" << Id << ")"; +} + +static void printDstChange(raw_ostream &OS, diff::ASTDiff &Diff, + diff::SyntaxTree &SrcTree, diff::SyntaxTree &DstTree, + diff::NodeId Dst) { + const diff::Node &DstNode = DstTree.getNode(Dst); + diff::NodeId Src = Diff.getMapped(DstTree, Dst); + switch (DstNode.ChangeKind) { + case diff::None: +break; + case diff::Delete: +llvm_unreachable("The destination tree can't have deletions."); + case diff::Update: +OS << "Update "; +printNode(OS, SrcTree, Src); +OS << " to " << DstTree.getNodeValue(Dst) << "\n"; +break; + case diff::Insert: + case diff::Move: + case diff::UpdateMove: +if (DstNode.ChangeKind == diff::Insert) + OS << "Insert"; +else if (DstNode.ChangeKind == diff::Move) + OS << "Move"; +else if (DstNode.ChangeKind == diff::UpdateMove) + OS << "Update and Move"; +OS << " "; +printNode(OS, DstTree, Dst); +OS << " into "; +printNode(OS, DstTree, DstNode.Parent); +OS << " at " << DstTree.findPositionInParent(Dst) << "\n"; +break; + } +} + int main(int argc, const char **argv) { std::string ErrorMessage; std::unique_ptr CommonCompilations = @@ -199,11 +245,26 @@ Options.MaxSize = MaxSize; diff::SyntaxTree SrcTree(Src->getASTContext()); diff::SyntaxTree DstTree(Dst->getASTContext()); - diff::ASTDiff DiffTool(SrcTree, DstTree, Options); - for (const auto &Match : DiffTool.getMatches()) -DiffTool.printMatch(llvm::outs(), Match); - for (const auto &Change : DiffTool.getChanges()) -DiffTool.printChange(llvm::outs(), Change); + diff::ASTDiff Diff(SrcTree, DstTree, Options); + + for (diff::NodeId Dst : DstTree) { +diff::NodeId Src = Diff.getMapped(DstTree, Dst); +if (Src.isValid()) { + llvm::outs() << "Match "; + printNode(llvm::outs(), SrcTree, Src); + llvm::outs() << " to "; + printNode(llvm::outs(), DstTree, Dst); + llvm::outs() << "\n"; +} +printDstChange(llvm::outs(), Diff, SrcTree, DstTree, Dst); + } + for (diff::NodeId Src : SrcTree) { +if (Diff.getMapped(SrcTree, Src).isInvalid()) { + llvm::outs() << "Delete "; + printNode(llvm::outs(), SrcTree, Src); + llvm::outs() << "\n"; +} + } return 0; } Index: test/Tooling/clang-diff-basic.cpp === --- test/Tooling/clang-diff-basic.cpp +++ test/Tooling/clang-diff-basic.cpp @@ -31,6 +31,10 @@ int id(int i) { return i; } }; } + +void m() { int x = 0 + 0 + 0; } +int um = 1 + 2 + 3; + #else // CHECK: Match TranslationUnitDecl{{.*}} to TranslationUnitDecl // CHECK: Match NamespaceDecl: src{{.*}} to NamespaceDecl: dst @@ -54,8 +58,8 @@ typedef unsigned nat; // CHECK: Match VarDecl: p(int){{.*}} to VarDecl: prod(double) -// CHECK: Match BinaryOperator: *{{.*}} to BinaryOperator: * // CHECK: Update VarDecl: p(int){{.*}} to prod(double) +// CHECK: Match BinaryOperator: *{{.*}} to BinaryOperator: * double prod = 1 * 2 * 10; // CHECK: Update DeclRefExpr int squared = prod * prod; @@ -70,9 +74,15 @@ return "foo"; return 0; } - // CHECK: Delete AccessSpecDecl: public - X(){}; - // CHECK: Delete CXXMethodDecl + X(){} }; } + +// CHECK: Move DeclStmt{{.*}} into CompoundStmt +void m() { { int x = 0 + 0 + 0; } } +// CHECK: Update and Move IntegerLiteral: 7{{.*}} into BinaryOperator: +({{.*}}) at 1 +int um = 1 + 7; #endif + +// CHECK: Delete AccessSpecDecl: public +// CHECK: Delete CXXMethodDecl Index: lib/Tooling/ASTDiff/ASTDiff.cpp === --- lib/Tooling/ASTDiff/ASTDiff.cpp +++ lib/Tooling/ASTDiff/ASTDiff.cpp @@ -82,26 +82,23 @@ class ASTDiff::Impl { public: SyntaxTree::Impl
[PATCH] D36179: [clang-diff] Move printing of matches and changes to clang-diff
arphaman added inline comments. Comment at: include/clang/Tooling/ASTDiff/ASTDiff.h:40 NodeId Parent, LeftMostDescendant, RightMostDescendant; - int Depth, Height; + int Depth, Height, Shift; ast_type_traits::DynTypedNode ASTNode; Looks like `Shift` isn't initialized anywhere. https://reviews.llvm.org/D36179 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D36179: [clang-diff] Move printing of matches and changes to clang-diff
johannes updated this revision to Diff 110552. johannes added a comment. add test for 'Move' and 'Update and Move' in output https://reviews.llvm.org/D36179 Files: include/clang/Tooling/ASTDiff/ASTDiff.h include/clang/Tooling/ASTDiff/ASTDiffInternal.h lib/Tooling/ASTDiff/ASTDiff.cpp test/Tooling/clang-diff-basic.cpp tools/clang-diff/ClangDiff.cpp Index: tools/clang-diff/ClangDiff.cpp === --- tools/clang-diff/ClangDiff.cpp +++ tools/clang-diff/ClangDiff.cpp @@ -129,7 +129,7 @@ auto Offsets = Tree.getSourceRangeOffsets(N); OS << R"(,"begin":)" << Offsets.first; OS << R"(,"end":)" << Offsets.second; - std::string Value = Tree.getNodeValue(N.ASTNode); + std::string Value = Tree.getNodeValue(N); if (!Value.empty()) { OS << R"(,"value":")"; printJsonString(OS, Value); @@ -153,6 +153,52 @@ OS << "]}"; } +static void printNode(raw_ostream &OS, diff::SyntaxTree &Tree, + diff::NodeId Id) { + if (Id.isInvalid()) { +OS << "None"; +return; + } + OS << Tree.getNode(Id).getTypeLabel(); + std::string Value = Tree.getNodeValue(Id); + if (!Value.empty()) +OS << ": " << Value; + OS << "(" << Id << ")"; +} + +static void printDstChange(raw_ostream &OS, diff::ASTDiff &Diff, + diff::SyntaxTree &SrcTree, diff::SyntaxTree &DstTree, + diff::NodeId Dst) { + const diff::Node &DstNode = DstTree.getNode(Dst); + diff::NodeId Src = Diff.getMapped(DstTree, Dst); + switch (DstNode.ChangeKind) { + case diff::None: +break; + case diff::Delete: +llvm_unreachable("The destination tree can't have deletions."); + case diff::Update: +OS << "Update "; +printNode(OS, SrcTree, Src); +OS << " to " << DstTree.getNodeValue(Dst) << "\n"; +break; + case diff::Insert: + case diff::Move: + case diff::UpdateMove: +if (DstNode.ChangeKind == diff::Insert) + OS << "Insert"; +else if (DstNode.ChangeKind == diff::Move) + OS << "Move"; +else if (DstNode.ChangeKind == diff::UpdateMove) + OS << "Update and Move"; +OS << " "; +printNode(OS, DstTree, Dst); +OS << " into "; +printNode(OS, DstTree, DstNode.Parent); +OS << " at " << DstTree.findPositionInParent(Dst) << "\n"; +break; + } +} + int main(int argc, const char **argv) { std::string ErrorMessage; std::unique_ptr CommonCompilations = @@ -199,11 +245,26 @@ Options.MaxSize = MaxSize; diff::SyntaxTree SrcTree(Src->getASTContext()); diff::SyntaxTree DstTree(Dst->getASTContext()); - diff::ASTDiff DiffTool(SrcTree, DstTree, Options); - for (const auto &Match : DiffTool.getMatches()) -DiffTool.printMatch(llvm::outs(), Match); - for (const auto &Change : DiffTool.getChanges()) -DiffTool.printChange(llvm::outs(), Change); + diff::ASTDiff Diff(SrcTree, DstTree, Options); + + for (diff::NodeId Dst : DstTree) { +diff::NodeId Src = Diff.getMapped(DstTree, Dst); +if (Src.isValid()) { + llvm::outs() << "Match "; + printNode(llvm::outs(), SrcTree, Src); + llvm::outs() << " to "; + printNode(llvm::outs(), DstTree, Dst); + llvm::outs() << "\n"; +} +printDstChange(llvm::outs(), Diff, SrcTree, DstTree, Dst); + } + for (diff::NodeId Src : SrcTree) { +if (Diff.getMapped(SrcTree, Src).isInvalid()) { + llvm::outs() << "Delete "; + printNode(llvm::outs(), SrcTree, Src); + llvm::outs() << "\n"; +} + } return 0; } Index: test/Tooling/clang-diff-basic.cpp === --- test/Tooling/clang-diff-basic.cpp +++ test/Tooling/clang-diff-basic.cpp @@ -31,6 +31,10 @@ int id(int i) { return i; } }; } + +void m() { int x = 0 + 0 + 0; } +int um = 1 + 2 + 3; + #else // CHECK: Match TranslationUnitDecl{{.*}} to TranslationUnitDecl // CHECK: Match NamespaceDecl: src{{.*}} to NamespaceDecl: dst @@ -54,8 +58,8 @@ typedef unsigned nat; // CHECK: Match VarDecl: p(int){{.*}} to VarDecl: prod(double) -// CHECK: Match BinaryOperator: *{{.*}} to BinaryOperator: * // CHECK: Update VarDecl: p(int){{.*}} to prod(double) +// CHECK: Match BinaryOperator: *{{.*}} to BinaryOperator: * double prod = 1 * 2 * 10; // CHECK: Update DeclRefExpr int squared = prod * prod; @@ -70,9 +74,15 @@ return "foo"; return 0; } - // CHECK: Delete AccessSpecDecl: public - X(){}; - // CHECK: Delete CXXMethodDecl + X(){} }; } + +// CHECK: Move DeclStmt{{.*}} into CompoundStmt +void m() { { int x = 0 + 0 + 0; } } +// CHECK: Update and Move IntegerLiteral: 7{{.*}} into BinaryOperator: +({{.*}}) at 1 +int um = 1 + 7; #endif + +// CHECK: Delete AccessSpecDecl: public +// CHECK: Delete CXXMethodDecl Index: lib/Tooling/ASTDiff/ASTDiff.cpp === --- lib/Tooling/ASTDiff/ASTDiff.cpp +++ lib/Tooling/ASTDiff/ASTDiff.cpp @@ -82,26 +82,23 @@ class ASTDiff::Impl {
[PATCH] D36179: [clang-diff] Move printing of matches and changes to clang-diff
arphaman added a comment. Previous `Move` resulted in `llvm_unreachable("TODO");`, but now it seems to be fixed. The new output for `Move` and `UpdateMove` should be tested in a test. https://reviews.llvm.org/D36179 ___ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D36179: [clang-diff] Move printing of matches and changes to clang-diff
johannes updated this revision to Diff 109417. johannes added a comment. remove unused SubtreeIterator https://reviews.llvm.org/D36179 Files: include/clang/Tooling/ASTDiff/ASTDiff.h include/clang/Tooling/ASTDiff/ASTDiffInternal.h lib/Tooling/ASTDiff/ASTDiff.cpp test/Tooling/clang-diff-basic.cpp tools/clang-diff/ClangDiff.cpp Index: tools/clang-diff/ClangDiff.cpp === --- tools/clang-diff/ClangDiff.cpp +++ tools/clang-diff/ClangDiff.cpp @@ -134,7 +134,7 @@ auto Offsets = Tree.getSourceRangeOffsets(N); OS << R"(,"begin":)" << Offsets.first; OS << R"(,"end":)" << Offsets.second; - std::string Value = Tree.getNodeValue(N.ASTNode); + std::string Value = Tree.getNodeValue(N); if (!Value.empty()) { OS << R"(,"value":")"; printJsonString(OS, Value); @@ -158,6 +158,52 @@ OS << "]}"; } +static void printNode(raw_ostream &OS, diff::SyntaxTree &Tree, + diff::NodeId Id) { + if (Id.isInvalid()) { +OS << "None"; +return; + } + OS << Tree.getNode(Id).getTypeLabel(); + std::string Value = Tree.getNodeValue(Id); + if (!Value.empty()) +OS << ": " << Value; + OS << "(" << Id << ")"; +} + +static void printDstChange(raw_ostream &OS, diff::ASTDiff &Diff, + diff::SyntaxTree &SrcTree, diff::SyntaxTree &DstTree, + diff::NodeId Dst) { + const diff::Node &DstNode = DstTree.getNode(Dst); + diff::NodeId Src = Diff.getMapped(DstTree, Dst); + switch (DstNode.ChangeKind) { + case diff::None: +break; + case diff::Delete: +llvm_unreachable("The destination tree can't have deletions."); + case diff::Update: +OS << "Update "; +printNode(OS, SrcTree, Src); +OS << " to " << DstTree.getNodeValue(Dst) << "\n"; +break; + case diff::Insert: + case diff::Move: + case diff::UpdateMove: +if (DstNode.ChangeKind == diff::Insert) + OS << "Insert"; +else if (DstNode.ChangeKind == diff::Move) + OS << "Move"; +else if (DstNode.ChangeKind == diff::UpdateMove) + OS << "Update and Move"; +OS << " "; +printNode(OS, DstTree, Dst); +OS << " into "; +printNode(OS, DstTree, DstNode.Parent); +OS << " at " << DstTree.findPositionInParent(Dst) << "\n"; +break; + } +} + int main(int argc, const char **argv) { std::string ErrorMessage; std::unique_ptr CommonCompilations = @@ -212,11 +258,26 @@ } diff::SyntaxTree SrcTree(Src->getASTContext()); diff::SyntaxTree DstTree(Dst->getASTContext()); - diff::ASTDiff DiffTool(SrcTree, DstTree, Options); - for (const auto &Match : DiffTool.getMatches()) -DiffTool.printMatch(llvm::outs(), Match); - for (const auto &Change : DiffTool.getChanges()) -DiffTool.printChange(llvm::outs(), Change); + diff::ASTDiff Diff(SrcTree, DstTree, Options); + + for (diff::NodeId Dst : DstTree) { +diff::NodeId Src = Diff.getMapped(DstTree, Dst); +if (Src.isValid()) { + llvm::outs() << "Match "; + printNode(llvm::outs(), SrcTree, Src); + llvm::outs() << " to "; + printNode(llvm::outs(), DstTree, Dst); + llvm::outs() << "\n"; +} +printDstChange(llvm::outs(), Diff, SrcTree, DstTree, Dst); + } + for (diff::NodeId Src : SrcTree) { +if (Diff.getMapped(SrcTree, Src).isInvalid()) { + llvm::outs() << "Delete "; + printNode(llvm::outs(), SrcTree, Src); + llvm::outs() << "\n"; +} + } return 0; } Index: test/Tooling/clang-diff-basic.cpp === --- test/Tooling/clang-diff-basic.cpp +++ test/Tooling/clang-diff-basic.cpp @@ -54,8 +54,8 @@ typedef unsigned nat; // CHECK: Match VarDecl: p(int){{.*}} to VarDecl: prod(double) -// CHECK: Match BinaryOperator: *{{.*}} to BinaryOperator: * // CHECK: Update VarDecl: p(int){{.*}} to prod(double) +// CHECK: Match BinaryOperator: *{{.*}} to BinaryOperator: * double prod = 1 * 2 * 10; // CHECK: Update DeclRefExpr int squared = prod * prod; Index: lib/Tooling/ASTDiff/ASTDiff.cpp === --- lib/Tooling/ASTDiff/ASTDiff.cpp +++ lib/Tooling/ASTDiff/ASTDiff.cpp @@ -82,26 +82,23 @@ class ASTDiff::Impl { public: SyntaxTree::Impl &T1, &T2; - bool IsMappingDone = false; Mapping TheMapping; Impl(SyntaxTree::Impl &T1, SyntaxTree::Impl &T2, - const ComparisonOptions &Options) - : T1(T1), T2(T2), Options(Options) {} + const ComparisonOptions &Options); /// Matches nodes one-by-one based on their similarity. void computeMapping(); - std::vector getMatches(Mapping &M); + // Compute ChangeKind for each node based on similarity. + void computeChangeKinds(Mapping &M); - /// Finds an edit script that converts T1 to T2. - std::vector computeChanges(Mapping &M); - - void printChangeImpl(raw_ostream &OS, const Change &Chg) const; - void printMatchImpl(raw_ostream &OS, const Match &M) const; - -