Disallow Go keywords as param names. Append an underscore to the end -- e.g. for the param `type`, substitute `type_`. It's ugly and not Go style, but it's bulletproof because we it will never clash with lowerCamelCase.
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/9811a286 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/9811a286 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/9811a286 Branch: refs/heads/master Commit: 9811a28660992d06caef7a2ebb926291bafb47ff Parents: be31f59 Author: Marvin Humphrey <[email protected]> Authored: Thu Apr 9 19:02:47 2015 -0700 Committer: Marvin Humphrey <[email protected]> Committed: Wed May 6 14:28:16 2015 -0700 ---------------------------------------------------------------------- compiler/src/CFCGoTypeMap.c | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/9811a286/compiler/src/CFCGoTypeMap.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCGoTypeMap.c b/compiler/src/CFCGoTypeMap.c index 50aaa32..dd3f374 100644 --- a/compiler/src/CFCGoTypeMap.c +++ b/compiler/src/CFCGoTypeMap.c @@ -15,6 +15,7 @@ */ #include <string.h> +#include <stdio.h> #include <ctype.h> #include <stdlib.h> @@ -64,6 +65,36 @@ static struct { static int num_conversions = sizeof(conversions) / sizeof(conversions[0]); +static const char* go_keywords[] = { + "break", + "case", + "chan", + "const", + "continue", + "default", + "defer", + "else", + "fallthrough", + "for", + "func", + "go", + "goto", + "if", + "import", + "interface", + "map", + "package", + "range", + "return", + "select", + "struct", + "switch", + "type", + "var" +}; + +static int num_go_keywords = sizeof(go_keywords) / sizeof(go_keywords[0]); + /* TODO: Optimize local conversions by creating a static wrapper function * which takes a buffer and allocates memory only if the buffer isn't big * enough. */ @@ -204,6 +235,17 @@ CFCGoTypeMap_go_arg_name(CFCParamList *param_list, size_t tick, char *buf, if (buf_len < max_required || buf_len < 5) { CFCUtil_die("Buffer length too short: %d", buf_len); } + + // If the argument name is a Go keyword, append an underscore. This is + // ugly but bulletproof. + for (int i = 0; i < num_go_keywords; i++) { + if (strcmp(orig, go_keywords[i]) == 0) { + sprintf(buf, "%s_", orig); + return; + } + } + + // Transform into lowerCamelCase. size_t dest_tick = 0; int last_was_underscore = 0; for (size_t i = 0; i <= strlen(orig); i++) {
