Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preprocessor: trailing backslash in stringize output is allowed if escaped #671

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(\\\\)
Loading