This commit adds a function diff::patch that takes three syntax trees
as inputs: a source, a destination and a target. It tries to apply the
differences between source and destination to target. Currently,
comments and preprocessor statements are ignored.
Details
- Reviewers
arphaman
Diff Detail
- Build Status
Buildable 11858 Build 11858: arc lint + arc unit
Event Timeline
I don't think AST manipulation is the right way to do patching. You've already hit the limitations here in this patch where you can't really remove a statement unless its parent is a CompoundStmt.
I think the right way to do AST patching is to take a 3rd AST(lets call it a target AST), find the set of matching nodes that correspond to all nodes that have to be patched and then perform patching by rewriting the source for the target AST.
Let's say you want to start with remove. If you take the following files:
$ cat src.cpp void printf(const char *, ...); void foo(int x) { printf("%d", x, x); } $ cat dst.cpp void printf(const char *, ...); void foo(int x) { printf("%d", x); // the second 'x' is removed. } $ cat target.cpp void printf(const char *, ...); void foo(int x) { printf("different string %d", x, x); }
You'll find that the difference between src.cpp and dst.cpp is Delete DeclRefExpr: x(10). Then you can take target.cpp, find the matching DeclRefExpr node, and create a source replacement (see tooling's Replacement) that removes ", x" from target.cpp.
fixes
include/clang/Tooling/ASTDiff/ASTDiff.h | ||
---|---|---|
81 | ok, i use optional now instead in the unittest |
Why do you need to create an empty tree? What about using llvm::Optional instead?