This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Improving bugprone-sizeof-expr check.
ClosedPublic

Authored by balazske on Nov 16 2020, 7:37 AM.

Details

Summary

Do not warn for "pointer to aggregate" in a sizeof(A) / sizeof(A[0])
expression if A is an array of pointers. This is the usual way of
calculating the array length even if the array is of pointers.

Diff Detail

Event Timeline

balazske created this revision.Nov 16 2020, 7:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 16 2020, 7:37 AM
balazske requested review of this revision.Nov 16 2020, 7:37 AM
Eugene.Zelenko added a project: Restricted Project.
aaron.ballman accepted this revision.Nov 17 2020, 5:25 AM

LGTM aside from a testing request.

clang-tools-extra/test/clang-tidy/checkers/bugprone-sizeof-expression.cpp
315

I think this is missing some test coverage for cases like:

sum += sizeof(PtrArray) / sizeof(PtrArray[1]); // Bad array index
sum += sizeof(PtrArray) / sizeof(A[0]); // Different array types

Also, if you can add a comment to the sum += sizeof(PtrArray) / sizeof(PtrArray1[0]); test case about why this passes (same canonical types), that'd be appreciated since the test case is a bit subtle.

This revision is now accepted and ready to land.Nov 17 2020, 5:25 AM
balazske updated this revision to Diff 305801.Nov 17 2020, 8:04 AM

Improving tests.

This checker has multiple weaknesses. There are more cases when the warnings should not appear (probably if the argument of sizeof is a template parameter), or more than one warning is generated for a code construct. The test code could be further improved too (but there are many cases to handle). This fix addresses only a single problematic case.

This checker has multiple weaknesses. There are more cases when the warnings should not appear (probably if the argument of sizeof is a template parameter), or more than one warning is generated for a code construct. The test code could be further improved too (but there are many cases to handle). This fix addresses only a single problematic case.

Sorry about being unclear -- I was intending to ensure we had test coverage for the changes in the patch. Specifically, the ZeroLiteral and ArrayOfSamePointersExpr matchers did not appear to have negative tests showing that we aren't diagnosing that code. I don't expect this patch to actually handle those cases (or for you to be on the hook for the fixes), it's more about documenting expectations.

clang-tools-extra/test/clang-tidy/checkers/bugprone-sizeof-expression.cpp
317

Thanks for the new test case, can you also add one for array[1] (with the incorrect index) and add a FIXME to the comment so it's clear that we know we don't handle these cases currently but might like to someday?

balazske added inline comments.Nov 18 2020, 8:32 AM
clang-tools-extra/test/clang-tidy/checkers/bugprone-sizeof-expression.cpp
317

The test for array[1] is not the same that is inserted on line 245?

aaron.ballman accepted this revision.Nov 18 2020, 8:39 AM

LGTM!

clang-tools-extra/test/clang-tidy/checkers/bugprone-sizeof-expression.cpp
317

Weirdly, I didn't see the test cases added around line 245 until this review. Huh, very odd -- but yes, that test covers it nicely.

balazske updated this revision to Diff 306134.Nov 18 2020, 8:59 AM

Adding FIXME comments.

This revision was automatically updated to reflect the committed changes.