Skip to content

cgen: stop treating every .m as an Objective-C import#26794

Merged
JalonSolov merged 1 commit into
vlang:masterfrom
Maokaman1:fix-cgen-dot-m-path-detection
Mar 31, 2026
Merged

cgen: stop treating every .m as an Objective-C import#26794
JalonSolov merged 1 commit into
vlang:masterfrom
Maokaman1:fix-cgen-dot-m-path-detection

Conversation

@Maokaman1

Copy link
Copy Markdown
Contributor

This fixes a cgen bug where C hash directives could disappear from the generated C output if their text happened to contain .m in the wrong place.

The old logic used a plain contains('.m') check to decide whether a directive should be treated as an Objective-C include and deferred until after V type definitions. That was too broad. It could misclassify things like:

  • #insert with a path that contains .m
  • #define with a value that contains .m

When that happened, the directive was moved out of the normal include flow and never emitted.

This change tightens the check so the Objective-C path only applies to #include and #preinclude directives whose target file actually ends with .m.

cgen was using a plain `contains('.m')` check when deciding whether a hash directive should be deferred until after V type definitions. That worked for real Objective-C includes, but it also caught unrelated cases like a `#insert` under a `@VMODROOT` path that happened to include a `.m` directory, or a `#define` value containing `.m`.

When that happened, the directive was moved out of the normal include section and never emitted.

Tighten the check so only `#include` and `#preinclude` directives whose target file actually ends in `.m` get the Objective-C treatment, and add regression tests for both the `#insert` and `#define` cases.

@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: c75c5e2890

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread vlib/v/gen/c/cgen.v
Comment on lines +6203 to +6205
if target.len >= 2 && ((target.starts_with('"') && target.ends_with('"'))
|| (target.starts_with('<') && target.ends_with('>'))) {
target = target[1..target.len - 1]

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 Handle Objective-C includes with trailing block comments

The new is_objc_hash_target logic only strips quotes when the target both starts and ends with "/<...>, so #include "foo.m" /* note */ is no longer recognized as an Objective-C import. In V parsing, hash directives keep /* ... */ text in node.main (only // is stripped), so this case is reachable and regresses from the previous contains('.m') behavior. When this happens, the .m include is emitted in the normal include section instead of definitions, which can break builds if that imported Objective-C file relies on V type definitions being available.

Useful? React with 👍 / 👎.

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.

#include "foo.m" /* note */ is rejected in the checker before cgen runs, so it does not reach is_objc_hash_target. The checker only strips // comments and still requires the target to end in " or >, which makes that form invalid today. So this patch does not regress a reachable Objective-C import case. Supporting trailing /* ... */ after hash directives would be a separate parser/checker change.

@JalonSolov JalonSolov merged commit edd6f3d into vlang:master Mar 31, 2026
89 of 90 checks passed
medvednikov pushed a commit that referenced this pull request Apr 4, 2026
cgen was using a plain `contains('.m')` check when deciding whether a hash directive should be deferred until after V type definitions. That worked for real Objective-C includes, but it also caught unrelated cases like a `#insert` under a `@VMODROOT` path that happened to include a `.m` directory, or a `#define` value containing `.m`.

When that happened, the directive was moved out of the normal include section and never emitted.

Tighten the check so only `#include` and `#preinclude` directives whose target file actually ends in `.m` get the Objective-C treatment, and add regression tests for both the `#insert` and `#define` cases.
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.

2 participants