[clang] [clang] Catch missing format attributes (PR #70024)

Aaron Puchert via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 10 14:06:44 PST 2023


================
@@ -6849,6 +6849,73 @@ static void handleSwiftAsyncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
     checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// Warn if parent function misses format attribute. Parent function misses
+// format attribute if there is an argument format string forwarded to calling
+// function with format attribute, parent function has a parameter which type
+// is either string or pointer to char and parent function format attribute
+// type does not match with calling function format attribute type.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+                                           ArrayRef<const Expr *> Args,
+                                           SourceLocation Loc) {
+  assert(FDecl);
+
+  // Check if function has format attribute with forwarded format string.
+  IdentifierInfo *AttrType;
+  if (!llvm::any_of(
+          FDecl->specific_attrs<FormatAttr>(), [&](const FormatAttr *Attr) {
+            if (!Args[Attr->getFirstArg()]->getReferencedDeclOfCallee())
----------------
aaronpuchert wrote:

Do we need to do a bounds check before we access into the array or has this already been checked?

Also, the attribute arguments start counting at 1, but array access starts at 0, so I would assume we're off by one here?

And shouldn't we look up `Attr->getFormatIdx()` instead? The "first argument" can be 0, if the caller takes a `va_list` for example.

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


More information about the cfe-commits mailing list