]> granicus.if.org Git - postgresql/commit
Provide some static-assertion functionality on all compilers.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 1 Oct 2012 02:46:29 +0000 (22:46 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 1 Oct 2012 02:46:29 +0000 (22:46 -0400)
commit0d0aa5d29175c539db1981be27dbbf059be6f3b1
treeb9d7269cca0ea25458f8d30f59a0ac3589f8ac8c
parentea473fb2dee7dfe61f8fbdfac9d9da87d84e564e
Provide some static-assertion functionality on all compilers.

On reflection (especially after noticing how many buildfarm critters have
__builtin_types_compatible_p but not _Static_assert), it seems like we
ought to try a bit harder to make these macros do something everywhere.
The initial cut at it would have been no help to code that is compiled only
on platforms without _Static_assert, for instance; and in any case not all
our contributors do their initial coding on the latest gcc version.

Some googling about static assertions turns up quite a bit of prior art
for making it work in compilers that lack _Static_assert.  The method
that seems closest to our needs involves defining a struct with a bit-field
that has negative width if the assertion condition fails.  There seems no
reliable way to get the error message string to be output, but throwing a
compile error with a confusing message is better than missing the problem
altogether.

In the same spirit, if we don't have __builtin_types_compatible_p we can at
least insist that the variable have the same width as the type.  This won't
catch errors such as "wrong pointer type", but it's far better than
nothing.

In addition to changing the macro definitions, adjust a
compile-time-constant Assert in contrib/hstore to use StaticAssertStmt,
so we can get some buildfarm coverage on whether that macro behaves sanely
or not.  There's surely more places that could be converted, but this is
the first one I came across.
contrib/hstore/hstore_compat.c
src/include/c.h