[clang] [Clang][UnsafeBufferUsage] Warn about two-arg string_view constructors. (PR #180471)
Rohan Jacob-Rao via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 10 10:47:11 PST 2026
================
@@ -1805,6 +1805,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;
+
+ if (!IsStringView || isSafeSpanTwoParamConstruct(*CE, Ctx))
----------------
rohanjr wrote:
Does reusing `isSafeSpanTwoParamConstruct` make sense for `string_view`? It looks like there's a subset of safe cases that would apply to `string_view`s. I would at least add some tests for the "safe" 2 parameter constructions.
https://github.com/llvm/llvm-project/pull/180471
More information about the cfe-commits
mailing list