diff --git a/src/aro/Builtins.zig b/src/aro/Builtins.zig index 795c5dcb..eb67a2f3 100644 --- a/src/aro/Builtins.zig +++ b/src/aro/Builtins.zig @@ -350,7 +350,7 @@ test Iterator { } test "All builtins" { - var comp = Compilation.init(std.testing.allocator); + var comp = Compilation.init(std.testing.allocator, std.fs.cwd()); defer comp.deinit(); _ = try comp.generateBuiltinMacros(.include_system_defines); var arena = std.heap.ArenaAllocator.init(std.testing.allocator); @@ -373,7 +373,7 @@ test "All builtins" { test "Allocation failures" { const Test = struct { fn testOne(allocator: std.mem.Allocator) !void { - var comp = Compilation.init(allocator); + var comp = Compilation.init(allocator, std.fs.cwd()); defer comp.deinit(); _ = try comp.generateBuiltinMacros(.include_system_defines); var arena = std.heap.ArenaAllocator.init(comp.gpa); diff --git a/src/aro/Compilation.zig b/src/aro/Compilation.zig index 671939a5..f94e72e2 100644 --- a/src/aro/Compilation.zig +++ b/src/aro/Compilation.zig @@ -127,22 +127,27 @@ types: struct { } = .{}, string_interner: StrInt = .{}, interner: Interner = .{}, +/// If this is not null, the directory containing the specified Source will be searched for includes +/// Used by MS extensions which allow searching for includes relative to the directory of the main source file. ms_cwd_source_id: ?Source.Id = null, +cwd: std.fs.Dir, -pub fn init(gpa: Allocator) Compilation { +pub fn init(gpa: Allocator, cwd: std.fs.Dir) Compilation { return .{ .gpa = gpa, .diagnostics = Diagnostics.init(gpa), + .cwd = cwd, }; } /// Initialize Compilation with default environment, /// pragma handlers and emulation mode set to target. -pub fn initDefault(gpa: Allocator) !Compilation { +pub fn initDefault(gpa: Allocator, cwd: std.fs.Dir) !Compilation { var comp: Compilation = .{ .gpa = gpa, .environment = try Environment.loadAll(gpa), .diagnostics = Diagnostics.init(gpa), + .cwd = cwd, }; errdefer comp.deinit(); try comp.addDefaultPragmaHandlers(); @@ -1090,7 +1095,7 @@ pub fn getCharSignedness(comp: *const Compilation) std.builtin.Signedness { pub fn addBuiltinIncludeDir(comp: *Compilation, aro_dir: []const u8) !void { var search_path = aro_dir; while (std.fs.path.dirname(search_path)) |dirname| : (search_path = dirname) { - var base_dir = std.fs.cwd().openDir(dirname, .{}) catch continue; + var base_dir = comp.cwd.openDir(dirname, .{}) catch continue; defer base_dir.close(); base_dir.access("include/stddef.h", .{}) catch continue; @@ -1296,7 +1301,7 @@ fn addSourceFromPathExtra(comp: *Compilation, path: []const u8, kind: Source.Kin return error.FileNotFound; } - const file = try std.fs.cwd().openFile(path, .{}); + const file = try comp.cwd.openFile(path, .{}); defer file.close(); const contents = file.readToEndAlloc(comp.gpa, std.math.maxInt(u32)) catch |err| switch (err) { @@ -1379,10 +1384,9 @@ pub fn hasInclude( return false; } - const cwd = std.fs.cwd(); if (std.fs.path.isAbsolute(filename)) { if (which == .next) return false; - return !std.meta.isError(cwd.access(filename, .{})); + return !std.meta.isError(comp.cwd.access(filename, .{})); } const cwd_source_id = switch (include_type) { @@ -1402,7 +1406,7 @@ pub fn hasInclude( while (try it.nextWithFile(filename, sf_allocator)) |found| { defer sf_allocator.free(found.path); - if (!std.meta.isError(cwd.access(found.path, .{}))) return true; + if (!std.meta.isError(comp.cwd.access(found.path, .{}))) return true; } return false; } @@ -1422,7 +1426,7 @@ fn getFileContents(comp: *Compilation, path: []const u8, limit: ?u32) ![]const u return error.FileNotFound; } - const file = try std.fs.cwd().openFile(path, .{}); + const file = try comp.cwd.openFile(path, .{}); defer file.close(); var buf = std.ArrayList(u8).init(comp.gpa); @@ -1620,7 +1624,7 @@ pub const addDiagnostic = Diagnostics.add; test "addSourceFromReader" { const Test = struct { fn addSourceFromReader(str: []const u8, expected: []const u8, warning_count: u32, splices: []const u32) !void { - var comp = Compilation.init(std.testing.allocator); + var comp = Compilation.init(std.testing.allocator, std.fs.cwd()); defer comp.deinit(); var buf_reader = std.io.fixedBufferStream(str); @@ -1632,7 +1636,7 @@ test "addSourceFromReader" { } fn withAllocationFailures(allocator: std.mem.Allocator) !void { - var comp = Compilation.init(allocator); + var comp = Compilation.init(allocator, std.fs.cwd()); defer comp.deinit(); _ = try comp.addSourceFromBuffer("path", "spliced\\\nbuffer\n"); @@ -1674,7 +1678,7 @@ test "addSourceFromReader - exhaustive check for carriage return elimination" { const alen = alphabet.len; var buf: [alphabet.len]u8 = [1]u8{alphabet[0]} ** alen; - var comp = Compilation.init(std.testing.allocator); + var comp = Compilation.init(std.testing.allocator, std.fs.cwd()); defer comp.deinit(); var source_count: u32 = 0; @@ -1702,7 +1706,7 @@ test "ignore BOM at beginning of file" { const Test = struct { fn run(buf: []const u8) !void { - var comp = Compilation.init(std.testing.allocator); + var comp = Compilation.init(std.testing.allocator, std.fs.cwd()); defer comp.deinit(); var buf_reader = std.io.fixedBufferStream(buf); diff --git a/src/aro/Preprocessor.zig b/src/aro/Preprocessor.zig index 3fd98882..adb034af 100644 --- a/src/aro/Preprocessor.zig +++ b/src/aro/Preprocessor.zig @@ -3383,7 +3383,7 @@ test "Preserve pragma tokens sometimes" { var buf = std.ArrayList(u8).init(allocator); defer buf.deinit(); - var comp = Compilation.init(allocator); + var comp = Compilation.init(allocator, std.fs.cwd()); defer comp.deinit(); try comp.addDefaultPragmaHandlers(); @@ -3443,7 +3443,7 @@ test "destringify" { try std.testing.expectEqualStrings(destringified, pp.char_buf.items); } }; - var comp = Compilation.init(allocator); + var comp = Compilation.init(allocator, std.fs.cwd()); defer comp.deinit(); var pp = Preprocessor.init(&comp); defer pp.deinit(); @@ -3501,7 +3501,7 @@ test "Include guards" { } fn testIncludeGuard(allocator: std.mem.Allocator, comptime template: []const u8, tok_id: RawToken.Id, expected_guards: u32) !void { - var comp = Compilation.init(allocator); + var comp = Compilation.init(allocator, std.fs.cwd()); defer comp.deinit(); var pp = Preprocessor.init(&comp); defer pp.deinit(); diff --git a/src/aro/Tokenizer.zig b/src/aro/Tokenizer.zig index c7901069..f703940f 100644 --- a/src/aro/Tokenizer.zig +++ b/src/aro/Tokenizer.zig @@ -2151,7 +2151,7 @@ test "C23 keywords" { } test "Tokenizer fuzz test" { - var comp = Compilation.init(std.testing.allocator); + var comp = Compilation.init(std.testing.allocator, std.fs.cwd()); defer comp.deinit(); const input_bytes = std.testing.fuzzInput(.{}); @@ -2173,7 +2173,7 @@ test "Tokenizer fuzz test" { } fn expectTokensExtra(contents: []const u8, expected_tokens: []const Token.Id, standard: ?LangOpts.Standard) !void { - var comp = Compilation.init(std.testing.allocator); + var comp = Compilation.init(std.testing.allocator, std.fs.cwd()); defer comp.deinit(); if (standard) |provided| { comp.langopts.standard = provided; diff --git a/src/aro/Value.zig b/src/aro/Value.zig index 4d4c02ba..aad6f0e0 100644 --- a/src/aro/Value.zig +++ b/src/aro/Value.zig @@ -67,7 +67,7 @@ test "minUnsignedBits" { } }; - var comp = Compilation.init(std.testing.allocator); + var comp = Compilation.init(std.testing.allocator, std.fs.cwd()); defer comp.deinit(); const target_query = try std.Target.Query.parse(.{ .arch_os_abi = "x86_64-linux-gnu" }); comp.target = try std.zig.system.resolveTargetQuery(target_query); @@ -102,7 +102,7 @@ test "minSignedBits" { } }; - var comp = Compilation.init(std.testing.allocator); + var comp = Compilation.init(std.testing.allocator, std.fs.cwd()); defer comp.deinit(); const target_query = try std.Target.Query.parse(.{ .arch_os_abi = "x86_64-linux-gnu" }); comp.target = try std.zig.system.resolveTargetQuery(target_query); diff --git a/src/aro/toolchains/Linux.zig b/src/aro/toolchains/Linux.zig index f166e9e6..ee24c223 100644 --- a/src/aro/toolchains/Linux.zig +++ b/src/aro/toolchains/Linux.zig @@ -424,7 +424,7 @@ test Linux { defer arena_instance.deinit(); const arena = arena_instance.allocator(); - var comp = Compilation.init(std.testing.allocator); + var comp = Compilation.init(std.testing.allocator, std.fs.cwd()); defer comp.deinit(); comp.environment = .{ .path = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", diff --git a/src/main.zig b/src/main.zig index 3ef6dee9..5bbd6b9e 100644 --- a/src/main.zig +++ b/src/main.zig @@ -37,7 +37,7 @@ pub fn main() u8 { }; defer gpa.free(aro_name); - var comp = Compilation.initDefault(gpa) catch |er| switch (er) { + var comp = Compilation.initDefault(gpa, std.fs.cwd()) catch |er| switch (er) { error.OutOfMemory => { std.debug.print("out of memory\n", .{}); if (fast_exit) process.exit(1); diff --git a/test/record_runner.zig b/test/record_runner.zig index f9357a15..1398a602 100644 --- a/test/record_runner.zig +++ b/test/record_runner.zig @@ -212,7 +212,7 @@ fn runTestCases(allocator: std.mem.Allocator, test_dir: []const u8, wg: *std.Thr fn singleRun(alloc: std.mem.Allocator, test_dir: []const u8, test_case: TestCase, stats: *Stats) !void { const path = test_case.path; - var comp = aro.Compilation.init(alloc); + var comp = aro.Compilation.init(alloc, std.fs.cwd()); defer comp.deinit(); try comp.addDefaultPragmaHandlers(); diff --git a/test/runner.zig b/test/runner.zig index a1d6cbf7..83283d15 100644 --- a/test/runner.zig +++ b/test/runner.zig @@ -54,7 +54,7 @@ fn addCommandLineArgs(comp: *aro.Compilation, file: aro.Source, macro_buf: anyty } fn testOne(allocator: std.mem.Allocator, path: []const u8, test_dir: []const u8) !void { - var comp = aro.Compilation.init(allocator); + var comp = aro.Compilation.init(allocator, std.fs.cwd()); defer comp.deinit(); try comp.addDefaultPragmaHandlers(); @@ -159,7 +159,7 @@ pub fn main() !void { }); // prepare compiler - var initial_comp = aro.Compilation.init(gpa); + var initial_comp = aro.Compilation.init(gpa, std.fs.cwd()); defer initial_comp.deinit(); const cases_include_dir = try std.fs.path.join(gpa, &.{ args[1], "include" });