[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


================
@@ -1505,23 +1741,51 @@ class DataInvocationGadget : public WarningGadget {
   const ExplicitCastExpr *Op;
 
 public:
-  DataInvocationGadget(const MatchFinder::MatchResult &Result)
+  DataInvocationGadget(const MatchResult &Result)
       : WarningGadget(Kind::DataInvocation),
-        Op(Result.Nodes.getNodeAs<ExplicitCastExpr>(OpTag)) {}
+        Op(Result.getNodeAs<ExplicitCastExpr>(OpTag)) {}
 
   static bool classof(const Gadget *G) {
     return G->getKind() == Kind::DataInvocation;
   }
 
-  static Matcher matcher() {
+  static bool checkCallExpr(const CXXMemberCallExpr *call) {
+    if (!call)
+      return false;
+    auto *callee = call->getDirectCallee();
+    if (!callee || !isa<CXXMethodDecl>(callee))
+      return false;
+    auto *method = cast<CXXMethodDecl>(callee);
+    if (method->getNameAsString() == "data" &&
+        (method->getParent()->getQualifiedNameAsString() == "std::span" ||
----------------
ilya-biryukov wrote:

`getQualifiedNameAsString` is a code smell as it will unnecessarily allocate memory for the string output.

This code could easily be rewritten to (meta-code, but get the idea):
```cpp
Parent->IsInStdNamespace() && Parent->Name in {"span", "array", "vector"}
```

Could we do that?

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


More information about the cfe-commits mailing list