[libcxx-commits] [libcxx] 7dc9a03 - [libc++] Add missing __format__ attributes

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Nov 26 08:03:20 PST 2021


Author: Louis Dionne
Date: 2021-11-26T11:03:14-05:00
New Revision: 7dc9a03cfd789e6a08ca96666e9fbb81431eb34f

URL: https://github.com/llvm/llvm-project/commit/7dc9a03cfd789e6a08ca96666e9fbb81431eb34f
DIFF: https://github.com/llvm/llvm-project/commit/7dc9a03cfd789e6a08ca96666e9fbb81431eb34f.diff

LOG: [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

Added: 
    

Modified: 
    libcxx/include/__bsd_locale_fallbacks.h
    libcxx/include/__config
    libcxx/src/filesystem/filesystem_common.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__bsd_locale_fallbacks.h b/libcxx/include/__bsd_locale_fallbacks.h
index 2d5c2eca4679a..a5788d9777b5c 100644
--- a/libcxx/include/__bsd_locale_fallbacks.h
+++ b/libcxx/include/__bsd_locale_fallbacks.h
@@ -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);

diff  --git a/libcxx/include/__config b/libcxx/include/__config
index dbf4383cd6e38..c84b654a40de0 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -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

diff  --git a/libcxx/src/filesystem/filesystem_common.h b/libcxx/src/filesystem/filesystem_common.h
index 70092fe4e24dd..a2c340e61083c 100644
--- a/libcxx/src/filesystem/filesystem_common.h
+++ b/libcxx/src/filesystem/filesystem_common.h
@@ -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);


        


More information about the libcxx-commits mailing list