Skip to content
Snippets Groups Projects
Commit 45c50b6a authored by Mark Wielaard's avatar Mark Wielaard
Browse files

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.
parent 72af65b9
No related branches found
No related tags found
No related merge requests found
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> 2020-05-20 Nathan Sidwell <nathan@acm.org>
* c-common.c (try_to_locate_new_include_insertion_point): Revert change. * c-common.c (try_to_locate_new_include_insertion_point): Revert change.
......
...@@ -158,6 +158,14 @@ get_stdlib_header_for_name (const char *name, enum stdlib lib) ...@@ -158,6 +158,14 @@ get_stdlib_header_for_name (const char *name, enum stdlib lib)
for (size_t i = 0; i < num_hints; i++) for (size_t i = 0; i < num_hints; i++)
if (strcmp (name, hints[i].name) == 0) if (strcmp (name, hints[i].name) == 0)
return hints[i].header[lib]; 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; return NULL;
} }
......
2020-05-22 Mark Wielaard <mark@klomp.org>
* gcc.dg/spellcheck-stdbool.c: New test.
2020-05-22 Mark Wielaard <mark@klomp.org> 2020-05-22 Mark Wielaard <mark@klomp.org>
   
* gcc.dg/analyzer/signal-exit.c: New testcase. * gcc.dg/analyzer/signal-exit.c: New testcase.
......
/* { 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 } */
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment