public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/autopar_devel] Suggest including <stdint.h> or <cstdint> for [u]int[8|16|32|64]_t
@ 2020-08-22 21:31 Giuliano Belinassi
  0 siblings, 0 replies; only message in thread
From: Giuliano Belinassi @ 2020-08-22 21:31 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:a36a06501bc5423dd29ea62f3c44759c6354ae9d

commit a36a06501bc5423dd29ea62f3c44759c6354ae9d
Author: Mark Wielaard <mark@klomp.org>
Date:   Wed May 20 00:55:00 2020 +0200

    Suggest including <stdint.h> or <cstdint> for [u]int[8|16|32|64]_t
    
    Plus [u]intptr_t and associated constants.
    
    Refactor the bool, true, false, <stdbool.h> code so it fits into the
    new table based design.
    
    gcc/c-family/ChangeLog:
    
            * known-headers.cc (get_stdlib_header_for_name): Add a new
            stdlib_hint array for stdbool and stdint.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/spellcheck-stdint.c: New test.
            * g++.dg/spellcheck-stdint.C: Likewise.

Diff:
---
 gcc/c-family/ChangeLog                   |  5 +++
 gcc/c-family/known-headers.cc            | 42 +++++++++++++++++---
 gcc/testsuite/ChangeLog                  |  5 +++
 gcc/testsuite/g++.dg/spellcheck-stdint.C | 68 ++++++++++++++++++++++++++++++++
 gcc/testsuite/gcc.dg/spellcheck-stdint.c | 62 +++++++++++++++++++++++++++++
 5 files changed, 176 insertions(+), 6 deletions(-)

diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog
index 42184039753..0ec2b9662a8 100644
--- a/gcc/c-family/ChangeLog
+++ b/gcc/c-family/ChangeLog
@@ -1,3 +1,8 @@
+2020-05-22  Mark Wielaard  <mark@klomp.org>
+
+	* known-headers.cc (get_stdlib_header_for_name): Add a new
+	stdlib_hint array for stdbool and stdint.
+
 2020-05-22  Mark Wielaard  <mark@klomp.org>
 
 	* known-headers.cc (get_stdlib_header_for_name): Return
diff --git a/gcc/c-family/known-headers.cc b/gcc/c-family/known-headers.cc
index 183ce2834af..1e2bf49c439 100644
--- a/gcc/c-family/known-headers.cc
+++ b/gcc/c-family/known-headers.cc
@@ -159,12 +159,42 @@ get_stdlib_header_for_name (const char *name, enum stdlib lib)
     if (strcmp (name, hints[i].name) == 0)
       return hints[i].header[lib];
 
-  /* Only for C99 and higher.  */
-  if (lib == STDLIB_C && flag_isoc99)
-    if (strcmp (name, "bool") == 0
-	|| strcmp (name, "true") == 0
-	|| strcmp (name, "false") == 0)
-      return "<stdbool.h>";
+  static const stdlib_hint c99_cxx11_hints[] = {
+    /* <stdbool.h>.  Defined natively in C++.  */
+    {"bool", {"<stdbool.h>", NULL} },
+    {"true", {"<stdbool.h>", NULL} },
+    {"false", {"<stdbool.h>", NULL} },
+
+    /* <stdint.h> and <cstdint>.  */
+    {"int8_t", {"<stdint.h>", "<cstdint>"} },
+    {"uint8_t", {"<stdint.h>", "<cstdint>"} },
+    {"int16_t", {"<stdint.h>", "<cstdint>"} },
+    {"uint16_t", {"<stdint.h>", "<cstdint>"} },
+    {"int32_t", {"<stdint.h>", "<cstdint>"} },
+    {"uint32_t", {"<stdint.h>", "<cstdint>"} },
+    {"int64_t", {"<stdint.h>", "<cstdint>"} },
+    {"uint64_t", {"<stdint.h>", "<cstdint>"} },
+    {"intptr_t", {"<stdint.h>", "<cstdint>"} },
+    {"uintptr_t", {"<stdint.h>", "<cstdint>"} },
+    {"INT8_MAX", {"<stdint.h>", "<cstdint>"} },
+    {"INT16_MAX", {"<stdint.h>", "<cstdint>"} },
+    {"INT32_MAX", {"<stdint.h>", "<cstdint>"} },
+    {"INT64_MAX", {"<stdint.h>", "<cstdint>"} },
+    {"UINT8_MAX", {"<stdint.h>", "<cstdint>"} },
+    {"UINT16_MAX", {"<stdint.h>", "<cstdint>"} },
+    {"UINT32_MAX", {"<stdint.h>", "<cstdint>"} },
+    {"UINT64_MAX", {"<stdint.h>", "<cstdint>"} },
+    {"INTPTR_MAX", {"<stdint.h>", "<cstdint>"} },
+    {"UINTPTR_MAX", {"<stdint.h>", "<cstdint>"} }
+  };
+
+  const size_t num_c99_cxx11_hints = sizeof (c99_cxx11_hints)
+					     / sizeof (c99_cxx11_hints[0]);
+  if ((lib == STDLIB_C && flag_isoc99)
+      || (lib == STDLIB_CPLUSPLUS && cxx_dialect >= cxx11 ))
+    for (size_t i = 0; i < num_c99_cxx11_hints; i++)
+      if (strcmp (name, c99_cxx11_hints[i].name) == 0)
+	return c99_cxx11_hints[i].header[lib];
 
   return NULL;
 }
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index cb3a2d1fa6f..9711a27e29c 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2020-05-22  Mark Wielaard  <mark@klomp.org>
+
+	* gcc.dg/spellcheck-stdint.c: New test.
+	* g++.dg/spellcheck-stdint.C: Likewise.
+
 2020-05-22  Mark Wielaard  <mark@klomp.org>
 
 	* gcc.dg/spellcheck-stdbool.c: New test.
