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

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 29 08:42:21 PDT 2024


================
@@ -7096,6 +7096,111 @@ static void handleSwiftAsyncAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
     checkSwiftAsyncErrorBlock(S, D, ErrorAttr, AsyncAttr);
 }
 
+// This function is called only if function call is not inside template body.
+// TODO: Add call for function calls inside template body.
+// Check if parent function misses format attribute. If misses, emit warning.
+void Sema::DiagnoseMissingFormatAttributes(const FunctionDecl *FDecl,
+                                           ArrayRef<const Expr *> Args,
+                                           SourceLocation Loc) {
+  assert(FDecl);
+
+  const FunctionDecl *ParentFuncDecl = getCurFunctionDecl();
+  if (!ParentFuncDecl)
+    return;
+
+  // If function is a member of struct/union/class, format attribute argument
+  // indexing starts from 2. Otherwise, it starts from 1.
+  const unsigned int FormatArgumentIndexOffset =
+      isInstanceMethod(FDecl) ? 2 : 1;
+  const unsigned int ParentFunctionFormatArgumentIndexOffset =
+      isInstanceMethod(ParentFuncDecl) ? 2 : 1;
----------------
AaronBallman wrote:

```suggestion
  unsigned int FormatArgumentIndexOffset =
      isInstanceMethod(FDecl) ? 2 : 1;
  unsigned int ParentFunctionFormatArgumentIndexOffset =
      isInstanceMethod(ParentFuncDecl) ? 2 : 1;
```
We don't generally use top-level `const` in the project.

Be sure to add test coverage for explicit object instance methods (C++23) to make sure the indexing behavior still makes sense.

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


More information about the cfe-commits mailing list