diff mbox series

[28/39] Suppress Clang warning on adding an integer to a string

Message ID 20241222000509.2205895-29-hjl.tools@gmail.com
State Accepted
Commit 27a4b6421f488753aab982dfa2b873498fe58fa2
Headers show
Series [01/39] conform: Use -dD instead of -dN on compiler invocation | expand

Commit Message

H.J. Lu Dec. 22, 2024, 12:04 a.m. UTC
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>

Suppress Clang warning on adding an integer to a string, like:

tst-iconv-sticky-input-error.c:125:42: error: adding 'int' to a string does not append to the string [-Werror,-Wstring-plus-int]
  125 |                 expected_output = "ABXY" + skip;
      |                                   ~~~~~~~^~~~~~
tst-iconv-sticky-input-error.c:125:42: note: use array indexing to silence this warning
  125 |                 expected_output = "ABXY" + skip;
      |                                          ^
      |                                   &      [     ]

Co-Authored-By: H.J. Lu <hjl.tools@gmail.com>
---
 dirent/tst-readdir-long.c            | 4 ++++
 dirent/tst-rewinddir.c               | 4 ++++
 iconv/tst-iconv-sticky-input-error.c | 4 ++++
 posix/bug-regex24.c                  | 4 ++++
 4 files changed, 16 insertions(+)

Comments

Sam James Dec. 22, 2024, 4:50 a.m. UTC | #1
"H.J. Lu" <hjl.tools@gmail.com> writes:

> From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
>
> Suppress Clang warning on adding an integer to a string, like:
>
> tst-iconv-sticky-input-error.c:125:42: error: adding 'int' to a string does not append to the string [-Werror,-Wstring-plus-int]
>   125 |                 expected_output = "ABXY" + skip;
>       |                                   ~~~~~~~^~~~~~
> tst-iconv-sticky-input-error.c:125:42: note: use array indexing to silence this warning
>   125 |                 expected_output = "ABXY" + skip;
>       |                                          ^
>       |                                   &      [     ]
>

GCC has https://gcc.gnu.org/PR62181 but no movement on implementation,
so OK.

Reviewed-by: Sam James <sam@gentoo.org>

> Co-Authored-By: H.J. Lu <hjl.tools@gmail.com>
> ---
>  dirent/tst-readdir-long.c            | 4 ++++
>  dirent/tst-rewinddir.c               | 4 ++++
>  iconv/tst-iconv-sticky-input-error.c | 4 ++++
>  posix/bug-regex24.c                  | 4 ++++
>  4 files changed, 16 insertions(+)
>
> diff --git a/dirent/tst-readdir-long.c b/dirent/tst-readdir-long.c
> index 409318fa52..2a9db5901d 100644
> --- a/dirent/tst-readdir-long.c
> +++ b/dirent/tst-readdir-long.c
> @@ -26,6 +26,7 @@
>  #include <support/support.h>
>  #include <support/xdirent.h>
>  #include <support/readdir.h>
> +#include <libc-diag.h>
>  
>  /* If positive, at this length an EMSGSIZE error is injected.  */
>  static _Atomic int inject_error_at_length;
> @@ -59,7 +60,10 @@ add_directory_entry (struct support_fuse_dirstream *d, uint64_t offset)
>    if (offset <= 1)
>      {
>        type = DT_DIR;
> +      DIAG_PUSH_NEEDS_COMMENT_CLANG;
> +      DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wstring-plus-int");
>        name = ".." + !offset;    /* "." or "..".  */
> +      DIAG_POP_NEEDS_COMMENT_CLANG;
>        ino = 1;
>      }
>    else if (length == 1000)
> diff --git a/dirent/tst-rewinddir.c b/dirent/tst-rewinddir.c
> index 1479766ebe..891c7e7e48 100644
> --- a/dirent/tst-rewinddir.c
> +++ b/dirent/tst-rewinddir.c
> @@ -24,17 +24,21 @@
>  #include <support/readdir.h>
>  #include <support/support.h>
>  #include <support/xdirent.h>
> +#include <libc-diag.h>
>  
>  /* Return the file name at the indicated directory offset.  */
>  static char *
>  name_at_offset (unsigned int offset)
>  {
> +  DIAG_PUSH_NEEDS_COMMENT_CLANG;
> +  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wstring-plus-int");
>    if (offset <= 1)
>      return xstrdup (".." + !offset); /* "." or "..".  */
>    else
>      /* Pad the name with a lot of zeros, so that the dirent buffer gets
>         filled more quickly.  */
>      return xasprintf ("file%0240u", offset);
> +  DIAG_POP_NEEDS_COMMENT_CLANG;
>  }
>  
>  /* This many directory entries, including "." and "..".  */
> diff --git a/iconv/tst-iconv-sticky-input-error.c b/iconv/tst-iconv-sticky-input-error.c
> index 34a245f185..68698646ae 100644
> --- a/iconv/tst-iconv-sticky-input-error.c
> +++ b/iconv/tst-iconv-sticky-input-error.c
> @@ -26,6 +26,7 @@
>  #include <support/support.h>
>  #include <support/test-driver.h>
>  #include <stdio.h>
> +#include <libc-diag.h>
>  
>  /* FROM is the input character set, TO the output character set.  If
>     IGNORE is true, the iconv descriptor is set up in the same way as
> @@ -121,12 +122,15 @@ do_test (void)
>            for (int skip = 0; skip < 3; ++skip)
>              {
>                const char *expected_output;
> +	      DIAG_PUSH_NEEDS_COMMENT_CLANG;
> +	      DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wstring-plus-int");
>                if (do_ignore || strstr (charsets[to_idx], "//IGNORE") != NULL)
>                  expected_output = "ABXY" + skip;
>                else
>                  expected_output = "AB" + skip;
>                one_direction (charsets[from_idx], charsets[to_idx], do_ignore,
>                               "AB\xffXY" + skip, expected_output, limit);
> +	      DIAG_POP_NEEDS_COMMENT_CLANG;
>              }
>  
>    return 0;
> diff --git a/posix/bug-regex24.c b/posix/bug-regex24.c
> index 97c5c3508a..72f3d76afe 100644
> --- a/posix/bug-regex24.c
> +++ b/posix/bug-regex24.c
> @@ -1,6 +1,7 @@
>  #include <regex.h>
>  #include <stdio.h>
>  #include <string.h>
> +#include <libc-diag.h>
>  
>  #define str "civic"
>  
> @@ -45,11 +46,14 @@ do_test (void)
>        {
>  	int len = m[i].rm_eo - m[i].rm_so;
>  
> +	DIAG_PUSH_NEEDS_COMMENT_CLANG;
> +	DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wstring-plus-int");
>  	printf ("m[%d] = \"%.*s\"\n", i, len, str + m[i].rm_so);
>  
>  	if (strlen (expected[i]) != len
>  	    || memcmp (expected[i], str + m[i].rm_so, len) != 0)
>  	  result = 1;
> +	DIAG_POP_NEEDS_COMMENT_CLANG;
>        }
>  
>    return result;
diff mbox series

Patch

diff --git a/dirent/tst-readdir-long.c b/dirent/tst-readdir-long.c
index 409318fa52..2a9db5901d 100644
--- a/dirent/tst-readdir-long.c
+++ b/dirent/tst-readdir-long.c
@@ -26,6 +26,7 @@ 
 #include <support/support.h>
 #include <support/xdirent.h>
 #include <support/readdir.h>
+#include <libc-diag.h>
 
 /* If positive, at this length an EMSGSIZE error is injected.  */
 static _Atomic int inject_error_at_length;
