Skip to content

Commit 0a46b45

Browse files
luisleo526claude
andcommitted
The public C headers compile as strict C99, -pedantic-errors included (R5 lane F4, item 9)
Audit AUDIT3 §2.2 P2c: `cc -std=c99 -pedantic-errors -include pineforge/pineforge.h` passed before P2c and failed after it -- the sharpe_tv / sortino_tv aliases are anonymous unions, a C11 construct -- while ADR-0001's portability note promised a strict C99 consumer "a warning, not an error". pineforge.h now spells the two unions PF_ANONYMOUS_UNION, which GCC and Clang expand to `__extension__ union`: the exemption covers exactly that one declaration and nothing else of the consumer's (plain `union` elsewhere). Same layout, same offsets (48 / 56), same aliasing, same serialized keys; PF_ABI_VERSION stays 4. Nothing else in either public header needed C11 -- every enumeration this lane added is C99-clean (enumerator values within int, no other anonymous members). The ADR's portability note is corrected to what the tree now does and names its witness. New CTest row test_native_c_api_c99 (tests/test_native_c_api_c99.c, C_STANDARD 99, C_EXTENSIONS OFF, -pedantic-errors -Wall -Wextra): compiles both public C headers as strict C99, executes the alias both ways, and drives one native C run through the typed enumerations (the calculation point's words, BATCH_COMPLETE, a WORKING_UNITS_SATISFIED fill, the NATIVE_MARKET_V1 contract). It links pineforge only, so it runs in the kernel profile too: the integrator adds 1 to KERNEL_MIN_TESTS and 1 to RELEASE_MIN_TESTS. Fail-before (the new TU at -std=c99 -pedantic-errors against the unchanged header): rc=1, `include/pineforge/pineforge.h:280:5: error: anonymous unions are a C11 extension [-Werror,-Wc11-extensions]` and the same at :290. After: rc=0; test_native_c_api_c99: ok. Deviation: tests/CMakeLists.txt (the brief's "+ new C rows"). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> (cherry picked from commit c8be30e5b7cd90066cc746117b29830eec367a66)
1 parent 4a0f1fe commit 0a46b45

4 files changed

Lines changed: 203 additions & 9 deletions

File tree

‎docs/adr/0001-kernel-adapter-boundary.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,14 @@ comment saying it is the historical spelling of `sharpe_monthly` / `sortino_mont
459459
**Portability note for the C alias.** An anonymous union is C11. This project already compiles C at
460460
C11 (`CMAKE_C_STANDARD 11`, and the native C examples document `cc -std=c11`), and C++ has had
461461
anonymous unions since C++98, so every consumer in this repository is covered. A consumer compiling
462-
the public header as strict C99 gets a warning (`-Wc11-extensions`), not an error. The rejected
463-
alternative was `#define sharpe_tv sharpe_monthly`: a macro leaks into every translation unit that
464-
includes the header and would rewrite an unrelated consumer's own `sharpe_tv`.
462+
the public header as strict C99 compiles it too, `-pedantic-errors` included: the union is spelled
463+
`PF_ANONYMOUS_UNION`, which GCC and Clang expand to `__extension__ union` — an exemption for that one
464+
declaration and nothing else of the consumer's. (The first version of this note promised such a
465+
consumer "a warning, not an error"; under `-pedantic-errors` it was an error, which R5 lane F4 fixed.)
466+
The CTest row `test_native_c_api_c99` compiles both public C headers at `-std=c99 -pedantic-errors`
467+
with extensions off and executes the alias both ways. The rejected alternative was
468+
`#define sharpe_tv sharpe_monthly`: a macro leaks into every translation unit that includes the header
469+
and would rewrite an unrelated consumer's own `sharpe_tv`.
465470

466471
## Kernel capabilities the Pine adapter does not declare (the rulings the feature gate holds)
467472

‎include/pineforge/pineforge.h‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@
8080
#define PF_DEPRECATED(msg)
8181
#endif
8282

