[clang-tools-extra] [wip][clang-tidy] Add new `bugprone-suspicious-pointer-arithmetics-using-sizeof` (`cert-arr39-c`) check (PR #106061)

via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 26 06:14:29 PDT 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 7b12d8bf8a1ff1540e32345b045f813644708a71 7e3b35dbf09d99fba55d42d9c4c5c571dd4703a6 --extensions cpp,h -- clang-tools-extra/clang-tidy/bugprone/SuspiciousPointerArithmeticsUsingSizeofCheck.cpp clang-tools-extra/clang-tidy/bugprone/SuspiciousPointerArithmeticsUsingSizeofCheck.h clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousPointerArithmeticsUsingSizeofCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/SuspiciousPointerArithmeticsUsingSizeofCheck.cpp
index a1710916f762..e56a1f2b4fa1 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousPointerArithmeticsUsingSizeofCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousPointerArithmeticsUsingSizeofCheck.cpp
@@ -17,46 +17,56 @@ using namespace clang::ast_matchers;
 
 namespace clang::tidy::bugprone {
 
-//static const char *bin_op_bind = "ptr-sizeof-expression";	
+// static const char *bin_op_bind = "ptr-sizeof-expression";
 static constexpr llvm::StringLiteral BinOp{"bin-op"};
 static constexpr llvm::StringLiteral PointedType{"pointed-type"};
-static const auto IgnoredType = qualType(anyOf(asString("char"),asString("unsigned char"),asString("signed char"),asString("int8_t"),asString("uint8_t"),asString("std::byte"),asString("const char"),asString("const unsigned char"),asString("const signed char"),asString("const int8_t"),asString("const uint8_t"),asString("const std::byte")));
-static const auto InterestingPointer = pointerType(unless(pointee(IgnoredType)));
+static const auto IgnoredType = qualType(
+    anyOf(asString("char"), asString("unsigned char"), asString("signed char"),
+          asString("int8_t"), asString("uint8_t"), asString("std::byte"),
+          asString("const char"), asString("const unsigned char"),
+          asString("const signed char"), asString("const int8_t"),
+          asString("const uint8_t"), asString("const std::byte")));
+static const auto InterestingPointer =
+    pointerType(unless(pointee(IgnoredType)));
 
-SuspiciousPointerArithmeticsUsingSizeofCheck::SuspiciousPointerArithmeticsUsingSizeofCheck(
-    StringRef Name, ClangTidyContext *Context)
-    : ClangTidyCheck(Name, Context) {
-}
+SuspiciousPointerArithmeticsUsingSizeofCheck::
+    SuspiciousPointerArithmeticsUsingSizeofCheck(StringRef Name,
+                                                 ClangTidyContext *Context)
+    : ClangTidyCheck(Name, Context) {}
 
-void SuspiciousPointerArithmeticsUsingSizeofCheck::registerMatchers(MatchFinder *Finder) {
-    Finder->addMatcher(
-	     expr(anyOf(
-/*                    binaryOperator(hasAnyOperatorName("+","-"),
-                      hasEitherOperand(hasType(pointerType())),
-		      hasEitherOperand(sizeOfExpr(expr())),
-		      unless(allOf(hasLHS(hasType(pointerType())),
-				   hasRHS(hasType(pointerType()))))
-		      ).bind(bin_op_bind),
-		    binaryOperator(hasAnyOperatorName("+=","-="),
-	              hasLHS(hasType(pointerType())),
-		      hasRHS(sizeOfExpr(expr()))
-		      ).bind(bin_op_bind)
+void SuspiciousPointerArithmeticsUsingSizeofCheck::registerMatchers(
+    MatchFinder *Finder) {
+  Finder->addMatcher(
+      expr(anyOf(
+          /*                    binaryOperator(hasAnyOperatorName("+","-"),
+                                hasEitherOperand(hasType(pointerType())),
+                                hasEitherOperand(sizeOfExpr(expr())),
+                                unless(allOf(hasLHS(hasType(pointerType())),
+                                             hasRHS(hasType(pointerType()))))
+                                ).bind(bin_op_bind),
+                              binaryOperator(hasAnyOperatorName("+=","-="),
+                                hasLHS(hasType(pointerType())),
+                                hasRHS(sizeOfExpr(expr()))
+                                ).bind(bin_op_bind)
 
-		    binaryOperator(hasAnyOperatorName("+=","-=","+","-" ),
-	              hasLHS(hasType(InterestingPointer)),
-		      hasRHS(sizeOfExpr(expr()))).bind(BinOp),
-		    binaryOperator(hasAnyOperatorName("+","-" ),
-	              hasRHS(hasType(InterestingPointer)),
-		      hasLHS(sizeOfExpr(expr()))).bind(BinOp)
-*/		    
-		    binaryOperator(hasAnyOperatorName("+=","-=","+","-" ),
-	              hasLHS(hasType(pointerType(pointee(qualType().bind(PointedType))))),
-		      hasRHS(sizeOfExpr(expr()))).bind(BinOp),
-		    binaryOperator(hasAnyOperatorName("+","-" ),
-	              hasRHS(hasType(pointerType(pointee(qualType().bind(PointedType))))),
-		      hasLHS(sizeOfExpr(expr()))).bind(BinOp)
-            )),
-        this);
+                              binaryOperator(hasAnyOperatorName("+=","-=","+","-"
+             ), hasLHS(hasType(InterestingPointer)),
+                                hasRHS(sizeOfExpr(expr()))).bind(BinOp),
+                              binaryOperator(hasAnyOperatorName("+","-" ),
+                                hasRHS(hasType(InterestingPointer)),
+                                hasLHS(sizeOfExpr(expr()))).bind(BinOp)
+          */
+          binaryOperator(hasAnyOperatorName("+=", "-=", "+", "-"),
+                         hasLHS(hasType(pointerType(
+                             pointee(qualType().bind(PointedType))))),
+                         hasRHS(sizeOfExpr(expr())))
+              .bind(BinOp),
+          binaryOperator(hasAnyOperatorName("+", "-"),
+                         hasRHS(hasType(pointerType(
+                             pointee(qualType().bind(PointedType))))),
+                         hasLHS(sizeOfExpr(expr())))
+              .bind(BinOp))),
+      this);
 }
 
 static CharUnits getSizeOfType(const ASTContext &Ctx, const Type *Ty) {
@@ -66,19 +76,21 @@ static CharUnits getSizeOfType(const ASTContext &Ctx, const Type *Ty) {
   return Ctx.getTypeSizeInChars(Ty);
 }
 
-void SuspiciousPointerArithmeticsUsingSizeofCheck::check(const MatchFinder::MatchResult &Result) {
-    const ASTContext &Ctx = *Result.Context;
-    const BinaryOperator* Matched = Result.Nodes.getNodeAs<BinaryOperator>(BinOp);
-    const QualType* SuspiciousQualTypePtr = Result.Nodes.getNodeAs<QualType>(PointedType);
-    const Type* SuspiciousTypePtr = SuspiciousQualTypePtr->getTypePtr();
+void SuspiciousPointerArithmeticsUsingSizeofCheck::check(
+    const MatchFinder::MatchResult &Result) {
+  const ASTContext &Ctx = *Result.Context;
+  const BinaryOperator *Matched = Result.Nodes.getNodeAs<BinaryOperator>(BinOp);
+  const QualType *SuspiciousQualTypePtr =
+      Result.Nodes.getNodeAs<QualType>(PointedType);
+  const Type *SuspiciousTypePtr = SuspiciousQualTypePtr->getTypePtr();
 
-    std::size_t sz = getSizeOfType(Ctx,SuspiciousTypePtr).getQuantity();
-    if ( sz > 1 )
-    {
-        diag(Matched->getExprLoc(),"Suspicious pointer arithmetics using sizeof() operator: sizeof(%0) is %1") << SuspiciousQualTypePtr->getAsString(Ctx.getPrintingPolicy())
-	                       << sz
-		               << Matched->getSourceRange();
-    }
+  std::size_t sz = getSizeOfType(Ctx, SuspiciousTypePtr).getQuantity();
+  if (sz > 1) {
+    diag(Matched->getExprLoc(), "Suspicious pointer arithmetics using sizeof() "
+                                "operator: sizeof(%0) is %1")
+        << SuspiciousQualTypePtr->getAsString(Ctx.getPrintingPolicy()) << sz
+        << Matched->getSourceRange();
+  }
 }
 
 } // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/clang-tidy/bugprone/SuspiciousPointerArithmeticsUsingSizeofCheck.h b/clang-tools-extra/clang-tidy/bugprone/SuspiciousPointerArithmeticsUsingSizeofCheck.h
index d39e15f0ccc3..9fad48ddf4a3 100644
--- a/clang-tools-extra/clang-tidy/bugprone/SuspiciousPointerArithmeticsUsingSizeofCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/SuspiciousPointerArithmeticsUsingSizeofCheck.h
@@ -1,4 +1,5 @@
-//===--- SuspiciousPointerArithmeticsUsingSizeofCheck.h - clang-tidy -*- C++ -*-===//
+//===--- SuspiciousPointerArithmeticsUsingSizeofCheck.h - clang-tidy -*- C++
+//-*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -19,8 +20,9 @@ namespace clang::tidy::bugprone {
 /// http://clang.llvm.org/extra/clang-tidy/checks/bugprone/suspicious-pointer-arithmetics-sizeof.html
 class SuspiciousPointerArithmeticsUsingSizeofCheck : public ClangTidyCheck {
 public:
-  SuspiciousPointerArithmeticsUsingSizeofCheck(StringRef Name, ClangTidyContext *Context);
-//  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
+  SuspiciousPointerArithmeticsUsingSizeofCheck(StringRef Name,
+                                               ClangTidyContext *Context);
+  //  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
 };
diff --git a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp
index 17a7e4bc5104..731a95da933e 100644
--- a/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp
@@ -280,8 +280,9 @@ public:
 
     // C checkers
     // ARR
-    CheckFactories.registerCheck<bugprone::SuspiciousPointerArithmeticsUsingSizeofCheck>(
-        "cert-arr39-c");		    
+    CheckFactories
+        .registerCheck<bugprone::SuspiciousPointerArithmeticsUsingSizeofCheck>(
+            "cert-arr39-c");
     // CON
     CheckFactories.registerCheck<bugprone::SpuriouslyWakeUpFunctionsCheck>(
         "cert-con36-c");

``````````

</details>


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


More information about the cfe-commits mailing list