[cfe-dev] better way to test for explicit C++ constructor?
Eli Friedman
eli.friedman at gmail.com
Tue Dec 11 13:38:29 PST 2012
On Tue, Dec 11, 2012 at 12:28 PM, Peeter Joot <peeter.joot at gmail.com> wrote:
> 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 ;
Here's the code used to implement -Wglobal-constructor:
Expr *Init = var->getInit();
bool IsGlobal = var->hasGlobalStorage() && !var->isStaticLocal();
QualType baseType = Context.getBaseElementType(type);
if (!var->getDeclContext()->isDependentContext() &&
Init && !Init->isValueDependent()) {
if (IsGlobal && !var->isConstexpr() &&
getDiagnostics().getDiagnosticLevel(diag::warn_global_constructor,
var->getLocation())
!= DiagnosticsEngine::Ignored &&
!Init->isConstantInitializer(Context, baseType->isReferenceType()))
Diag(var->getLocation(), diag::warn_global_constructor)
<< Init->getSourceRange();
-Eli
More information about the cfe-dev
mailing list