@@ -16,7 +16,8 @@ pub fn main() !void {
1616
1717 if (i != 2 ) {
1818 std .debug .print ("usage: addHeaders <dir>\n " , .{});
19- std .debug .print ("adds headers from <dir> into uh_workspace\n " , .{});
19+ std .debug .print ("normalizes headers from <dir> into uh_norm/<dir>\n " , .{});
20+ std .debug .print ("adds headers from uh_norm/<dir> into uh_workspace\n " , .{});
2021 return ;
2122 }
2223 }
@@ -26,6 +27,11 @@ pub fn main() !void {
2627 const headerDir = args .next () orelse return ;
2728
2829 var buf = try arena .alloc (u8 , 1000 );
30+
31+ const normalized_dir = try std .fmt .bufPrint (buf , "uh_norm/{s}" , .{std .fs .path .basename (headerDir )});
32+ std .fs .cwd ().makePath (normalized_dir ) catch {};
33+
34+ buf = try arena .alloc (u8 , 1000 );
2935 const versionStr = try std .fmt .bufPrint (buf , "{s}|" , .{std .fs .path .basename (headerDir )});
3036
3137 var dir = try std .fs .cwd ().openIterableDir (headerDir , .{});
@@ -41,7 +47,7 @@ pub fn main() !void {
4147 continue ;
4248 }
4349
44- //if (!std.mem.eql(u8, entry.basename, "_ctype .h")) continue;
50+ //if (!std.mem.eql(u8, entry.basename, "ucontext .h")) continue;
4551
4652 //std.debug.print("entry: base {s} path {s}\n", .{ entry.basename, entry.path });
4753
@@ -50,6 +56,7 @@ pub fn main() !void {
5056
5157 var filepath = try std .fs .path .join (arena , &.{ headerDir , entry .path });
5258 var workpath = try std .fs .path .join (arena , &.{ "uh_workspace" , entry .path });
59+ var normalized_path = try std .fs .path .join (arena , &.{ normalized_dir , entry .path });
5360
5461 // read all the lines of our work-in-progress file
5562 var worklines = std .ArrayList ([]const u8 ).init (arena );
@@ -60,13 +67,15 @@ pub fn main() !void {
6067 }
6168 var file = try std .fs .cwd ().createFile (workpath , .{ .read = true , .truncate = false });
6269 defer file .close ();
63- var contents = try file .reader ().readAllAlloc (arena , 20 * 1024 * 1024 );
70+ var contents = try file .reader ().readAllAlloc (arena , 100 * 1024 * 1024 );
71+
6472 while (contents .len > 0 and contents [contents .len - 1 ] == '\n ' ) {
6573 contents = contents [0 .. contents .len - 1 ];
6674 }
6775
6876 if (contents .len > 0 ) {
6977 var it = std .mem .splitScalar (u8 , contents , '\n ' );
78+
7079 while (it .next ()) | line | {
7180 try worklines .append (line );
7281 }
@@ -80,7 +89,7 @@ pub fn main() !void {
8089 {
8190 var file = try std .fs .cwd ().createFile (versionpath , .{ .read = true , .truncate = false });
8291 defer file .close ();
83- var contents = try file .reader ().readAllAlloc (arena , 20 * 1024 * 1024 );
92+ var contents = try file .reader ().readAllAlloc (arena , 100 * 1024 * 1024 );
8493 while (contents .len > 0 and contents [contents .len - 1 ] == '\n ' ) {
8594 contents = contents [0 .. contents .len - 1 ];
8695 }
@@ -98,7 +107,37 @@ pub fn main() !void {
98107 {
99108 var file = try std .fs .cwd ().openFile (filepath , .{});
100109 defer file .close ();
101- var contents = try file .reader ().readAllAlloc (arena , 20 * 1024 * 1024 );
110+ var contents = try file .reader ().readAllAlloc (arena , 100 * 1024 * 1024 );
111+
112+ // filter out comments
113+ var i : usize = 0 ;
114+ var k : usize = 0 ;
115+ var in_comment = false ;
116+ while (i < contents .len ) {
117+ if (i + 1 < contents .len ) {
118+ if (! in_comment and std .mem .eql (u8 , contents [i .. ][0.. 2], "/*" )) {
119+ in_comment = true ;
120+ i += 2 ;
121+ continue ;
122+ }
123+
124+ if (in_comment and std .mem .eql (u8 , contents [i .. ][0.. 2], "*/" )) {
125+ in_comment = false ;
126+ i += 2 ;
127+ continue ;
128+ }
129+ }
130+
131+ if (! in_comment ) {
132+ contents [k ] = contents [i ];
133+ k += 1 ;
134+ }
135+
136+ i += 1 ;
137+ }
138+
139+ contents .len = k ;
140+
102141 while (contents .len > 0 and contents [contents .len - 1 ] == '\n ' ) {
103142 contents = contents [0 .. contents .len - 1 ];
104143 }
@@ -109,7 +148,32 @@ pub fn main() !void {
109148 // normalize whitespace (replace tabs with spaces)
110149 buf = try arena .alloc (u8 , line .len );
111150 _ = std .mem .replace (u8 , line , "\t " , " " , buf );
112- try filelines .append (buf );
151+
152+ // filter out empty lines
153+ var empty = true ;
154+ for (buf ) | ch | {
155+ if (ch != ' ' ) {
156+ empty = false ;
157+ break ;
158+ }
159+ }
160+
161+ if (! empty ) {
162+ try filelines .append (buf );
163+ }
164+ }
165+ }
166+
167+ // write out normalized file back to disk for debugging
168+ {
169+ if (std .fs .path .dirname (normalized_path )) | dirname | {
170+ try std .fs .cwd ().makePath (dirname );
171+ }
172+ var nfile = try std .fs .cwd ().createFile (normalized_path , .{});
173+ defer nfile .close ();
174+ var writer = nfile .writer ();
175+ for (filelines .items ) | line | {
176+ try writer .print ("{s}\n " , .{line });
113177 }
114178 }
115179 }
@@ -339,7 +403,6 @@ pub fn addContext(arena: std.mem.Allocator, lines: *std.ArrayList([]const u8)) !
339403 for (seen_contexts .items ) | sc | {
340404 if (ctx == sc ) {
341405 ctx += 1 ;
342- break ;
343406 }
344407 }
345408 try context .append (ctx );
@@ -368,7 +431,6 @@ pub fn addContext(arena: std.mem.Allocator, lines: *std.ArrayList([]const u8)) !
368431 for (seen_contexts .items ) | sc | {
369432 if (ctx == sc ) {
370433 ctx += 1 ;
371- break ;
372434 }
373435 }
374436 try context .append (ctx );
0 commit comments