From 45c50b6a63a120d3eb6957883c4c7b968c84d010 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Tue, 19 May 2020 23:18:09 +0200 Subject: [PATCH] Suggest including 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 . gcc/c-family/ChangeLog: * known-headers.cc (get_stdlib_header_for_name): Return "" for "bool", "true" or "false" when STDLIB_C and flag_isoc99. gcc/testsuite/ChangeLog: * gcc.dg/spellcheck-stdbool.c: New test. --- 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(+) create mode 100644 gcc/testsuite/gcc.dg/spellcheck-stdbool.c 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 + + * known-headers.cc (get_stdlib_header_for_name): Return + "" for "bool", "true" or "false" when STDLIB_C and + flag_isoc99. + 2020-05-20 Nathan Sidwell * 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 ""; + 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 + + * gcc.dg/spellcheck-stdbool.c: New test. + 2020-05-22 Mark Wielaard * 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 . */ + +bool b; /* { dg-error "unknown type name 'bool'" } */ +/* { dg-message "'bool' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ + +int test_true (void) +{ + return true; /* { dg-error "'true' undeclared" } */ + /* { dg-message "'true' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ +} + +int test_false (void) +{ + return false; /* { dg-error "'false' undeclared" } */ + /* { dg-message "'false' is defined in header ''; did you forget to '#include '?" "" { target *-*-* } .-1 } */ +} -- 2.30.2