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 : List *list = list_make1(node);
41 :
42 3 : foreach_ptr(RangeTblRef, rtr, list)
43 : {
44 : (void) rtr;
45 : }
46 :
47 3 : foreach_node(RangeTblRef, rtr, list)
48 : {
49 : (void) rtr;
50 : }
51 :
52 : StaticAssertStmt(sizeof(int32) == 4, "int32 should be 4 bytes");
53 : (void) StaticAssertExpr(sizeof(int64) == 8, "int64 should be 8 bytes");
54 :
55 1 : list_free(list);
56 1 : pfree(node);
57 :
58 1 : switch (a)
59 : {
60 1 : case 1:
61 1 : elog(DEBUG1, "1");
62 : pg_fallthrough;
63 : case 2:
64 1 : elog(DEBUG1, "2");
65 1 : break;
66 : }
67 :
68 1 : PG_RETURN_INT32(a + b);
69 : }
|