| author | |
| committer | |
| log | d95724454c84b22e9030aec69b88d1fa9fd5175b |
| tree | 8d5d93801b2c550bca2695a14258bd61f95850af |
| parent | 819f2a01a16c7db82ee407850773dabcf3ea8587 |
In strictly conforming C, identifiers cannot container dollar signs.
However GCC and Clang allow them by default, so translate-c should
handle them. See http://gcc.gnu.org/onlinedocs/cpp/Tokenization.html
I encountered this in the wild in windows.h
Fixes #75852 files changed, 18 insertions(+), 2 deletions(-)
lib/std/c/tokenizer.zig+2-2| ... | ... | @@ -446,7 +446,7 @@ pub const Tokenizer = struct { |
| 446 | 446 | 'L' => { |
| 447 | 447 | state = .L; |
| 448 | 448 | }, |
| 449 | 'a'...'t', 'v'...'z', 'A'...'K', 'M'...'T', 'V'...'Z', '_' => { | |
| 449 | 'a'...'t', 'v'...'z', 'A'...'K', 'M'...'T', 'V'...'Z', '_', '$' => { | |
| 450 | 450 | state = .Identifier; |
| 451 | 451 | }, |
| 452 | 452 | '=' => { |
| ... | ... | @@ -776,7 +776,7 @@ pub const Tokenizer = struct { |
| 776 | 776 | }, |
| 777 | 777 | }, |
| 778 | 778 | .Identifier => switch (c) { |
| 779 | 'a'...'z', 'A'...'Z', '_', '0'...'9' => {}, | |
| 779 | 'a'...'z', 'A'...'Z', '_', '0'...'9', '$' => {}, | |
| 780 | 780 | else => { |
| 781 | 781 | result.id = Token.getKeyword(self.buffer[result.start..self.index], self.prev_tok_id == .Hash and !self.pp_directive) orelse .Identifier; |
| 782 | 782 | if (self.prev_tok_id == .Hash) |
test/run_translated_c.zig+16| ... | ... | @@ -687,4 +687,20 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void { |
| 687 | 687 | \\ return 0; |
| 688 | 688 | \\} |
| 689 | 689 | , ""); |
| 690 | ||
| 691 | cases.add("dollar sign in identifiers", | |
| 692 | \\#include <stdlib.h> | |
| 693 | \\#define $FOO 2 | |
| 694 | \\#define $foo bar$ | |
| 695 | \\#define $baz($x) ($x + $FOO) | |
| 696 | \\int $$$(int $x$) { return $x$ + $FOO; } | |
| 697 | \\int main() { | |
| 698 | \\ int bar$ = 42; | |
| 699 | \\ if ($foo != 42) abort(); | |
| 700 | \\ if (bar$ != 42) abort(); | |
| 701 | \\ if ($baz(bar$) != 44) abort(); | |
| 702 | \\ if ($$$(bar$) != 44) abort(); | |
| 703 | \\ return 0; | |
| 704 | \\} | |
| 705 | , ""); | |
| 690 | 706 | } |