Skip to content

Commit

Permalink
Preprocessor: trailing backslash in stringize output is allowed if es…
Browse files Browse the repository at this point in the history
…caped
  • Loading branch information
ehaas committed Apr 6, 2024
1 parent 9f31df3 commit a01d24f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/aro/Preprocessor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1329,15 +1329,30 @@ fn stringify(pp: *Preprocessor, tokens: []const TokenWithExpansionLocs) !void {
try pp.char_buf.append(c);
}
}
if (pp.char_buf.items[pp.char_buf.items.len - 1] == '\\') {
try pp.char_buf.ensureUnusedCapacity(2);
if (pp.char_buf.items[pp.char_buf.items.len - 1] != '\\') {
pp.char_buf.appendSliceAssumeCapacity("\"\n");
return;
}
pp.char_buf.appendAssumeCapacity('"');
var tokenizer: Tokenizer = .{
.buf = pp.char_buf.items,
.index = 0,
.source = .generated,
.langopts = pp.comp.langopts,
.line = 0,
};
const item = tokenizer.next();
if (item.id == .unterminated_string_literal) {
const tok = tokens[tokens.len - 1];
try pp.comp.addDiagnostic(.{
.tag = .invalid_pp_stringify_escape,
.loc = tok.loc,
}, tok.expansionSlice());
pp.char_buf.items.len -= 1;
pp.char_buf.items.len -= 2; // erase unpaired backslash and appended end quote
pp.char_buf.appendAssumeCapacity('"');
}
try pp.char_buf.appendSlice("\"\n");
pp.char_buf.appendAssumeCapacity('\n');
}

fn reconstructIncludeString(pp: *Preprocessor, param_toks: []const TokenWithExpansionLocs, embed_args: ?*[]const TokenWithExpansionLocs, first: TokenWithExpansionLocs) !?[]const u8 {
Expand Down
8 changes: 8 additions & 0 deletions test/cases/stringify backslashes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#define NO_ERROR_VALIDATION

#define str(s) #s
x[str()
x[str(\)
x[str(\\)
x[str(\\\)
x[str(\\\\)

0 comments on commit a01d24f

Please sign in to comment.