[clang] [-Wunsafe-buffer-usage] Add fixits for array to pointer assignment (PR #81343)

via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 9 16:57:19 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 47c18be02329b853513d0955b8443e3bd4f04778 -- 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-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-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 7b1ff89628..7b07acdb18 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 =

``````````

</details>


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


More information about the cfe-commits mailing list