Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c8b2ef0

Browse files
committed
Convert *GetDatum() and DatumGet*() macros to inline functions
The previous macro implementations just cast the argument to a target type but did not check whether the input type was appropriate. The function implementation can do better type checking of the input type. For the *GetDatumFast() macros, converting to an inline function doesn't work in the !USE_FLOAT8_BYVAL case, but we can use AssertVariableIsOfTypeMacro() to get a similar level of type checking. Reviewed-by: Aleksander Alekseev <aleksander@timescale.com> Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://www.postgresql.org/message-id/flat/8528fb7e-0aa2-6b54-85fb-0c0886dbd6ed%40enterprisedb.com
1 parent 8caf96d commit c8b2ef0

27 files changed

+651
-190
lines changed

contrib/intarray/_int_gist.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ g_int_consistent(PG_FUNCTION_ARGS)
5151

5252
/* Oid subtype = PG_GETARG_OID(3); */
5353
bool *recheck = (bool *) PG_GETARG_POINTER(4);
54-
bool retval;
54+
bool retval = false; /* silence compiler warning */
5555

5656
/* this is exact except for RTSameStrategyNumber */
5757
*recheck = (strategy == RTSameStrategyNumber);

doc/src/sgml/xfunc.sgml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2763,7 +2763,7 @@ c_overpaid(PG_FUNCTION_ARGS)
27632763
is null. <function>GetAttributeByName</function> returns a <type>Datum</type>
27642764
value that you can convert to the proper data type by using the
27652765
appropriate <function>DatumGet<replaceable>XXX</replaceable>()</function>
2766-
macro. Note that the return value is meaningless if the null flag is
2766+
function. Note that the return value is meaningless if the null flag is
27672767
set; always check the null flag before trying to do anything with the
27682768
result.
27692769
</para>

src/backend/access/gist/gistutil.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ gistMakeUnionKey(GISTSTATE *giststate, int attno,
280280
bool
281281
gistKeyIsEQ(GISTSTATE *giststate, int attno, Datum a, Datum b)
282282
{
283-
bool result;
283+
bool result = false; /* silence compiler warning */
284284

285285
FunctionCall3Coll(&giststate->equalFn[attno],
286286
giststate->supportCollation[attno],

src/backend/tsearch/ts_parse.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ void
354354
parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
355355
{
356356
int type,
357-
lenlemm;
357+
lenlemm = 0; /* silence compiler warning */
358358
char *lemm = NULL;
359359
LexizeData ldata;
360360
TSLexeme *norms;
@@ -529,7 +529,7 @@ void
529529
hlparsetext(Oid cfgId, HeadlineParsedText *prs, TSQuery query, char *buf, int buflen)
530530
{
531531
int type,
532-
lenlemm;
532+
lenlemm = 0; /* silence compiler warning */
533533
char *lemm = NULL;
534534
LexizeData ldata;
535535
TSLexeme *norms;

src/backend/utils/mb/mbutils.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,8 @@ pg_do_encoding_conversion(unsigned char *src, int len,
409409
(void) OidFunctionCall6(proc,
410410
Int32GetDatum(src_encoding),
411411
Int32GetDatum(dest_encoding),
412-
CStringGetDatum(src),
413-
CStringGetDatum(result),
412+
CStringGetDatum((char *) src),
413+
CStringGetDatum((char *) result),
414414
Int32GetDatum(len),
415415
BoolGetDatum(false));
416416

@@ -485,8 +485,8 @@ pg_do_encoding_conversion_buf(Oid proc,
485485
result = OidFunctionCall6(proc,
486486
Int32GetDatum(src_encoding),
487487
Int32GetDatum(dest_encoding),
488-
CStringGetDatum(src),
489-
CStringGetDatum(dest),
488+
CStringGetDatum((char *) src),
489+
CStringGetDatum((char *) dest),
490490
Int32GetDatum(srclen),
491491
BoolGetDatum(noError));
492492
return DatumGetInt32(result);
@@ -910,8 +910,8 @@ pg_unicode_to_server(pg_wchar c, unsigned char *s)
910910
FunctionCall6(Utf8ToServerConvProc,
911911
Int32GetDatum(PG_UTF8),
912912
Int32GetDatum(server_encoding),
913-
CStringGetDatum(c_as_utf8),
914-
CStringGetDatum(s),
913+
CStringGetDatum((char *) c_as_utf8),
914+
CStringGetDatum((char *) s),
915915
Int32GetDatum(c_as_utf8_len),
916916
BoolGetDatum(false));
917917
}

src/include/access/gin.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,18 @@ typedef char GinTernaryValue;
6262
#define GIN_MAYBE 2 /* don't know if item is present / don't know
6363
* if matches */
6464

65-
#define DatumGetGinTernaryValue(X) ((GinTernaryValue)(X))
66-
#define GinTernaryValueGetDatum(X) ((Datum)(X))
65+
static inline GinTernaryValue
66+
DatumGetGinTernaryValue(Datum X)
67+
{
68+
return (GinTernaryValue) X;
69+
}
70+
71+
static inline Datum
72+
GinTernaryValueGetDatum(GinTernaryValue X)
73+
{
74+
return (Datum) X;
75+
}
76+
6777
#define PG_RETURN_GIN_TERNARY_VALUE(x) return GinTernaryValueGetDatum(x)
6878

6979
/* GUC parameters */

src/include/funcapi.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ extern TupleDesc build_function_result_tupdesc_t(HeapTuple procTuple);
204204
* Datum HeapTupleHeaderGetDatum(HeapTupleHeader tuple) - convert a
205205
* HeapTupleHeader to a Datum.
206206
*
207-
* Macro declarations:
207+
* Inline declarations:
208208
* HeapTupleGetDatum(HeapTuple tuple) - convert a HeapTuple to a Datum.
209209
*
210210
* Obsolete routines and macros:
@@ -217,10 +217,6 @@ extern TupleDesc build_function_result_tupdesc_t(HeapTuple procTuple);
217217
*----------
218218
*/
219219

220-
#define HeapTupleGetDatum(tuple) HeapTupleHeaderGetDatum((tuple)->t_data)
221-
/* obsolete version of above */
222-
#define TupleGetDatum(_slot, _tuple) HeapTupleGetDatum(_tuple)
223-
224220
extern TupleDesc RelationNameGetTupleDesc(const char *relname);
225221
extern TupleDesc TypeGetTupleDesc(Oid typeoid, List *colaliases);
226222

@@ -230,6 +226,14 @@ extern AttInMetadata *TupleDescGetAttInMetadata(TupleDesc tupdesc);
230226
extern HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values);
231227
extern Datum HeapTupleHeaderGetDatum(HeapTupleHeader tuple);
232228

229+
static inline Datum
230+
HeapTupleGetDatum(const HeapTupleData *tuple)
231+
{
232+
return HeapTupleHeaderGetDatum(tuple->t_data);
233+
}
234+
/* obsolete version of above */
235+
#define TupleGetDatum(_slot, _tuple) HeapTupleGetDatum(_tuple)
236+
233237

234238
/*----------
235239
* Support for Set Returning Functions (SRFs)

0 commit comments

Comments
 (0)