[cfe-dev] Implementing missing-braces warning

Douglas Gregor dgregor at apple.com
Thu Jan 28 13:46:46 PST 2010

On Jan 27, 2010, at 3:44 PM, Tanya Lattner wrote:

> Hello,
> I'm attempting to implement the missing-braces warning.
> With this example:
> int a[2][2] = { 0, 1, 2, 3 };
> It should generate a warning because its missing braces around the first two values, and the last 2 (int a[2][2] = { { 0, 1 }, { 2, 3 } }). 
> gcc outputs the warning like this:
> /tmp/warn-missing-braces.c:3: warning: missing braces around initializer
> /tmp/warn-missing-braces.c:3: warning: (near initialization for ‘a[0]’)
> So I have two questions.
> When I output this warning, what should it look like?

I think GCC has the warning mostly right. Pointing out which subobject we're in ('a[0]') is really helpful, although it should be folded into the diagnostic itself. Clang should also highlight (with a source range) all of the initializers that are conceptually initializing that subobject, e.g.,

warning: missing braces around initialization of subobject 'a[0]'
int a[2][2] = { 0, 1, 2, 3 };
                {   }

I'm not thrilled with the use of the word "missing", which seems to imply an actual error. I somewhat prefer:

	warning: suggest braces around initialization of subobject 'a[0]'

> Should code modification hints be given?

Yes! The hints should definitely insert the '{' and '}' in the right place.

> Is one warning output or one for each set of braces needed?

One for each set of braces, IMO.

> As for implementation details, in SemaInit::CheckSubElementType we assume brace elision. I assume that this is where I should output my warning, but when i do it there, I get one for each set (so 2 warnings). Any advice here? 

Did you try CheckImplicitInitList? IIRC, that's where we're implicitly stepping into a subobject when initializing a list.

	- Doug

More information about the cfe-dev mailing list