Am Samstag, dem 12.10.2024 um 18:44 +0200 schrieb Richard Biener: > > > Am 12.10.2024 um 16:43 schrieb Martin Uecker <uec...@tugraz.at>: > > > > > > There is code which should not fail at run-time. For this, > > it is helpful to get a warning when a compiler inserts traps > > (e.g. sanitizers, hardbools, __builtin_trap(), etc.). > > > > Having a warning for this also has many other use cases, e.g. > > one can use it with some sanitizer to rule out that some > > piece of code has certain undefined behavior such as > > signed overflow or undefined behavior in left-shifts > > (one gets a warning if the optimizer does not prove the > > trap is dead and it is emitted). > > > > Another potential use case could be writing tests. > > > > > > Bootstrapped and regression tested on x64_84. > > > > > > Add warning option that warns when a trap is generated. > > > > This adds a warning option -Wtrap that is emitted in > > expand_builtin_trap. It can be used to verify that traps > > are generated or - more importantly - are not generated > > under various conditions, e.g. for UBSan with -fsanitize-trap, > > hardbools, etc. > > Isn’t it better to diagnose with more context from the callers that insert > the trap?
More context would be better. Should there be additional arguments when creating the call to the builtin? Martin > > > gcc/ChangeLog: > > * common.opt (Wtrap): Add warning. > > * builtins.cc (expand_builtin_trap): Add warning. > > * doc/invoke.texi (Wtrap): Document warning. > > > > gcc/testsuite/ChangeLog: > > * gcc.dg/Wtrap.c: New test. > > > > diff --git a/gcc/builtins.cc b/gcc/builtins.cc > > index 37c7c98e5c7..76268f62481 100644 > > --- a/gcc/builtins.cc > > +++ b/gcc/builtins.cc > > @@ -5896,6 +5896,14 @@ expand_builtin_assume_aligned (tree exp, rtx target) > > void > > expand_builtin_trap (void) > > { > > + if (warn_trap) > > + { > > + location_t current_location = > > + linemap_unwind_to_first_non_reserved_loc (line_table, input_location, > > + NULL); > > + warning_at (current_location, OPT_Wtrap, "trap generated"); > > + } > > + > > if (targetm.have_trap ()) > > { > > rtx_insn *insn = emit_insn (targetm.gen_trap ()); > > diff --git a/gcc/common.opt b/gcc/common.opt > > index 12b25ff486d..1cc4936ebbf 100644 > > --- a/gcc/common.opt > > +++ b/gcc/common.opt > > @@ -812,6 +812,10 @@ Wsystem-headers > > Common Var(warn_system_headers) Warning > > Do not suppress warnings from system headers. > > > > +Wtrap > > +Common Var(warn_trap) Warning > > +Warn whenever a trap is generated. > > + > > Wtrampolines > > Common Var(warn_trampolines) Warning > > Warn whenever a trampoline is generated. > > diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi > > index 575dffd2a2f..68833c4350f 100644 > > --- a/gcc/doc/invoke.texi > > +++ b/gcc/doc/invoke.texi > > @@ -8784,6 +8784,14 @@ made up of data only and thus requires no special > > treatment. But, for > > most targets, it is made up of code and thus requires the stack to be > > made executable in order for the program to work properly. > > > > +@opindex Wtrap > > +@opindex Wno-trap > > +@item -Wtrap > > +Warn when explicit traps are generated. Traps may be generated for > > +a variety of reasons, e.g. when using the undefined behavior sanitizer > > +together with @option{-fsanitize-trap=undefined}. > > + > > + > > @opindex Wfloat-equal > > @opindex Wno-float-equal > > @item -Wfloat-equal > > diff --git a/gcc/testsuite/gcc.dg/Wtrap.c b/gcc/testsuite/gcc.dg/Wtrap.c > > new file mode 100644 > > index 00000000000..2f2878f9d45 > > --- /dev/null > > +++ b/gcc/testsuite/gcc.dg/Wtrap.c > > @@ -0,0 +1,8 @@ > > +/* { dg-do compile } */ > > +/* { dg-options "-O2 -Wtrap" } */ > > + > > +void foo(void) > > +{ > > + __builtin_trap(); /* { dg-warning "trap generated" } */ > > +} > > + > >