authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-13 15:03:53-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-09-24 20:01:19-07:00
logf0d3b7abb8e96aede796c80ab73a0f82ecced722
treec30830c47e465bee6ccf4eedbeee4b261ed2819f
parent9e979e5a9dbee39d45ac7dc4fbedb63ddcae8e99

aro: fix dep file logic

also add ability to omit main source file from dep file as it messes up caching strategy

9 files changed, 45 insertions(+), 36 deletions(-)

lib/compiler/aro/aro/Compilation.zig+7-2
......@@ -934,7 +934,7 @@ pub fn generateBuiltinMacros(comp: *Compilation, system_defines_mode: SystemDefi
934934 error.WriteFailed, error.OutOfMemory => return error.OutOfMemory,
935935 };
936936
937 if (allocating.getWritten().len > std.math.maxInt(u32)) return error.FileTooBig;
937 if (allocating.written().len > std.math.maxInt(u32)) return error.FileTooBig;
938938
939939 const contents = try allocating.toOwnedSlice();
940940 errdefer comp.gpa.free(contents);
......@@ -1589,6 +1589,7 @@ pub fn hasInclude(
15891589 include_type: IncludeType,
15901590 /// __has_include vs __has_include_next
15911591 which: WhichInclude,
1592 opt_dep_file: ?*DepFile,
15921593) Compilation.Error!bool {
15931594 if (try FindInclude.run(comp, filename, switch (which) {
15941595 .next => .{ .only_search_after_dir = comp.getSource(includer_token_source).path },
......@@ -1596,7 +1597,11 @@ pub fn hasInclude(
15961597 .quotes => .{ .allow_same_dir = comp.getSource(includer_token_source).path },
15971598 .angle_brackets => .only_search,
15981599 },
1599 })) |_| {
1600 })) |found| {
1601 if (opt_dep_file) |dep_file| {
1602 const source = comp.getSource(found.source);
1603 try dep_file.addDependency(comp.gpa, source.path);
1604 }
16001605 return true;
16011606 } else {
16021607 return false;
lib/compiler/aro/aro/Driver.zig+11-6
......@@ -831,7 +831,7 @@ pub fn err(d: *Driver, fmt: []const u8, args: anytype) Compilation.Error!void {
831831 defer allocating.deinit();
832832
833833 Diagnostics.formatArgs(&allocating.writer, fmt, args) catch return error.OutOfMemory;
834 try d.diagnostics.add(.{ .kind = .@"error", .text = allocating.getWritten(), .location = null });
834 try d.diagnostics.add(.{ .kind = .@"error", .text = allocating.written(), .location = null });
835835}
836836
837837pub fn warn(d: *Driver, fmt: []const u8, args: anytype) Compilation.Error!void {
......@@ -840,7 +840,7 @@ pub fn warn(d: *Driver, fmt: []const u8, args: anytype) Compilation.Error!void {
840840 defer allocating.deinit();
841841
842842 Diagnostics.formatArgs(&allocating.writer, fmt, args) catch return error.OutOfMemory;
843 try d.diagnostics.add(.{ .kind = .warning, .text = allocating.getWritten(), .location = null });
843 try d.diagnostics.add(.{ .kind = .warning, .text = allocating.written(), .location = null });
844844}
845845
846846pub fn unsupportedOptionForTarget(d: *Driver, target: std.Target, opt: []const u8) Compilation.Error!void {
......@@ -856,7 +856,7 @@ pub fn fatal(d: *Driver, comptime fmt: []const u8, args: anytype) error{ FatalEr
856856 defer allocating.deinit();
857857
858858 Diagnostics.formatArgs(&allocating.writer, fmt, args) catch return error.OutOfMemory;
859 try d.diagnostics.add(.{ .kind = .@"fatal error", .text = allocating.getWritten(), .location = null });
859 try d.diagnostics.add(.{ .kind = .@"fatal error", .text = allocating.written(), .location = null });
860860 unreachable;
861861}
862862
......@@ -986,7 +986,12 @@ pub fn main(d: *Driver, tc: *Toolchain, args: []const []const u8, comptime fast_
986986}
987987
988988/// Initializes a DepFile if requested by driver options.
989pub fn initDepFile(d: *Driver, source: Source, buf: *[std.fs.max_name_bytes]u8) Compilation.Error!?DepFile {
989pub fn initDepFile(
990 d: *Driver,
991 source: Source,
992 buf: *[std.fs.max_name_bytes]u8,
993 omit_source: bool,
994) Compilation.Error!?DepFile {
990995 if (!d.dependencies.m and !d.dependencies.md) return null;
991996 var dep_file: DepFile = .{
992997 .target = undefined,
......@@ -1004,7 +1009,7 @@ pub fn initDepFile(d: *Driver, source: Source, buf: *[std.fs.max_name_bytes]u8)
10041009 return d.fatal("dependency file name too long for filesystem '{s}{s}'", args);
10051010 }
10061011
1007 try dep_file.addDependency(d.comp.gpa, source.path);
1012 if (!omit_source) try dep_file.addDependency(d.comp.gpa, source.path);
10081013 errdefer comptime unreachable;
10091014
10101015 return dep_file;
......@@ -1101,7 +1106,7 @@ fn processSource(
11011106 defer pp.deinit();
11021107
11031108 var name_buf: [std.fs.max_name_bytes]u8 = undefined;
1104 var opt_dep_file = try d.initDepFile(source, &name_buf);
1109 var opt_dep_file = try d.initDepFile(source, &name_buf, false);
11051110 defer if (opt_dep_file) |*dep_file| dep_file.deinit(pp.gpa);
11061111
11071112 if (opt_dep_file) |*dep_file| pp.dep_file = dep_file;
lib/compiler/aro/aro/Parser.zig+6-6
......@@ -232,7 +232,7 @@ fn checkIdentifierCodepointWarnings(p: *Parser, codepoint: u21, loc: Source.Loca
232232
233233 try p.diagnostics.add(.{
234234 .kind = diagnostic.kind,
235 .text = allocating.getWritten(),
235 .text = allocating.written(),
236236 .extension = diagnostic.extension,
237237 .opt = diagnostic.opt,
238238 .location = loc.expand(p.comp),
......@@ -244,7 +244,7 @@ fn checkIdentifierCodepointWarnings(p: *Parser, codepoint: u21, loc: Source.Loca
244244
245245 try p.diagnostics.add(.{
246246 .kind = diagnostic.kind,
247 .text = allocating.getWritten(),
247 .text = allocating.written(),
248248 .extension = diagnostic.extension,
249249 .opt = diagnostic.opt,
250250 .location = loc.expand(p.comp),
......@@ -441,7 +441,7 @@ pub fn err(p: *Parser, tok_i: TokenIndex, diagnostic: Diagnostic, args: anytype)
441441 }
442442 try p.diagnostics.addWithLocation(p.comp, .{
443443 .kind = diagnostic.kind,
444 .text = allocating.getWritten(),
444 .text = allocating.written(),
445445 .opt = diagnostic.opt,
446446 .extension = diagnostic.extension,
447447 .location = loc.expand(p.comp),
......@@ -1487,13 +1487,13 @@ fn staticAssertMessage(p: *Parser, cond_node: Node.Index, maybe_message: ?Result
14871487
14881488 if (maybe_message) |message| {
14891489 assert(message.node.get(&p.tree) == .string_literal_expr);
1490 if (allocating.getWritten().len > 0) {
1490 if (allocating.written().len > 0) {
14911491 try w.writeByte(' ');
14921492 }
14931493 const bytes = p.comp.interner.get(message.val.ref()).bytes;
14941494 try Value.printString(bytes, message.qt, p.comp, w);
14951495 }
1496 return allocating.getWritten();
1496 return allocating.written();
14971497}
14981498
14991499/// staticAssert
......@@ -9248,7 +9248,7 @@ fn primaryExpr(p: *Parser) Error!?Result {
92489248 func_qt.printNamed(p.tokSlice(p.func.name), p.comp, &allocating.writer) catch return error.OutOfMemory;
92499249 allocating.writer.writeByte(0) catch return error.OutOfMemory;
92509250
9251 const predef = try p.makePredefinedIdentifier(allocating.getWritten());
9251 const predef = try p.makePredefinedIdentifier(allocating.written());
92529252 qt = predef.qt;
92539253 p.func.pretty_ident = predef;
92549254 } else {
lib/compiler/aro/aro/Pragma.zig+1-1
......@@ -203,7 +203,7 @@ pub fn err(pp: *Preprocessor, tok_i: TokenIndex, diagnostic: Diagnostic, args: a
203203 try pp.diagnostics.addWithLocation(pp.comp, .{
204204 .kind = diagnostic.kind,
205205 .opt = diagnostic.opt,
206 .text = allocating.getWritten(),
206 .text = allocating.written(),
207207 .location = pp.tokens.items(.loc)[tok_i].expand(pp.comp),
208208 .extension = diagnostic.extension,
209209 }, pp.expansionSlice(tok_i), true);
lib/compiler/aro/aro/Preprocessor.zig+8-13
......@@ -769,7 +769,7 @@ fn err(pp: *Preprocessor, loc: anytype, diagnostic: Diagnostic, args: anytype) C
769769 Diagnostics.formatArgs(&allocating.writer, diagnostic.fmt, args) catch return error.OutOfMemory;
770770 try pp.diagnostics.addWithLocation(pp.comp, .{
771771 .kind = diagnostic.kind,
772 .text = allocating.getWritten(),
772 .text = allocating.written(),
773773 .opt = diagnostic.opt,
774774 .extension = diagnostic.extension,
775775 .location = switch (@TypeOf(loc)) {
......@@ -798,7 +798,7 @@ fn fatal(pp: *Preprocessor, raw: RawToken, comptime fmt: []const u8, args: anyty
798798 Diagnostics.formatArgs(&allocating.writer, fmt, args) catch return error.OutOfMemory;
799799 try pp.diagnostics.add(.{
800800 .kind = .@"fatal error",
801 .text = allocating.getWritten(),
801 .text = allocating.written(),
802802 .location = (Source.Location{
803803 .id = raw.source,
804804 .byte_offset = raw.start,
......@@ -1618,18 +1618,13 @@ fn handleBuiltinMacro(pp: *Preprocessor, builtin: RawToken.Id, param_toks: []con
16181618 else => unreachable,
16191619 };
16201620 const filename = include_str[1 .. include_str.len - 1];
1621 const res = res: {
1622 if (builtin == .macro_param_has_include or pp.include_depth == 0) {
1623 if (builtin == .macro_param_has_include_next) {
1624 try pp.err(src_loc, .include_next_outside_header, .{});
1625 }
1626 break :res try pp.comp.hasInclude(filename, src_loc.id, include_type, .first);
1621 if (builtin == .macro_param_has_include or pp.include_depth == 0) {
1622 if (builtin == .macro_param_has_include_next) {
1623 try pp.err(src_loc, .include_next_outside_header, .{});
16271624 }
1628 break :res try pp.comp.hasInclude(filename, src_loc.id, include_type, .next);
1629 };
1630
1631 if (res) if (pp.dep_file) |dep_file| try dep_file.addDependencyDupe(pp.gpa, pp.comp.arena, filename);
1632 return res;
1625 return pp.comp.hasInclude(filename, src_loc.id, include_type, .first, pp.dep_file);
1626 }
1627 return pp.comp.hasInclude(filename, src_loc.id, include_type, .next, pp.dep_file);
16331628 },
16341629 else => unreachable,
16351630 }
lib/compiler/aro/aro/pragmas/message.zig+1-1
......@@ -51,7 +51,7 @@ fn preprocessorHandler(_: *Pragma, pp: *Preprocessor, start_idx: TokenIndex) Pra
5151 Diagnostics.formatArgs(&allocating.writer, diagnostic.fmt, .{str}) catch return error.OutOfMemory;
5252
5353 try pp.diagnostics.add(.{
54 .text = allocating.getWritten(),
54 .text = allocating.written(),
5555 .kind = diagnostic.kind,
5656 .opt = diagnostic.opt,
5757 .location = loc.expand(pp.comp),
lib/compiler/aro/aro/text_literal.zig+1-1
......@@ -328,7 +328,7 @@ pub const Parser = struct {
328328 offset_location.byte_offset += p.offset;
329329 try p.comp.diagnostics.addWithLocation(p.comp, .{
330330 .kind = diagnostic.kind,
331 .text = allocating.getWritten(),
331 .text = allocating.written(),
332332 .opt = diagnostic.opt,
333333 .extension = diagnostic.extension,
334334 .location = offset_location.expand(p.comp),
lib/compiler/translate-c/Translator.zig+5-5
......@@ -1005,7 +1005,7 @@ fn transStaticAssert(t: *Translator, scope: *Scope, static_assert: Node.StaticAs
10051005 allocating.writer.end -= 1; // printString adds a terminating " so we need to remove it
10061006 allocating.writer.writeAll("\\\"\"") catch return error.OutOfMemory;
10071007
1008 break :str try ZigTag.string_literal.create(t.arena, try t.arena.dupe(u8, allocating.getWritten()));
1008 break :str try ZigTag.string_literal.create(t.arena, try t.arena.dupe(u8, allocating.written()));
10091009 } else try ZigTag.string_literal.create(t.arena, "\"static assertion failed\"");
10101010
10111011 const assert_node = try ZigTag.static_assert.create(t.arena, .{ .lhs = condition, .rhs = diagnostic });
......@@ -1020,7 +1020,7 @@ fn transGlobalAsm(t: *Translator, scope: *Scope, global_asm: Node.SimpleAsm) Err
10201020 defer allocating.deinit();
10211021 aro.Value.printString(bytes, global_asm.asm_str.qt(t.tree), t.comp, &allocating.writer) catch return error.OutOfMemory;
10221022
1023 const str_node = try ZigTag.string_literal.create(t.arena, try t.arena.dupe(u8, allocating.getWritten()));
1023 const str_node = try ZigTag.string_literal.create(t.arena, try t.arena.dupe(u8, allocating.written()));
10241024
10251025 const asm_node = try ZigTag.asm_simple.create(t.arena, str_node);
10261026 const block = try ZigTag.block_single.create(t.arena, asm_node);
......@@ -1037,7 +1037,7 @@ fn getTypeStr(t: *Translator, qt: QualType) ![]const u8 {
10371037 var allocating: std.Io.Writer.Allocating = .init(t.gpa);
10381038 defer allocating.deinit();
10391039 qt.print(t.comp, &allocating.writer) catch return error.OutOfMemory;
1040 return t.arena.dupe(u8, allocating.getWritten());
1040 return t.arena.dupe(u8, allocating.written());
10411041}
10421042
10431043fn transType(t: *Translator, scope: *Scope, qt: QualType, source_loc: TokenIndex) TypeError!ZigNode {
......@@ -3345,7 +3345,7 @@ fn transFloatLiteral(
33453345 defer allocating.deinit();
33463346 _ = val.print(float_literal.qt, t.comp, &allocating.writer) catch return error.OutOfMemory;
33473347
3348 const float_lit_node = try ZigTag.float_literal.create(t.arena, try t.arena.dupe(u8, allocating.getWritten()));
3348 const float_lit_node = try ZigTag.float_literal.create(t.arena, try t.arena.dupe(u8, allocating.written()));
33493349 if (suppress_as == .no_as) {
33503350 return t.maybeSuppressResult(used, float_lit_node);
33513351 }
......@@ -3390,7 +3390,7 @@ fn transNarrowStringLiteral(
33903390
33913391 aro.Value.printString(bytes, literal.qt, t.comp, &allocating.writer) catch return error.OutOfMemory;
33923392
3393 return ZigTag.string_literal.create(t.arena, try t.arena.dupe(u8, allocating.getWritten()));
3393 return ZigTag.string_literal.create(t.arena, try t.arena.dupe(u8, allocating.written()));
33943394}
33953395
33963396/// Translate a string literal that is initializing an array. In general narrow string
lib/compiler/translate-c/main.zig+5-1
......@@ -145,7 +145,11 @@ fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: [][:0]u8) !void {
145145 defer pp.deinit();
146146
147147 var name_buf: [std.fs.max_name_bytes]u8 = undefined;
148 var opt_dep_file = try d.initDepFile(source, &name_buf);
148 // Omit the source file from the dep file so that it can be tracked separately.
149 // In the Zig compiler we want to omit it from the cache hash since it will
150 // be written to a tmp file then renamed into place, meaning the path will be
151 // wrong as soon as the work is done.
152 var opt_dep_file = try d.initDepFile(source, &name_buf, true);
149153 defer if (opt_dep_file) |*dep_file| dep_file.deinit(pp.gpa);
150154
151155 if (opt_dep_file) |*dep_file| pp.dep_file = dep_file;