diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -7780,13 +7780,16 @@ // } if (HasVAListArg) { if (const ParmVarDecl *PV = dyn_cast(VD)) { - if (const NamedDecl *ND = dyn_cast(PV->getDeclContext())) { - int PVIndex = PV->getFunctionScopeIndex() + 1; - for (const auto *PVFormat : ND->specific_attrs()) { + if (const Decl *D = dyn_cast(PV->getDeclContext())) { + int PVIndex = 1; + if (const NamedDecl *ND = dyn_cast(D)) { + PVIndex += PV->getFunctionScopeIndex(); // adjust for implicit parameter if (const CXXMethodDecl *MD = dyn_cast(ND)) if (MD->isInstance()) ++PVIndex; + } + for (const auto *PVFormat : D->specific_attrs()) { // We also check if the formats are compatible. // We can't pass a 'scanf' string to a 'printf' function. if (PVIndex == PVFormat->getFormatIdx() && diff --git a/clang/test/Sema/format-strings.c b/clang/test/Sema/format-strings.c --- a/clang/test/Sema/format-strings.c +++ b/clang/test/Sema/format-strings.c @@ -1,5 +1,5 @@ -// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -isystem %S/Inputs %s -// RUN: %clang_cc1 -fsyntax-only -verify -Wformat-nonliteral -isystem %S/Inputs -fno-signed-char %s +// RUN: %clang_cc1 -fblocks -fsyntax-only -verify -Wformat-nonliteral -isystem %S/Inputs %s +// RUN: %clang_cc1 -fblocks -fsyntax-only -verify -Wformat-nonliteral -isystem %S/Inputs -fno-signed-char %s #include #include @@ -714,3 +714,16 @@ void test_printf_opaque_ptr(void *op) { printf("%s", op); // expected-warning{{format specifies type 'char *' but the argument has type 'void *'}} } + +void test_block() { + void __attribute__((__format__(__printf__, 1, 2))) (^printf_block)(const char *, ...) = + ^(const char *fmt, ...) __attribute__((__format__(__printf__, 1, 2))) { + va_list ap; + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + }; + + printf_block("%s string %i\n", "aaa", 123); + printf_block("%s string\n", 123); // expected-warning{{format specifies type 'char *' but the argument has type 'int'}} +}