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

Peeter Joot peeter.joot at gmail.com
Wed Dec 12 15:07:13 PST 2012


>
>
>
> But why not instead just directly look at the initializer for
> theBigGiantGlobalVariable, which will be a CXXConstructExpr pointing
> directly to the default constructor?  You can then walk over the
> initializers in that constructor — which, since it's an implicitly-defined
> default constructor, should all be non-trivial — and see what constructors
> they use, etc.
>
>
I can get as far as finding the CXXConstructExpr like you mention (using
code from Eli's reply) :

   bool VisitVarDecl( VarDecl * var )
   {
      Expr * Init = var->getInit() ;
      bool IsGlobal = var->hasGlobalStorage() && !var->isStaticLocal() ;
      QualType type = var->getType();
      ASTContext & Context = ci.getASTContext() ;
      QualType baseType = Context.getBaseElementType( type ) ;

      if (!var->getDeclContext()->isDependentContext() && Init &&
!Init->isValueDependent())
      {
         if (IsGlobal && !var->isConstexpr() &&
              !Init->isConstantInitializer(Context,
baseType->isReferenceType()))
         {
            if ( const CXXConstructExpr * r = dyn_cast<CXXConstructExpr>(
Init ) )
            {
               cout << "found global CXXConstructExpr" << endl ;
            }
         }
      }

      return true ;
   }

How do I walk over the initializers in that constructor?  I'd tried:

               for ( CXXConstructExpr::const_arg_iterator b =
r->arg_begin(), e = r->arg_end() ;
                     b != e ; ++b )

but this appears to be wrong, and results in an empty iteration.

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


More information about the cfe-dev mailing list