pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Add explicit casts in four places to simplehash.h
4+ messages / 2 participants
[nested] [flat]

Commits from this thread:
  2022-11-03 cf8b7d374a 50467082c9 725cd4d2e4 eeb5461e76  Add casts to simplehash.h to silence C++ warnings.
* Add explicit casts in four places to simplehash.h
@ 2022-11-03 11:34  David Geier <geidav.pg@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: David Geier @ 2022-11-03 11:34 UTC (permalink / raw)
  To: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>

Hi hackers,

While working on an extension, I found that simplehash.h is missing 
explicit casts in four places. Without these casts, compiling code 
including simplehash.h yields warnings if the code is compiled with 
-Wc++-compat.

PostgreSQL seems to mostly prefer omitting the explicit casts, however 
there are many places where an explicit cast is actually used. Among 
many others, see e.g.

bool.c:
     state = (BoolAggState *) MemoryContextAlloc(agg_context, 
sizeof(BoolAggState));

domains.c:
    my_extra = (DomainIOData *) MemoryContextAlloc(mcxt, 
sizeof(DomainIOData));

What about, while not being strictly necessary for PostgreSQL itself, 
also adding such casts to simplehash.h so that it can be used in code 
where -Wc++-compat is enabled?

Attached is a small patch that adds the aforementioned casts.
Thanks for your consideration!

--
David Geier
(ServiceNow)

Attachments:

0001-Add-explicit-casts-to-simplehash.h.patch (text/x-patch, 1.8K) download From d2665b4065227ffa6efd37ee912e4add082869cb Mon Sep 17 00:00:00 2001 From: David Geier <geidav.pg@gmail.com> Date: Thu, 3 Nov 2022 12:28:08 +0100 Subject: [PATCH] Add explicit casts to simplehash.h Without these casts compilation yields warnings when compiling the code with -Wc++-compat. --- src/include/lib/simplehash.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/include/lib/simplehash.h b/src/include/lib/simplehash.h index 329687c1a5..cdfae537a1 100644 --- a/src/include/lib/simplehash.h +++ b/src/include/lib/simplehash.h @@ -438,7 +438,7 @@ SH_CREATE(MemoryContext ctx, uint32 nelements, void *private_data) #ifdef SH_RAW_ALLOCATOR tb = SH_RAW_ALLOCATOR(sizeof(SH_TYPE)); #else - tb = MemoryContextAllocZero(ctx, sizeof(SH_TYPE)); + tb = (SH_TYPE *) MemoryContextAllocZero(ctx, sizeof(SH_TYPE)); tb->ctx = ctx; #endif tb->private_data = private_data; @@ -448,7 +448,7 @@ SH_CREATE(MemoryContext ctx, uint32 nelements, void *private_data) SH_COMPUTE_PARAMETERS(tb, size); - tb->data = SH_ALLOCATE(tb, sizeof(SH_ELEMENT_TYPE) * tb->size); + tb->data = (SH_ELEMENT_TYPE *) SH_ALLOCATE(tb, sizeof(SH_ELEMENT_TYPE) * tb->size); return tb; } @@ -493,7 +493,7 @@ SH_GROW(SH_TYPE * tb, uint64 newsize) /* compute parameters for new table */ SH_COMPUTE_PARAMETERS(tb, newsize); - tb->data = SH_ALLOCATE(tb, sizeof(SH_ELEMENT_TYPE) * tb->size); + tb->data = (SH_ELEMENT_TYPE *) SH_ALLOCATE(tb, sizeof(SH_ELEMENT_TYPE) * tb->size); newdata = tb->data; @@ -1059,7 +1059,7 @@ SH_STAT(SH_TYPE * tb) double fillfactor; uint32 i; - uint32 *collisions = palloc0(tb->size * sizeof(uint32)); + uint32 *collisions = (uint32 *) palloc0(tb->size * sizeof(uint32)); uint32 total_collisions = 0; uint32 max_collisions = 0; double avg_collisions; -- 2.34.1
^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: Add explicit casts in four places to simplehash.h
@ 2022-11-03 14:50  Tom Lane <tgl@sss.pgh.pa.us>
  parent: David Geier <geidav.pg@gmail.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2022-11-03 14:50 UTC (permalink / raw)
  To: David Geier <geidav.pg@gmail.com>; +Cc: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>

David Geier <geidav.pg@gmail.com> writes:
> What about, while not being strictly necessary for PostgreSQL itself, 
> also adding such casts to simplehash.h so that it can be used in code 
> where -Wc++-compat is enabled?

Seems reasonable, so done (I fixed one additional spot you missed).

The bigger picture here is that we do actually endeavor to keep
(most of) our headers C++ clean, but our tool cpluspluscheck misses
these problems because it doesn't try to use these macros.
I wonder whether there is a way to do better.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Add explicit casts in four places to simplehash.h
@ 2022-11-04 08:23  David Geier <geidav.pg@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: David Geier @ 2022-11-04 08:23 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>

On 11/3/22 15:50, Tom Lane wrote:
> Seems reasonable, so done (I fixed one additional spot you missed).
Thanks!
> The bigger picture here is that we do actually endeavor to keep
> (most of) our headers C++ clean, but our tool cpluspluscheck misses
> these problems because it doesn't try to use these macros.
> I wonder whether there is a way to do better.

What about having a custom header alongside cpluspluscheck which 
references all macros we care about?
We could start with the really big macros like the ones from 
simplehash.h and add as we go.
I could give this a try if deemed useful.

--
David Geier
(ServiceNow)






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Add explicit casts in four places to simplehash.h
@ 2022-11-04 13:25  Tom Lane <tgl@sss.pgh.pa.us>
  parent: David Geier <geidav.pg@gmail.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2022-11-04 13:25 UTC (permalink / raw)
  To: David Geier <geidav.pg@gmail.com>; +Cc: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>

David Geier <geidav.pg@gmail.com> writes:
> On 11/3/22 15:50, Tom Lane wrote:
>> The bigger picture here is that we do actually endeavor to keep
>> (most of) our headers C++ clean, but our tool cpluspluscheck misses
>> these problems because it doesn't try to use these macros.
>> I wonder whether there is a way to do better.

> What about having a custom header alongside cpluspluscheck which 
> references all macros we care about?

Can't see that that would help, because of the hand maintenance
required to make it useful.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2022-11-04 13:25 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2022-11-03 11:34 Add explicit casts in four places to simplehash.h David Geier <geidav.pg@gmail.com>
2022-11-03 14:50 ` Tom Lane <tgl@sss.pgh.pa.us>
2022-11-04 08:23   ` David Geier <geidav.pg@gmail.com>
2022-11-04 13:25     ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox