[clang] [clang][sema] Add nonnull attribute to builtin format functions (PR #158626)

Radovan Božić via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 16 06:46:12 PDT 2025


================
@@ -17097,6 +17097,15 @@ void Sema::AddKnownFunctionAttributes(FunctionDecl *FD) {
       }
     }
 
+    SmallVector<int, 4> Indxs;
+    if (Context.BuiltinInfo.IsNonNull(BuiltinID, Indxs) &&
+        !FD->hasAttr<NonNullAttr>()) {
+      llvm::SmallVector<ParamIdx, 4> ParamIndxs;
+      for (int I : Indxs)
+        ParamIndxs.push_back(ParamIdx(I + 1, FD));
+      FD->addAttr(NonNullAttr::CreateImplicit(Context, ParamIndxs.data(),
+                                              ParamIndxs.size()));
----------------
bozicrHT wrote:

`ParamIdx` requires a `Decl` pointer as its second argument, which is only available in Sema, not in Builtins (which is included in the preamble). Because of that, constructing ParamIdx directly inside isNonNull() isn’t possible. That’s why I opted to create them later in Sema. We could consider a small refactor (e.g. a helper in Sema) if we want to make the one-based logic cleaner, but I think the current approach is the most practical.

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


More information about the cfe-commits mailing list