Manipulate C-family ASTs with Clang
C++ Shell C
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
aur
etc
tools
.gitignore
ASTMutate.cpp
ASTMutate.h
Makefile
NOTES
README
clang-mutate.cpp

README

Manipulate C-family ASTs with Clang

This tool performs a number of operations on C-language source files.

      ids | prints the total number of statements
   number | numbers each statement
 annotate | annotate each statement with its class
     list | list every statement's id, class and range
      cut | cuts that numbered statement
   insert | copies the second numbered statement before the first
     swap | swaps the two numbered statements
      get | get the text of a stmt
      set | set the text of a stmt

Installation

  clang-mutate uses the clang LibTooling [1] interface.  Thus the
  clang-mutate executable works best when run from the same directory
  as clang.  Run "make install" to build and install.

  On Arch Linux, aur scripts are provided in aur/ which may be used to
  install both llvm/clang with header files and clang-mutate.  Known
  working versions are listed in the aur PKGBUILD files and in NOTES.

  [1] http://clang.llvm.org/docs/LibTooling.html

Examples

    $ cat etc/hello.c
    #include <stdio.h>
    int main(int argc, char *argv[])
    {
      puts("hello");
      return 0;
    }

    $ clang-mutate -ids etc/hello.c --
    Processing: hello.c.
    9

    $ clang-mutate -number etc/hello.c --
    Processing: hello.c.
    #include <stdio.h>
    int main(int argc, char *argv[])
    /* 0[ */{
      /* 3[ *//* 2[ *//* 1[ */puts/* ]2 *//* ]3 */(/* 6[ *//* 5[ *//* 4[ */"hello"/* ]4 *//* ]5 *//* ]6 */);/* ]1 */
      /* 7[ */return /* 8[ */0;/* ]7 *//* ]8 */
    }/* ]0 */

    $ clang-mutate -cut -stmt1=7 etc/hello.c --
    Processing: hello.c.
    #include <stdio.h>
    int main(int argc, char *argv[])
    {
      puts("hello");
      /* cut:7 */;
    }

    $ clang-mutate -set -stmt1=5 -value='"good bye"' etc/hello.c --
    Processing: /home/eschulte/src/clang-mutate/hello.c.
    #include <stdio.h>
    int main(int argc, char *argv[])
    {
      puts("good bye");
      return 0;
    }

  remove loop executions using the tools/perforate-loops script

    $ make etc/loop
    cc     etc/loop.c   -o etc/loop
    $ ./etc/loop
    hello 0
    hello 1
    hello 2
    hello 3
    hello 4
    hello 5
    hello 6
    hello 7
    hello 8
    hello 9
    $ ./tools/perforate-loops etc/loop.c > /tmp/faster.c
    $ make /tmp/faster
    cc     /tmp/faster.c   -o /tmp/faster
    $ /tmp/faster
    hello 0
    hello 2
    hello 4
    hello 6
    hello 8

  replace string literals using tools/strings-to

    $ ./tools/strings-to etc/hello.c foo
    #include <stdio.h>
    int main(int argc, char *argv[])
    {
      puts("foo");
      return 0;
    }