[cfe-dev] Implementing missing-braces warning

Douglas Gregor dgregor at apple.com
Fri Jan 29 16:22:48 CST 2010


On Jan 29, 2010, at 1:58 PM, Tanya Lattner wrote:

> 
>> 
>> 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]'
>> 
> 
> Ok. So, how do I get "a[0]" from the InitListExpr?

You can't really get it from the InitListExpr. However, the InitializedEntity has this information through the entity kinds (member, array element, and vector element are the only ones that matter, until it terminates at the variable) if you start at the current entity and walk the parent list, you could construct  the subobject path correctly even deeply-nested cases.

I think this would be great, and could be used to provide great diagnostics for any failed initialization within an initializer list, but that might be more work than you meant to sign up for. Alternatively, you could reword the diagnostic to avoid having to construct that path, e.g.,

	warning: suggest braces around initialization of subobject of type 'int[2]'

>>> Should code modification hints be given?
>> 
>> Yes! The hints should definitely insert the '{' and '}' in the right place.
>> 
> 
> Ok, did this.
> 
>>> Is one warning output or one for each set of braces needed?
>> 
>> One for each set of braces, IMO.
> 
> Ok, did this too.

Cool.

	- Doug


More information about the cfe-dev mailing list