[libc++] Add missing __format__ attributes

-Wformat-nonliteral was turned on in https://reviews.llvm.org/D112927,
however we forgot to apply some __format__ attributes in Linux specific
code paths, which led to warnings when building on Linux. This patch
addresses that oversight.

Differential Revision: https://reviews.llvm.org/D113876
This commit is contained in:
Louis Dionne 2021-11-16 15:22:10 -05:00
parent 0df522969a
commit 7dc9a03cfd
3 changed files with 13 additions and 11 deletions

View File

@ -108,7 +108,7 @@ size_t __libcpp_mbsrtowcs_l(wchar_t *__dest, const char **__src, size_t __len,
}
#endif
inline
inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 4, 5)
int __libcpp_snprintf_l(char *__s, size_t __n, locale_t __l, const char *__format, ...) {
va_list __va;
va_start(__va, __format);
@ -118,7 +118,7 @@ int __libcpp_snprintf_l(char *__s, size_t __n, locale_t __l, const char *__forma
return __res;
}
inline
inline _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4)
int __libcpp_asprintf_l(char **__s, locale_t __l, const char *__format, ...) {
va_list __va;
va_start(__va, __format);
@ -128,7 +128,7 @@ int __libcpp_asprintf_l(char **__s, locale_t __l, const char *__format, ...) {
return __res;
}
inline
inline _LIBCPP_ATTRIBUTE_FORMAT(__scanf__, 3, 4)
int __libcpp_sscanf_l(const char *__s, locale_t __l, const char *__format, ...) {
va_list __va;
va_start(__va, __format);

View File

@ -1376,10 +1376,12 @@ extern "C" _LIBCPP_FUNC_VIS void __sanitizer_annotate_contiguous_container(
#endif
#if defined(__GNUC__) || defined(__clang__)
#define _LIBCPP_FORMAT_PRINTF(a, b) \
__attribute__((__format__(__printf__, a, b)))
// The attribute uses 1-based indices for ordinary and static member functions.
// The attribute uses 2-based indices for non-static member functions.
# define _LIBCPP_ATTRIBUTE_FORMAT(archetype, format_string_index, first_format_arg_index) \
__attribute__((__format__(archetype, format_string_index, first_format_arg_index)))
#else
#define _LIBCPP_FORMAT_PRINTF(a, b)
# define _LIBCPP_ATTRIBUTE_FORMAT(archetype, format_string_index, first_format_arg_index) /* nothing */
#endif
#endif // __cplusplus

View File

@ -60,7 +60,7 @@ errc __win_err_to_errc(int err);
namespace {
static _LIBCPP_FORMAT_PRINTF(1, 0) string
static _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 1, 0) string
format_string_impl(const char* msg, va_list ap) {
array<char, 256> buf;
@ -84,7 +84,7 @@ format_string_impl(const char* msg, va_list ap) {
return result;
}
static _LIBCPP_FORMAT_PRINTF(1, 2) string
static _LIBCPP_ATTRIBUTE_FORMAT(__printf__, 1, 2) string
format_string(const char* msg, ...) {
string ret;
va_list ap;
@ -172,7 +172,7 @@ struct ErrorHandler {
_LIBCPP_UNREACHABLE();
}
_LIBCPP_FORMAT_PRINTF(3, 0)
_LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 0)
void report_impl(const error_code& ec, const char* msg, va_list ap) const {
if (ec_) {
*ec_ = ec;
@ -191,7 +191,7 @@ struct ErrorHandler {
_LIBCPP_UNREACHABLE();
}
_LIBCPP_FORMAT_PRINTF(3, 4)
_LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4)
T report(const error_code& ec, const char* msg, ...) const {
va_list ap;
va_start(ap, msg);
@ -213,7 +213,7 @@ struct ErrorHandler {
return report(make_error_code(err));
}
_LIBCPP_FORMAT_PRINTF(3, 4)
_LIBCPP_ATTRIBUTE_FORMAT(__printf__, 3, 4)
T report(errc const& err, const char* msg, ...) const {
va_list ap;
va_start(ap, msg);