How to slice the source code?

Hi all,

I’m doing the program slicing [1] in LLVM. Now I implemented the Weiser’s algorithm [2] in a simple way by writing a plugin to analyze the IR. My final goal is to slice the source code, thus I recorded all the lines of source code to be sliced. The last step is to delete the lines which are not in the program slice. However, I met a problem when deleting the source code directly:

To make it clear, I gave an example following, in which the following lines (underline decoration) can be deleted. We can see that the line 4 should also be deleted, but it’s not.

1 for (int i = 0; i < BUFSIZE; i++) {
2 for (int j = 0; j < np; j++)
3 buf[i] = ((rank + j) % np);
4 }

I got the LoC info from meta-data of the IR which can be deleted. However, there is no according IR in the first place, which represents the line 4 (whose functionality is to end a block). I’m not familiar with clang and its AST. A friend told me to parse the code using python to find the matching “}” (or END in Fortran) and delete them. I’d like to know if there is an LLVM way to do this.

Any idea?

Thank you!



maybe you can have a look at the CReduce project:

This project implements many source-to-source transformations of C programs, e.g. removing functions, changing variable names, deleting branches in if-statements, etc. Some of these are purely based on the source code, other use the Clang frontend.

Hope this helps,

It will definitely help me a lot.

Thank you.