authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-14 19:52:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-14 19:52:36-07:00
log0e94530c515dab388828becacc0fe295c5f2b917
treefd1546a2185c5242abc22a9544975bc394ca8247
parent29d9743f9d3a36cbeb1b2618f011551b11474e4d

stage2: refactor 2 CObject fields to use CSourceFile


2 files changed, 11 insertions(+), 20 deletions(-)

BRANCH_TODO-1
......@@ -1,4 +1,3 @@
1 * refactor 2 CObject fields to use CSourceFile
21 * integrate code model and have_frame_pointer to main() and c objects
32 * integrate target features into building C source files
43 * integrate target features into building assembly code
src-self-hosted/Compilation.zig+11-19
......@@ -117,10 +117,7 @@ const WorkItem = union(enum) {
117117
118118pub const CObject = struct {
119119 /// Relative to cwd. Owned by arena.
120 src_path: []const u8,
121 /// Owned by arena.
122 extra_flags: []const []const u8,
123 arena: std.heap.ArenaAllocator.State,
120 src: CSourceFile,
124121 status: union(enum) {
125122 new,
126123 success: struct {
......@@ -154,7 +151,7 @@ pub const CObject = struct {
154151
155152 pub fn destroy(self: *CObject, gpa: *Allocator) void {
156153 _ = self.clearStatus(gpa);
157 self.arena.promote(gpa).deinit();
154 gpa.destroy(self);
158155 }
159156};
160157
......@@ -627,17 +624,12 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
627624 // Add a `CObject` for each `c_source_files`.
628625 try comp.c_object_table.ensureCapacity(gpa, options.c_source_files.len);
629626 for (options.c_source_files) |c_source_file| {
630 var local_arena = std.heap.ArenaAllocator.init(gpa);
631 errdefer local_arena.deinit();
632
633 const c_object = try local_arena.allocator.create(CObject);
627 const c_object = try gpa.create(CObject);
628 errdefer gpa.destroy(c_object);
634629
635630 c_object.* = .{
636631 .status = .{ .new = {} },
637 // TODO look into refactoring to turn these 2 fields simply into a CSourceFile
638 .src_path = try local_arena.allocator.dupe(u8, c_source_file.src_path),
639 .extra_flags = try local_arena.allocator.dupe([]const u8, c_source_file.extra_flags),
640 .arena = local_arena.state,
632 .src = c_source_file,
641633 };
642634 comp.c_object_table.putAssumeCapacityNoClobber(c_object, {});
643635 }
......@@ -798,7 +790,7 @@ pub fn getAllErrorsAlloc(self: *Compilation) !AllErrors {
798790 for (self.failed_c_objects.items()) |entry| {
799791 const c_object = entry.key;
800792 const err_msg = entry.value;
801 try AllErrors.add(&arena, &errors, c_object.src_path, "", err_msg.*);
793 try AllErrors.add(&arena, &errors, c_object.src.src_path, "", err_msg.*);
802794 }
803795 if (self.bin_file.options.module) |module| {
804796 for (module.failed_files.items()) |entry| {
......@@ -981,13 +973,13 @@ fn updateCObject(comp: *Compilation, c_object: *CObject) !void {
981973 // TODO this logic can likely be improved by utilizing clang_options_data.zig.
982974 const file_args = [_][]const u8{"-include"};
983975 var arg_i: usize = 0;
984 while (arg_i < c_object.extra_flags.len) : (arg_i += 1) {
985 const arg = c_object.extra_flags[arg_i];
976 while (arg_i < c_object.src.extra_flags.len) : (arg_i += 1) {
977 const arg = c_object.src.extra_flags[arg_i];
986978 ch.hash.addBytes(arg);
987979 for (file_args) |file_arg| {
988 if (mem.eql(u8, file_arg, arg) and arg_i + 1 < c_object.extra_flags.len) {
980 if (mem.eql(u8, file_arg, arg) and arg_i + 1 < c_object.src.extra_flags.len) {
989981 arg_i += 1;
990 _ = try ch.addFile(c_object.extra_flags[arg_i], null);
982 _ = try ch.addFile(c_object.src.extra_flags[arg_i], null);
991983 }
992984 }
993985 }
......@@ -1028,7 +1020,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject) !void {
10281020 try argv.append(out_obj_path);
10291021
10301022 try argv.append(c_object.src_path);
1031 try argv.appendSlice(c_object.extra_flags);
1023 try argv.appendSlice(c_object.src.extra_flags);
10321024
10331025 if (comp.debug_cc) {
10341026 for (argv.items[0 .. argv.items.len - 1]) |arg| {