[clang] [clang] Validate return type when instantiating `returns_nonnull` atttribute (PR #200117)

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Thu May 28 00:31:36 PDT 2026


================
@@ -1013,6 +1013,17 @@ void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
       continue;
     }
 
+    if (auto *A = dyn_cast<ReturnsNonNullAttr>(TmplAttr)) {
+      auto *FD = cast<FunctionDecl>(New);
+      QualType ResultType = getFunctionOrMethodResultType(FD);
+      if (!isValidPointerAttrType(ResultType))
+        Diag(A->getLocation(), diag::warn_attribute_return_pointers_only)
+            << A << SourceRange() << getFunctionOrMethodResultSourceRange(FD);
+      else if (!New->hasAttr<ReturnsNonNullAttr>())
+        New->addAttr(A->clone(Context));
+      continue;
+    }
+
----------------
cor3ntin wrote:

we have a `attrNonNullArgCheck` function - could we reuse  that?

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


More information about the cfe-commits mailing list