[clang] [Clang] Optimize -Wunsafe-buffer-usage. (PR #125492)

Ilya Biryukov via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 27 03:52:05 PST 2025


================
@@ -1238,27 +1413,36 @@ class SpanTwoParamConstructorGadget : public WarningGadget {
   const CXXConstructExpr *Ctor; // the span constructor expression
 
 public:
-  SpanTwoParamConstructorGadget(const MatchFinder::MatchResult &Result)
+  SpanTwoParamConstructorGadget(const MatchResult &Result)
       : WarningGadget(Kind::SpanTwoParamConstructor),
-        Ctor(Result.Nodes.getNodeAs<CXXConstructExpr>(
-            SpanTwoParamConstructorTag)) {}
+        Ctor(Result.getNodeAs<CXXConstructExpr>(SpanTwoParamConstructorTag)) {}
 
   static bool classof(const Gadget *G) {
     return G->getKind() == Kind::SpanTwoParamConstructor;
   }
 
-  static Matcher matcher() {
-    auto HasTwoParamSpanCtorDecl = hasDeclaration(
-        cxxConstructorDecl(hasDeclContext(isInStdNamespace()), hasName("span"),
-                           parameterCountIs(2)));
-
-    return stmt(cxxConstructExpr(HasTwoParamSpanCtorDecl,
-                                 unless(isSafeSpanTwoParamConstruct()))
-                    .bind(SpanTwoParamConstructorTag));
+  static bool matches(const Stmt *S, const ASTContext &Ctx,
+                      MatchResult &Result) {
+    const auto *CE = dyn_cast<CXXConstructExpr>(S);
+    if (!CE)
+      return false;
+    const auto *CDecl = CE->getConstructor();
+    const auto *DCtx = CDecl->getDeclContext();
----------------
ilya-biryukov wrote:

NIT: Use `getParent()` instead, it will return a `CXXRecordDecl`, will allow to avoid the cast below and is generally more readable.

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


More information about the cfe-commits mailing list