Skip to content

Commit 4ae3795

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 e780767 commit 4ae3795

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
@@ -5092,6 +5092,45 @@ Gid_t getegid (void);
50925092
Perl_deb(aTHX_ "%s scope %ld (savestack=%ld) at %s:%d\n", \
50935093
where, (long)PL_scopestack_ix, (long)PL_savestack_ix, \
50945094
__FILE__, __LINE__));
5095+
/*
5096+
=for apidoc_section $directives
5097+
=for apidoc Am|void|assert_|bool expr
5098+
=for apidoc_item | |__ASSERT_
5099+
5100+
These are synonymous, used to wrap the libc C<assert()> call in comma
5101+
expressions in macro expansions, but you probably don't want to use them nor
5102+
plain C<assert>; read on.
5103+
5104+
In DEBUGGING builds, each expands to an assert of its argument, followed by
5105+
a comma. (That is what the trailing underscore signifies.)
5106+
5107+
In non-DEBUGGING builds, each expands to nothing.
5108+
5109+
They thus can be used to string together a bunch of asserts in a comma
5110+
expression that is syntactically valid in either type of build.
5111+
5112+
NOTE, however, use of these (and plain C<assert()>) is discouraged in a macro.
5113+
This is because their usual use is to validate some of the arguments to that
5114+
macro. That will likely lead to the evaluation of those arguments more than
5115+
once during the macro expansion. If such an argument is an expression with
5116+
side effects, the behavior of the macro will differ between DEBUGGING and
5117+
non-DEBUGGING builds.
5118+
5119+
And, they are necessary only on platforms where the libc C<assert()> expands to
5120+
nothing when not in a DEBUGGING build. There should be no such platforms now
5121+
in existence, as the C89 standard forbids that, and Perl requires at least C99.
5122+
So, you can just us plain C<assert>, and say S<C<assert(...), assert(...),>>
5123+
and everything will compile (and will work if none of the arguments to the
5124+
asserts is an expression with side effects).
5125+
5126+
These macros are retained for backward compatibility.
5127+
5128+
Do NOT use C<__ASSERT_>. A name with two leading underscores followed by a
5129+
capital letter is reserved for the use of the compiler and libc in some
5130+
contexts in C, and in all contexts in C++.
5131+
5132+
=cut
5133+
*/
50955134

50965135
/* Keep the old croak based assert for those who want it, and as a fallback if
50975136
the platform is so heretically non-ANSI that it can't assert. */
@@ -5110,9 +5149,11 @@ Gid_t getegid (void);
51105149
" file \"" __FILE__ "\", line %" LINE_Tf, \
51115150
STRINGIFY(what), (line_t) __LINE__))
51125151
# define assert_(what) assert(what),
5152+
# define __ASSERT_(statement) assert(statement),
51135153
#else
51145154
# define Perl_assert(what) ((void) 0)
51155155
# define assert_(what)
5156+
# define __ASSERT_(statement)
51165157
#endif
51175158

51185159
struct ufuncs {

0 commit comments

Comments
 (0)