[cfe-dev] Refactoring/Rewriter questions

Manuel Klimek klimek at google.com
Sun Jun 5 11:16:33 PDT 2011


Hi Terry,

we've been doing similar code transformations (over bigger code bases)
and I've been working on getting the framework we're using back into
the clang codebase - whether the patches will go in is currently open
for discussion:
http://lists.cs.uiuc.edu/pipermail/cfe-dev/2011-June/015434.html
An example how you can implement code transformations similar to what
you want: http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/Tooling/RemoveCStrCalls/RemoveCStrCalls.cpp?revision=132374&view=markup&pathrev=132461

I'm also working on a follow-up patch, under review here:
http://codereview.appspot.com/4526098/
which adds support to allow to run those code transformations over
multiple translation units in one process, as that's important to do
the deduplication of changes that appear multiple times (for example
when the same header is included in multiple translation units).

The open issues with that framework are:
- parallelization: currently if we run in one process, we only run one
translation unit at a time; what we're doing so far as a workaround is
output textual representations of the changes, run many tools in
parallel and have a python script that does the deduplication and
application of the changes; for a 1MLOC code base running in serial is
probably on the edge of bearable
- re-layouting: replacing arbitrary text regions can make the code
look broken; we need a clang-based layout tool

Cheers,
/Manuel

On Sat, Jun 4, 2011 at 7:49 PM, Terry Wilson <twilson at digium.com> wrote:
> I have a code base of around 1M lines and want to make one of the most used (and fairly huge) structures opaque--adding accessor functions. So basically taking something like:
>
> struct foo {
>   char *bar;
>   int baz;
>   struct foo *other;
> };
>
> struct foo *f;
> ...
> printf("%d: %s\n", f->baz, get_other_foo(f)->bar);
>
> and turning it into
>
> struct foo *f;
> ...
> printf("%d: %s\n", get_foo_baz(f), get_foo_bar(get_other_foo(f));
>
> My initial thought was to just write accessor functions, then try to write a clang plugin and call with the -add-plugin option to do the source rewriting to use them as the project compiled. Maybe displaying the recommended change and prompting for oversight.  Is this even possible? Difficult? I've been reading over the doxygen docs and the objc rewriter for the last couple of days and it seems like getter functions should be doable.  Am I looking in the right places? Anyone have any tips/pointers? Has someone already written a plugin to do something similar that I just haven't found?
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>




More information about the cfe-dev mailing list