[clang] [-Wunsafe-buffer-usage] Fixits for array decayed to pointer (PR #80347)

via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 8 16:00:02 PST 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 86cd2fbdfe67d70a7fe061ed5d3a644f50f070f5 b7471ab0ec3e7d20e971ed61dea727b55c944d5a -- clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-array-assign-to-ptr.cpp clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-array-inits-ptr.cpp clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-local-var-array.cpp clang/include/clang/Analysis/Analyses/UnsafeBufferUsage.h clang/lib/Analysis/UnsafeBufferUsage.cpp clang/lib/Sema/AnalysisBasedWarnings.cpp clang/test/SemaCXX/warn-unsafe-buffer-usage-debug.cpp clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-local-var-span.cpp clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-pointer-access.cpp clang/test/SemaCXX/warn-unsafe-buffer-usage.cpp
``````````

</details>

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

``````````diff
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index 32fae49577..c25ab18e2a 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -776,8 +776,8 @@ private:
 public:
   PtrToPtrAssignmentGadget(const MatchFinder::MatchResult &Result)
       : FixableGadget(Kind::PtrToPtrAssignment),
-    PtrLHS(Result.Nodes.getNodeAs<DeclRefExpr>(PointerAssignLHSTag)),
-    PtrRHS(Result.Nodes.getNodeAs<DeclRefExpr>(PointerAssignRHSTag)) {}
+        PtrLHS(Result.Nodes.getNodeAs<DeclRefExpr>(PointerAssignLHSTag)),
+        PtrRHS(Result.Nodes.getNodeAs<DeclRefExpr>(PointerAssignRHSTag)) {}
 
   static bool classof(const Gadget *G) {
     return G->getKind() == Kind::PtrToPtrAssignment;
@@ -824,27 +824,28 @@ class CArrayToPtrAssignmentGadget : public FixableGadget {
 private:
   static constexpr const char *const PointerAssignLHSTag = "ptrLHS";
   static constexpr const char *const PointerAssignRHSTag = "ptrRHS";
-  const DeclRefExpr * PtrLHS;         // the LHS pointer expression in `PA`
-  const DeclRefExpr * PtrRHS;         // the RHS pointer expression in `PA`
+  const DeclRefExpr *PtrLHS; // the LHS pointer expression in `PA`
+  const DeclRefExpr *PtrRHS; // the RHS pointer expression in `PA`
 
 public:
   CArrayToPtrAssignmentGadget(const MatchFinder::MatchResult &Result)
       : FixableGadget(Kind::CArrayToPtrAssignment),
-    PtrLHS(Result.Nodes.getNodeAs<DeclRefExpr>(PointerAssignLHSTag)),
-    PtrRHS(Result.Nodes.getNodeAs<DeclRefExpr>(PointerAssignRHSTag)) {}
+        PtrLHS(Result.Nodes.getNodeAs<DeclRefExpr>(PointerAssignLHSTag)),
+        PtrRHS(Result.Nodes.getNodeAs<DeclRefExpr>(PointerAssignRHSTag)) {}
 
   static bool classof(const Gadget *G) {
     return G->getKind() == Kind::CArrayToPtrAssignment;
   }
 
   static Matcher matcher() {
-    auto PtrAssignExpr = binaryOperator(allOf(hasOperatorName("="),
-      hasRHS(ignoringParenImpCasts(declRefExpr(hasType(hasCanonicalType(constantArrayType())),
-                                               toSupportedVariable()).
-                                   bind(PointerAssignRHSTag))),
-                                   hasLHS(declRefExpr(hasPointerType(),
-                                                      toSupportedVariable()).
-                                          bind(PointerAssignLHSTag))));
+    auto PtrAssignExpr = binaryOperator(
+        allOf(hasOperatorName("="),
+              hasRHS(ignoringParenImpCasts(
+                  declRefExpr(hasType(hasCanonicalType(constantArrayType())),
+                              toSupportedVariable())
+                      .bind(PointerAssignRHSTag))),
+              hasLHS(declRefExpr(hasPointerType(), toSupportedVariable())
+                         .bind(PointerAssignLHSTag))));
 
     return stmt(isInUnspecifiedUntypedContext(PtrAssignExpr));
   }
@@ -1511,7 +1512,8 @@ PtrToPtrAssignmentGadget::getFixits(const FixitStrategy &S) const {
 }
 
 /// \returns fixit that adds .data() call after \DRE.
-static inline std::optional<FixItList> createDataFixit(const ASTContext& Ctx, const DeclRefExpr * DRE);
+static inline std::optional<FixItList> createDataFixit(const ASTContext &Ctx,
+                                                       const DeclRefExpr *DRE);
 
 std::optional<FixItList>
 CArrayToPtrAssignmentGadget::getFixits(const FixitStrategy &S) const {
@@ -1520,27 +1522,30 @@ CArrayToPtrAssignmentGadget::getFixits(const FixitStrategy &S) const {
   switch (S.lookup(LeftVD)) {
   case FixitStrategy::Kind::Span: {
     switch (S.lookup(RightVD)) {
-      case FixitStrategy::Kind::Array:
-      case FixitStrategy::Kind::Vector:
-      case FixitStrategy::Kind::Wontfix:
-        return FixItList{};
-      default: break;
+    case FixitStrategy::Kind::Array:
+    case FixitStrategy::Kind::Vector:
+    case FixitStrategy::Kind::Wontfix:
+      return FixItList{};
+    default:
+      break;
     }
     break;
   }
   case FixitStrategy::Kind::Wontfix: {
     switch (S.lookup(RightVD)) {
-      case FixitStrategy::Kind::Wontfix:
-        return FixItList{}; // tautological
-      case FixitStrategy::Kind::Array:
-      case FixitStrategy::Kind::Span:
-        return createDataFixit(LeftVD->getASTContext(), PtrRHS);
-        // FIXME: Points inside a macro expansion.
-      default: break;
+    case FixitStrategy::Kind::Wontfix:
+      return FixItList{}; // tautological
+    case FixitStrategy::Kind::Array:
+    case FixitStrategy::Kind::Span:
+      return createDataFixit(LeftVD->getASTContext(), PtrRHS);
+      // FIXME: Points inside a macro expansion.
+    default:
+      break;
     }
     break;
   }
-  default: break;
+  default:
+    break;
   }
   return std::nullopt;
 }
@@ -1962,7 +1967,8 @@ PointerDereferenceGadget::getFixits(const FixitStrategy &S) const {
   return std::nullopt;
 }
 
-static inline std::optional<FixItList> createDataFixit(const ASTContext& Ctx, const DeclRefExpr * DRE) {
+static inline std::optional<FixItList> createDataFixit(const ASTContext &Ctx,
+                                                       const DeclRefExpr *DRE) {
   const SourceManager &SM = Ctx.getSourceManager();
   // Inserts the .data() after the DRE
   std::optional<SourceLocation> EndOfOperand =
@@ -2112,8 +2118,9 @@ UPCPreIncrementGadget::getFixits(const FixitStrategy &S) const {
 // In many cases, this function cannot figure out the actual extent `S`.  It
 // then will use a place holder to replace `S` to ask users to fill `S` in.  The
 // initializer shall be used to initialize a variable of type `std::span<T>`.
-// In some cases (e. g. constant size array) the initializer should remain unchanged and the function returns empty list.
-// In case the function can't provide the right fixit it will return nullopt.
+// In some cases (e. g. constant size array) the initializer should remain
+// unchanged and the function returns empty list. In case the function can't
+// provide the right fixit it will return nullopt.
 //
 // FIXME: Support multi-level pointers
 //
@@ -2122,7 +2129,7 @@ UPCPreIncrementGadget::getFixits(const FixitStrategy &S) const {
 //   `Ctx` a reference to the ASTContext
 static std::optional<FixItList>
 FixVarInitializerWithSpan(const Expr *Init, ASTContext &Ctx,
-                                 const StringRef UserFillPlaceHolder) {
+                          const StringRef UserFillPlaceHolder) {
   const SourceManager &SM = Ctx.getSourceManager();
   const LangOptions &LangOpts = Ctx.getLangOpts();
 
@@ -2170,9 +2177,9 @@ FixVarInitializerWithSpan(const Expr *Init, ASTContext &Ctx,
       ExtentText = One;
   } else if (const auto *CArrTy = Ctx.getAsConstantArrayType(
                  Init->IgnoreImpCasts()->getType())) {
-    // std::span has a single parameter constructor for initialization with constant size array.
-    // The size is auto-deduced as the constructor is a function template.
-    // The correct fixit is empty - no changes should happen.
+    // std::span has a single parameter constructor for initialization with
+    // constant size array. The size is auto-deduced as the constructor is a
+    // function template. The correct fixit is empty - no changes should happen.
     return FixItList{};
   } else {
     // In cases `Init` is of the form `&Var` after stripping of implicit

``````````

</details>


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


More information about the cfe-commits mailing list