Skip to content

Commit

Permalink
Allow dollar sign $ in identifiers in translate-c
Browse files Browse the repository at this point in the history
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 #7585
  • Loading branch information
ehaas authored and andrewrk committed Jan 4, 2021
1 parent 819f2a0 commit d957244
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/std/c/tokenizer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ pub const Tokenizer = struct {
'L' => {
state = .L;
},
'a'...'t', 'v'...'z', 'A'...'K', 'M'...'T', 'V'...'Z', '_' => {
'a'...'t', 'v'...'z', 'A'...'K', 'M'...'T', 'V'...'Z', '_', '$' => {
state = .Identifier;
},
'=' => {
Expand Down Expand Up @@ -776,7 +776,7 @@ pub const Tokenizer = struct {
},
},
.Identifier => switch (c) {
'a'...'z', 'A'...'Z', '_', '0'...'9' => {},
'a'...'z', 'A'...'Z', '_', '0'...'9', '$' => {},
else => {
result.id = Token.getKeyword(self.buffer[result.start..self.index], self.prev_tok_id == .Hash and !self.pp_directive) orelse .Identifier;
if (self.prev_tok_id == .Hash)
Expand Down
16 changes: 16 additions & 0 deletions test/run_translated_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -687,4 +687,20 @@ pub fn addCases(cases: *tests.RunTranslatedCContext) void {
\\ return 0;
\\}
, "");

cases.add("dollar sign in identifiers",
\\#include <stdlib.h>
\\#define $FOO 2
\\#define $foo bar$
\\#define $baz($x) ($x + $FOO)
\\int $$$(int $x$) { return $x$ + $FOO; }
\\int main() {
\\ int bar$ = 42;
\\ if ($foo != 42) abort();
\\ if (bar$ != 42) abort();
\\ if ($baz(bar$) != 44) abort();
\\ if ($$$(bar$) != 44) abort();
\\ return 0;
\\}
, "");
}

0 comments on commit d957244

Please sign in to comment.