[cfe-dev] better way to test for explicit C++ constructor?

Peeter Joot peeter.joot at gmail.com
Tue Dec 11 12:28:10 PST 2012


I needed to hunt down code that uses explicit global constructors.  I was
able to do this, for example, identifing global1 in the following:

struct withConstructor
{
   int x ;

   withConstructor() : x(3) {}
} ;

struct noConstructor
{
   int x ;
} ;

void foo()
{
   withConstructor local1 ;
   noConstructor local2 ;
}

withConstructor global1 ;
noConstructor global2 ;

However, my AST visitor function for this contained the following very
clumsy seeming check:

   bool VisitCXXRecordDecl( CXXRecordDecl* r )
   {
      if ( r->isThisDeclarationADefinition() )
      {
         //cout << "VisitCXXRecordDecl:: CLASS: " << r->getName().str() <<
endl ;

         if ( r->hasConstCopyConstructor() ||
              r->hasUserDeclaredConstructor() ||
              r->hasUserProvidedDefaultConstructor() ||
              r->hasUserDeclaredCopyConstructor() ||
              r->hasNonTrivialDefaultConstructor() ||
              r->hasConstexprDefaultConstructor() ||
              r->hasNonTrivialCopyConstructor() ||
              r->hasUserDeclaredMoveConstructor()  ||
              r->hasFailedImplicitMoveConstructor()  ||
              r->hasConstexprNonCopyMoveConstructor()  ||
              r->hasNonTrivialMoveConstructor()   )
         {
            cout << r->getName().str() << " : CONSTRUCTOR" << endl ;
         }
...

Is there any easier way to look for constructors, but exclude the ones that
will be defined internally by the compiler for default initialization, or
is this as good as it gets?

-- 
Peeter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20121211/30d0ab8d/attachment.html>


More information about the cfe-dev mailing list