Skip to content

Escape keyword-named identifiers in the C generator [patch] - #446

Merged
matt-edmondson merged 3 commits into
mainfrom
claude/c-keyword-identifiers
Nov 11, 2026
Merged

matt-edmondson merged 3 commits into
mainfrom
claude/c-keyword-identifiers

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Part of #106 (C target). This stands alone on main and is independent of the Go PR #445. After both merge, only JavaScript is left.

What was wrong

The C generator had no reserved-word set. Struct fields are not prefixed with the type name; only member functions are (Holder_x). So a field, parameter, local or free function named after a C keyword came out as written, and the header did not compile:

keywords.h:3:9: error: expected identifier or '(' before 'auto'
    3 |     int auto;
keywords.h:11:5: error: expected identifier or '(' before 'for'
   11 | int for(int do, int case)

Change

  • CGenerator.ReservedWords lists the C11 keywords plus the ones C23 adds (bool, true, nullptr, typeof, …). It uses the base EscapeIdentifier, which appends _.
  • Fields, parameters and locals already go through CFamilyGenerator.SpellDeclarator/SpellConstantDeclarator, which call EscapeIdentifier. Designated initialisers (.auto_ = 3) and references go through the shared hooks. Adding the set is enough for all of them.
  • SpellFunctionName escapes the name of a free function. A member function is already Type_name, so it can never be a keyword.
  • GenerateCallExpression spells a call with no receiver through SpellCallee, so for(5, 6) becomes for_(5, 6). A call with a receiver keeps the name the caller wrote, which is the already-lowered member function name.
  • IsReceiverWord keeps true, false and nullptr as written when a reference means the literal.

Not covered here: the function-pointer member an interface's vtable struct declares, (*name), is still written as it is.

Tests

  • C_KeywordNamesCompileAndResolve builds a struct with auto and register fields and a designated-initialiser constructor, plus a free function for(do, case) with a local goto and a relay that calls it. It compiles them with cc -std=c11 -pedantic-errors and runs a driver that reads each escaped name back. It failed before this change with the errors above.
  • Full suite: 1665 passed, 0 failed (dotnet test --project Coder.Test/Coder.Test.csproj).

Generated by Claude Code

A struct field, parameter, local or free function named after a C
keyword came out verbatim, so the header failed to compile: a field
called auto or register, or a function called for, was enough. C now
appends an underscore at each declaration, and references, designated
initialisers and calls to a free function go through the same hook so
they agree. The set is the C11 keywords plus the ones C23 adds; true,
false and nullptr are left as written when a reference means them.
Member functions are already spelled after their type (Holder_default)
and need nothing.

Part of #106.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3mc54mR7dMPMARSVv
So it does not sit at the same insertion point as the Go generator's
keyword tests, which would make whichever of the two pull requests
merges second conflict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3mc54mR7dMPMARSVv
Keeps both the C reserved words and main's SharesNameWithGetter override.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5jpiuVuBRXSgitSY1
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit c268362 into main Nov 11, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the claude/c-keyword-identifiers branch November 11, 2026 01:08
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