llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.81k stars 11.91k forks source link

-Wformat too aggressive on variadic templates with __attribute__((format)) #64704

Closed zmodem closed 1 year ago

zmodem commented 1 year ago

@apple-fcloutier's 92edd74b37c7a96b1d47dc67cda7f92b65066025 made it possible to put __attribute__((format)) on variadic templates.

In https://crbug.com/1472532 @pkasting raised an interesting point that because arguments to such templates don't undergo the same promotion that arguments to the regular printf/scanf varargs do, the warning becomes more aggressive https://godbolt.org/z/ajE5cYhKM:

template <typename... Args>
void __attribute__((format(printf, 1, 2))) variadic_template_printf(const char* format, Args&&... args) {
  // ...
}

void foo(float f) {
  variadic_template_printf("f: %f", f);
}
<source>:7:37: warning: format specifies type 'double' but the argument has type 'float' [-Wformat]
    7 |   variadic_template_printf("f: %f", f);
      |                                ~~   ^
      |                                %f
1 warning generated.
Compiler returned: 0

Providing a float value for a %f conversion with printf is allowed since it will get promoted to double, but that doesn't work with this template function. And there doesn't seem to be any good way of working around the warning!

Perhaps -Wformat needs to be relaxed somehow for this case?

inclyc commented 1 year ago

Providing a float value for a %f conversion with printf is allowed since it will get promoted to double, but that doesn't work with this template function.

"Default argument promotion", IIRC, is not applied for template functions. Do you mean that we should treat them as variadic functions?

inclyc commented 1 year ago

Duplicate of https://github.com/llvm/llvm-project/issues/59824