safer node casting
Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
From: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
To: pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2016-12-31T17:08:22Z
Lists: pgsql-hackers
Attachments
- 0001-Add-castNode-macro.patch (text/x-patch) patch 0001
There is a common coding pattern that goes like this:
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
Assert(IsA(rinfo, RestrictInfo));
(Arguably, the Assert should come before the cast, but I guess it's done
this way out of convenience.)
(Not to mention the other common coding pattern of just doing the cast
and hoping for the best.)
I propose a macro castNode() that combines the assertion and the cast,
so this would become
RestrictInfo *rinfo = castNode(RestrictInfo, lfirst(lc));
This is inspired by the dynamic_cast operator in C++, but follows the
syntax of the well-known makeNode() macro.
Besides saving a bunch of code and making things safer, the function
syntax also makes some code easier to read by saving levels of
parentheses, for example:
- Assert(IsA(sstate->testexpr, BoolExprState));
- oplist = ((BoolExprState *) sstate->testexpr)->args;
+ oplist = castNode(BoolExprState, sstate->testexpr)->args;
Attached is a patch that shows how this would work. There is a lot more
that can be done, but I just stopped after a while for now.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Commits
-
Suppress unused-variable warning.
- c56ac2913a1f 10.0 landed
-
Make more use of castNode()
- 38d103763d14 10.0 cited
-
Add castNode(type, ptr) for safe casting between NodeTag based types.
- 1ad163ef0130 9.3.16 landed
- 14d0e290cbe7 9.2.20 landed
- cf8c86af950b 9.4.11 landed
- 8fef0c34143b 9.5.6 landed
- 5bcab111426e 10.0 landed
- 574b091e5835 9.6.2 landed
-
Use the new castNode() macro in a number of places.
- 9ba8a9ce4548 10.0 landed