Skip to content

cl: emit // llgo:type C for records with callback fields - #953

Merged
xushiwei merged 1 commit into
mainfrom
fennoai/issue-952-1791294668
Oct 6, 2026
Merged

xushiwei merged 1 commit into
mainfrom
fennoai/issue-952-1791294668

Conversation

@fennoai

@fennoai fennoai Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

Requested by @xushiwei

Fixes #952.

A C/C++ struct/class whose members include callback (function-pointer) fields now carries the // llgo:type C directive on its generated Go type, so those callbacks use the C calling convention — consistent with how function-pointer typedefs are already handled.

What changed

  • cl/class.go: in initClassTypeEx, after visiting the record members, if featHasCallback was set (propagated up from toTypeEx when a field is a function pointer), upgrade the doc-only comment to the directive form via the existing directiveTypeC helper — reusing the same logic path as defineTypedef.
  • Updated the affected golden test expectations:
    • TestPython: MemAllocatorEx in tool/_testc/python-3.14.8/pythread.go.
    • TestClang / TestSingleC: CursorAndRangeVisitor and IndexerCallbacks in tool/_testc/clang-c-22.1.8/Index.go and Index/out.go.

Verification

  • go build ./cl/... and go vet ./cl/ pass.
  • A repo-wide sweep confirms these are the only generated structs with function-pointer fields, and all now carry the directive.
  • TestPython/TestClang/TestSingleC compare against the clang C library + llgo runtime and are macOS-gated, so they cannot execute in this Linux sandbox (the link step fails on C.clang_*/llgo.* symbols, unrelated to this change). The golden files were updated to match the new generator output and should be confirmed on macOS/CI.

A C/C++ struct/class whose members include function-pointer (callback)
fields must carry the // llgo:type C directive so those callbacks use the
C calling convention, consistent with how function-pointer typedefs are
already handled in defineTypedef.

featHasCallback is propagated up from toTypeEx while visiting the record
members, so after the member-visit loop in initClassTypeEx we upgrade the
doc-only comment to the directive form via directiveTypeC.

Updates the affected golden test expectations (TestPython MemAllocatorEx,
plus CursorAndRangeVisitor and IndexerCallbacks in the clang-c cases).

Fixes #952

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: emit // llgo:type C for records with callback fields

The change is clean and well-targeted. A record (struct) that holds a function-pointer (callback) field now carries the // llgo:type C directive so those callbacks use the C calling convention, mirroring the existing defineTypedef handling. The explanatory comment is accurate and the generated test outputs under tool/_testc/ are formatted consistently with directiveComments (doc block + // separator + directive, or a bare directive when there is no doc).

Verified:

  • featHasCallback is set in toTypeEx (cl/type.go:162,174) during member visiting, so the flag is genuinely only known at this point — the overwrite of the doc set at line 225 is correct.
  • The featAllIgnore early-return at line 251 runs before the callback check, so a record that will be dropped never gets the directive.
  • featHasCallback propagation is sound: anonymous nested records are emitted via emitClass/emitUnion with their own feats, so a nested callback does not spuriously flag the parent.

No blocking issues. One minor, optional observation inline.

Note: the macOS-gated codegen tests that regenerate the tool/_testc/ golden outputs could not be run in this Linux sandbox — worth confirming on CI.

Comment thread cl/class.go
@codecov

codecov Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.00%. Comparing base (f72fb49) to head (570457e).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #953   +/-   ##
=======================================
  Coverage   87.99%   88.00%           
=======================================
  Files          23       23           
  Lines        2665     2667    +2     
=======================================
+ Hits         2345     2347    +2     
  Misses        320      320           
Flag Coverage Δ
llgo-tests 88.00% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@xushiwei
xushiwei merged commit 41406e0 into main Oct 6, 2026
4 checks passed
@fennoai
fennoai Bot deleted the fennoai/issue-952-1791294668 branch October 6, 2026 14:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Struct/class with callback-pointer fields is missing // llgo:type C directive

1 participant