bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: proposal: lib/verify.h


From: Jim Meyering
Subject: Re: proposal: lib/verify.h
Date: Fri, 01 Jul 2005 01:06:02 +0200

Paul Eggert <address@hidden> wrote:
> Jim Meyering <address@hidden> writes:
>
>>   #define GL_CONCAT...
>>   #define VERIFY(assertion) \
>>     struct GL_CONCAT (compile_time_assert_, __LINE__) \
>>       { char a[(assertion) ? 1 : -1]; }
>
> This trick won't work if VERIFY is used in two different files with
> the same line number.  Typically the problem would occur if there are
> multiple headers that use VERIFY, or a header and a source file that
> both use VERIFY.

That's true, but maybe it won't matter in practice?
All of my uses of the statement-form have ended up being
expanded in .c files.

> Admittedly it's a bit of a pain to have to come up with a name for
> each requirement, as is the case with mktime.c now.
>
> So, how about if we simply dump the named-requirement variant, and
> stick only with the unnamed variant?  If there is a need to verify
> stuff at the top level, we can do something like this:
>
> static inline void
> verify_mktime_requirements (void)
> {
>   verify (TYPE_IS_INTEGER (time_t));
>   verify (TYPE_TWOS_COMPLEMENT (int));
>   ...
> }

That still requires coming up with a name: the function name.
I want something so that my recent patch to ls.c (below)
stays concise and continues to work.

Why not continue on with two macros?
If someone tries to use the statement form but cannot
because of a conflict, they can then resort to choosing
a name and adding a static inline function.

> (The "inline" is to pacify GCC so that it doesn't warn about the
> unused function.)
>
> One other thought: "assert" uses lower-case letters, and I'd
> like "verify" to do so too.

Sure.  Lower case does look better.

> So, how about the following macro instead?
>
> /* Verify REQUIREMENT at compile-time, as an expression.
>    Unlike assert, there is no run-time overhead.
>    Return void.  */
> #define verify(requirement) \
>   ((void) sizeof (struct { char a[(requirement) ? 1 : -1]; }))

Index: src/ls.c
===================================================================
RCS file: /fetish/cu/src/ls.c,v
retrieving revision 1.390
retrieving revision 1.391
diff -u -p -u -1 -r1.390 -r1.391
--- src/ls.c    29 Jun 2005 10:03:10 -0000      1.390
+++ src/ls.c    30 Jun 2005 16:47:58 -0000      1.391
@@ -500,2 +499,3 @@ static enum indicator_style const indica
 };
+ARGMATCH_VERIFY (indicator_style_args, indicator_style_types);
 
@@ -797,2 +796,3 @@ static enum format const format_types[] 
 };
+ARGMATCH_VERIFY (format_args, format_types);
 
@@ -807,2 +806,3 @@ static enum sort_type const sort_types[]
 };
+ARGMATCH_VERIFY (sort_args, sort_types);
 
@@ -817,2 +816,3 @@ static enum time_type const time_types[]
 };
+ARGMATCH_VERIFY (time_args, time_types);
 
@@ -832,2 +831,3 @@ static enum color_type const color_types
 };
+ARGMATCH_VERIFY (color_args, color_types);
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]