[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
GitOrigin-RevId: 7dc9a03cfd789e6a08ca96666e9fbb81431eb34f
diff --git a/include/__bsd_locale_fallbacks.h b/include/__bsd_locale_fallbacks.h
index 2d5c2ec..a5788d9 100644
--- a/include/__bsd_locale_fallbacks.h
+++ b/include/__bsd_locale_fallbacks.h
@@ -108,7 +108,7 @@
}
#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 @@
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 @@
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);
diff --git a/include/__config b/include/__config
index dbf4383..c84b654 100644
--- a/include/__config
+++ b/include/__config
@@ -1376,10 +1376,12 @@
#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
diff --git a/src/filesystem/filesystem_common.h b/src/filesystem/filesystem_common.h
index 70092fe..a2c340e 100644
--- a/src/filesystem/filesystem_common.h
+++ b/src/filesystem/filesystem_common.h
@@ -60,7 +60,7 @@
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 @@
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 @@
_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 @@
_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 @@
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);