All About Performance

and other stuff by Taras Glek

Nicely Rewriting Outparams

Automatic code rewriting business can be a little depressing sometimes. I tend to run into funny issues caused by CPP, oink limitations or just unpleasant-to-rewrite parts of C++. After banging my head against the wall due to all these issues I finally arrived at a workable approach for the easy part of the outparam rewrite.

Currently I have a dehydra script that finds all non-virtual getters that return either NS_OK or NS_SOMETHING_IS_WRONG. The script then outputs data for squash to base the rewrites on. Then squash takes over.

In order to preserve sanity, pretty-printing is not used at all for rewriting the getter functions. This way one doesn’t have to worry about oink generating invalid C++ and the output is much more aesthetically pleasing. Instead, squash finds interesting expressions in the .i file. Then it extracts the corresponding strings from .h/.cpp files. The strings are used to fudge the position information obtained from the .i file to vaguely correspond to the original source files. After various C++ string-foo, squash produces a promising looking patch like this.

This also relies on a fair amount of semantic information provided by elsa/oink. For example when removing a parameter, squash inserts a local variable with the same name and then removes all of the derefences of the old parameter. Since there could be multiple variables with the same name, squash relies on elsa’s variable resolution.

I think squash is now 50% feature complete with respect to outparam stuff. The other 50% is the hard part of rewriting all of the call-sites. I’m not counting easy parts like wrapping the return type in already_AddRefed<>, eliminating redundant assignments in the getter or removing the error variable once it is no longer needed.

Comments