[cfe-dev] [PATCH] attempt for bug 6904 - clang allows redefinition of static const member

Faisal Vali faisalv at gmail.com
Mon Aug 23 10:48:47 PDT 2010


On Mon, Aug 23, 2010 at 9:23 AM, Douglas Gregor <dgregor at apple.com> wrote:
>
> On Aug 23, 2010, at 4:42 AM, Faisal Vali wrote:
>
>
> Index: SemaDecl.cpp
>
> ===================================================================
> --- SemaDecl.cpp        (revision 111791)
> +++ SemaDecl.cpp        (working copy)
> @@ -4079,6 +4079,25 @@
>     return;
>   }
>
> +  // FV ADDED 8/22/10
> +  // BUG Report  Bug 6904, Bug 7954 (duplicate)
> +  // Check to see if this VDecl has been initialized before
> +  // This should really only happen if this is a const static class variable of int or enum type
> +  // and was initialized in the class and now is being re-initialized
> +  // i.e. struct S { static const int i = 5; }; const int S::i = 2; BAD!!
> +  VarDecl *First = VDecl->getFirstDeclaration();
> +  for (VarDecl::redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
> +         I != E; ++I) {
> +    if ((*I)->hasInit())
> +    {
> +      Diag(VDecl->getLocation(), diag::err_redefinition) << VDecl->getDeclName();
> +      Diag((*I)->getLocation(), diag::note_previous_definition);
> +      VDecl->setInvalidDecl();
> +      return;
> +    }
> +  }
>
> Why walk the redeclaration chain here, rather than just using VarDecl::getAnyInitializer?
>

Aah - thanks for the tip - yes that would be clearer.  I was avoiding
getPreviousDeclaration for fear of missing some corner case that might
lead to multiple previous declarations of such variables - but
getAnyInitializer should be robust enough.

Thanks - I will post a patch in the evening to this group - unless you
want me to post it to cfe-commit.

regards,

-- 
Faisal Vali




More information about the cfe-dev mailing list