]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c-family: Add fix-it suggestions for more <stdlib.h> names [PR101052]
authorJonathan Wakely <jwakely@redhat.com>
Mon, 14 Jun 2021 10:38:11 +0000 (11:38 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 14 Jun 2021 17:24:10 +0000 (18:24 +0100)
PR c++/101052

gcc/c-family/ChangeLog:

* known-headers.cc (get_stdlib_header_for_name): Add known
headers for EXIT_FAILURE, EXIT_SUCCESS, abort, atexit, calloc,
exit, and getenv.

gcc/testsuite/ChangeLog:

* g++.dg/spellcheck-stdlib.C: Add checks for <cstdlib> names.
* gcc.dg/spellcheck-stdlib.c: Likewise.

gcc/c-family/known-headers.cc
gcc/testsuite/g++.dg/spellcheck-stdlib.C
gcc/testsuite/gcc.dg/spellcheck-stdlib.c

index 85924718c8b371b48adc67ff0ddf5c44c08750c6..a3912468a8d95cc341da26633d964e42446cfe8c 100644 (file)
@@ -162,7 +162,14 @@ get_stdlib_header_for_name (const char *name, enum stdlib lib)
     {"stdout", {"<stdio.h>", "<cstdio>"} },
 
     /* <stdlib.h> and <cstdlib>.  */
+    {"EXIT_FAILURE", {"<stdlib.h>", "<cstdlib>"} },
+    {"EXIT_SUCCESS", {"<stdlib.h>", "<cstdlib>"} },
+    {"abort", {"<stdlib.h>", "<cstdlib>"} },
+    {"atexit", {"<stdlib.h>", "<cstdlib>"} },
+    {"calloc", {"<stdlib.h>", "<cstdlib>"} },
+    {"exit", {"<stdlib.h>", "<cstdlib>"} },
     {"free", {"<stdlib.h>", "<cstdlib>"} },
+    {"getenv", {"<stdlib.h>", "<cstdlib>"} },
     {"malloc", {"<stdlib.h>", "<cstdlib>"} },
     {"realloc", {"<stdlib.h>", "<cstdlib>"} },
 
index 31e91fe8d1b4c5937d0e4c076b241f5e52bd9234..87736b25e54bdbe0064f8ad32902f1b010aa524e 100644 (file)
@@ -138,6 +138,24 @@ void test_cstdlib (void *q)
   // { dg-message "'#include <cstdlib>'" "" { target *-*-* } .-1 }
   q = realloc (q, 1024); // { dg-error "was not declared" }
   // { dg-message "'#include <cstdlib>'" "" { target *-*-* } .-1 }
+  q = calloc (8, 8); // { dg-error "was not declared" }
+  // { dg-message "'#include <cstdlib>'" "" { target *-*-* } .-1 }
+
+  void callback ();
+  atexit (callback); // { dg-error "was not declared" }
+  // { dg-message "'#include <cstdlib>'" "" { target *-*-* } .-1 }
+  int i;
+  i = EXIT_SUCCESS; // { dg-error "was not declared" }
+  // { dg-message "'#include <cstdlib>'" "" { target *-*-* } .-1 }
+  i = EXIT_FAILURE; // { dg-error "was not declared" }
+  // { dg-message "'#include <cstdlib>'" "" { target *-*-* } .-1 }
+  exit (i); // { dg-error "was not declared" }
+  // { dg-message "'#include <cstdlib>'" "" { target *-*-* } .-1 }
+  abort (); // { dg-error "was not declared" }
+  // { dg-message "'#include <cstdlib>'" "" { target *-*-* } .-1 }
+
+  getenv ("foo"); // { dg-error "was not declared" }
+  // { dg-message "'#include <cstdlib>'" "" { target *-*-* } .-1 }
 }
 
 /* Verify that we don't offer suggestions to stdlib globals names when
index 1ae3b5e33abd915a8a842c5bc6f8904637511824..7297a92368f648d98bc6e35a60dfd688b9b4937f 100644 (file)
@@ -38,6 +38,16 @@ void test_stdio_h (void)
   /* { dg-message "'EOF' is defined in header '<stdio.h>'; did you forget to '#include <stdio.h>'?" "" { target *-*-* } .-1 } */
 }
 
+/* Missing <stdlib.h>.  */
+
+void test_stdlib (int i)
+{
+  i = EXIT_SUCCESS; /* { dg-error "'EXIT_SUCCESS' undeclared" } */
+  /* { dg-message "'EXIT_SUCCESS' is defined in header '<stdlib.h>'; did you forget to '#include <stdlib.h>'?" "" { target *-*-* } .-1 } */
+  i = EXIT_FAILURE; /* { dg-error "'EXIT_FAILURE' undeclared" } */
+  /* { dg-message "'EXIT_FAILURE' is defined in header '<stdlib.h>'; did you forget to '#include <stdlib.h>'?" "" { target *-*-* } .-1 } */
+}
+
 /* Missing <errno.h>.  */
 
 int test_errno_h (void)