@@ -13,24 +13,33 @@ pub fn main() u8 {
13
13
const gpa = general_purpose_allocator .allocator ();
14
14
defer _ = general_purpose_allocator .deinit ();
15
15
16
- var arena_instance = std .heap .ArenaAllocator .init (std . heap . page_allocator );
16
+ var arena_instance = std .heap .ArenaAllocator .init (gpa );
17
17
defer arena_instance .deinit ();
18
18
const arena = arena_instance .allocator ();
19
19
20
- const args = process .argsAlloc (arena ) catch {
20
+ var args = process .argsAlloc (arena ) catch {
21
21
std .debug .print ("ran out of memory allocating arguments\n " , .{});
22
22
if (fast_exit ) process .exit (1 );
23
23
return 1 ;
24
24
};
25
25
26
+ var zig_integration = false ;
27
+ if (args .len > 1 and std .mem .eql (u8 , args [1 ], "--zig-integration" )) {
28
+ zig_integration = true ;
29
+ }
30
+
26
31
var stderr_buf : [1024 ]u8 = undefined ;
27
32
var stderr = std .fs .File .stderr ().writer (& stderr_buf );
28
- var diagnostics : aro.Diagnostics = . {
29
- .output = .{ .to_writer = .{
33
+ var diagnostics : aro.Diagnostics = switch ( zig_integration ) {
34
+ false = > .{ .output = .{ .to_writer = .{
30
35
.color = .detect (stderr .file ),
31
36
.writer = & stderr .interface ,
32
- } },
37
+ } } },
38
+ true = > .{ .output = .{ .to_list = .{
39
+ .arena = .init (gpa ),
40
+ } } },
33
41
};
42
+ defer diagnostics .deinit ();
34
43
35
44
var comp = aro .Compilation .initDefault (gpa , arena , & diagnostics , std .fs .cwd ()) catch | err | switch (err ) {
36
45
error .OutOfMemory = > {
@@ -47,13 +56,22 @@ pub fn main() u8 {
47
56
var toolchain : aro.Toolchain = .{ .driver = & driver , .filesystem = .{ .real = comp .cwd } };
48
57
defer toolchain .deinit ();
49
58
50
- translate (& driver , & toolchain , args ) catch | err | switch (err ) {
59
+ translate (& driver , & toolchain , args , zig_integration ) catch | err | switch (err ) {
51
60
error .OutOfMemory = > {
52
61
std .debug .print ("ran out of memory translating\n " , .{});
53
62
if (fast_exit ) process .exit (1 );
54
63
return 1 ;
55
64
},
56
- error .FatalError = > {
65
+ error .FatalError = > if (zig_integration ) {
66
+ serveErrorBundle (arena , & diagnostics ) catch | bundle_err | {
67
+ std .debug .print ("unable to serve error bundle: {}\n " , .{bundle_err });
68
+ if (fast_exit ) process .exit (1 );
69
+ return 1 ;
70
+ };
71
+
72
+ if (fast_exit ) process .exit (0 );
73
+ return 0 ;
74
+ } else {
57
75
if (fast_exit ) process .exit (1 );
58
76
return 1 ;
59
77
},
@@ -63,10 +81,23 @@ pub fn main() u8 {
63
81
return 1 ;
64
82
},
65
83
};
84
+
85
+ assert (comp .diagnostics .errors == 0 or ! zig_integration );
66
86
if (fast_exit ) process .exit (@intFromBool (comp .diagnostics .errors != 0 ));
67
87
return @intFromBool (comp .diagnostics .errors != 0 );
68
88
}
69
89
90
+ fn serveErrorBundle (arena : std.mem.Allocator , diagnostics : * const aro.Diagnostics ) ! void {
91
+ const error_bundle = try diagnostics .toErrorBundle (arena , "failed during translation" );
92
+ var stdout_buffer : [1024 ]u8 = undefined ;
93
+ var stdout_writer = std .fs .File .stdout ().writer (& stdout_buffer );
94
+ var server : std.zig.Server = .{
95
+ .out = & stdout_writer .interface ,
96
+ .in = undefined ,
97
+ };
98
+ try server .serveErrorBundle (error_bundle );
99
+ }
100
+
70
101
pub const usage =
71
102
\\Usage {s}: [options] file [CC options]
72
103
\\
@@ -79,7 +110,7 @@ pub const usage =
79
110
\\
80
111
;
81
112
82
- fn translate (d : * aro.Driver , tc : * aro.Toolchain , args : [][:0 ]u8 ) ! void {
113
+ fn translate (d : * aro.Driver , tc : * aro.Toolchain , args : [][:0 ]u8 , zig_integration : bool ) ! void {
83
114
const gpa = d .comp .gpa ;
84
115
85
116
const aro_args = args : {
@@ -99,6 +130,9 @@ fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: [][:0]u8) !void {
99
130
try stdout .interface .writeAll ("0.0.0-dev\n " );
100
131
try stdout .interface .flush ();
101
132
return ;
133
+ } else if (mem .eql (u8 , arg , "--zig-integration" )) {
134
+ if (i != 1 or ! zig_integration )
135
+ return d .fatal ("--zig-integration must be the first argument" , .{});
102
136
} else {
103
137
i += 1 ;
104
138
}
@@ -116,6 +150,14 @@ fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: [][:0]u8) !void {
116
150
return d .fatal ("user provided macro source exceeded max size" , .{});
117
151
}
118
152
153
+ const has_output_file = if (d .output_name ) | path |
154
+ ! std .mem .eql (u8 , path , "-" )
155
+ else
156
+ false ;
157
+ if (zig_integration and ! has_output_file ) {
158
+ return d .fatal ("--zig-integration requires specifying an output file" , .{});
159
+ }
160
+
119
161
const content = try macro_buf .toOwnedSlice (gpa );
120
162
errdefer gpa .free (content );
121
163
@@ -160,7 +202,7 @@ fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: [][:0]u8) !void {
160
202
defer c_tree .deinit ();
161
203
162
204
if (d .diagnostics .errors != 0 ) {
163
- if (fast_exit ) process .exit (1 );
205
+ if (fast_exit and ! zig_integration ) process .exit (1 );
164
206
return error .FatalError ;
165
207
}
166
208
@@ -212,7 +254,7 @@ fn translate(d: *aro.Driver, tc: *aro.Toolchain, args: [][:0]u8) !void {
212
254
if (out_writer .err ) | write_err |
213
255
return d .fatal ("failed to write result to '{s}': {s}" , .{ out_file_path , aro .Driver .errorDescription (write_err ) });
214
256
215
- if (fast_exit ) process .exit (0 );
257
+ if (fast_exit and ! zig_integration ) process .exit (0 );
216
258
}
217
259
218
260
test {
0 commit comments