Commit Graph

3 Commits

Author SHA1 Message Date
David Bolvansky fd07568074 [Diagnostics] Refactor code for -Wsizeof-pointer-div, catch more cases; also add -Wsizeof-array-div
Previously, -Wsizeof-pointer-div failed to catch:
const int *r;
sizeof(r) / sizeof(int);

Now fixed.
Also introduced -Wsizeof-array-div which catches bugs like:
sizeof(r) / sizeof(short);

(Array element type does not match type of sizeof operand).

llvm-svn: 371222
2019-09-06 16:12:48 +00:00
David Bolvansky b4806822d2 [Diagnostics] Improve -Wsizeof-pointer-div
Emit diag note with a location of pointer declaration.
Revisited/added tests.

llvm-svn: 369206
2019-08-18 10:10:09 +00:00
David Bolvansky b8dc05260c [Diagnostics] Implement -Wsizeof-pointer-div
Summary:
void test(int *arr) {
    int arr_len = sizeof(arr) / sizeof(*arr);  // warn, incorrect way to compute number of array elements
}

Enabled under -Wall (same behaviour as GCC)

Reviewers: rsmith, MTC, aaron.ballman

Reviewed By: aaron.ballman

Subscribers: MTC, thakis, jfb, cfe-commits

Differential Revision: https://reviews.llvm.org/D52949

llvm-svn: 345847
2018-11-01 16:26:10 +00:00