https://github.com/localspook updated https://github.com/llvm/llvm-project/pull/159701
>From 4be37e6a2d9964238ff6d67f5ea2883c79123c84 Mon Sep 17 00:00:00 2001 From: Victor Chernyakin <[email protected]> Date: Fri, 19 Sep 2025 04:26:30 +0000 Subject: [PATCH 1/2] [clang-tidy] Fix `bugprone-sizeof-expression` crash on arrays of dependent type --- .../clang-tidy/bugprone/SizeofExpressionCheck.cpp | 3 +-- .../clang-tidy/checkers/bugprone/sizeof-expression.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp index 139213ed359ba..2455b513f570f 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).isOne()) { diag(SzOfExpr->getBeginLoc(), "suspicious usage of 'sizeof' in the loop") << SzOfExpr->getSourceRange(); 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..f83d5ed052b77 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 @@ -227,6 +227,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)' >From 736347b61704580725c76293cc25a40227b41008 Mon Sep 17 00:00:00 2001 From: Victor Chernyakin <[email protected]> Date: Fri, 19 Sep 2025 04:47:32 +0000 Subject: [PATCH 2/2] Do it properly, also add release notes --- .../clang-tidy/bugprone/SizeofExpressionCheck.cpp | 2 +- clang-tools-extra/docs/ReleaseNotes.rst | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp index 2455b513f570f..cdb6a088b9d0a 100644 --- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp @@ -378,7 +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 - if (!getSizeOfType(Ctx, Type).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 3f403c42a168a..6b3e61e51c868 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -231,6 +231,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 template 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`` _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
