[clang-tools-extra] e855fea - [clang-tidy] fix bugprone-sizeof-expression when sizeof expression with template types (#115275)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 11 19:36:37 PST 2024
Author: Congcong Cai
Date: 2024-11-12T11:36:33+08:00
New Revision: e855feac41fd89aebf540a155d21f12a3e82f05b
URL: https://github.com/llvm/llvm-project/commit/e855feac41fd89aebf540a155d21f12a3e82f05b
DIFF: https://github.com/llvm/llvm-project/commit/e855feac41fd89aebf540a155d21f12a3e82f05b.diff
LOG: [clang-tidy] fix bugprone-sizeof-expression when sizeof expression with template types (#115275)
Fixed: #115175.
`dependent type` are not the same even pointers are the same.
---------
Co-authored-by: whisperity <whisperity at gmail.com>
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 628d30ce7f73fe..f3d4c2255d86ec 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
@@ -400,7 +400,9 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
"suspicious usage of 'sizeof(array)/sizeof(...)';"
" denominator
diff ers from the size of array elements")
<< E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
- } else if (NumTy && DenomTy && NumTy == DenomTy) {
+ } else if (NumTy && DenomTy && NumTy == DenomTy &&
+ !NumTy->isDependentType()) {
+ // Dependent type should not be compared.
diag(E->getOperatorLoc(),
"suspicious usage of 'sizeof(...)/sizeof(...)'; both expressions "
"have the same type")
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 442fb7180555ea..db971f08ca3dbc 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -175,7 +175,8 @@ Changes in existing checks
- Improved :doc:`bugprone-sizeof-expression
<clang-tidy/checks/bugprone/sizeof-expression>` check to find suspicious
usages of ``sizeof()``, ``alignof()``, and ``offsetof()`` when adding or
- subtracting from a pointer directly or when used to scale a numeric value.
+ subtracting from a pointer directly or when used to scale a numeric value and
+ fix false positive when sizeof expression with template types.
- Improved :doc:`bugprone-throw-keyword-missing
<clang-tidy/checks/bugprone/throw-keyword-missing>` by fixing a false positive
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 81efd87345c97e..5e6f394152e9d1 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
@@ -385,3 +385,10 @@ int ValidExpressions() {
sum += sizeof(PtrArray) / sizeof(A[0]);
return sum;
}
+
+namespace gh115175 {
+template<class T>
+int ValidateTemplateTypeExpressions(T t) {
+ return sizeof(t.val) / sizeof(t.val[0]);
+}
+} // namespace gh115175
More information about the cfe-commits
mailing list