@@ -59,7 +60,10 @@  add_directory_entry (struct support_fuse_dirstream *d, uint64_t offset)
   if (offset <= 1)
     {
       type = DT_DIR;
+      DIAG_PUSH_NEEDS_COMMENT_CLANG;
+      DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wstring-plus-int");
       name = ".." + !offset;    /* "." or "..".  */
+      DIAG_POP_NEEDS_COMMENT_CLANG;
       ino = 1;
     }
   else if (length == 1000)
diff --git a/dirent/tst-rewinddir.c b/dirent/tst-rewinddir.c
index 1479766ebe..891c7e7e48 100644
--- a/dirent/tst-rewinddir.c
+++ b/dirent/tst-rewinddir.c
@@ -24,17 +24,21 @@ 
 #include <support/readdir.h>
 #include <support/support.h>
 #include <support/xdirent.h>
+#include <libc-diag.h>
 
 /* Return the file name at the indicated directory offset.  */
 static char *
 name_at_offset (unsigned int offset)
 {
+  DIAG_PUSH_NEEDS_COMMENT_CLANG;
+  DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wstring-plus-int");
   if (offset <= 1)
     return xstrdup (".." + !offset); /* "." or "..".  */
   else
     /* Pad the name with a lot of zeros, so that the dirent buffer gets
        filled more quickly.  */
     return xasprintf ("file%0240u", offset);
+  DIAG_POP_NEEDS_COMMENT_CLANG;
 }
 
 /* This many directory entries, including "." and "..".  */
diff --git a/iconv/tst-iconv-sticky-input-error.c b/iconv/tst-iconv-sticky-input-error.c
index 34a245f185..68698646ae 100644
--- a/iconv/tst-iconv-sticky-input-error.c
+++ b/iconv/tst-iconv-sticky-input-error.c
@@ -26,6 +26,7 @@ 
 #include <support/support.h>
 #include <support/test-driver.h>
 #include <stdio.h>
+#include <libc-diag.h>
 
 /* FROM is the input character set, TO the output character set.  If
    IGNORE is true, the iconv descriptor is set up in the same way as
@@ -121,12 +122,15 @@  do_test (void)
           for (int skip = 0; skip < 3; ++skip)
             {
               const char *expected_output;
+	      DIAG_PUSH_NEEDS_COMMENT_CLANG;
+	      DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wstring-plus-int");
               if (do_ignore || strstr (charsets[to_idx], "//IGNORE") != NULL)
                 expected_output = "ABXY" + skip;
               else
                 expected_output = "AB" + skip;
               one_direction (charsets[from_idx], charsets[to_idx], do_ignore,
                              "AB\xffXY" + skip, expected_output, limit);
+	      DIAG_POP_NEEDS_COMMENT_CLANG;
             }
 
   return 0;
diff --git a/posix/bug-regex24.c b/posix/bug-regex24.c
index 97c5c3508a..72f3d76afe 100644
--- a/posix/bug-regex24.c
+++ b/posix/bug-regex24.c
@@ -1,6 +1,7 @@ 
 #include <regex.h>
 #include <stdio.h>
 #include <string.h>
+#include <libc-diag.h>
 
 #define str "civic"
 
@@ -45,11 +46,14 @@  do_test (void)
       {
 	int len = m[i].rm_eo - m[i].rm_so;
 
+	DIAG_PUSH_NEEDS_COMMENT_CLANG;
+	DIAG_IGNORE_NEEDS_COMMENT_CLANG (13, "-Wstring-plus-int");
 	printf ("m[%d] = \"%.*s\"\n", i, len, str + m[i].rm_so);
 
 	if (strlen (expected[i]) != len
 	    || memcmp (expected[i], str + m[i].rm_so, len) != 0)
 	  result = 1;
+	DIAG_POP_NEEDS_COMMENT_CLANG;
       }
 
   return result;