[clang-tools-extra] [clang-tidy] Add WarnOnSizeOfPointer mode to bugprone-sizeof-expression (PR #94356)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 6 07:15:58 PDT 2024


================
@@ -292,11 +318,17 @@ void SizeofExpressionCheck::check(const MatchFinder::MatchResult &Result) {
     diag(E->getBeginLoc(),
          "suspicious usage of 'sizeof(char*)'; do you mean 'strlen'?")
         << E->getSourceRange();
-  } else if (const auto *E =
-                 Result.Nodes.getNodeAs<Expr>("sizeof-pointer-to-aggregate")) {
-    diag(E->getBeginLoc(),
-         "suspicious usage of 'sizeof(A*)'; pointer to aggregate")
-        << E->getSourceRange();
+  } else if (const auto *E = Result.Nodes.getNodeAs<Expr>("sizeof-pointer")) {
+    if (Result.Nodes.getNodeAs<Type>("struct-type")) {
+      diag(E->getBeginLoc(),
+           "suspicious usage of 'sizeof(A*)' on pointer-to-aggregate type; did "
+           "you mean 'sizeof(A)'?")
+          << E->getSourceRange();
+    } else {
+      diag(E->getBeginLoc(), "suspicious usage of 'sizeof()' on an expression "
+                             "that results in a pointer")
----------------
NagyDonat wrote:

I used this phrasing to be consistent with the analogous message "suspicious usage of 'sizeof()' on an expression that results in an integer". I would also prefer "suspicious use of 'sizeof()' on an expression of pointer type", but then it would be good to update the message for integers, and then it would be good to update all the other awkward messages printed by this check...

I'd prefer to keep the current consistent and (IMO) barely acceptable message in this commit; and leave a general cleanup of all the messages of this checker for a separate followup commit. (I'm not very enthusiastic for doing this cleanup, but I can do it if the community feels that it would be important.)

https://github.com/llvm/llvm-project/pull/94356


More information about the cfe-commits mailing list