diff --git a/gcc/testsuite/g++.dg/spellcheck-stdint.C b/gcc/testsuite/g++.dg/spellcheck-stdint.C
new file mode 100644
index 00000000000..b9ce3b7aed8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/spellcheck-stdint.C
@@ -0,0 +1,68 @@
+/* { dg-options "-std=c++11" } */
+/* Missing <cstdint>.  */
+
+char c = INT8_MAX; // { dg-error "'INT8_MAX' was not declared" }
+// { dg-message "'INT8_MAX' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+
+short s = INT16_MAX; // { dg-error "'INT16_MAX' was not declared" }
+// { dg-message "'INT16_MAX' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+
+int i = INT32_MAX; // { dg-error "'INT32_MAX' was not declared" }
+// { dg-message "'INT32_MAX' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+
+long l = INT64_MAX; // { dg-error "'INT64_MAX' was not declared" }
+// { dg-message "'INT64_MAX' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+
+intptr_t test_intptr (void) // { dg-error "'intptr_t' does not name a type" }
+// { dg-message "'intptr_t' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+{
+  return 0;
+}
+
+int test_intptr_max (void)
+{
+  return (int) INTPTR_MAX; // { dg-error "'INTPTR_MAX' was not declared" }
+// { dg-message "'INTPTR_MAX' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+}
+
+uintptr_t test_uintptr (void) // { dg-error "'uintptr_t' does not name a type" }
+// { dg-message "'uintptr_t' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+{
+  return 0;
+}
+
+unsigned int test_uintptr_max (void)
+{
+  return (unsigned int) UINTPTR_MAX; // { dg-error "'UINTPTR_MAX' was not declared" }
+// { dg-message "'UINTPTR_MAX' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+}
+
+int8_t i8; // { dg-error "'int8_t' does not name a type" }
+// { dg-message "'int8_t' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+int16_t i16; // { dg-error "'int16_t' does not name a type" }
+// { dg-message "'int16_t' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+int32_t i32; // { dg-error "'int32_t' does not name a type" }
+// { dg-message "'int32_t' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+int64_t i64; // { dg-error "'int64_t' does not name a type" }
+// { dg-message "'int64_t' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+
+void test_uint_t (void)
+{
+  char bu8[(unsigned int)UINT8_MAX]; // { dg-error "'UINT8_MAX' was not declared" }
+  // { dg-message "'UINT8_MAX' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+  char bu16[(unsigned int)UINT16_MAX]; // { dg-error "'UINT16_MAX' was not declared" }
+  // { dg-message "'UINT16_MAX' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+  char bu32[(unsigned int)UINT32_MAX]; // { dg-error "'UINT32_MAX' was not declared" }
+  // { dg-message "'UINT32_MAX' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+  char bu64[(unsigned int)UINT64_MAX]; // { dg-error "'UINT64_MAX' was not declared" }
+  // { dg-message "'UINT64_MAX' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+
+  auto ui8 = (uint8_t) 8; // { dg-error "'uint8_t' was not declared" }
+  // { dg-message "'uint8_t' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+  auto ui16 = (uint16_t) 16; // { dg-error "'uint16_t' was not declared" }
+  // { dg-message "'uint16_t' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+  auto ui32 = (uint32_t) 32; // { dg-error "'uint32_t' was not declared" }
+  // { dg-message "'uint32_t' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+  auto ui64 = (uint64_t) 64; // { dg-error "'uint64_t' was not declared" }
+  // { dg-message "'uint64_t' is defined in header '<cstdint>'; did you forget to '#include <cstdint>'?" "" { target *-*-* } .-1 }
+}
diff --git a/gcc/testsuite/gcc.dg/spellcheck-stdint.c b/gcc/testsuite/gcc.dg/spellcheck-stdint.c
new file mode 100644
index 00000000000..852c86954b6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/spellcheck-stdint.c
@@ -0,0 +1,62 @@
+/* { dg-options "-std=gnu99" } */
+/* Missing <stdint.h>.  */
+
+char c = INT8_MAX; // { dg-error "'INT8_MAX' undeclared" }
+// { dg-message "'INT8_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+
+short s = INT16_MAX; // { dg-error "'INT16_MAX' undeclared" }
+// { dg-message "'INT16_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+
+int i = INT32_MAX; // { dg-error "'INT32_MAX' undeclared" }
+// { dg-message "'INT32_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+
+long l = INT64_MAX; // { dg-error "'INT64_MAX' undeclared" }
+// { dg-message "'INT64_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+
+intptr_t test_intptr (void) // { dg-error "unknown type name 'intptr_t'" }
+// { dg-message "'intptr_t' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+{
+  return INTPTR_MAX; // { dg-error "'INTPTR_MAX' undeclared" }
+// { dg-message "'INTPTR_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+}
+
+uintptr_t test_uintptr (void) // { dg-error "unknown type name 'uintptr_t'" }
+// { dg-message "'uintptr_t' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+{
+  return UINTPTR_MAX; // { dg-error "'UINTPTR_MAX' undeclared" }
+// { dg-message "'UINTPTR_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+}
+
+int8_t i8; // { dg-error "unknown type name 'int8_t'" }
+// { dg-message "'int8_t' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+int16_t i16; // { dg-error "unknown type name 'int16_t'" }
+// { dg-message "'int16_t' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+int32_t i32; // { dg-error "unknown type name 'int32_t'" }
+// { dg-message "'int32_t' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+int64_t i64; // { dg-error "unknown type name 'int64_t'" }
+// { dg-message "'int64_t' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+
+void test_uint_t (void)
+{
+  char bu8[(unsigned int)UINT8_MAX]; // { dg-error "'UINT8_MAX' undeclared" }
+  // { dg-message "'UINT8_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+  char bu16[(unsigned int)UINT16_MAX]; // { dg-error "'UINT16_MAX' undeclared" }
+  // { dg-message "'UINT16_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+  char bu32[(unsigned int)UINT32_MAX]; // { dg-error "'UINT32_MAX' undeclared" }
+  // { dg-message "'UINT32_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+  char bu64[(unsigned int)UINT64_MAX]; // { dg-error "'UINT64_MAX' undeclared" }
+  // { dg-message "'UINT64_MAX' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-1 }
+
+  char ui8 = (uint8_t) 8; // { dg-error "'uint8_t' undeclared" }
+  // { dg-error "expected" "" { target *-*-* } .-1 }
+  // { dg-message "'uint8_t' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-2 }
+  short ui16 = (uint16_t) 16; // { dg-error "'uint16_t' undeclared" }
+  // { dg-error "expected" "" { target *-*-* } .-1 }
+  // { dg-message "'uint16_t' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-2 }
+  int ui32 = (uint32_t) 32; // { dg-error "'uint32_t' undeclared" }
+  // { dg-error "expected" "" { target *-*-* } .-1 }
+  // { dg-message "'uint32_t' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-2 }
+  long ui64 = (uint64_t) 64; // { dg-error "'uint64_t' undeclared" }
+  // { dg-error "expected" "" { target *-*-* } .-1 }
+  // { dg-message "'uint64_t' is defined in header '<stdint.h>'; did you forget to '#include <stdint.h>'?" "" { target *-*-* } .-2 }
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2020-08-22 21:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-22 21:31 [gcc/devel/autopar_devel] Suggest including <stdint.h> or <cstdint> for [u]int[8|16|32|64]_t Giuliano Belinassi

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).