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.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
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 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? |
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? |
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. |
I think this is missing some test coverage for cases like:
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.