Skip to content

[clang-tidy] fix bugprone-sizeof-expression when sizeof expression with template types #115275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

HerrCai0907
Copy link
Contributor

Fixed: #115175.
dependent type are not the same even pointers are the same.

…th template types

Fixed: llvm#115175.
`dependent type` are not the same even pointers are the same.
@llvmbot
Copy link
Member

llvmbot commented Nov 7, 2024

@llvm/pr-subscribers-clang-tidy

Author: Congcong Cai (HerrCai0907)

Changes

Fixed: #115175.
dependent type are not the same even pointers are the same.


Full diff: https://github.com/llvm/llvm-project/pull/115275.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp (+3-1)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+2-1)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp (+5)
diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
index 628d30ce7f73fe..7269683384f6b5 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 differs 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 abcdcc25705bf5..4fa8292ff89745 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-unchecked-optional-access
   <clang-tidy/checks/bugprone/unchecked-optional-access>` to support
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..f3ab78c7355b28 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,8 @@ int ValidExpressions() {
   sum += sizeof(PtrArray) / sizeof(A[0]);
   return sum;
 }
+
+template<class T>
+int ValidateTemplateTypeExpressions(T t) {
+  return sizeof(t.val) / sizeof(t.val[0]);
+}

@llvmbot
Copy link
Member

llvmbot commented Nov 7, 2024

@llvm/pr-subscribers-clang-tools-extra

Author: Congcong Cai (HerrCai0907)

Changes

Fixed: #115175.
dependent type are not the same even pointers are the same.


Full diff: https://github.com/llvm/llvm-project/pull/115275.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp (+3-1)
  • (modified) clang-tools-extra/docs/ReleaseNotes.rst (+2-1)
  • (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp (+5)
diff --git a/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
index 628d30ce7f73fe..7269683384f6b5 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 differs 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 abcdcc25705bf5..4fa8292ff89745 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-unchecked-optional-access
   <clang-tidy/checks/bugprone/unchecked-optional-access>` to support
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..f3ab78c7355b28 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,8 @@ int ValidExpressions() {
   sum += sizeof(PtrArray) / sizeof(A[0]);
   return sum;
 }
+
+template<class T>
+int ValidateTemplateTypeExpressions(T t) {
+  return sizeof(t.val) / sizeof(t.val[0]);
+}

Comment on lines +178 to +179
subtracting from a pointer directly or when used to scale a numeric value and
fix false positive when sizeof expression with template types.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there were no releases since this change, then there is no need for the release note entry. Basically you're hotfixing a change that is coming out in the same hypothetical next release.

If the change in this patch is not related to what is mentioned in this changelog entry, then it is worth a full new changelog entry, like this:

Suggested change
subtracting from a pointer directly or when used to scale a numeric value and
fix false positive when sizeof expression with template types.
subtracting from a pointer directly or when used to scale a numeric value.
- Fixed :doc:`bugprone-sizeof-expression
<clang-tidy/checks/bugprone/sizeof-expression>` check not to report on
``sizeof()/sizeof()`` expressions for template types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think not. clang-tidy release-note always combines the all changes of rule in one item instead of spilt them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example:
readability-container-contains
readability-enum-initial-value

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there were no releases since this change, then there is no need for the release note entry. Basically you're hotfixing a change that is coming out in the same hypothetical next release.

I'm not sure what you meant here. There should be a release note, unless the issue was introduced in the current, release cycle. The diagnostic reproduces in clang-tidy 18 for me, or did you mean something different (e.g., general in-case-of comment)?

clang-tidy release-note always combines the all changes of rule in one item instead of spilt them.

+1, that's the current approach

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there were no releases since this change, then there is no need for the release note entry. Basically you're hotfixing a change that is coming out in the same hypothetical next release.

I'm not sure what you meant here. There should be a release note, unless the issue was introduced in the current, release cycle. The diagnostic reproduces in clang-tidy 18 for me, or did you mean something different (e.g., general in-case-of comment)?

Because of the merged release note I thought this is related to #106061, hence the confusion.

Comment on lines +389 to +392
template<class T>
int ValidateTemplateTypeExpressions(T t) {
return sizeof(t.val) / sizeof(t.val[0]);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note the issue somewhere on this test-case (115175) (e.g. namespace)

@HerrCai0907 HerrCai0907 merged commit e855fea into llvm:main Nov 12, 2024
5 of 7 checks passed
@HerrCai0907 HerrCai0907 deleted the 115175-clang-tidy-bugprone-sizeof-expression-false-positives branch November 12, 2024 03:37
Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
…th template types (llvm#115275)

Fixed: llvm#115175.
`dependent type` are not the same even pointers are the same.

---------

Co-authored-by: whisperity <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-tidy] bugprone-sizeof-expression false positives
4 participants