[PATCH] D37005: Add include/clang/Tooling/ASTDiff/ASTPatch.h

Alex Lorenz via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 25 06:12:22 PDT 2017


arphaman added a comment.

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.


https://reviews.llvm.org/D37005





More information about the cfe-commits mailing list