Skip to content

Commit 9a53e6b

Browse files
committed
Move __ASSERT_() definition adjacent to assert_()
And fix up the documentation, adding a caution that their use can easily lead to differing behavior in DEBUGGING vs non-DEBUGGING builds. These two macros are synonymous. This documents assert_() for the first time and clarifies their usage. Thanks to Tony Cook for clarifying this for me.
1 parent 28cffa9 commit 9a53e6b

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
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: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5109,6 +5109,45 @@ 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. */
@@ -5128,9 +5167,11 @@ Gid_t getegid (void);
51285167
STRINGIFY(what), (line_t) __LINE__), \
51295168
(void) 0))
51305169
# define assert_(what) assert(what),
5170+
# define __ASSERT_(statement) assert(statement),
51315171
#else
51325172
# define Perl_assert(what) ((void) 0)
51335173
# define assert_(what)
5174+
# define __ASSERT_(statement)
51345175
#endif
51355176

51365177
struct ufuncs {

0 commit comments

Comments
 (0)