[clang] [clang] Catch missing format attributes (PR #70024)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Fri May 31 08:00:46 PDT 2024
================
@@ -7131,6 +7138,114 @@ 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 and has implicit object
+ // parameter, format attribute argument indexing starts from 2. Otherwise, it
+ // starts from 1.
+ unsigned int FormatArgumentIndexOffset =
+ checkIfMethodHasImplicitObjectParameter(FDecl) ? 2 : 1;
+ unsigned int ParentFunctionFormatArgumentIndexOffset =
+ checkIfMethodHasImplicitObjectParameter(ParentFuncDecl) ? 2 : 1;
+
+ // Check if function has format attribute with forwarded format string.
+ IdentifierInfo *AttrType;
+ const ParmVarDecl *FormatArg;
+ if (!llvm::any_of(FDecl->specific_attrs<FormatAttr>(),
+ [&](const FormatAttr *Attr) {
+ const int FormatIndexOffseted =
----------------
AaronBallman wrote:
```suggestion
int OffsetFormatIndex =
```
https://github.com/llvm/llvm-project/pull/70024
More information about the cfe-commits
mailing list