authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-09 12:48:38-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-09 12:48:38-04:00
logeb65410b624d3aa8c56e7f2d2e68502030f8e981
tree10ca4c149ed0918531b9e73cf7045fe224707e99
parent62065a9aea1c3c93cfea617403b7d5dc8344e36a
signature Commit is signed but in an unrecognized format.

translate-c: enough C tokenization/parsing to handle shifting in macros

See #2451

4 files changed, 37 insertions(+), 0 deletions(-)

src/c_tokenizer.cpp+19
...@@ -124,6 +124,8 @@ static void begin_token(CTokenize *ctok, CTokId id) {...@@ -124,6 +124,8 @@ static void begin_token(CTokenize *ctok, CTokId id) {
124 case CTokIdAsterisk:124 case CTokIdAsterisk:
125 case CTokIdBang:125 case CTokIdBang:
126 case CTokIdTilde:126 case CTokIdTilde:
127 case CTokIdShl:
128 case CTokIdLt:
127 break;129 break;
128 }130 }
129}131}
...@@ -223,6 +225,10 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {...@@ -223,6 +225,10 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
223 begin_token(ctok, CTokIdDot);225 begin_token(ctok, CTokIdDot);
224 end_token(ctok);226 end_token(ctok);
225 break;227 break;
228 case '<':
229 begin_token(ctok, CTokIdLt);
230 ctok->state = CTokStateGotLt;
231 break;
226 case '(':232 case '(':
227 begin_token(ctok, CTokIdLParen);233 begin_token(ctok, CTokIdLParen);
228 end_token(ctok);234 end_token(ctok);
...@@ -251,6 +257,18 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {...@@ -251,6 +257,18 @@ void tokenize_c_macro(CTokenize *ctok, const uint8_t *c) {
251 return mark_error(ctok);257 return mark_error(ctok);
252 }258 }
253 break;259 break;
260 case CTokStateGotLt:
261 switch (*c) {
262 case '<':
263 ctok->cur_tok->id = CTokIdShl;
264 end_token(ctok);
265 ctok->state = CTokStateStart;
266 break;
267 default:
268 ctok->state = CTokStateStart;
269 continue;
270 }
271 break;
254 case CTokStateFloat:272 case CTokStateFloat:
255 switch (*c) {273 switch (*c) {
256 case '.':274 case '.':
...@@ -791,6 +809,7 @@ found_end_of_macro:...@@ -791,6 +809,7 @@ found_end_of_macro:
791 case CTokStateNumLitIntSuffixL:809 case CTokStateNumLitIntSuffixL:
792 case CTokStateNumLitIntSuffixUL:810 case CTokStateNumLitIntSuffixUL:
793 case CTokStateNumLitIntSuffixLL:811 case CTokStateNumLitIntSuffixLL:
812 case CTokStateGotLt:
794 end_token(ctok);813 end_token(ctok);
795 break;814 break;
796 case CTokStateFloat:815 case CTokStateFloat:
src/c_tokenizer.hpp+3
...@@ -25,6 +25,8 @@ enum CTokId {...@@ -25,6 +25,8 @@ enum CTokId {
25 CTokIdAsterisk,25 CTokIdAsterisk,
26 CTokIdBang,26 CTokIdBang,
27 CTokIdTilde,27 CTokIdTilde,
28 CTokIdShl,
29 CTokIdLt,
28};30};
2931
30enum CNumLitSuffix {32enum CNumLitSuffix {
...@@ -78,6 +80,7 @@ enum CTokState {...@@ -78,6 +80,7 @@ enum CTokState {
78 CTokStateNumLitIntSuffixL,80 CTokStateNumLitIntSuffixL,
79 CTokStateNumLitIntSuffixLL,81 CTokStateNumLitIntSuffixLL,
80 CTokStateNumLitIntSuffixUL,82 CTokStateNumLitIntSuffixUL,
83 CTokStateGotLt,
81};84};
8285
83struct CTokenize {86struct CTokenize {
src/translate_c.cpp+9
...@@ -4922,6 +4922,8 @@ static AstNode *parse_ctok_primary_expr(Context *c, CTokenize *ctok, size_t *tok...@@ -4922,6 +4922,8 @@ static AstNode *parse_ctok_primary_expr(Context *c, CTokenize *ctok, size_t *tok
4922 case CTokIdAsterisk:4922 case CTokIdAsterisk:
4923 case CTokIdBang:4923 case CTokIdBang:
4924 case CTokIdTilde:4924 case CTokIdTilde:
4925 case CTokIdShl:
4926 case CTokIdLt:
4925 // not able to make sense of this4927 // not able to make sense of this
4926 return nullptr;4928 return nullptr;
4927 }4929 }
...@@ -4953,6 +4955,13 @@ static AstNode *parse_ctok_suffix_op_expr(Context *c, CTokenize *ctok, size_t *t...@@ -4953,6 +4955,13 @@ static AstNode *parse_ctok_suffix_op_expr(Context *c, CTokenize *ctok, size_t *t
4953 *tok_i += 1;4955 *tok_i += 1;
49544956
4955 node = trans_create_node_ptr_type(c, false, false, node, PtrLenC);4957 node = trans_create_node_ptr_type(c, false, false, node, PtrLenC);
4958 } else if (first_tok->id == CTokIdShl) {
4959 *tok_i += 1;
4960
4961 AstNode *rhs_node = parse_ctok_expr(c, ctok, tok_i);
4962 if (rhs_node == nullptr)
4963 return nullptr;
4964 node = trans_create_node_bin_op(c, node, BinOpTypeBitShiftLeft, rhs_node);
4956 } else {4965 } else {
4957 return node;4966 return node;
4958 }4967 }
test/translate_c.zig+6
...@@ -2,6 +2,12 @@ const tests = @import("tests.zig");...@@ -2,6 +2,12 @@ const tests = @import("tests.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn addCases(cases: *tests.TranslateCContext) void {4pub fn addCases(cases: *tests.TranslateCContext) void {
5 cases.add("macro with left shift",
6 \\#define REDISMODULE_READ (1<<0)
7 ,
8 \\pub const REDISMODULE_READ = 1 << 0;
9 );
10
5 cases.add("casting pointers to ints and ints to pointers",11 cases.add("casting pointers to ints and ints to pointers",
6 \\void foo(void);12 \\void foo(void);
7 \\void bar(void) {13 \\void bar(void) {