unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Using static_assert in Emacs?
@ 2024-07-18 15:23 Stefan Kangas
  2024-07-18 15:42 ` Eli Zaretskii
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Kangas @ 2024-07-18 15:23 UTC (permalink / raw)
  To: emacs-devel; +Cc: Paul Eggert

Is there any reason not to use static_assert (C11) in our code?

This node seems to suggest that Gnulib has backported it to older
compilers:

https://www.gnu.org/software/gnulib/manual/html_node/static_005fassert.html



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Using static_assert in Emacs?
  2024-07-18 15:23 Using static_assert in Emacs? Stefan Kangas
@ 2024-07-18 15:42 ` Eli Zaretskii
  2024-07-18 16:39   ` Stefan Kangas
  0 siblings, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2024-07-18 15:42 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: emacs-devel, eggert

> From: Stefan Kangas <stefankangas@gmail.com>
> Date: Thu, 18 Jul 2024 08:23:18 -0700
> Cc: Paul Eggert <eggert@cs.ucla.edu>
> 
> Is there any reason not to use static_assert (C11) in our code?

We already have it in config.h, so we can use it.  (Gnulib actually
uses it, see lib/*.c.)  The real question is: where and how will that
be useful?



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Using static_assert in Emacs?
  2024-07-18 15:42 ` Eli Zaretskii
@ 2024-07-18 16:39   ` Stefan Kangas
  2024-07-18 16:51     ` Collin Funk
  2024-07-18 17:35     ` Eli Zaretskii
  0 siblings, 2 replies; 6+ messages in thread
From: Stefan Kangas @ 2024-07-18 16:39 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: emacs-devel, eggert

Eli Zaretskii <eliz@gnu.org> writes:

> We already have it in config.h, so we can use it.  (Gnulib actually
> uses it, see lib/*.c.)

Good to know, thanks.

> The real question is: where and how will that be useful?

I'm looking at something like this, where the sizes are known at compile
time:

    #define FOO "some text"
    static_assert (sizeof buf >= sizeof FOO);
    memcpy (buf, FOO, sizeof FOO);



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Using static_assert in Emacs?
  2024-07-18 16:39   ` Stefan Kangas
@ 2024-07-18 16:51     ` Collin Funk
  2024-07-18 17:35     ` Eli Zaretskii
  1 sibling, 0 replies; 6+ messages in thread
From: Collin Funk @ 2024-07-18 16:51 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: Eli Zaretskii, emacs-devel, eggert

Stefan Kangas <stefankangas@gmail.com> writes:

>> The real question is: where and how will that be useful?
>
> I'm looking at something like this, where the sizes are known at compile
> time:
>
>     #define FOO "some text"
>     static_assert (sizeof buf >= sizeof FOO);
>     memcpy (buf, FOO, sizeof FOO);

BTW, Emacs uses Gnulib's verify.h for this. So I guess just no one got
around to updating it:

    src/lisp.h:#include <verify.h>
    src/lisp.h:verify (BITS_WORD_MAX >> (BITS_PER_BITS_WORD - 1) == 1);
    src/lisp.h:/* Define Emacs versions of <assert.h>'s 'assert (COND)' and <verify.h>'s
    src/lisp.h:   requires alignment, use verify (GCALIGNED (T)) to verify the
    src/lisp.h:verify (GCALIGNED (struct Lisp_Symbol));
    src/lisp.h:verify (GCALIGNED (struct Lisp_Cons));
    src/lisp.h:verify (GCALIGNED (struct Lisp_String));

Collin



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Using static_assert in Emacs?
  2024-07-18 16:39   ` Stefan Kangas
  2024-07-18 16:51     ` Collin Funk
@ 2024-07-18 17:35     ` Eli Zaretskii
  2024-07-19  0:48       ` Po Lu
  1 sibling, 1 reply; 6+ messages in thread
From: Eli Zaretskii @ 2024-07-18 17:35 UTC (permalink / raw)
  To: Stefan Kangas; +Cc: emacs-devel, eggert

> From: Stefan Kangas <stefankangas@gmail.com>
> Date: Thu, 18 Jul 2024 09:39:43 -0700
> Cc: emacs-devel@gnu.org, eggert@cs.ucla.edu
> 
> Eli Zaretskii <eliz@gnu.org> writes:
> 
> > We already have it in config.h, so we can use it.  (Gnulib actually
> > uses it, see lib/*.c.)
> 
> Good to know, thanks.
> 
> > The real question is: where and how will that be useful?
> 
> I'm looking at something like this, where the sizes are known at compile
> time:
> 
>     #define FOO "some text"
>     static_assert (sizeof buf >= sizeof FOO);
>     memcpy (buf, FOO, sizeof FOO);

If we have such code (do we?), and if there's a danger that someone
makes FOO longer but forgets to change buf, such an assertion could be
useful.  IME, such code is rare, though.



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Using static_assert in Emacs?
  2024-07-18 17:35     ` Eli Zaretskii
@ 2024-07-19  0:48       ` Po Lu
  0 siblings, 0 replies; 6+ messages in thread
From: Po Lu @ 2024-07-19  0:48 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: Stefan Kangas, emacs-devel, eggert

Eli Zaretskii <eliz@gnu.org> writes:

>> I'm looking at something like this, where the sizes are known at
>> compile
>> time:
>> 
>>     #define FOO "some text"
>>     static_assert (sizeof buf >= sizeof FOO);
>>     memcpy (buf, FOO, sizeof FOO);
>
> If we have such code (do we?), and if there's a danger that someone
> makes FOO longer but forgets to change buf, such an assertion could be
> useful.  IME, such code is rare, though.

And our convention is to use Gnulib's verify rather than static_assert.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-07-19  0:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-18 15:23 Using static_assert in Emacs? Stefan Kangas
2024-07-18 15:42 ` Eli Zaretskii
2024-07-18 16:39   ` Stefan Kangas
2024-07-18 16:51     ` Collin Funk
2024-07-18 17:35     ` Eli Zaretskii
2024-07-19  0:48       ` Po Lu

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).