83+
/* An anonymous union member: C11 and C++ have them, and so does every
84+
* compiler this runtime builds with, but strict C99 does not -- a C99
85+
* consumer compiling with -pedantic-errors would reject the declaration.
86+
* GCC and Clang mark it __extension__, which exempts exactly that one
87+
* declaration from -pedantic and nothing else in the consumer. */
88+
#if defined(__GNUC__) || defined(__clang__)
89+
#define PF_ANONYMOUS_UNION __extension__ union
90+
#else
91+
#define PF_ANONYMOUS_UNION union
92+
#endif
93+
8394
/** Monotonic ABI version of pf_report_t / pf_trade_t layout. Bumped
8495
* whenever a caller-visible struct grows. Consumers MUST verify
8596
* pf_abi_version() == PF_ABI_VERSION before calling run_backtest.
@@ -282,13 +293,13 @@ typedef struct pf_equity_stats_s {
282293
* Uses sample (N-1) stddev. NaN with <2 monthly returns or zero deviation.
283294
*
284295
* `sharpe_tv` is the historical spelling of this same field. Both names
285-
* are one `double` at one offset (a C11 anonymous union of two members of
286-
* the same type), so the struct's size and every field offset are
287-
* unchanged and a caller compiled against either spelling reads the same
288-
* storage. The old spelling is DEPRECATED and is removed at the next
296+
* are one `double` at one offset (an anonymous union of two members of
297+
* the same type, #PF_ANONYMOUS_UNION so a strict C99 consumer compiles
298+
* it too), so the struct's size and every field offset are unchanged and
299+
* a caller compiled against either spelling reads the same storage. The old spelling is DEPRECATED and is removed at the next
289300
* #PF_ABI_VERSION; see ADR-0001 "Deprecated public spellings". The
290301
* serialized report key stays `sharpe_tv` (report-schema name). */
291-
union {
302+
PF_ANONYMOUS_UNION {
292303
double sharpe_monthly;
293304
PF_DEPRECATED("sharpe_tv is the historical spelling of sharpe_monthly; "
294305
"removed at PF_ABI_VERSION 5")
@@ -300,7 +311,7 @@ typedef struct pf_equity_stats_s {
300311
*
301312
* `sortino_tv` is the historical spelling of this same field, on the same
302313
* terms as sharpe_monthly / sharpe_tv above. */
303-
union {
314+
PF_ANONYMOUS_UNION {
304315
double sortino_monthly;
305316
PF_DEPRECATED("sortino_tv is the historical spelling of sortino_monthly; "
306317
"removed at PF_ABI_VERSION 5")

‎tests/CMakeLists.txt‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,22 @@ endif()
15851585
pineforge_add_twin_test(test_native_c_api test_native_c_api)
15861586
pineforge_add_twin_test(test_native_c_api_frozen_header test_native_c_api_frozen_header)
15871587

1588+
# R5 lane F4: the public C headers as a strict C99 consumer compiles them --
1589+
# ADR-0001's P2c portability note. The one C11 construct they hold, the
1590+
# anonymous union behind sharpe_tv / sortino_tv, is PF_ANONYMOUS_UNION; with
1591+
# extensions off and -pedantic-errors, any other C11-only construct in
1592+
# <pineforge/pineforge.h> or <pineforge/native_c_api.h> is a build failure.
1593+
# The row executes the alias and one native C run.
1594+
add_executable(test_native_c_api_c99 test_native_c_api_c99.c)
1595+
set_target_properties(test_native_c_api_c99 PROPERTIES
1596+
C_STANDARD 99 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF LINKER_LANGUAGE CXX)
1597+
target_link_libraries(test_native_c_api_c99 PRIVATE pineforge)
1598+
target_compile_options(test_native_c_api_c99 PRIVATE -UNDEBUG -pedantic-errors -Wall -Wextra)
1599+
if(PINEFORGE_ENABLE_COVERAGE)
1600+
target_compile_options(test_native_c_api_c99 PRIVATE ${_pf_cov_test_flags})
1601+
endif()
1602+
pineforge_add_twin_test(test_native_c_api_c99 test_native_c_api_c99)
1603+
15881604
add_test(NAME test_market_admission_schema COMMAND ${Python3_EXECUTABLE}
15891605
${PROJECT_SOURCE_DIR}/scripts/check_market_admission_schema.py)
15901606
add_test(NAME test_market_admission_schema_mutations COMMAND ${Python3_EXECUTABLE}

‎tests/test_native_c_api_c99.c‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* test_native_c_api_c99.c -- the public C headers as a strict C99 consumer
3+
* sees them (R5 lane F4, item 9).
4+
*
5+
* ADR-0001's P2c ruling keeps `sharpe_tv` / `sortino_tv` as deprecated
6+
* spellings of `sharpe_monthly` / `sortino_monthly`: one double behind an
7+
* anonymous union, which is a C11 construct. Its portability note promised a
8+
* strict C99 consumer a warning; under -pedantic-errors it was an error
9+
* (audit AUDIT3 §2.2 P2c). tests/CMakeLists.txt compiles this translation unit
10+
* as C99 with extensions off and -pedantic-errors, so any C11-only construct
11+
* in <pineforge/pineforge.h> or <pineforge/native_c_api.h> fails the build.
12+
* Its body executes what such a caller relies on: the alias reads one storage
13+
* both ways, and a native C host configures through the typed enumerations,
14+
* runs, and reads its words back by their C names.
15+
*
16+
* No assert(): the Release gate defines NDEBUG and would no-op every row.
17+
*/
18+
19+
#include <pineforge/pineforge.h>
20+
21+
#include <stddef.h>
22+
#include <stdio.h>
23+
#include <string.h>
24+
25+
static int failures = 0;
26+
27+
#define CHECK(cond, msg) \
28+
do { \
29+
if (!(cond)) { \
30+
fprintf(stderr, "FAIL %s:%d %s\n", __FILE__, __LINE__, (msg)); \
31+
++failures; \
32+
} \
33+
} while (0)
34+
35+
/* open, high, low, close, volume, timestamp (Unix milliseconds). */
36+
static const pf_bar_t kBars[] = {
37+
{100.0, 102.0, 99.0, 101.0, 4.0, 0},
38+
{101.0, 103.0, 100.0, 102.0, 4.0, 300000},
39+
{102.0, 104.0, 101.0, 103.0, 4.0, 600000},
40+
{103.0, 105.0, 102.0, 104.0, 4.0, 900000}
41+
};
42+
43+
struct c99_host {
44+
pf_strategy_t handle;
45+
int calculations;
46+
int unnamed;
47+
};
48+
49+
static int c99_on_bar(void* user, const pf_bar_t* bar, const pf_native_decision_v1* at) {
50+
struct c99_host* host = (struct c99_host*)user;
51+
pf_native_request_v1 request;
52+
(void)bar;
53+
if (at->provenance != PF_NATIVE_PROVENANCE_CALCULATION
54+
|| at->path_phase != PF_NATIVE_PATH_PHASE_NONE
55+
|| at->completion != PF_NATIVE_COMPLETION_KIND_CONFIRMED) {
56+
++host->unnamed;
57+
}
58+
if (++host->calculations != 1) return 0;
59+
memset(&request, 0, sizeof(request));
60+
request.struct_size = (uint32_t)sizeof(request);
61+
request.version = PF_NATIVE_API_VERSION;
62+
request.intent = PF_NATIVE_INTENT_TRANSACT;
63+
request.intent_value = 1.0;
64+
request.trigger = PF_NATIVE_TRIGGER_MARKET;
65+
return strategy_native_submit_v1(host->handle, &request, NULL, NULL) == PF_NATIVE_OK ? 0 : 1;
66+
}
67+
68+
static void check_deprecated_alias(void) {
69+
pf_equity_stats_t stats;
70+
CHECK(offsetof(pf_equity_stats_t, sharpe_tv) == offsetof(pf_equity_stats_t, sharpe_monthly),
71+
"sharpe_tv is not sharpe_monthly's storage");
72+
CHECK(offsetof(pf_equity_stats_t, sortino_tv)
73+
== offsetof(pf_equity_stats_t, sortino_monthly),
74+
"sortino_tv is not sortino_monthly's storage");
75+
memset(&stats, 0, sizeof(stats));
76+
stats.sharpe_monthly = 1.25;
77+
stats.sortino_tv = -0.5;
78+
CHECK(stats.sharpe_tv == 1.25, "a write through sharpe_monthly does not read as sharpe_tv");
79+
CHECK(stats.sortino_monthly == -0.5,
80+
"a write through sortino_tv does not read as sortino_monthly");
81+
}
82+
83+
static void check_native_run(void) {
84+
struct c99_host host;
85+
pf_native_callbacks_v1 callbacks;
86+
pf_native_run_spec_v1 spec;
87+
pf_native_state_v1 state;
88+
pf_native_event_v1 events[16];
89+
double units = 0.0;
90+
int written, i, applied = 0;
91+
92+
memset(&host, 0, sizeof(host));
93+
memset(&callbacks, 0, sizeof(callbacks));
94+
callbacks.struct_size = (uint32_t)sizeof(callbacks);
95+
callbacks.version = PF_NATIVE_API_VERSION;
96+
callbacks.user = &host;
97+
callbacks.on_bar = c99_on_bar;
98+
host.handle = strategy_native_host_create_v1(&callbacks);
99+
CHECK(host.handle != NULL, "a C99 caller's callback table was refused");
100+
if (!host.handle) return;
101+
102+
memset(&spec, 0, sizeof(spec));
103+
spec.struct_size = (uint32_t)sizeof(spec);
104+
spec.session_key = "native-c-api-c99";
105+
spec.run_number = 1;
106+
spec.input_tf = "5";
107+
spec.script_tf = "5";
108+
spec.ticker = "MOCK";
109+
spec.tickerid = "TEST:MOCK";
110+
spec.type = "crypto";
111+
spec.currency = "USDT";
112+
spec.basecurrency = "ETH";
113+
spec.description = "";
114+
spec.volumetype = "";
115+
spec.timezone = "UTC";
116+
spec.session = "24x7";
117+
spec.chart_timezone = "";
118+
spec.initial_capital = 10000.0;
119+
spec.point_value = 1.0;
120+
spec.account_fx = 1.0;
121+
spec.price_tick = 0.01;
122+
spec.fee_kind = PF_NATIVE_FEE_PERCENT;
123+
spec.close_execution = PF_NATIVE_CLOSE_EXECUTION_NEXT_ELIGIBLE_POINT;
124+
spec.allowed_open_directions = PF_NATIVE_OPEN_DIRECTIONS_BOTH;
125+
spec.optional_mask = PF_NATIVE_SPEC_OPTIONAL_QUANTITY_GRID;
126+
spec.quantity_grid = 1.0;
127+
CHECK(strategy_configure_native_v1(host.handle, &spec) == 0, "the C99 spec was refused");
128+
CHECK(strategy_execution_contract(host.handle) == PF_EXECUTION_CONTRACT_NATIVE_MARKET_V1,
129+
"a native host is not on the native market contract");
130+
CHECK(strategy_native_run_v1(host.handle, kBars, 4, NULL) == PF_NATIVE_OK,
131+
"the C99 run did not complete");
132+
CHECK(host.calculations == 4 && host.unnamed == 0,
133+
"a C99 calculation was not the named calculation point");
134+
135+
memset(&state, 0, sizeof(state));
136+
state.struct_size = (uint32_t)sizeof(state);
137+
CHECK(strategy_native_state_v1(host.handle, &state) == PF_NATIVE_OK, "C99 state read");
138+
CHECK(state.lifecycle == PF_NATIVE_LIFECYCLE_COMPLETED
139+
&& state.completion == PF_NATIVE_COMPLETION_BATCH_COMPLETE
140+
&& state.failure_code == PF_NATIVE_FAILURE_NONE,
141+
"the C99 run did not end as a completed batch");
142+
memset(events, 0, sizeof(events));
143+
written = strategy_native_events_v1(host.handle, 0, events, 16);
144+
for (i = 0; i < written; ++i) {
145+
if (events[i].kind == PF_NATIVE_EVENT_APPLIED && events[i].terminal
146+
&& events[i].reason == PF_NATIVE_TERMINAL_WORKING_UNITS_SATISFIED) {
147+
++applied;
148+
}
149+
}
150+
CHECK(applied == 1, "the C99 entry did not fill as one satisfied execution");
151+
CHECK(strategy_native_position_v1(host.handle, &units, NULL, NULL) == PF_NATIVE_OK
152+
&& units == 1.0,
153+
"the C99 entry did not leave a long 1");
154+
strategy_native_host_free(host.handle);
155+
}
156+
157+
int main(void) {
158+
check_deprecated_alias();
159+
check_native_run();
160+
printf("test_native_c_api_c99: %s\n", failures == 0 ? "ok" : "FAILED");
161+
return failures == 0 ? 0 : 1;
162+
}

0 commit comments

Comments
 (0)