Skip to content

Commit d41fbce

Browse files
committed
Translate-C changes
1 parent 25e7121 commit d41fbce

File tree

9 files changed

+208
-22
lines changed

9 files changed

+208
-22
lines changed

src-self-hosted/clang.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub const struct_ZigClangImplicitCastExpr = @OpaqueType();
4343
pub const struct_ZigClangIncompleteArrayType = @OpaqueType();
4444
pub const struct_ZigClangIntegerLiteral = @OpaqueType();
4545
pub const struct_ZigClangMacroDefinitionRecord = @OpaqueType();
46+
pub const struct_ZigClangMacroExpansion = @OpaqueType();
4647
pub const struct_ZigClangMacroQualifiedType = @OpaqueType();
4748
pub const struct_ZigClangMemberExpr = @OpaqueType();
4849
pub const struct_ZigClangNamedDecl = @OpaqueType();
@@ -889,6 +890,7 @@ pub const ZigClangImplicitCastExpr = struct_ZigClangImplicitCastExpr;
889890
pub const ZigClangIncompleteArrayType = struct_ZigClangIncompleteArrayType;
890891
pub const ZigClangIntegerLiteral = struct_ZigClangIntegerLiteral;
891892
pub const ZigClangMacroDefinitionRecord = struct_ZigClangMacroDefinitionRecord;
893+
pub const ZigClangMacroExpansion = struct_ZigClangMacroExpansion;
892894
pub const ZigClangMacroQualifiedType = struct_ZigClangMacroQualifiedType;
893895
pub const ZigClangMemberExpr = struct_ZigClangMemberExpr;
894896
pub const ZigClangNamedDecl = struct_ZigClangNamedDecl;
@@ -1128,3 +1130,5 @@ pub extern fn ZigClangCompoundAssignOperator_getBeginLoc(*const ZigClangCompound
11281130
pub extern fn ZigClangCompoundAssignOperator_getOpcode(*const ZigClangCompoundAssignOperator) ZigClangBO;
11291131
pub extern fn ZigClangCompoundAssignOperator_getLHS(*const ZigClangCompoundAssignOperator) *const ZigClangExpr;
11301132
pub extern fn ZigClangCompoundAssignOperator_getRHS(*const ZigClangCompoundAssignOperator) *const ZigClangExpr;
1133+
1134+
pub extern fn ZigClangMacroExpansion_getDefinition(*const ZigClangMacroExpansion) *const ZigClangMacroDefinitionRecord;

src/all_types.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,8 +1036,15 @@ struct AstNodeFloatLiteral {
10361036
bool overflow;
10371037
};
10381038

1039+
enum IntLiteralFormat {
1040+
IntLiteralFormatNone,
1041+
IntLiteralFormatHex,
1042+
IntLiteralFormatOctal,
1043+
};
1044+
10391045
struct AstNodeIntLiteral {
10401046
BigInt *bigint;
1047+
IntLiteralFormat format = IntLiteralFormatNone;
10411048
};
10421049

10431050
struct AstNodeStructValueField {
@@ -2129,6 +2136,7 @@ struct CodeGen {
21292136
bool verbose_llvm_ir;
21302137
bool verbose_cimport;
21312138
bool verbose_cc;
2139+
bool quiet_translate_c;
21322140
bool error_during_imports;
21332141
bool generate_error_name_table;
21342142
bool enable_cache; // mutually exclusive with output_dir

src/ast_render.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,21 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
615615
{
616616
Buf rendered_buf = BUF_INIT;
617617
buf_resize(&rendered_buf, 0);
618-
bigint_append_buf(&rendered_buf, node->data.int_literal.bigint, 10);
619-
fprintf(ar->f, "%s", buf_ptr(&rendered_buf));
618+
619+
switch (node->data.int_literal.format) {
620+
case IntLiteralFormatHex:
621+
bigint_append_buf(&rendered_buf, node->data.int_literal.bigint, 16);
622+
fprintf(ar->f, "0x%s", buf_ptr(&rendered_buf));
623+
break;
624+
case IntLiteralFormatOctal:
625+
bigint_append_buf(&rendered_buf, node->data.int_literal.bigint, 8);
626+
fprintf(ar->f, "0o%s", buf_ptr(&rendered_buf));
627+
break;
628+
case IntLiteralFormatNone:
629+
bigint_append_buf(&rendered_buf, node->data.int_literal.bigint, 10);
630+
fprintf(ar->f, "%s", buf_ptr(&rendered_buf));
631+
break;
632+
}
620633
}
621634
break;
622635
case NodeTypeStringLiteral:

src/c_tokenizer.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ static void begin_token(CTokenize *ctok, CTokId id) {
116116
break;
117117
case CTokIdCharLit:
118118
case CTokIdNumLitFloat:
119+
case CTokIdPlus:
119120
case CTokIdMinus:
120121
case CTokIdLParen:
121122
case CTokIdRParen:
@@ -125,7 +126,11 @@ static void begin_token(CTokenize *ctok, CTokId id) {
125126
case CTokIdBang:
126127
case CTokIdTilde:
127128
case CTokIdShl:
129+
case CTokIdShr:
128130
case CTokIdLt:
131+
case CTokIdGt:
132+
case CTokIdInc:
133+
case CTokIdDec:
129134
break;
130135
}
131136
}
@@ -229,6 +234,10 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
229234
begin_token(ctok, CTokIdLt);
230235
ctok->state = CTokStateGotLt;
231236
break;
237+
case '>':
238+
begin_token(ctok, CTokIdGt);
239+
ctok->state = CTokStateGotGt;
240+
break;
232241
case '(':
233242
begin_token(ctok, CTokIdLParen);
234243
end_token(ctok);
@@ -241,8 +250,13 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
241250
begin_token(ctok, CTokIdAsterisk);
242251
end_token(ctok);
243252
break;
253+
case '+':
254+
begin_token(ctok, CTokIdPlus);
255+
ctok->state = CTokStateGotPlus;
256+
break;
244257
case '-':
245258
begin_token(ctok, CTokIdMinus);
259+
ctok->state = CTokStateGotMinus;
246260
end_token(ctok);
247261
break;
248262
case '!':
@@ -270,6 +284,45 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
270284
continue;
271285
}
272286
break;
287+
case CTokStateGotGt:
288+
switch (*c) {
289+
case '>':
290+
ctok->cur_tok->id = CTokIdShr;
291+
end_token(ctok);
292+
ctok->state = CTokStateStart;
293+
break;
294+
default:
295+
end_token(ctok);
296+
ctok->state = CTokStateStart;
297+
continue;
298+
}
299+
break;
300+
case CTokStateGotPlus:
301+
switch (*c) {
302+
case '+':
303+
ctok->cur_tok->id = CTokIdInc;
304+
end_token(ctok);
305+
ctok->state = CTokStateStart;
306+
break;
307+
default:
308+
end_token(ctok);
309+
ctok->state = CTokStateStart;
310+
continue;
311+
}
312+
break;
313+
case CTokStateGotMinus:
314+
switch (*c) {
315+
case '-':
316+
ctok->cur_tok->id = CTokIdDec;
317+
end_token(ctok);
318+
ctok->state = CTokStateStart;
319+
break;
320+
default:
321+
end_token(ctok);
322+
ctok->state = CTokStateStart;
323+
continue;
324+
}
325+
break;
273326
case CTokStateFloat:
274327
switch (*c) {
275328
case '.':
@@ -375,6 +428,7 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
375428
case 'x':
376429
case 'X':
377430
ctok->state = CTokStateHex;
431+
ctok->cur_tok->data.num_lit_int.format = CNumLitFormatHex;
378432
break;
379433
case '.':
380434
ctok->state = CTokStateFloat;
@@ -391,6 +445,7 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
391445
default:
392446
c -= 1;
393447
ctok->state = CTokStateOctal;
448+
ctok->cur_tok->data.num_lit_int.format = CNumLitFormatOctal;
394449
continue;
395450
}
396451
break;
@@ -811,6 +866,9 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
811866
case CTokStateNumLitIntSuffixUL:
812867
case CTokStateNumLitIntSuffixLL:
813868
case CTokStateGotLt:
869+
case CTokStateGotGt:
870+
case CTokStateGotPlus:
871+
case CTokStateGotMinus:
814872
end_token(ctok);
815873
break;
816874
case CTokStateFloat:

src/c_tokenizer.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ enum CTokId {
1717
CTokIdNumLitInt,
1818
CTokIdNumLitFloat,
1919
CTokIdSymbol,
20+
CTokIdPlus,
2021
CTokIdMinus,
2122
CTokIdLParen,
2223
CTokIdRParen,
@@ -26,7 +27,11 @@ enum CTokId {
2627
CTokIdBang,
2728
CTokIdTilde,
2829
CTokIdShl,
30+
CTokIdShr,
2931
CTokIdLt,
32+
CTokIdGt,
33+
CTokIdInc,
34+
CTokIdDec,
3035
};
3136

3237
enum CNumLitSuffix {
@@ -38,9 +43,16 @@ enum CNumLitSuffix {
3843
CNumLitSuffixLLU,
3944
};
4045

46+
enum CNumLitFormat {
47+
CNumLitFormatNone,
48+
CNumLitFormatHex,
49+
CNumLitFormatOctal,
50+
};
51+
4152
struct CNumLitInt {
4253
uint64_t x;
4354
CNumLitSuffix suffix;
55+
CNumLitFormat format;
4456
};
4557

4658
struct CTok {
@@ -81,6 +93,9 @@ enum CTokState {
8193
CTokStateNumLitIntSuffixLL,
8294
CTokStateNumLitIntSuffixUL,
8395
CTokStateGotLt,
96+
CTokStateGotGt,
97+
CTokStateGotPlus,
98+
CTokStateGotMinus,
8499
};
85100

86101
struct CTokenize {

src/main.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) {
9393
" --verbose-llvm-ir enable compiler debug output for LLVM IR\n"
9494
" --verbose-cimport enable compiler debug output for C imports\n"
9595
" --verbose-cc enable compiler debug output for C compilation\n"
96+
" --quiet-translate-c disable translate C warnings\n"
9697
" -dirafter [dir] add directory to AFTER include search path\n"
9798
" -isystem [dir] add directory to SYSTEM include search path\n"
9899
" -I[dir] add directory to include search path\n"
@@ -479,6 +480,7 @@ int main(int argc, char **argv) {
479480
bool verbose_llvm_ir = false;
480481
bool verbose_cimport = false;
481482
bool verbose_cc = false;
483+
bool quiet_translate_c = false;
482484
ErrColor color = ErrColorAuto;
483485
CacheOpt enable_cache = CacheOptAuto;
484486
Buf *dynamic_linker = nullptr;
@@ -692,6 +694,8 @@ int main(int argc, char **argv) {
692694
verbose_cimport = true;
693695
} else if (strcmp(arg, "--verbose-cc") == 0) {
694696
verbose_cc = true;
697+
} else if (strcmp(arg, "--quiet-translate-c") == 0) {
698+
quiet_translate_c = true;
695699
} else if (strcmp(arg, "-rdynamic") == 0) {
696700
rdynamic = true;
697701
} else if (strcmp(arg, "--each-lib-rpath") == 0) {
@@ -886,7 +890,7 @@ int main(int argc, char **argv) {
886890
} else if (strcmp(arg, "--linker-script") == 0) {
887891
linker_script = argv[i];
888892
} else if (strcmp(arg, "--version-script") == 0) {
889-
version_script = buf_create_from_str(argv[i]);
893+
version_script = buf_create_from_str(argv[i]);
890894
} else if (strcmp(arg, "-target-glibc") == 0) {
891895
target_glibc = argv[i];
892896
} else if (strcmp(arg, "-rpath") == 0) {
@@ -1212,7 +1216,7 @@ int main(int argc, char **argv) {
12121216
codegen_set_lib_version(g, ver_major, ver_minor, ver_patch);
12131217
g->want_single_threaded = want_single_threaded;
12141218
codegen_set_linker_script(g, linker_script);
1215-
g->version_script_path = version_script;
1219+
g->version_script_path = version_script;
12161220
if (each_lib_rpath)
12171221
codegen_set_each_lib_rpath(g, each_lib_rpath);
12181222

@@ -1228,6 +1232,7 @@ int main(int argc, char **argv) {
12281232
g->verbose_llvm_ir = verbose_llvm_ir;
12291233
g->verbose_cimport = verbose_cimport;
12301234
g->verbose_cc = verbose_cc;
1235+
g->quiet_translate_c = quiet_translate_c;
12311236
g->output_dir = output_dir;
12321237
g->disable_gen_h = disable_gen_h;
12331238
g->bundle_compiler_rt = bundle_compiler_rt;

0 commit comments

Comments
 (0)