[cfe-users] Has anybody written a clang rewriter to move class methods out of line?

Peeter Joot peeter.joot at gmail.com
Fri May 29 12:51:38 PDT 2015


I'd like a tool that could move (explicit or implicit inline) function
bodies of C++ class methods declared in the class definition, to just after
the class.  For example:

   struct X
   {
      bool foo( int x = 1 )
      {
         return true ;
      }

      inline bool moo( int x, int y )
      {
         return true ;
      }

      static bool boo(void)
      (
         return true ;
      )
   } ;

and produce rewritten code like:

   struct X
   {
      inline bool foo( int x = 1 ) ;

      inline bool moo( int x, int y ) ;

      static inline bool boo( void ) ;
   } ;

   inline bool X::foo( int x )
   {
      return true ;
   }

   inline bool X::moo( int x, int y )
   {
      return true ;
   }

   inline bool X::boo( void ) ;
   (
      return true ;
   )

Having done this manually a number of times (to decouple class definitions
from implementation, and reduce #include tree sizes), the steps are:

1) add inline in the declaration of the function if required.
2) delete the body at the declaration point.
3) add a semicolon.
4) move the body to after the class (i.e. as prep for manually moving
problematic inlines elsewhere that introduce dependencies)
5) remove any static specifier from the relocated body.
6) make sure there's an inline specifier in the relocated function body.
7) remove any default parameters that should only be included with the
prototype.
8) add the class specifier (X:: in the example above)

This seems like a highly automatable task given the libtooling
infrastructure, and it wouldn't suprise me if a rewriter like this has been
thought about as a companion for google's include_what_you_use libtooling
tool.

Before trying to implement such a beast, I thought I'd ask if anybody knows
of such a tool.  If no such tool is known, perhaps somebody point me to an
example (or examples) of a transformation tools that have one or more
aspects of the rewriting steps above.

-- 
Peeter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20150529/5154b77c/attachment.html>


More information about the cfe-users mailing list