Skip to content

Commit a285955

Browse files
committed
Document assert_(), combine with __ASSERT_; fixup Perl_assert()
The combined documentation now cautions about macro parameters being evaluated more than once. Perl_assert now works correctly on platforms where LINE is not an int., and expands to a no-op under Coverity, or on systems without adequate macro buffer space.
2 parents b1f152e + ea42cd3 commit a285955

File tree

2 files changed

+56
-34
lines changed

2 files changed

+56
-34
lines changed

handy.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -348,28 +348,6 @@ don't, so that you can portably take advantage of this C99 feature.
348348
/* The largest unsigned number that will fit into n bits */
349349
#define nBIT_UMAX(n) nBIT_MASK(n)
350350

351-
/*
352-
=for apidoc_section $directives
353-
=for apidoc Am||__ASSERT_|bool expr
354-
355-
This is a helper macro to avoid preprocessor issues, replaced by nothing
356-
unless under DEBUGGING, where it expands to an assert of its argument,
357-
followed by a comma (hence the comma operator). If we just used a straight
358-
assert(), we would get a comma with nothing before it when not DEBUGGING.
359-
360-
=cut
361-
362-
We also use empty definition under Coverity since the __ASSERT_
363-
checks often check for things that Really Cannot Happen, and Coverity
364-
detects that and gets all excited. */
365-
366-
#if defined(DEBUGGING) && !defined(__COVERITY__) \
367-
&& ! defined(PERL_SMALL_MACRO_BUFFER)
368-
# define __ASSERT_(statement) assert(statement),
369-
#else
370-
# define __ASSERT_(statement)
371-
#endif
372-
373351
/*
374352
=for apidoc_section $SV
375353

perl.h

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5109,25 +5109,69 @@ Gid_t getegid (void);
51095109
Perl_deb(aTHX_ "%s scope %ld (savestack=%ld) at %s:%d\n", \
51105110
where, (long)PL_scopestack_ix, (long)PL_savestack_ix, \
51115111
__FILE__, __LINE__));
5112+
/*
5113+
=for apidoc_section $directives
5114+
=for apidoc Am|void|assert_|bool expr
5115+
=for apidoc_item | |__ASSERT_
5116+
5117+
These are synonymous, used to wrap the libc C<assert()> call in comma
5118+
expressions in macro expansions, but you probably don't want to use them nor
5119+
plain C<assert>; read on.
5120+
5121+
In DEBUGGING builds, each expands to an assert of its argument, followed by
5122+
a comma. (That is what the trailing underscore signifies.)
5123+
5124+
In non-DEBUGGING builds, each expands to nothing.
5125+
5126+
They thus can be used to string together a bunch of asserts in a comma
5127+
expression that is syntactically valid in either type of build.
5128+
5129+
NOTE, however, use of these (and plain C<assert()>) is discouraged in a macro.
5130+
This is because their usual use is to validate some of the arguments to that
5131+
macro. That will likely lead to the evaluation of those arguments more than
5132+
once during the macro expansion. If such an argument is an expression with
5133+
side effects, the behavior of the macro will differ between DEBUGGING and
5134+
non-DEBUGGING builds.
5135+
5136+
And, they are necessary only on platforms where the libc C<assert()> expands to
5137+
nothing when not in a DEBUGGING build. There should be no such platforms now
5138+
in existence, as the C89 standard forbids that, and Perl requires at least C99.
5139+
So, you can just use plain C<assert>, and say S<C<assert(...), assert(...),>>
5140+
and everything will compile (and will work if none of the arguments to the
5141+
asserts is an expression with side effects).
5142+
5143+
These macros are retained for backward compatibility.
5144+
5145+
Do NOT use C<__ASSERT_>. A name with two leading underscores followed by a
5146+
capital letter is reserved for the use of the compiler and libc in some
5147+
contexts in C, and in all contexts in C++.
5148+
5149+
=cut
5150+
*/
51125151

51135152
/* Keep the old croak based assert for those who want it, and as a fallback if
51145153
the platform is so heretically non-ANSI that it can't assert. */
51155154

5116-
#define Perl_assert(what) PERL_DEB2( \
5117-
((what) ? ((void) 0) : \
5118-
(Perl_croak_nocontext("Assertion %s failed: file \"" __FILE__ \
5119-
"\", line %d", STRINGIFY(what), __LINE__), \
5120-
(void) 0)), ((void)0))
5121-
5122-
/* assert() gets defined if DEBUGGING.
5123-
* If no DEBUGGING, the <assert.h> has not been included. */
51245155
#ifndef assert
5125-
# define assert(what) Perl_assert(what)
5126-
#endif
5127-
#ifdef DEBUGGING
5128-
# define assert_(what) assert(what),
5156+
# define assert(what) Perl_assert(what)
5157+
#endif
5158+
5159+
#if defined DEBUGGING \
5160+
&& ! defined(__COVERITY__) \
5161+
&& ! defined(PERL_SMALL_MACRO_BUFFER)
5162+
# define Perl_assert(what) \
5163+
((what) \
5164+
? ((void) 0) \
5165+
: (Perl_croak_nocontext("Assertion %s failed:" \
5166+
" file \"" __FILE__ "\", line %" LINE_Tf, \
5167+
STRINGIFY(what), (line_t) __LINE__), \
5168+
(void) 0))
5169+
# define assert_(what) assert(what),
5170+
# define __ASSERT_(statement) assert(statement),
51295171
#else
5172+
# define Perl_assert(what) ((void) 0)
51305173
# define assert_(what)
5174+
# define __ASSERT_(statement)
51315175
#endif
51325176

51335177
struct ufuncs {

0 commit comments

Comments
 (0)