[gcc/devel/c++-modules] Suggest including <stdbool.h> for bool, true and false
Nathan Sidwell
nathan@gcc.gnu.org
Wed Jun 10 16:41:52 GMT 2020
https://gcc.gnu.org/g:45c50b6a63a120d3eb6957883c4c7b968c84d010
commit 45c50b6a63a120d3eb6957883c4c7b968c84d010
Author: Mark Wielaard <mark@klomp.org>
Date: Tue May 19 23:18:09 2020 +0200
Suggest including <stdbool.h> for bool, true and false
Currently gcc suggests to use _Bool instead of bool and doesn't give
any suggestions when true or false are used, but undefined. This patch
makes it so that (for C99 or higher) a fixit hint is emitted to include
<stdbool.h>.
gcc/c-family/ChangeLog:
* known-headers.cc (get_stdlib_header_for_name): Return
"<stdbool.h>" for "bool", "true" or "false" when STDLIB_C and
flag_isoc99.
gcc/testsuite/ChangeLog:
* gcc.dg/spellcheck-stdbool.c: New test.
Diff:
---
gcc/c-family/ChangeLog | 6 ++++++
gcc/c-family/known-headers.cc | 8 ++++++++
gcc/testsuite/ChangeLog | 4 ++++
gcc/testsuite/gcc.dg/spellcheck-stdbool.c | 17 +++++++++++++++++
4 files changed, 35 insertions(+)
diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog
index 50614cf1f68..42184039753 100644
--- a/gcc/c-family/ChangeLog
+++ b/gcc/c-family/ChangeLog
@@ -1,3 +1,9 @@
+2020-05-22 Mark Wielaard <mark@klomp.org>
+
+ * known-headers.cc (get_stdlib_header_for_name): Return
+ "<stdbool.h>" for "bool", "true" or "false" when STDLIB_C and
+ flag_isoc99.
+
2020-05-20 Nathan Sidwell <nathan@acm.org>
* c-common.c (try_to_locate_new_include_insertion_point): Revert change.
diff --git a/gcc/c-family/known-headers.cc b/gcc/c-family/known-headers.cc
index a21166860c0..183ce2834af 100644
--- a/gcc/c-family/known-headers.cc
+++ b/gcc/c-family/known-headers.cc
@@ -158,6 +158,14 @@ get_stdlib_header_for_name (const char *name, enum stdlib lib)
for (size_t i = 0; i < num_hints; i++)
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>";
+
return NULL;
}
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index bedaf9aa735..cb3a2d1fa6f 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2020-05-22 Mark Wielaard <mark@klomp.org>
+
+ * gcc.dg/spellcheck-stdbool.c: New test.
+
2020-05-22 Mark Wielaard <mark@klomp.org>
* gcc.dg/analyzer/signal-exit.c: New testcase.
diff --git a/gcc/testsuite/gcc.dg/spellcheck-stdbool.c b/gcc/testsuite/gcc.dg/spellcheck-stdbool.c
new file mode 100644
index 00000000000..01f12da35cf
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/spellcheck-stdbool.c
@@ -0,0 +1,17 @@
+/* { dg-options "-std=c99" } */
+/* Missing <stdbool.h>. */
+
+bool b; /* { dg-error "unknown type name 'bool'" } */
+/* { dg-message "'bool' is defined in header '<stdbool.h>'; did you forget to '#include <stdbool.h>'?" "" { target *-*-* } .-1 } */
+
+int test_true (void)
+{
+ return true; /* { dg-error "'true' undeclared" } */
+ /* { dg-message "'true' is defined in header '<stdbool.h>'; did you forget to '#include <stdbool.h>'?" "" { target *-*-* } .-1 } */
+}
+
+int test_false (void)
+{
+ return false; /* { dg-error "'false' undeclared" } */
+ /* { dg-message "'false' is defined in header '<stdbool.h>'; did you forget to '#include <stdbool.h>'?" "" { target *-*-* } .-1 } */
+}
More information about the Gcc-cvs
mailing list