Commit 710cbe2a

710cbe2a7b80Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified

Description

fix(codegen): sanitize_name suffixes C keywords with _kw (#772)

define_method(:for) (or any other Ruby method name that happens to be a C keyword) used to emit sp_X_for(...) -- which fails C compilation because for is a reserved word in function position.

Add a check at the tail of sanitize_name: if the cleaned-up result matches one of the C11 keywords (control flow + storage classes + type specifiers + sizeof), append _kw so the emitted function name is sp_X_for_kw instead.

The disambiguation suffix is one-directional -- a user who happens to name a Ruby method for_kw would collide with the escaped form of for. The collision is theoretical (no user program in the wild names methods this way) and the alternative (separate keyword name table on the side) costs more complexity than the tradeoff is worth.

Closes #772

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
710cbe2a7b80
Parent
512d4d996c97

spinel_codegen.rbmodifiedView file

@@ -4671,6 +4671,22 @@
46714671 end
46724672 i = i + 1
46734673 end
4674+ # Issue #772: C keywords would produce illegal C function names if
4675+ # used as method names (e.g. `def for` -> `sp_X_for`, which is a
4676+ # C `for` keyword in the function-name position). Suffix `_kw` to
4677+ # disambiguate. Covers all C11 keywords commonly hit by
4678+ # define_method experiments.
4679+ if result == "for" || result == "while" || result == "do" || result == "return" ||
4680+ result == "if" || result == "else" || result == "switch" || result == "case" ||
4681+ result == "default" || result == "break" || result == "continue" ||
4682+ result == "goto" || result == "auto" || result == "register" || result == "static" ||
4683+ result == "extern" || result == "typedef" || result == "struct" || result == "union" ||
4684+ result == "enum" || result == "const" || result == "volatile" || result == "restrict" ||
4685+ result == "inline" || result == "void" || result == "char" || result == "short" ||
4686+ result == "int" || result == "long" || result == "float" || result == "double" ||
4687+ result == "signed" || result == "unsigned" || result == "sizeof"
4688+ result = result + "_kw"
4689+ end
46744690 result
46754691 end
46764692