authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-06 19:31:05-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-08-06 19:31:05-04:00
logd8227c79a2ff7882a52bf53f9ded2db4e7081fc8
tree8efcb880f09098631a88344be86345113f23ff4d
parent1268bdfa606db87791d55541b95274ce5448c022

limit generated C preprocessor tokens to alphabet

closes #407 The mangling strategy replaces bytes outside the alphabet with "_xx_" where xx is the hex code of the byte.

1 files changed, 35 insertions(+), 4 deletions(-)

src/codegen.cpp+35-4
...@@ -5132,6 +5132,38 @@ static void get_c_type(CodeGen *g, TypeTableEntry *type_entry, Buf *out_buf) {...@@ -5132,6 +5132,38 @@ static void get_c_type(CodeGen *g, TypeTableEntry *type_entry, Buf *out_buf) {
5132 }5132 }
5133}5133}
51345134
5135static const char *preprocessor_alphabet1 = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
5136static const char *preprocessor_alphabet2 = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
5137
5138static bool need_to_preprocessor_mangle(Buf *src) {
5139 for (size_t i = 0; i < buf_len(src); i += 1) {
5140 const char *alphabet = (i == 0) ? preprocessor_alphabet1 : preprocessor_alphabet2;
5141 uint8_t byte = buf_ptr(src)[i];
5142 if (strchr(alphabet, byte) == nullptr) {
5143 return true;
5144 }
5145 }
5146 return false;
5147}
5148
5149static Buf *preprocessor_mangle(Buf *src) {
5150 if (!need_to_preprocessor_mangle(src)) {
5151 return buf_create_from_buf(src);
5152 }
5153 Buf *result = buf_alloc();
5154 for (size_t i = 0; i < buf_len(src); i += 1) {
5155 const char *alphabet = (i == 0) ? preprocessor_alphabet1 : preprocessor_alphabet2;
5156 uint8_t byte = buf_ptr(src)[i];
5157 if (strchr(alphabet, byte) == nullptr) {
5158 // perform escape
5159 buf_appendf(result, "_%02x_", byte);
5160 } else {
5161 buf_append_char(result, byte);
5162 }
5163 }
5164 return result;
5165}
5166
5135static void gen_h_file(CodeGen *g) {5167static void gen_h_file(CodeGen *g) {
5136 if (!g->want_h_file)5168 if (!g->want_h_file)
5137 return;5169 return;
...@@ -5148,10 +5180,10 @@ static void gen_h_file(CodeGen *g) {...@@ -5148,10 +5180,10 @@ static void gen_h_file(CodeGen *g) {
5148 if (!out_h)5180 if (!out_h)
5149 zig_panic("unable to open %s: %s", buf_ptr(g->out_h_path), strerror(errno));5181 zig_panic("unable to open %s: %s", buf_ptr(g->out_h_path), strerror(errno));
51505182
5151 Buf *export_macro = buf_sprintf("%s_EXPORT", buf_ptr(g->root_out_name));5183 Buf *export_macro = preprocessor_mangle(buf_sprintf("%s_EXPORT", buf_ptr(g->root_out_name)));
5152 buf_upcase(export_macro);5184 buf_upcase(export_macro);
51535185
5154 Buf *extern_c_macro = buf_sprintf("%s_EXTERN_C", buf_ptr(g->root_out_name));5186 Buf *extern_c_macro = preprocessor_mangle(buf_sprintf("%s_EXTERN_C", buf_ptr(g->root_out_name)));
5155 buf_upcase(extern_c_macro);5187 buf_upcase(extern_c_macro);
51565188
5157 Buf h_buf = BUF_INIT;5189 Buf h_buf = BUF_INIT;
...@@ -5194,8 +5226,7 @@ static void gen_h_file(CodeGen *g) {...@@ -5194,8 +5226,7 @@ static void gen_h_file(CodeGen *g) {
51945226
5195 }5227 }
51965228
5197 Buf *ifdef_dance_name = buf_sprintf("%s_%s_H",5229 Buf *ifdef_dance_name = preprocessor_mangle(buf_sprintf("%s_H", buf_ptr(g->root_out_name)));
5198 buf_ptr(g->root_out_name), buf_ptr(g->root_out_name));
5199 buf_upcase(ifdef_dance_name);5230 buf_upcase(ifdef_dance_name);
52005231
5201 fprintf(out_h, "#ifndef %s\n", buf_ptr(ifdef_dance_name));5232 fprintf(out_h, "#ifndef %s\n", buf_ptr(ifdef_dance_name));