mirror of
https://github.com/ziglang/zig.git
synced 2024-11-16 17:15:37 +00:00
translate-c cleanup and zig fmt
This commit is contained in:
parent
a016fb8c62
commit
ca3bf6e6ad
@ -205,7 +205,7 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
|
||||
better_capacity += better_capacity / 2 + 8;
|
||||
if (better_capacity >= new_capacity) break;
|
||||
}
|
||||
|
||||
|
||||
const new_memory = try self.allocator.realloc(self.allocatedSlice(), better_capacity);
|
||||
self.items.ptr = new_memory.ptr;
|
||||
self.capacity = new_memory.len;
|
||||
@ -250,7 +250,7 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
|
||||
if (self.items.len == 0) return null;
|
||||
return self.pop();
|
||||
}
|
||||
|
||||
|
||||
// For a nicer API, `items.len` is the length, not the capacity.
|
||||
// This requires "unsafe" slicing.
|
||||
fn allocatedSlice(self: Self) Slice {
|
||||
@ -448,16 +448,16 @@ test "std.ArrayList(u8) implements outStream" {
|
||||
}
|
||||
|
||||
test "std.ArrayList.shrink still sets length on error.OutOfMemory" {
|
||||
// use an arena allocator to make sure realloc returns error.OutOfMemory
|
||||
var arena = std.heap.ArenaAllocator.init(testing.allocator);
|
||||
defer arena.deinit();
|
||||
// use an arena allocator to make sure realloc returns error.OutOfMemory
|
||||
var arena = std.heap.ArenaAllocator.init(testing.allocator);
|
||||
defer arena.deinit();
|
||||
|
||||
var list = ArrayList(i32).init(&arena.allocator);
|
||||
var list = ArrayList(i32).init(&arena.allocator);
|
||||
|
||||
try list.append(1);
|
||||
try list.append(2);
|
||||
try list.append(3);
|
||||
try list.append(1);
|
||||
try list.append(2);
|
||||
try list.append(3);
|
||||
|
||||
list.shrink(1);
|
||||
testing.expect(list.items.len == 1);
|
||||
list.shrink(1);
|
||||
testing.expect(list.items.len == 1);
|
||||
}
|
||||
|
@ -1467,7 +1467,6 @@ pub const Int = struct {
|
||||
|
||||
r.swap(&x);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// Storage must live for the lifetime of the returned value
|
||||
|
@ -2584,7 +2584,7 @@ pub fn fstat(fd: fd_t) FStatError!Stat {
|
||||
}
|
||||
}
|
||||
|
||||
pub const FStatAtError = FStatError || error{NameTooLong, FileNotFound};
|
||||
pub const FStatAtError = FStatError || error{ NameTooLong, FileNotFound };
|
||||
|
||||
pub fn fstatat(dirfd: fd_t, pathname: []const u8, flags: u32) FStatAtError!Stat {
|
||||
const pathname_c = try toPosixPath(pathname);
|
||||
|
@ -42,4 +42,4 @@ pub extern "gdi32" fn ChoosePixelFormat(
|
||||
|
||||
pub extern "gdi32" fn SwapBuffers(hdc: ?HDC) callconv(.Stdcall) bool;
|
||||
pub extern "gdi32" fn wglCreateContext(hdc: ?HDC) callconv(.Stdcall) ?HGLRC;
|
||||
pub extern "gdi32" fn wglMakeCurrent(hdc: ?HDC, hglrc: ?HGLRC) callconv(.Stdcall) bool;
|
||||
pub extern "gdi32" fn wglMakeCurrent(hdc: ?HDC, hglrc: ?HGLRC) callconv(.Stdcall) bool;
|
||||
|
@ -169,4 +169,4 @@ pub extern "user32" fn GetMessageA(
|
||||
|
||||
pub extern "user32" fn TranslateMessage(lpMsg: *const MSG) callconv(.Stdcall) bool;
|
||||
pub extern "user32" fn DispatchMessageA(lpMsg: *const MSG) callconv(.Stdcall) LRESULT;
|
||||
pub extern "user32" fn PostQuitMessage(nExitCode: i32) callconv(.Stdcall) void;
|
||||
pub extern "user32" fn PostQuitMessage(nExitCode: i32) callconv(.Stdcall) void;
|
||||
|
@ -387,7 +387,7 @@ test "zig fmt: correctly space struct fields with doc comments" {
|
||||
\\ c: u8,
|
||||
\\};
|
||||
\\
|
||||
,
|
||||
,
|
||||
\\pub const S = struct {
|
||||
\\ /// A
|
||||
\\ a: u8,
|
||||
|
@ -903,13 +903,14 @@ fn enumToString(value: var, type_name: []const u8) ![]const u8 {
|
||||
if (e.is_exhaustive) {
|
||||
return std.fmt.allocPrint(std.heap.c_allocator, ".{}", .{@tagName(value)});
|
||||
} else {
|
||||
return std.fmt.allocPrint(std.heap.c_allocator,
|
||||
return std.fmt.allocPrint(
|
||||
std.heap.c_allocator,
|
||||
"@intToEnum({}, {})",
|
||||
.{type_name, @enumToInt(value)}
|
||||
.{ type_name, @enumToInt(value) },
|
||||
);
|
||||
}
|
||||
},
|
||||
else => unreachable
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
|
||||
@ -1136,7 +1137,7 @@ const Stage2Target = extern struct {
|
||||
\\
|
||||
, .{
|
||||
try enumToString(target.os.version_range.windows.min, "Target.Os.WindowsVersion"),
|
||||
try enumToString(target.os.version_range.windows.max, "Target.Os.WindowsVersion")
|
||||
try enumToString(target.os.version_range.windows.max, "Target.Os.WindowsVersion"),
|
||||
}),
|
||||
}
|
||||
try os_builtin_str_buffer.appendSlice("};\n");
|
||||
|
@ -274,7 +274,7 @@ pub fn translate(
|
||||
errdefer tree_arena.deinit();
|
||||
|
||||
const tree = try tree_arena.allocator.create(ast.Tree);
|
||||
tree.* = ast.Tree{
|
||||
tree.* = .{
|
||||
.source = undefined, // need to use toOwnedSlice later
|
||||
.root_node = undefined,
|
||||
.arena_allocator = tree_arena,
|
||||
@ -644,7 +644,7 @@ fn visitVarDecl(c: *Context, var_decl: *const ZigClangVarDecl) Error!void {
|
||||
};
|
||||
|
||||
const node = try c.a().create(ast.Node.VarDecl);
|
||||
node.* = ast.Node.VarDecl{
|
||||
node.* = .{
|
||||
.doc_comments = null,
|
||||
.visib_token = visib_tok,
|
||||
.thread_local_token = thread_local_token,
|
||||
@ -3479,7 +3479,7 @@ fn qualTypeToLog2IntRef(rp: RestorePoint, qt: ZigClangQualType, source_loc: ZigC
|
||||
// we can perform the log2 now.
|
||||
const cast_bit_width = math.log2_int(u64, int_bit_width);
|
||||
const node = try rp.c.a().create(ast.Node.IntegerLiteral);
|
||||
node.* = ast.Node.IntegerLiteral{
|
||||
node.* = .{
|
||||
.token = try appendTokenFmt(rp.c, .Identifier, "u{}", .{cast_bit_width}),
|
||||
};
|
||||
return &node.base;
|
||||
@ -3502,7 +3502,7 @@ fn qualTypeToLog2IntRef(rp: RestorePoint, qt: ZigClangQualType, source_loc: ZigC
|
||||
const import_fn_call = try transCreateNodeBuiltinFnCall(rp.c, "@import");
|
||||
const std_token = try appendToken(rp.c, .StringLiteral, "\"std\"");
|
||||
const std_node = try rp.c.a().create(ast.Node.StringLiteral);
|
||||
std_node.* = ast.Node.StringLiteral{
|
||||
std_node.* = .{
|
||||
.token = std_token,
|
||||
};
|
||||
try import_fn_call.params.push(&std_node.base);
|
||||
@ -3783,7 +3783,7 @@ fn transCreateNodePrefixOp(
|
||||
bytes: []const u8,
|
||||
) !*ast.Node.PrefixOp {
|
||||
const node = try c.a().create(ast.Node.PrefixOp);
|
||||
node.* = ast.Node.PrefixOp{
|
||||
node.* = .{
|
||||
.op_token = try appendToken(c, op_tok_id, bytes),
|
||||
.op = op,
|
||||
.rhs = undefined, // translate and set afterward
|
||||
@ -3980,7 +3980,7 @@ fn transCreateNodeBoolLiteral(c: *Context, value: bool) !*ast.Node {
|
||||
fn transCreateNodeArrayInitializer(c: *Context, ty: *ast.Node) !*ast.Node.SuffixOp {
|
||||
_ = try appendToken(c, .LBrace, "{");
|
||||
const node = try c.a().create(ast.Node.SuffixOp);
|
||||
node.* = ast.Node.SuffixOp{
|
||||
node.* = .{
|
||||
.lhs = .{ .node = ty },
|
||||
.op = .{
|
||||
.ArrayInitializer = ast.Node.SuffixOp.Op.InitList.init(c.a()),
|
||||
@ -3993,7 +3993,7 @@ fn transCreateNodeArrayInitializer(c: *Context, ty: *ast.Node) !*ast.Node.Suffix
|
||||
fn transCreateNodeStructInitializer(c: *Context, ty: *ast.Node) !*ast.Node.SuffixOp {
|
||||
_ = try appendToken(c, .LBrace, "{");
|
||||
const node = try c.a().create(ast.Node.SuffixOp);
|
||||
node.* = ast.Node.SuffixOp{
|
||||
node.* = .{
|
||||
.lhs = .{ .node = ty },
|
||||
.op = .{
|
||||
.StructInitializer = ast.Node.SuffixOp.Op.InitList.init(c.a()),
|
||||
@ -4306,7 +4306,7 @@ fn transCreateNodeShiftOp(
|
||||
cast_node.rparen_token = try appendToken(rp.c, .RParen, ")");
|
||||
|
||||
const node = try rp.c.a().create(ast.Node.InfixOp);
|
||||
node.* = ast.Node.InfixOp{
|
||||
node.* = .{
|
||||
.op_token = op_token,
|
||||
.lhs = lhs,
|
||||
.op = op,
|
||||
@ -4626,8 +4626,7 @@ fn finishTransFnProto(
|
||||
const type_node = try transQualType(rp, param_qt, source_loc);
|
||||
|
||||
const param_node = try rp.c.a().create(ast.Node.ParamDecl);
|
||||
param_node.* = ast.Node.ParamDecl{
|
||||
.base = ast.Node{ .id = ast.Node.Id.ParamDecl },
|
||||
param_node.* = .{
|
||||
.doc_comments = null,
|
||||
.comptime_token = null,
|
||||
.noalias_token = noalias_tok,
|
||||
@ -4648,8 +4647,7 @@ fn finishTransFnProto(
|
||||
}
|
||||
|
||||
const var_arg_node = try rp.c.a().create(ast.Node.ParamDecl);
|
||||
var_arg_node.* = ast.Node.ParamDecl{
|
||||
.base = ast.Node{ .id = ast.Node.Id.ParamDecl },
|
||||
var_arg_node.* = .{
|
||||
.doc_comments = null,
|
||||
.comptime_token = null,
|
||||
.noalias_token = null,
|
||||
@ -4774,14 +4772,12 @@ pub fn failDecl(c: *Context, loc: ZigClangSourceLocation, name: []const u8, comp
|
||||
const semi_tok = try appendToken(c, .Semicolon, ";");
|
||||
|
||||
const msg_node = try c.a().create(ast.Node.StringLiteral);
|
||||
msg_node.* = ast.Node.StringLiteral{
|
||||
.base = ast.Node{ .id = ast.Node.Id.StringLiteral },
|
||||
msg_node.* = .{
|
||||
.token = msg_tok,
|
||||
};
|
||||
|
||||
const call_node = try c.a().create(ast.Node.BuiltinCall);
|
||||
call_node.* = ast.Node.BuiltinCall{
|
||||
.base = ast.Node{ .id = ast.Node.Id.BuiltinCall },
|
||||
call_node.* = .{
|
||||
.builtin_token = builtin_tok,
|
||||
.params = ast.Node.BuiltinCall.ParamList.init(c.a()),
|
||||
.rparen_token = rparen_tok,
|
||||
@ -4789,8 +4785,7 @@ pub fn failDecl(c: *Context, loc: ZigClangSourceLocation, name: []const u8, comp
|
||||
try call_node.params.push(&msg_node.base);
|
||||
|
||||
const var_decl_node = try c.a().create(ast.Node.VarDecl);
|
||||
var_decl_node.* = ast.Node.VarDecl{
|
||||
.base = ast.Node{ .id = ast.Node.Id.VarDecl },
|
||||
var_decl_node.* = .{
|
||||
.doc_comments = null,
|
||||
.visib_token = pub_tok,
|
||||
.thread_local_token = null,
|
||||
@ -4825,7 +4820,7 @@ fn appendTokenFmt(c: *Context, token_id: Token.Id, comptime format: []const u8,
|
||||
const new_token = try c.tree.tokens.addOne();
|
||||
errdefer c.tree.tokens.shrink(token_index);
|
||||
|
||||
new_token.* = Token{
|
||||
new_token.* = .{
|
||||
.id = token_id,
|
||||
.start = start_index,
|
||||
.end = end_index,
|
||||
@ -5576,17 +5571,17 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
|
||||
// @as(dest, x) )
|
||||
|
||||
const if_1 = try transCreateNodeIf(c);
|
||||
const type_id_1 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
|
||||
const type_info_1 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
|
||||
const type_of_1 = try transCreateNodeBuiltinFnCall(c, "@TypeOf");
|
||||
try type_id_1.params.push(&type_of_1.base);
|
||||
try type_info_1.params.push(&type_of_1.base);
|
||||
try type_of_1.params.push(node_to_cast);
|
||||
type_of_1.rparen_token = try appendToken(c, .RParen, ")");
|
||||
type_id_1.rparen_token = try appendToken(c, .RParen, ")");
|
||||
type_info_1.rparen_token = try appendToken(c, .RParen, ")");
|
||||
|
||||
const cmp_1 = try c.a().create(ast.Node.InfixOp);
|
||||
cmp_1.* = .{
|
||||
.op_token = try appendToken(c, .EqualEqual, "=="),
|
||||
.lhs = &type_id_1.base,
|
||||
.lhs = &type_info_1.base,
|
||||
.op = .EqualEqual,
|
||||
.rhs = try transCreateNodeEnumLiteral(c, "Pointer"),
|
||||
};
|
||||
@ -5624,17 +5619,17 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
|
||||
if_1.@"else" = else_1;
|
||||
|
||||
const if_2 = try transCreateNodeIf(c);
|
||||
const type_id_2 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
|
||||
const type_info_2 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
|
||||
const type_of_2 = try transCreateNodeBuiltinFnCall(c, "@TypeOf");
|
||||
try type_id_2.params.push(&type_of_2.base);
|
||||
try type_info_2.params.push(&type_of_2.base);
|
||||
try type_of_2.params.push(node_to_cast);
|
||||
type_of_2.rparen_token = try appendToken(c, .RParen, ")");
|
||||
type_id_2.rparen_token = try appendToken(c, .RParen, ")");
|
||||
type_info_2.rparen_token = try appendToken(c, .RParen, ")");
|
||||
|
||||
const cmp_2 = try c.a().create(ast.Node.InfixOp);
|
||||
cmp_2.* = .{
|
||||
.op_token = try appendToken(c, .EqualEqual, "=="),
|
||||
.lhs = &type_id_2.base,
|
||||
.lhs = &type_info_2.base,
|
||||
.op = .EqualEqual,
|
||||
.rhs = try transCreateNodeEnumLiteral(c, "Int"),
|
||||
};
|
||||
@ -5646,13 +5641,13 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
|
||||
.op = .BoolAnd,
|
||||
.rhs = undefined,
|
||||
};
|
||||
const type_id_3 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
|
||||
try type_id_3.params.push(inner_node);
|
||||
type_id_3.rparen_token = try appendToken(c, .LParen, ")");
|
||||
const type_info_3 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
|
||||
try type_info_3.params.push(inner_node);
|
||||
type_info_3.rparen_token = try appendToken(c, .LParen, ")");
|
||||
const cmp_3 = try c.a().create(ast.Node.InfixOp);
|
||||
cmp_3.* = .{
|
||||
.op_token = try appendToken(c, .EqualEqual, "=="),
|
||||
.lhs = &type_id_3.base,
|
||||
.lhs = &type_info_3.base,
|
||||
.op = .EqualEqual,
|
||||
.rhs = try transCreateNodeEnumLiteral(c, "Pointer"),
|
||||
};
|
||||
|
@ -162,7 +162,7 @@ test "for copies its payload" {
|
||||
test "for on slice with allowzero ptr" {
|
||||
const S = struct {
|
||||
fn doTheTest(slice: []const u8) void {
|
||||
var ptr = @ptrCast([*]const allowzero u8, slice.ptr)[0..slice.len];
|
||||
var ptr = @ptrCast([*]allowzero const u8, slice.ptr)[0..slice.len];
|
||||
for (ptr) |x, i| expect(x == i + 1);
|
||||
for (ptr) |*x, i| expect(x.* == i + 1);
|
||||
}
|
||||
|
@ -2879,7 +2879,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
, &[_][]const u8{
|
||||
\\pub const FOO = 0x61626364;
|
||||
});
|
||||
|
||||
|
||||
cases.add("Make sure casts are grouped",
|
||||
\\typedef struct
|
||||
\\{
|
||||
@ -2903,7 +2903,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
|
||||
,
|
||||
\\pub const FOO = (if (@typeInfo(c_int) == .Pointer) @intToPtr(c_int, 0x8000) else @as(c_int, 0x8000));
|
||||
});
|
||||
|
||||
|
||||
if (std.Target.current.abi == .msvc) {
|
||||
cases.add("nameless struct fields",
|
||||
\\typedef struct NAMED
|
||||
|
Loading…
Reference in New Issue
Block a user