[cfe-commits] r125640 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td include/clang/Sema/Sema.h lib/Sema/SemaChecking.cpp lib/Sema/SemaExpr.cpp test/Analysis/out-of-bounds.c test/Sema/array-bounds.c
Frits van Bommel
fvbommel at gmail.com
Thu Feb 17 03:59:07 PST 2011
On Thu, Feb 17, 2011 at 12:08 PM, Chandler Carruth <chandlerc at google.com> wrote:
>
>
> On Thu, Feb 17, 2011 at 2:56 AM, Hans Wennborg <hans at chromium.org> wrote:
>> This breaks the Chrome build, where we have some code like this:
>>
>> template <bool extendArray>
>> void myFunc() {
>> int arr[3 + (extendArray ? 1 : 0)];
>>
>> if (extendArray)
>> arr[3] = 42;
>> }
>>
>> void f() {
>> myFunc<false>();
>> }
>>
>> (The real code is here:
>>
>> http://trac.webkit.org/browser/trunk/Source/WebCore/platform/graphics/filters/FEConvolveMatrix.cpp#L245)
>>
>> In fact, it seems to warn also in a case like this:
>>
>> void f() {
>> int arr[42];
>> if (0)
>> arr[100] = 5;
>> }
>
> Similarly, why write the code this way? Now, we could perhaps suppress the
> warning when the entire subscript expression comes from a macro expansion
> (the only conceivable way I see for this to be strongly intentional code),
> but I'd like to understand how often it happens in code.
>
>>
>> Would it be possible to make the warning a bit more conservative?
>
> The examples you gave are rather hard, they would involve proper control
> flow analysis. That doesn't belong in warnings.
Actually, all the examples Hans gave use compile-time constants as
conditions. If you know the array bounds, you should also be able to
figure out the values of those conditions. So it might be interesting
to just look 'up' the AST for conditions that are statically known to
be false, and suppress the warning in that case.
Of course, there are also more interesting cases like
int A[10 + SomethingConstant];
if (SomethingConstant && someFunction())
A[10] = 0;
where the condition isn't necessarily a constant, but it being true
(or false, if we're in an 'else') does imply something about a
constant (in this case, that it's non-zero).
More information about the cfe-commits
mailing list