public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: "François Dumont" <frs.dumont@gmail.com>
Cc: "libstdc++@gcc.gnu.org" <libstdc++@gcc.gnu.org>,
	gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] Help compiler detect invalid code
Date: Fri, 27 Sep 2019 16:45:00 -0000	[thread overview]
Message-ID: <20190927164507.GF9487@redhat.com> (raw)
In-Reply-To: <e04cfab3-f80f-f401-b5ec-448f97c69078@gmail.com>

On 27/09/19 18:24 +0200, François Dumont wrote:
>On 9/27/19 2:11 PM, Jonathan Wakely wrote:
>>On 19/09/19 22:27 +0200, François Dumont wrote:
>>>Hi
>>>
>>>    I start working on making recently added constexpr tests to 
>>>work in Debug mode.
>>
>>The attached patch seems to be necessary for that, right?
>>
>>
>On my side I had done this, almost the same.
>
>For the moment there is a FIXME in macros.h to find out how to 
>generate a nice compilation error when the condition is not meant.
>
>static_assert can't be called in this context, too bad.
>
>I also try to define a function with a 
>__attribute__((__error__("because"))) attribute. But when I make it 
>constexpr gcc complains about missing definition. When I provide a 
>definition gcc complains that this attribute must be on a declaration. 
>And when I split declaration and definition gcc does not produce the 
>expected compilation error.

Yes, I've tried similar things without success.

>Unless you have the solution I consider that we need help from the 
>front-end.
>
>For the moment if Debug mode finds a problem it will be reported as 
>_M_error function not being constexpr !

A reasonable workaround is to do:

#ifdef _GLIBCXX_HAVE_BUILTIN_IS_CONSTANT_EVALUATED
  if (__builtin_is_constant_evaluated())
    asm("Debug Mode assertion failed");
  else
#endif
  if (!(Cond))
    __gnu_debug::_Error_formatter::...

The builtin is available even for C++98, whereas
std::is_constant_evaluated() is only available for C++20.

This produces errors that include lines like:

asm.cc:12:17:   in ‘constexpr’ expansion of ‘f(-1)’
asm.cc:4:7: error: inline assembly is not a constant expression
    4 |       asm("debug mode assertion failed");
      |       ^~~
asm.cc:8:3: note: in expansion of macro ‘CHECK’
    8 |   _GLIBCXX_ASSERT(i > 0);
      |   ^~~~~
asm.cc:4:7: note: only unevaluated inline assembly is allowed in a ‘constexpr’ function in C++2a
    4 |       asm("debug mode assertion failed");
      |       ^~~
asm.cc:8:3: note: in expansion of macro ‘CHECK’
    8 |   CHECK(i > 0);
      |   ^~~~~

It's not ideal, but it does show the failed condition and the text
"debug mode assertion failed" (or whatever message you choose to use
there).


  reply	other threads:[~2019-09-27 16:45 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-19 20:28 François Dumont
2019-09-20  5:08 ` François Dumont
2019-09-25 20:40   ` François Dumont
2019-09-27 11:24   ` Jonathan Wakely
2019-10-01 20:05     ` François Dumont
2019-10-10 20:27       ` Jonathan Wakely
2019-10-16 20:35         ` François Dumont
2019-09-27 12:11 ` Jonathan Wakely
2019-09-27 16:24   ` François Dumont
2019-09-27 16:45     ` Jonathan Wakely [this message]
2019-09-28 21:12       ` [PATCH] Fix algo constexpr tests in Debug mode François Dumont
2019-09-30  9:03         ` Jonathan Wakely
2019-09-30 20:21           ` François Dumont
2019-10-01 11:21             ` Jonathan Wakely
2019-10-23 16:02         ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190927164507.GF9487@redhat.com \
    --to=jwakely@redhat.com \
    --cc=frs.dumont@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).