[clang] Add unique_ptr <T[]> accesses to -Wunsafe-buffer-usage (PR #156773)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Sep 16 10:18:59 PDT 2025
================
@@ -1318,6 +1319,105 @@ static bool isSupportedVariable(const DeclRefExpr &Node) {
return D != nullptr && isa<VarDecl>(D);
}
+static bool isUniquePtrArray(const CXXRecordDecl *RecordDecl) {
+ if (!RecordDecl || !RecordDecl->isInStdNamespace() ||
+ RecordDecl->getNameAsString() != "unique_ptr") {
+ return false;
+ }
+
+ const ClassTemplateSpecializationDecl *class_template_specialization_decl =
+ dyn_cast<ClassTemplateSpecializationDecl>(RecordDecl);
+ if (!class_template_specialization_decl) {
+ return false;
+ }
+
+ const TemplateArgumentList &template_args =
+ class_template_specialization_decl->getTemplateArgs();
+
+ if (template_args.size() == 0) {
+ return false;
+ }
+
+ const TemplateArgument &first_arg = template_args[0];
+
+ if (first_arg.getKind() != TemplateArgument::Type) {
+ return false;
+ }
+
+ QualType referred_type = first_arg.getAsType();
+
+ if (referred_type->isArrayType()) {
+ return true;
+ }
+
+ return false;
+}
+
+class UniquePtrArrayAccessGadget : public WarningGadget {
+ static constexpr const char *const AccessorTag = "unique_ptr_array_access";
+ const CXXOperatorCallExpr *TheAccessorExpr;
----------------
shreya-jain wrote:
I've seen this convention in other files in llvm, but within this file I don't see it, so I changed it to AccessorExpr.
https://github.com/llvm/llvm-project/pull/156773
More information about the cfe-commits
mailing list