public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH v2] C/C++: add hints for strerror
@ 2024-02-28  1:13 Oskari Pirhonen
  2024-05-29 13:03 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Oskari Pirhonen @ 2024-02-28  1:13 UTC (permalink / raw)
  To: gcc-patches; +Cc: dodji, dmalcolm

Add proper hints for implicit declaration of strerror.

The results could be confusing depending on the other included headers.
These example messages are from compiling a trivial program to print the
string for an errno value. It only includes stdio.h (cstdio for C++).

Before:
$ /tmp/gcc-master/bin/gcc test.c -o test_c
test.c: In function ‘main’:
test.c:4:20: warning: implicit declaration of function ‘strerror’; did you mean ‘perror’? [-Wimplicit-function-declaration]
    4 |     printf("%s\n", strerror(0));
      |                    ^~~~~~~~
      |                    perror

$ /tmp/gcc-master/bin/g++ test.cpp -o test_cpp
test.cpp: In function ‘int main()’:
test.cpp:4:20: error: ‘strerror’ was not declared in this scope; did you mean ‘stderr’?
    4 |     printf("%s\n", strerror(0));
      |                    ^~~~~~~~
      |                    stderr

After:
$ /tmp/gcc-known-headers/bin/gcc test.c -o test_c
test.c: In function ‘main’:
test.c:4:20: warning: implicit declaration of function ‘strerror’ [-Wimplicit-function-declaration]
    4 |     printf("%s\n", strerror(0));
      |                    ^~~~~~~~
test.c:2:1: note: ‘strerror’ is defined in header ‘<string.h>’; this is probably fixable by adding ‘#include <string.h>’
    1 | #include <stdio.h>
  +++ |+#include <string.h>
    2 |

$ /tmp/gcc-known-headers/bin/g++ test.cpp -o test_cpp
test.cpp: In function ‘int main()’:
test.cpp:4:20: error: ‘strerror’ was not declared in this scope
    4 |     printf("%s\n", strerror(0));
      |                    ^~~~~~~~
test.cpp:2:1: note: ‘strerror’ is defined in header ‘<cstring>’; this is probably fixable by adding ‘#include <cstring>’
    1 | #include <cstdio>
  +++ |+#include <cstring>
    2 |

gcc/c-family/ChangeLog:

	* known-headers.cc (get_stdlib_header_for_name): Add strerror.

gcc/testsuite/ChangeLog:

	* g++.dg/spellcheck-stdlib.C: Add check for strerror.
	* gcc.dg/spellcheck-stdlib-2.c: New test.

Signed-off-by: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
---
v2:
- check for error instead of warning in gcc.dg/spellcheck-stdlib-2.c
- from linaro ci notification email

 gcc/c-family/known-headers.cc              | 1 +
 gcc/testsuite/g++.dg/spellcheck-stdlib.C   | 2 ++
 gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c | 8 ++++++++
 3 files changed, 11 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c

diff --git a/gcc/c-family/known-headers.cc b/gcc/c-family/known-headers.cc
index dbc42eacde1..871fd714eb5 100644
--- a/gcc/c-family/known-headers.cc
+++ b/gcc/c-family/known-headers.cc
@@ -182,6 +182,7 @@ get_stdlib_header_for_name (const char *name, enum stdlib lib)
     {"strchr", {"<string.h>", "<cstring>"} },
     {"strcmp", {"<string.h>", "<cstring>"} },
     {"strcpy", {"<string.h>", "<cstring>"} },
+    {"strerror", {"<string.h>", "<cstring>"} },
     {"strlen", {"<string.h>", "<cstring>"} },
     {"strncat", {"<string.h>", "<cstring>"} },
     {"strncmp", {"<string.h>", "<cstring>"} },
diff --git a/gcc/testsuite/g++.dg/spellcheck-stdlib.C b/gcc/testsuite/g++.dg/spellcheck-stdlib.C
index fd0f3a9b8c9..33718b8034e 100644
--- a/gcc/testsuite/g++.dg/spellcheck-stdlib.C
+++ b/gcc/testsuite/g++.dg/spellcheck-stdlib.C
@@ -104,6 +104,8 @@ void test_cstring (char *dest, char *src)
   // { dg-message "'#include <cstring>'" "" { target *-*-* } .-1 }
   strcpy(dest, "test"); // { dg-error "was not declared" }
   // { dg-message "'#include <cstring>'" "" { target *-*-* } .-1 }
+  strerror(0); // { dg-error "was not declared" }
+  // { dg-message "'#include <cstring>'" "" { target *-*-* } .-1 }
   strlen("test"); // { dg-error "was not declared" }
   // { dg-message "'#include <cstring>'" "" { target *-*-* } .-1 }
   strncat(dest, "test", 3); // { dg-error "was not declared" }
diff --git a/gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c b/gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c
new file mode 100644
index 00000000000..4762e2ddbbd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c
@@ -0,0 +1,8 @@
+/* { dg-options "-Wimplicit-function-declaration" } */
+
+/* Missing <string.h>.  */
+void test_string_h (void)
+{
+  strerror (0); /* { dg-error "implicit declaration of function 'strerror'" } */
+  /* { dg-message "'strerror' is defined in header '<string.h>'" "" { target *-*-* } .-1 } */
+}
-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] C/C++: add hints for strerror
  2024-02-28  1:13 [PATCH v2] C/C++: add hints for strerror Oskari Pirhonen
@ 2024-05-29 13:03 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2024-05-29 13:03 UTC (permalink / raw)
  To: Oskari Pirhonen, gcc-patches; +Cc: dodji, dmalcolm

