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
end i = i + 1 end # Issue #772: C keywords would produce illegal C function names if # used as method names (e.g. `def for` -> `sp_X_for`, which is a # C `for` keyword in the function-name position). Suffix `_kw` to # disambiguate. Covers all C11 keywords commonly hit by # define_method experiments. if result == "for" || result == "while" || result == "do" || result == "return" || result == "if" || result == "else" || result == "switch" || result == "case" || result == "default" || result == "break" || result == "continue" || result == "goto" || result == "auto" || result == "register" || result == "static" || result == "extern" || result == "typedef" || result == "struct" || result == "union" || result == "enum" || result == "const" || result == "volatile" || result == "restrict" || result == "inline" || result == "void" || result == "char" || result == "short" || result == "int" || result == "long" || result == "float" || result == "double" || result == "signed" || result == "unsigned" || result == "sizeof" result = result + "_kw" end result end