Skip to content

Commit 74a0d91

Browse files
authored
[clang-tidy] Fix bugprone-sizeof-expression crash on arrays of dependent type (llvm#159701)
Fixes llvm#158422.
1 parent 74bea4c commit 74a0d91

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,7 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
378378
if (const auto *Type = dyn_cast<ArrayType>(SizeofArgTy)) {
379379
// check if the array element size is larger than one. If true,
380380
// the size of the array is higher than the number of elements
381-
CharUnits SSize = Ctx.getTypeSizeInChars(Type->getElementType());
382-
if (!SSize.isOne()) {
381+
if (!getSizeOfType(Ctx, Type->getElementType().getTypePtr()).isOne()) {
383382
diag(SzOfExpr->getBeginLoc(),
384383
"suspicious usage of 'sizeof' in the loop")
385384
<< SzOfExpr->getSourceRange();

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ Changes in existing checks
235235
<clang-tidy/checks/bugprone/signed-char-misuse>` check by fixing
236236
false positives on C23 enums with the fixed underlying type of signed char.
237237

238+
- Improved :doc:`bugprone-sizeof-expression
239+
<clang-tidy/checks/bugprone/sizeof-expression>` check by fixing
240+
a crash on ``sizeof`` of an array of dependent type.
241+
238242
- Improved :doc:`bugprone-tagged-union-member-count
239243
<clang-tidy/checks/bugprone/tagged-union-member-count>` by fixing a false
240244
positive when enums or unions from system header files or the ``std``

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// RUN: %check_clang_tidy %s bugprone-sizeof-expression %t -- -config="{CheckOptions: {bugprone-sizeof-expression.WarnOnSizeOfIntegerExpression: true}}" --
1+
// RUN: %check_clang_tidy %s bugprone-sizeof-expression %t \
2+
// RUN: -- -config="{CheckOptions: {bugprone-sizeof-expression.WarnOnSizeOfIntegerExpression: true}}" \
3+
// RUN: -- -fno-delayed-template-parsing
24

35
class C {
46
int size() { return sizeof(this); }
@@ -227,6 +229,13 @@ void loop_access_elements(int num, struct B b) {
227229
for(int i = 0, j = 0; i < sizeof(arr) && j < sizeof(buf); i++, j++) {}
228230
}
229231

232+
template <typename T>
233+
void templated_array() {
234+
T arr[10];
235+
// CHECK-MESSAGES: :[[@LINE+1]]:23: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression]
236+
for (int i = 0; i < sizeof(arr); ++i) {}
237+
}
238+
230239
template <int T>
231240
int Foo() { int A[T]; return sizeof(T); }
232241
// CHECK-MESSAGES: :[[@LINE-1]]:30: warning: suspicious usage of 'sizeof(K)'

0 commit comments

Comments
 (0)