[clang] [clang] Catch missing format attributes (PR #105479)
Budimir Aranđelović via cfe-commits
cfe-commits at lists.llvm.org
Fri Jan 31 05:03:35 PST 2025
================
@@ -5335,6 +5335,230 @@ static void handlePreferredTypeAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
D->addAttr(::new (S.Context) PreferredTypeAttr(S.Context, AL, ParmTSI));
}
+// Returns vector of format attributes. There are no two attributes with same
+// arguments in returning vector. There can be attributes that effectively only
+// store information about format type.
+static std::vector<FormatAttr *>
+GetMissingFormatAttributes(Sema &S, Stmt *Body, const FunctionDecl *FDecl) {
+ unsigned int ArgumentIndexOffset =
+ checkIfMethodHasImplicitObjectParameter(FDecl) ? 2 : 1;
+
+ std::vector<FormatAttr *> MissingAttributes;
+
+ // Iterate over body statements.
+ for (auto *Child : Body->children()) {
+ // If child statement is compound statement, recursively get missing
+ // attributes.
+ if (dyn_cast_or_null<CompoundStmt>(Child)) {
+ std::vector<FormatAttr *> CompoundStmtMissingAttributes =
+ GetMissingFormatAttributes(S, Child, FDecl);
+
+ // If there are already missing attributes with same arguments, do not add
+ // duplicates.
+ for (FormatAttr *FA : CompoundStmtMissingAttributes) {
+ if (!llvm::any_of(MissingAttributes, [&](const FormatAttr *Attr) {
+ return FA->getType() == Attr->getType() &&
+ FA->getFormatIdx() == Attr->getFormatIdx() &&
+ FA->getFirstArg() == Attr->getFirstArg();
+ }))
+ MissingAttributes.push_back(FA);
+ }
+
+ continue;
+ }
+
+ ValueStmt *VS = dyn_cast<ValueStmt>(Child);
+ if (!VS)
+ continue;
+ CallExpr *TheCall = dyn_cast_or_null<CallExpr>(VS->getExprStmt());
+ if (!TheCall)
+ continue;
+
+ const FunctionDecl *CalleeFunction =
+ dyn_cast_or_null<FunctionDecl>(TheCall->getCalleeDecl());
+ if (!CalleeFunction || !CalleeFunction->hasAttr<FormatAttr>())
+ continue;
+
+ // va_list is not intended to be passed to variadic function.
+ if (CalleeFunction->isVariadic())
+ continue;
----------------
budimirarandjelovichtec wrote:
ping @aaronpuchert
https://github.com/llvm/llvm-project/pull/105479
More information about the cfe-commits
mailing list