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

Compilation: store cwd in Compilation #738

Merged
merged 1 commit into from
Aug 1, 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
4 changes: 2 additions & 2 deletions src/aro/Builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
28 changes: 16 additions & 12 deletions src/aro/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR but this field should probably be moved to Preprocessor.

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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/aro/Preprocessor.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/aro/Tokenizer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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(.{});
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/aro/Value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/aro/toolchains/Linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion test/record_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions test/runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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" });
Expand Down
Loading