Gtk+ Automated Refactoring



Buenos dias,

Tim Janik wrote:
Hello Gtk+ Crowd.
...
Further comments are of course welcome, we intend to keep the
document updated as new issues are raised/solved.

The proposed plan to migrate to Gtk+ 3.0 is going to remove public struct
fields and introduce new accessors for these attributes.
I've been looking into automating that.

For instance:

  GTK_WIDGET (button)->style

will be invalid when running against Gtk+ 3.0 and have to be refactored into:

  gtk_widget_get_style (GTK_WIDGET (button))

These kind of refactorings can easily be automated using simple regexs.
by scanning for the macro syntax "GTK_WIDGET" and "->style".

I've written such a regex and generalized it into a python program which
can do multiple field accesses to multiple types.[1]

That program can successfully be run on the gtk+ tree for which it detects and "refactors" almost 500 field accesses. Some of them can be considered false positives as it's reasonable that the actual implementation of a widget should probably be allowed to access public fields.

However, not all field accesses are done using macros, eg:

  widget->style

Which is an example of a statement which cannot be converted using regexs, since the "widget" part can contain anything arbritary.

To be able to refactor that statement a better understanding of the
C language is required.
Fortunately someone else has thought of that problem and there are tools
available which can help in that process.
Elsa[1], is a C/C++ parser which has the option to output a C syntax tree
in XML form.

I spent a some time during the hackfest building Elsa and running it on
a source file in gtk+ (gtkbutton.c) which produced an XML output which
seems to be suitable for our purposes.

A generic tool which simplifies refactorings such as this one is a useful
piece of software which will help not only our platform to migrate to 3.0,
but more importantly, external applications.

Quick howto for elsa, since I probably won't have too much time in the near future:

* Download and build elsa
* cpp -I. -I.. -I../gdk `pkg-config --cflags gio-2.0 glib-2.0 gobject-2.0 pango cairo atk` gtkbutton.c > gtkbutton.c.preprocessed
* ccparse -tr c_lang,xmlPrintAST gtkbutton.c.preprocessed

Johan

[1]: http://www.gnome.org/~johan/the-big-refactoring.py
[2]: http://www.cs.berkeley.edu/~smcpeak/elsa/


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]