Pushed, thanks!

On 2/27/24 20:13, Oskari Pirhonen wrote:
> Add proper hints for implicit declaration of strerror.
> 
> The results could be confusing depending on the other included headers.
> These example messages are from compiling a trivial program to print the
> string for an errno value. It only includes stdio.h (cstdio for C++).
> 
> Before:
> $ /tmp/gcc-master/bin/gcc test.c -o test_c
> test.c: In function ‘main’:
> test.c:4:20: warning: implicit declaration of function ‘strerror’; did you mean ‘perror’? [-Wimplicit-function-declaration]
>      4 |     printf("%s\n", strerror(0));
>        |                    ^~~~~~~~
>        |                    perror
> 
> $ /tmp/gcc-master/bin/g++ test.cpp -o test_cpp
> test.cpp: In function ‘int main()’:
> test.cpp:4:20: error: ‘strerror’ was not declared in this scope; did you mean ‘stderr’?
>      4 |     printf("%s\n", strerror(0));
>        |                    ^~~~~~~~
>        |                    stderr
> 
> After:
> $ /tmp/gcc-known-headers/bin/gcc test.c -o test_c
> test.c: In function ‘main’:
> test.c:4:20: warning: implicit declaration of function ‘strerror’ [-Wimplicit-function-declaration]
>      4 |     printf("%s\n", strerror(0));
>        |                    ^~~~~~~~
> test.c:2:1: note: ‘strerror’ is defined in header ‘<string.h>’; this is probably fixable by adding ‘#include <string.h>’
>      1 | #include <stdio.h>
>    +++ |+#include <string.h>
>      2 |
> 
> $ /tmp/gcc-known-headers/bin/g++ test.cpp -o test_cpp
> test.cpp: In function ‘int main()’:
> test.cpp:4:20: error: ‘strerror’ was not declared in this scope
>      4 |     printf("%s\n", strerror(0));
>        |                    ^~~~~~~~
> test.cpp:2:1: note: ‘strerror’ is defined in header ‘<cstring>’; this is probably fixable by adding ‘#include <cstring>’
>      1 | #include <cstdio>
>    +++ |+#include <cstring>
>      2 |
> 
> gcc/c-family/ChangeLog:
> 
> 	* known-headers.cc (get_stdlib_header_for_name): Add strerror.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/spellcheck-stdlib.C: Add check for strerror.
> 	* gcc.dg/spellcheck-stdlib-2.c: New test.
> 
> Signed-off-by: Oskari Pirhonen <xxc3ncoredxx@gmail.com>
> ---
> v2:
> - check for error instead of warning in gcc.dg/spellcheck-stdlib-2.c
> - from linaro ci notification email
> 
>   gcc/c-family/known-headers.cc              | 1 +
>   gcc/testsuite/g++.dg/spellcheck-stdlib.C   | 2 ++
>   gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c | 8 ++++++++
>   3 files changed, 11 insertions(+)
>   create mode 100644 gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c
> 
> diff --git a/gcc/c-family/known-headers.cc b/gcc/c-family/known-headers.cc
> index dbc42eacde1..871fd714eb5 100644
> --- a/gcc/c-family/known-headers.cc
> +++ b/gcc/c-family/known-headers.cc
> @@ -182,6 +182,7 @@ get_stdlib_header_for_name (const char *name, enum stdlib lib)
>       {"strchr", {"<string.h>", "<cstring>"} },
>       {"strcmp", {"<string.h>", "<cstring>"} },
>       {"strcpy", {"<string.h>", "<cstring>"} },
> +    {"strerror", {"<string.h>", "<cstring>"} },
>       {"strlen", {"<string.h>", "<cstring>"} },
>       {"strncat", {"<string.h>", "<cstring>"} },
>       {"strncmp", {"<string.h>", "<cstring>"} },
> diff --git a/gcc/testsuite/g++.dg/spellcheck-stdlib.C b/gcc/testsuite/g++.dg/spellcheck-stdlib.C
> index fd0f3a9b8c9..33718b8034e 100644
> --- a/gcc/testsuite/g++.dg/spellcheck-stdlib.C
> +++ b/gcc/testsuite/g++.dg/spellcheck-stdlib.C
> @@ -104,6 +104,8 @@ void test_cstring (char *dest, char *src)
>     // { dg-message "'#include <cstring>'" "" { target *-*-* } .-1 }
>     strcpy(dest, "test"); // { dg-error "was not declared" }
>     // { dg-message "'#include <cstring>'" "" { target *-*-* } .-1 }
> +  strerror(0); // { dg-error "was not declared" }
> +  // { dg-message "'#include <cstring>'" "" { target *-*-* } .-1 }
>     strlen("test"); // { dg-error "was not declared" }
>     // { dg-message "'#include <cstring>'" "" { target *-*-* } .-1 }
>     strncat(dest, "test", 3); // { dg-error "was not declared" }
> diff --git a/gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c b/gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c
> new file mode 100644
> index 00000000000..4762e2ddbbd
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/spellcheck-stdlib-2.c
> @@ -0,0 +1,8 @@
> +/* { dg-options "-Wimplicit-function-declaration" } */
> +
> +/* Missing <string.h>.  */
> +void test_string_h (void)
> +{
> +  strerror (0); /* { dg-error "implicit declaration of function 'strerror'" } */
> +  /* { dg-message "'strerror' is defined in header '<string.h>'" "" { target *-*-* } .-1 } */
> +}


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-05-29 13:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-28  1:13 [PATCH v2] C/C++: add hints for strerror Oskari Pirhonen
2024-05-29 13:03 ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).