[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 5/7] maint: enable -Wsuggest-attribute=format
From: |
Paul Eggert |
Subject: |
[PATCH 5/7] maint: enable -Wsuggest-attribute=format |
Date: |
Sun, 31 Oct 2021 22:37:28 -0700 |
* configure.ac (WERROR_CFLAGS): Enable -Wsuggest-attribute=format
for lib/ and src/.
* src/copy.c (copy_attr_error, copy_attr_allerror):
Add ATTRIBUTE_FORMAT.
(copy_attr): Ignore -Wsuggest-attribute=format in the
small section of code that needs it ignored.
* src/test.c (test_syntax_error): Mark with ATTRIBUTE_FORMAT.
(binary_operator): Omit unnecessary NULL args, pacifying
-Wsuggest-attribute=format.
---
configure.ac | 2 +-
src/copy.c | 44 ++++++++++++++++++++++++++------------------
src/test.c | 7 ++++---
3 files changed, 31 insertions(+), 22 deletions(-)
diff --git a/configure.ac b/configure.ac
index 6960b486a..3f72068bf 100644
--- a/configure.ac
+++ b/configure.ac
@@ -150,7 +150,6 @@ if test $gl_gcc_warnings != no; then
nw="$nw -Wmissing-format-attribute" # copy.c
nw="$nw -Wunsafe-loop-optimizations" # a few src/*.c
nw="$nw -Winline" # system.h's
readdir_ignoring_dot_and_dotdot
- nw="$nw -Wsuggest-attribute=format" # warns about copy.c and factor.c
nw="$nw -Wvector-operation-performance" # warns about randperm.c
@@ -224,6 +223,7 @@ if test $gl_gcc_warnings != no; then
nw="$nw -Wstrict-prototypes"
# It's not worth being this picky about test programs.
nw="$nw -Wsuggest-attribute=const"
+ nw="$nw -Wsuggest-attribute=format"
nw="$nw -Wsuggest-attribute=pure"
gl_MANYWARN_COMPLEMENT([GNULIB_TEST_WARN_CFLAGS],
[$GNULIB_WARN_CFLAGS], [$nw])
diff --git a/src/copy.c b/src/copy.c
index 8c69400ec..3704e129e 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -668,6 +668,7 @@ errno_unsupported (int err)
}
#if USE_XATTR
+ATTRIBUTE_FORMAT ((printf, 2, 3))
static void
copy_attr_error (struct error_context *ctx _GL_UNUSED,
char const *fmt, ...)
@@ -684,9 +685,10 @@ copy_attr_error (struct error_context *ctx _GL_UNUSED,
}
}
+ATTRIBUTE_FORMAT ((printf, 2, 3))
static void
copy_attr_allerror (struct error_context *ctx _GL_UNUSED,
- char const *fmt, ...)
+ char const *fmt, ...)
{
int err = errno;
va_list ap;
@@ -728,26 +730,32 @@ static bool
copy_attr (char const *src_path, int src_fd,
char const *dst_path, int dst_fd, struct cp_options const *x)
{
- int ret;
bool all_errors = (!x->data_copy_required || x->require_preserve_xattr);
bool some_errors = (!all_errors && !x->reduce_diagnostics);
- bool selinux_done = (x->preserve_security_context ||
x->set_security_context);
- struct error_context ctx =
- {
- .error = all_errors ? copy_attr_allerror : copy_attr_error,
- .quote = copy_attr_quote,
- .quote_free = copy_attr_free
- };
- if (0 <= src_fd && 0 <= dst_fd)
- ret = attr_copy_fd (src_path, src_fd, dst_path, dst_fd,
- selinux_done ? check_selinux_attr : NULL,
- (all_errors || some_errors ? &ctx : NULL));
- else
- ret = attr_copy_file (src_path, dst_path,
- selinux_done ? check_selinux_attr : NULL,
- (all_errors || some_errors ? &ctx : NULL));
+ int (*check) (char const *, struct error_context *)
+ = (x->preserve_security_context || x->set_security_context
+ ? check_selinux_attr : NULL);
+
+# if 4 < __GNUC__ + (8 <= __GNUC_MINOR__)
+ /* Pacify gcc -Wsuggest-attribute=format through at least GCC 11.2.1. */
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
+# endif
+ struct error_context *ctx
+ = (all_errors || some_errors
+ ? (&(struct error_context) {
+ .error = all_errors ? copy_attr_allerror : copy_attr_error,
+ .quote = copy_attr_quote,
+ .quote_free = copy_attr_free
+ })
+ : NULL);
+# if 4 < __GNUC__ + (8 <= __GNUC_MINOR__)
+# pragma GCC diagnostic pop
+# endif
- return ret == 0;
+ return ! (0 <= src_fd && 0 <= dst_fd
+ ? attr_copy_fd (src_path, src_fd, dst_path, dst_fd, check, ctx)
+ : attr_copy_file (src_path, dst_path, check, ctx));
}
#else /* USE_XATTR */
diff --git a/src/test.c b/src/test.c
index 8f179ec02..79dc8d213 100644
--- a/src/test.c
+++ b/src/test.c
@@ -85,6 +85,7 @@ static bool or (void);
static void beyond (void);
+ATTRIBUTE_FORMAT ((printf, 1, 2))
static _Noreturn void
test_syntax_error (char const *format, ...)
{
@@ -323,7 +324,7 @@ binary_operator (bool l_is_l)
bool le, re;
pos += 3;
if (l_is_l || r_is_l)
- test_syntax_error (_("-nt does not accept -l"), NULL);
+ test_syntax_error (_("-nt does not accept -l"));
le = get_mtime (argv[op - 1], <);
re = get_mtime (argv[op + 1], &rt);
return le && (!re || timespec_cmp (lt, rt) > 0);
@@ -336,7 +337,7 @@ binary_operator (bool l_is_l)
/* ef - hard link? */
pos += 3;
if (l_is_l || r_is_l)
- test_syntax_error (_("-ef does not accept -l"), NULL);
+ test_syntax_error (_("-ef does not accept -l"));
return (stat (argv[op - 1], &stat_buf) == 0
&& stat (argv[op + 1], &stat_spare) == 0
&& stat_buf.st_dev == stat_spare.st_dev
@@ -352,7 +353,7 @@ binary_operator (bool l_is_l)
bool le, re;
pos += 3;
if (l_is_l || r_is_l)
- test_syntax_error (_("-ot does not accept -l"), NULL);
+ test_syntax_error (_("-ot does not accept -l"));
le = get_mtime (argv[op - 1], <);
re = get_mtime (argv[op + 1], &rt);
return re && (!le || timespec_cmp (lt, rt) < 0);
--
2.31.1
- [PATCH 1/7] maint: prefer attribute.h in .c files, Paul Eggert, 2021/11/01
- [PATCH 3/7] maint: remove unused __attribute__ defn, Paul Eggert, 2021/11/01
- [PATCH 2/7] b2sum: simplify attribute usage, Paul Eggert, 2021/11/01
- [PATCH 4/7] maint: modernize attribute usage, Paul Eggert, 2021/11/01
- [PATCH 5/7] maint: enable -Wsuggest-attribute=format,
Paul Eggert <=
- [PATCH 7/7] maint: use minmax.h instead of rolling our own, Paul Eggert, 2021/11/01
- [PATCH 6/7] maint: add function attributes to .h files, Paul Eggert, 2021/11/01
- Re: [PATCH 1/7] maint: prefer attribute.h in .c files, Pádraig Brady, 2021/11/01