| ... | ... | @@ -5132,6 +5132,38 @@ static void get_c_type(CodeGen *g, TypeTableEntry *type_entry, Buf *out_buf) { |
| 5132 | 5132 | } |
| 5133 | 5133 | } |
| 5134 | 5134 | |
| 5135 | static const char *preprocessor_alphabet1 = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 5136 | static const char *preprocessor_alphabet2 = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
| 5137 | |
| 5138 | static 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 | |
| 5149 | static 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 | |
| 5135 | 5167 | static void gen_h_file(CodeGen *g) { |
| 5136 | 5168 | if (!g->want_h_file) |
| 5137 | 5169 | return; |
| ... | ... | @@ -5148,10 +5180,10 @@ static void gen_h_file(CodeGen *g) { |
| 5148 | 5180 | if (!out_h) |
| 5149 | 5181 | zig_panic("unable to open %s: %s", buf_ptr(g->out_h_path), strerror(errno)); |
| 5150 | 5182 | |
| 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 | 5184 | buf_upcase(export_macro); |
| 5153 | 5185 | |
| 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 | 5187 | buf_upcase(extern_c_macro); |
| 5156 | 5188 | |
| 5157 | 5189 | Buf h_buf = BUF_INIT; |
| ... | ... | @@ -5194,8 +5226,7 @@ static void gen_h_file(CodeGen *g) { |
| 5194 | 5226 | |
| 5195 | 5227 | } |
| 5196 | 5228 | |
| 5197 | | Buf *ifdef_dance_name = buf_sprintf("%s_%s_H", |
| 5198 | | buf_ptr(g->root_out_name), buf_ptr(g->root_out_name)); |
| 5229 | Buf *ifdef_dance_name = preprocessor_mangle(buf_sprintf("%s_H", buf_ptr(g->root_out_name))); |
| 5199 | 5230 | buf_upcase(ifdef_dance_name); |
| 5200 | 5231 | |
| 5201 | 5232 | fprintf(out_h, "#ifndef %s\n", buf_ptr(ifdef_dance_name)); |