[clang] [Clang][UnsafeBufferUsage] Warn about two-arg string_view constructors. (PR #180471)
Rohan Jacob-Rao via cfe-commits
cfe-commits at lists.llvm.org
Fri Feb 13 14:28:16 PST 2026
================
@@ -1805,6 +1844,70 @@ class SpanTwoParamConstructorGadget : public WarningGadget {
SmallVector<const Expr *, 1> getUnsafePtrs() const override { return {}; }
};
+class StringViewTwoParamConstructorGadget : public WarningGadget {
+ static constexpr const char *const StringViewTwoParamConstructorTag =
+ "stringViewTwoParamConstructor";
+ const CXXConstructExpr *Ctor; // the string_view constructor expression
+
+public:
+ StringViewTwoParamConstructorGadget(const MatchResult &Result)
+ : WarningGadget(Kind::StringViewTwoParamConstructor),
+ Ctor(Result.getNodeAs<CXXConstructExpr>(
+ StringViewTwoParamConstructorTag)) {}
+
+ static bool classof(const Gadget *G) {
+ return G->getKind() == Kind::StringViewTwoParamConstructor;
+ }
+
+ static bool matches(const Stmt *S, ASTContext &Ctx, MatchResult &Result) {
+ const auto *CE = dyn_cast<CXXConstructExpr>(S);
+ if (!CE)
+ return false;
+ const auto *CDecl = CE->getConstructor();
+ const auto *CRecordDecl = CDecl->getParent();
+
+ // MATCH: std::basic_string_view
+ bool IsStringView =
+ CRecordDecl->isInStdNamespace() &&
+ CDecl->getDeclName().getAsString() == "basic_string_view" &&
+ CE->getNumArgs() == 2;
----------------
rohanjr wrote:
nit: the number of arguments is a separate condition to `IsStringView`. I'd either rename this variable or move this condition to the if statement below (slightly prefer the latter).
https://github.com/llvm/llvm-project/pull/180471
More information about the cfe-commits
mailing list