[clang] Warning for unsafe invocation of span::data (PR #75650)
Rashmi Mudduluru via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 15 13:48:55 PST 2023
================
@@ -721,6 +721,33 @@ class UnsafeBufferUsageAttrGadget : public WarningGadget {
DeclUseList getClaimedVarUseSites() const override { return {}; }
};
+// Warning gadget for unsafe invocation of span::data method.
+// Triggers when the pointer returned by the invocation is immediately
+// cast to a larger type.
+
+class DataInvocationGadget : public WarningGadget {
+ constexpr static const char *const OpTag = "data_invocation_expr";
+ const ExplicitCastExpr *Op;
+
+ public:
+ DataInvocationGadget(const MatchFinder::MatchResult &Result)
+ : WarningGadget(Kind::DataInvocation),
+ Op(Result.Nodes.getNodeAs<ExplicitCastExpr>(OpTag)) {}
+
+ static bool classof(const Gadget *G) {
+ return G->getKind() == Kind::DataInvocation;
+ }
+
+ static Matcher matcher() {
+ return stmt(
+ explicitCastExpr(has(cxxMemberCallExpr(callee(
+ cxxMethodDecl(hasName("data")))))).bind(OpTag));
----------------
t-rasmud wrote:
Will this also match on user defined functions called "data"? I think something like `cxxMethodDecl(ofClass(hasName("span")))` might be needed to match `span.data()` alone (but I might be wrong). In any case, maybe have a test case with a user defined function called "data" which'll show the matcher matches only on `span.data()`.
https://github.com/llvm/llvm-project/pull/75650
More information about the cfe-commits
mailing list