Skip to content

v2: fix CleanC self-host C output#12

Closed
GGRei wants to merge 3 commits into
masterfrom
v2-cleanc-selfhost-c-output
Closed

v2: fix CleanC self-host C output#12
GGRei wants to merge 3 commits into
masterfrom
v2-cleanc-selfhost-c-output

Conversation

@GGRei

@GGRei GGRei commented Jun 7, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes the remaining V2 CleanC C-output issues that prevented cmd/v2/test_v2_self.sh from completing cleanly with GCC and Clang.

The changes cover:

  • hosted Linux gettid emission, avoiding undeclared libc calls
  • trace_calls handling for no_gettid and musl builds
  • FnSortCB casts for sort_with_compare / sorted_with_compare, including anonymous and capturing comparators
  • function-pointer alias casts, including cross-module alias homonyms
  • label emission before generated C declarations, which Clang rejects without a null statement

Validation

  • VJOBS=2 ./v test vlib/v2/gen/cleanc: passed, 4 passed / 4 skipped
  • VJOBS=1 bash cmd/v2/test_v2_self.sh: passed
  • VJOBS=1 V2CC=clang bash cmd/v2/test_v2_self.sh: passed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 01829eb8dc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread vlib/v2/gen/cleanc/expr.v Outdated
Comment on lines +903 to +910
base_name := typ.name()
if base_name != '' {
candidates << base_name
if base_name.contains('.') {
candidates << base_name.replace('.', '__')
} else if g.cur_module != '' && g.cur_module != 'main' && g.cur_module != 'builtin'
&& !base_name.contains('__') {
candidates << '${g.cur_module}__${base_name}'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Prefer module-qualified aliases before short names

When generating a cast in a non-main module, this unqualified base_name candidate is checked before the current-module-qualified name, and c_type_is_fn_pointer_alias() also accepts function-pointer aliases recorded from main. If a local non-function alias such as bar.SignalHandler = int is cast inside bar, while main.SignalHandler is a function-pointer alias, the cast target can be misidentified as the short SignalHandler function alias instead of bar__SignalHandler, producing the wrong C type. The existing collision test only covers bar.SignalHandler(...) from main, not the unqualified same-module cast path.

Useful? React with 👍 / 👎.

Comment thread vlib/v2/gen/cleanc/fn.v
Comment on lines +7966 to +7968
ast.FnLiteral {}
else {
return false

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Cast qualified sort comparators too

This only handles bare identifiers and function literals, so a comparator passed as a qualified function value such as array__sort_with_compare(mut items, other.compare_items) falls through and is emitted without the new (FnSortCB) cast. That leaves the same incompatible function-pointer type in the generated C for imported/module-qualified comparator helpers that this change fixes for local comparators, so strict C builds can still fail for those inputs.

Useful? React with 👍 / 👎.

Comment thread vlib/v2/gen/cleanc/fn.v
Comment on lines +7972 to +7973
g.sb.write_string('(FnSortCB)')
g.expr(comparator)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Limit FnSortCB casts to FnSortCB parameters

This cast is applied solely from the C function name and argument index, before checking the actual parameter type. In skip-builtin/custom-runtime code that defines or calls array__sort_with_compare/array__sorted_with_compare with a non-FnSortCB second parameter, any identifier argument is now emitted as (FnSortCB)arg, producing invalid or wrong C for an otherwise valid call. The cast should be gated by the resolved expected parameter type rather than the helper name alone.

Useful? React with 👍 / 👎.

@GGRei GGRei marked this pull request as draft June 7, 2026 11:58
@GGRei GGRei marked this pull request as ready for review June 7, 2026 12:34
@GGRei GGRei closed this Jun 7, 2026
@GGRei GGRei reopened this Jun 8, 2026
@GGRei GGRei marked this pull request as draft June 8, 2026 17:13
@GGRei GGRei force-pushed the v2-cleanc-selfhost-c-output branch from b50d6e5 to 1ff5fb7 Compare June 8, 2026 17:32
@GGRei

GGRei commented Jun 8, 2026

Copy link
Copy Markdown
Owner Author

Rebased this branch on top of latest master after vlang#27390, and adapted the CleanC changes to the updated upstream structure.

@GGRei GGRei marked this pull request as ready for review June 8, 2026 17:35

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1ff5fb7a7f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread vlib/v2/gen/cleanc/fn.v Outdated
}
c_mod_name := g.resolve_module_name(mod_name).replace('.', '_')
c_name := '${c_mod_name}__${sel.rhs.name}'
return c_name in g.fn_return_types || c_name in g.fn_param_is_ptr

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Cast module-level comparator values as FnSortCB

When the comparator is a module selector that resolves to a function-pointer value rather than a function declaration, e.g. an imported __global/const callback other.callback passed to array__sort_with_compare, this helper returns false because it only recognizes names recorded in fn_return_types/fn_param_is_ptr. Local identifier callback values are now cast, but the module-selector equivalent is emitted without (FnSortCB), so strict C still sees the incompatible callback typedef when code stores the comparator in another module.

Useful? React with 👍 / 👎.

@GGRei GGRei marked this pull request as draft June 8, 2026 17:52
@GGRei GGRei marked this pull request as ready for review June 8, 2026 18:09
@GGRei GGRei closed this Jun 8, 2026
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.

1 participant