Skip to content

Commit d957244

Browse files
ehaasandrewrk
authored andcommitted
Allow dollar sign $ in identifiers in translate-c
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 ziglang#7585
1 parent 819f2a0 commit d957244

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/std/c/tokenizer.zig

+2-2
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ pub const Tokenizer = struct {
446446
'L' => {
447447
state = .L;
448448
},
449-
'a'...'t', 'v'...'z', 'A'...'K', 'M'...'T', 'V'...'Z', '_' => {
449+
'a'...'t', 'v'...'z', 'A'...'K', 'M'...'T', 'V'...'Z', '_', '$' => {
450450
state = .Identifier;
451451
},
452452
'=' => {
@@ -776,7 +776,7 @@ pub const Tokenizer = struct {
776776
},
777777
},
778778
.Identifier => switch (c) {
779-
'a'...'z', 'A'...'Z', '_', '0'...'9' => {},
779+
'a'...'z', 'A'...'Z', '_', '0'...'9', '$' => {},
780780
else => {
781781
result.id = Token.getKeyword(self.buffer[result.start..self.index], self.prev_tok_id == .Hash and !self.pp_directive) orelse .Identifier;
782782
if (self.prev_tok_id == .Hash)

test/run_translated_c.zig

+16
Original file line numberDiff line numberDiff line change
@@ -687,4 +687,20 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
687687
\\ return 0;
688688
\\}
689689
, "");
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+
, "");
690706
}

0 commit comments

Comments
 (0)