Skip to content

Commit

Permalink
Preprocessor: replace empty lines with line linemarkers
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexu committed Oct 4, 2023
1 parent 080c742 commit 65e2de6
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Preprocessor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2521,14 +2521,35 @@ pub fn prettyPrintTokens(pp: *Preprocessor, w: anytype) !void {
}

var expected_line: usize = 1;
while (true) : (i += 1) {
outer: while (true) : (i += 1) {
var cur: Token = pp.tokens.get(i);
switch (cur.id) {
.eof => {
if (pp.tokens.len > 1 and pp.tokens.items(.id)[i - 1] != .nl) try w.writeByte('\n');
break;
},
.nl => {
var newlines: u32 = 0;
for (pp.tokens.items(.id)[i..], i..) |id, j| {
if (id == .nl) {
newlines += 1;
} else if (id == .eof) {
try w.writeByte('\n');
return;
} else if (id != .whitespace) {
if (newlines < 3) break;
i = @intCast(j - 1);
try w.writeAll("\n");
if (pp.linemarkers != .none) {
const next = pp.tokens.get(i);
const source = pp.comp.getSource(next.loc.id);
const line_col = source.lineCol(next.loc);
try w.print("#{s} {d} \"{s}\"{s}\n", .{ pp.linemarkers.directiveString(), line_col.line_no, source.path, source.kind.preprocessorFlags() });
expected_line = line_col.line_no;
}
continue :outer;
}
}
try w.writeAll("\n");
expected_line += 1;
},
Expand Down

0 comments on commit 65e2de6

Please sign in to comment.