Author: Victor Chernyakin Date: 2025-09-19T09:43:02-06:00 New Revision: 74a0d913435218dc97e2ce2f4bbe7a5ba6d515ec
URL: https://github.com/llvm/llvm-project/commit/74a0d913435218dc97e2ce2f4bbe7a5ba6d515ec DIFF: https://github.com/llvm/llvm-project/commit/74a0d913435218dc97e2ce2f4bbe7a5ba6d515ec.diff LOG: [clang-tidy] Fix `bugprone-sizeof-expression` crash on arrays of dependent type (#159701) Fixes #158422. Added: Modified: clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp index 139213ed359ba..cdb6a088b9d0a 100644 --- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp @@ -378,8 +378,7 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) { if (const auto *Type = dyn_cast<ArrayType>(SizeofArgTy)) { // check if the array element size is larger than one. If true, // the size of the array is higher than the number of elements - CharUnits SSize = Ctx.getTypeSizeInChars(Type->getElementType()); - if (!SSize.isOne()) { + if (!getSizeOfType(Ctx, Type->getElementType().getTypePtr()).isOne()) { diag(SzOfExpr->getBeginLoc(), "suspicious usage of 'sizeof' in the loop") << SzOfExpr->getSourceRange(); diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index a4652a7a54858..6184b3bb9c434 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -235,6 +235,10 @@ Changes in existing checks <clang-tidy/checks/bugprone/signed-char-misuse>` check by fixing false positives on C23 enums with the fixed underlying type of signed char. +- Improved :doc:`bugprone-sizeof-expression + <clang-tidy/checks/bugprone/sizeof-expression>` check by fixing + a crash on ``sizeof`` of an array of dependent type. + - Improved :doc:`bugprone-tagged-union-member-count <clang-tidy/checks/bugprone/tagged-union-member-count>` by fixing a false positive when enums or unions from system header files or the ``std`` diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp index 33cf1cbea8377..e47f8b06f83c9 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp @@ -1,4 +1,6 @@ -// RUN: %check_clang_tidy %s bugprone-sizeof-expression %t -- -config="{CheckOptions: {bugprone-sizeof-expression.WarnOnSizeOfIntegerExpression: true}}" -- +// RUN: %check_clang_tidy %s bugprone-sizeof-expression %t \ +// RUN: -- -config="{CheckOptions: {bugprone-sizeof-expression.WarnOnSizeOfIntegerExpression: true}}" \ +// RUN: -- -fno-delayed-template-parsing class C { int size() { return sizeof(this); } @@ -227,6 +229,13 @@ void loop_access_elements(int num, struct B b) { for(int i = 0, j = 0; i < sizeof(arr) && j < sizeof(buf); i++, j++) {} } +template <typename T> +void templated_array() { + T arr[10]; + // CHECK-MESSAGES: :[[@LINE+1]]:23: warning: suspicious usage of 'sizeof' in the loop [bugprone-sizeof-expression] + for (int i = 0; i < sizeof(arr); ++i) {} +} + template <int T> int Foo() { int A[T]; return sizeof(T); } // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: suspicious usage of 'sizeof(K)' _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
