Line data Source code
1 : /*--------------------------------------------------------------------------
2 : *
3 : * test_cplusplusext.cpp
4 : * Test that PostgreSQL headers compile with a C++ compiler.
5 : *
6 : * This file is compiled with a C++ compiler to verify that PostgreSQL
7 : * headers remain compatible with C++ extensions.
8 : *
9 : * Copyright (c) 2025-2026, PostgreSQL Global Development Group
10 : *
11 : * IDENTIFICATION
12 : * src/test/modules/test_cplusplusext/test_cplusplusext.cpp
13 : *
14 : * -------------------------------------------------------------------------
15 : */
16 :
17 : extern "C" {
18 : #include "postgres.h"
19 : #include "fmgr.h"
20 : #include "nodes/pg_list.h"
21 : #include "nodes/primnodes.h"
22 :
23 1 : PG_MODULE_MAGIC;
24 :
25 2 : PG_FUNCTION_INFO_V1(test_cplusplus_add);
26 : }
27 :
28 : StaticAssertDecl(sizeof(int32) == 4, "int32 should be 4 bytes");
29 :
30 : /*
31 : * Simple function that returns the sum of two integers. This verifies that
32 : * C++ extension modules can be loaded and called correctly at runtime.
33 : */
34 : extern "C" Datum
35 1 : test_cplusplus_add(PG_FUNCTION_ARGS)
36 : {
37 1 : int32 a = PG_GETARG_INT32(0);
38 1 : int32 b = PG_GETARG_INT32(1);
39 1 : RangeTblRef *node = makeNode(RangeTblRef);
40 1 : const RangeTblRef *nodec = node;
41 1 : RangeTblRef *copy = copyObject(nodec);
42 1 : List *list = list_make1(node);
43 :
44 3 : foreach_ptr(RangeTblRef, rtr, list)
45 : {
46 : (void) rtr;
47 : }
48 :
49 3 : foreach_node(RangeTblRef, rtr, list)
50 : {
51 : (void) rtr;
52 : }
53 :
54 : StaticAssertStmt(sizeof(int32) == 4, "int32 should be 4 bytes");
55 : (void) StaticAssertExpr(sizeof(int64) == 8, "int64 should be 8 bytes");
56 :
57 1 : list_free(list);
58 1 : pfree(node);
59 1 : pfree(copy);
60 :
61 1 : switch (a)
62 : {
63 1 : case 1:
64 1 : elog(DEBUG1, "1");
65 : pg_fallthrough;
66 : case 2:
67 1 : elog(DEBUG1, "2");
68 1 : break;
69 : }
70 :
71 1 : PG_RETURN_INT32(a + b);
72 : }
|