authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-05 06:25:05-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-25 05:11:28-04:00
logda939b032e90795c25f23efa68a9a1c50397fb48
treed7c8fb65b3fdf5ec4bd6a833c426470d47a3b167
parent6f3654ad694946876e3aac971fd284230424c915

c: fix empty container warnings


1 files changed, 58 insertions(+), 36 deletions(-)

src/codegen/c.zig+58-36
......@@ -389,7 +389,7 @@ pub const DeclGen = struct {
389389 try dg.renderValue(writer, ty.slicePtrFieldType(&buf), val.slicePtr(), .Other);
390390 try writer.writeAll(", ");
391391 try writer.print("{d}", .{val.sliceLen(dg.module)});
392 try writer.writeAll("}");
392 try writer.writeByte('}');
393393 return;
394394 }
395395
......@@ -563,6 +563,7 @@ pub const DeclGen = struct {
563563 switch (ty.zigTypeTag()) {
564564 // Using '{}' for integer and floats seemed to error C compilers (both GCC and Clang)
565565 // with 'error: expected expression' (including when built with 'zig cc')
566 .Bool => return writer.writeAll("false"),
566567 .Int => {
567568 const c_bits = toCIntBits(ty.intInfo(dg.module.getTarget()).bits) orelse
568569 return dg.fail("TODO: C backend: implement integer types larger than 128 bits", .{});
......@@ -583,14 +584,19 @@ pub const DeclGen = struct {
583584 }
584585 },
585586 .Pointer => switch (dg.module.getTarget().cpu.arch.ptrBitWidth()) {
586 32 => return writer.writeAll("(void *)0xaaaaaaaa"),
587 64 => return writer.writeAll("(void *)0xaaaaaaaaaaaaaaaa"),
587 32 => return writer.writeAll("(void *)0xaaaaaaaau"),
588 64 => return writer.writeAll("(void *)0xaaaaaaaaaaaaaaaau"),
588589 else => unreachable,
589590 },
590591 .Struct, .ErrorUnion => {
591592 try writer.writeByte('(');
592593 try dg.renderTypecast(writer, ty);
593 return writer.writeAll("){0xaa}");
594 return writer.writeAll("){0xaau}");
595 },
596 .Array => {
597 try writer.writeByte('{');
598 try dg.renderValue(writer, ty.childType(), val, location);
599 return writer.writeByte('}');
594600 },
595601 else => {
596602 // This should lower to 0xaa bytes in safe modes, and for unsafe modes should
......@@ -652,7 +658,7 @@ pub const DeclGen = struct {
652658 try dg.renderValue(writer, ty.slicePtrFieldType(&buf), slice.ptr, location);
653659 try writer.writeAll(", ");
654660 try dg.renderValue(writer, Type.usize, slice.len, location);
655 try writer.writeAll("}");
661 try writer.writeByte('}');
656662 },
657663 .function => {
658664 const func = val.castTag(.function).?.data;
......@@ -684,6 +690,8 @@ pub const DeclGen = struct {
684690 const ai = ty.arrayInfo();
685691 if (ai.sentinel) |s| {
686692 try dg.renderValue(writer, ai.elem_type, s, location);
693 } else {
694 try writer.writeByte('0');
687695 }
688696 try writer.writeByte('}');
689697 },
......@@ -703,12 +711,12 @@ pub const DeclGen = struct {
703711 const ai = ty.arrayInfo();
704712 var index: usize = 0;
705713 while (index < ai.len) : (index += 1) {
706 if (index != 0) try writer.writeAll(",");
714 if (index != 0) try writer.writeByte(',');
707715 const elem_val = try val.elemValue(dg.module, arena_allocator, index);
708716 try dg.renderValue(writer, ai.elem_type, elem_val, .Other);
709717 }
710718 if (ai.sentinel) |s| {
711 if (index != 0) try writer.writeAll(",");
719 if (index != 0) try writer.writeByte(',');
712720 try dg.renderValue(writer, ai.elem_type, s, .Other);
713721 }
714722 try writer.writeByte('}');
......@@ -751,7 +759,7 @@ pub const DeclGen = struct {
751759 else => {
752760 // In this case we are rendering an error union which has a
753761 // 0 bits payload.
754 return writer.writeAll("0");
762 return writer.writeByte('0');
755763 },
756764 }
757765 },
......@@ -827,27 +835,29 @@ pub const DeclGen = struct {
827835 .Struct => {
828836 const field_vals = val.castTag(.aggregate).?.data;
829837
830 try writer.writeAll("(");
838 try writer.writeByte('(');
831839 try dg.renderTypecast(writer, ty);
832840 try writer.writeAll("){");
833841
834 var i: usize = 0;
842 var empty = true;
835843 for (field_vals) |field_val, field_index| {
836844 const field_ty = ty.structFieldType(field_index);
837845 if (!field_ty.hasRuntimeBits()) continue;
838846
839 if (i != 0) try writer.writeAll(",");
847 if (!empty) try writer.writeByte(',');
840848 try dg.renderValue(writer, field_ty, field_val, location);
841 i += 1;
849
850 empty = false;
842851 }
852 if (empty) try writer.writeByte('0');
843853
844 try writer.writeAll("}");
854 try writer.writeByte('}');
845855 },
846856 .Union => {
847857 const union_obj = val.castTag(.@"union").?.data;
848858 const layout = ty.unionGetLayout(target);
849859
850 try writer.writeAll("(");
860 try writer.writeByte('(');
851861 try dg.renderTypecast(writer, ty);
852862 try writer.writeAll("){");
853863
......@@ -866,11 +876,11 @@ pub const DeclGen = struct {
866876 if (field_ty.hasRuntimeBits()) {
867877 try writer.print(".{ } = ", .{fmtIdent(field_name)});
868878 try dg.renderValue(writer, field_ty, union_obj.val, location);
869 }
879 } else try writer.writeByte('0');
870880 if (ty.unionTagTypeSafety()) |_| {
871 try writer.writeAll("}");
881 try writer.writeByte('}');
872882 }
873 try writer.writeAll("}");
883 try writer.writeByte('}');
874884 },
875885
876886 .ComptimeInt => unreachable,
......@@ -913,9 +923,9 @@ pub const DeclGen = struct {
913923 } else {
914924 try w.writeAll("void");
915925 }
916 try w.writeAll(" ");
926 try w.writeByte(' ');
917927 try dg.renderDeclName(w, dg.decl_index);
918 try w.writeAll("(");
928 try w.writeByte('(');
919929
920930 var params_written: usize = 0;
921931 for (fn_info.param_types) |param_type, index| {
......@@ -1038,6 +1048,7 @@ pub const DeclGen = struct {
10381048 try buffer.appendSlice("typedef struct {\n");
10391049 {
10401050 var it = struct_obj.fields.iterator();
1051 var empty = true;
10411052 while (it.next()) |entry| {
10421053 const field_ty = entry.value_ptr.ty;
10431054 if (!field_ty.hasRuntimeBits()) continue;
......@@ -1047,7 +1058,10 @@ pub const DeclGen = struct {
10471058 try buffer.append(' ');
10481059 try dg.renderTypeAndName(buffer.writer(), field_ty, name, .Mut, alignment);
10491060 try buffer.appendSlice(";\n");
1061
1062 empty = false;
10501063 }
1064 if (empty) try buffer.appendSlice(" char empty_struct;\n");
10511065 }
10521066 try buffer.appendSlice("} ");
10531067
......@@ -1076,7 +1090,9 @@ pub const DeclGen = struct {
10761090
10771091 try buffer.appendSlice("typedef struct {\n");
10781092 {
1093 var empty = true;
10791094 for (tuple.types) |field_ty, i| {
1095 if (!field_ty.hasRuntimeBits()) continue;
10801096 const val = tuple.values[i];
10811097 if (val.tag() != .unreachable_value) continue;
10821098
......@@ -1087,7 +1103,10 @@ pub const DeclGen = struct {
10871103 try buffer.append(' ');
10881104 try dg.renderTypeAndName(writer, field_ty, .{ .bytes = name.items }, .Mut, 0);
10891105 try buffer.appendSlice(";\n");
1106
1107 empty = false;
10901108 }
1109 if (empty) try buffer.appendSlice(" char empty_tuple;\n");
10911110 }
10921111 try buffer.appendSlice("} ");
10931112
......@@ -1129,17 +1148,23 @@ pub const DeclGen = struct {
11291148 }
11301149
11311150 try buffer.appendSlice("union {\n");
1151 const fields = t.unionFields();
11321152 {
1133 var it = t.unionFields().iterator();
1153 var it = fields.iterator();
1154 var empty = true;
11341155 while (it.next()) |entry| {
11351156 const field_ty = entry.value_ptr.ty;
11361157 if (!field_ty.hasRuntimeBits()) continue;
1158
11371159 const alignment = entry.value_ptr.abi_align;
11381160 const name: CValue = .{ .identifier = entry.key_ptr.* };
11391161 try buffer.append(' ');
11401162 try dg.renderTypeAndName(buffer.writer(), field_ty, name, .Mut, alignment);
11411163 try buffer.appendSlice(";\n");
1164
1165 empty = false;
11421166 }
1167 if (empty) try buffer.appendSlice(" char empty_union;\n");
11431168 }
11441169 try buffer.appendSlice("} ");
11451170
......@@ -1725,7 +1750,7 @@ pub fn genDecl(o: *Object) !void {
17251750 if (variable.init.tag() != .unreachable_value) {
17261751 try o.dg.renderValue(w, tv.ty, variable.init, .Other);
17271752 }
1728 try w.writeAll(";");
1753 try w.writeByte(';');
17291754 try o.indent_writer.insertNewline();
17301755 } else {
17311756 const writer = o.writer();
......@@ -2035,7 +2060,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
20352060 }
20362061
20372062 f.object.indent_writer.popIndent();
2038 try writer.writeAll("}");
2063 try writer.writeByte('}');
20392064}
20402065
20412066fn airSliceField(f: *Function, inst: Air.Inst.Index, suffix: []const u8) !CValue {
......@@ -2154,7 +2179,7 @@ fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue {
21542179 const local = try f.allocLocal(f.air.typeOfIndex(inst), .Const);
21552180 try writer.writeAll(" = ");
21562181 try f.writeCValue(writer, array);
2157 try writer.writeAll("[");
2182 try writer.writeByte('[');
21582183 try f.writeCValue(writer, index);
21592184 try writer.writeAll("];\n");
21602185 return local;
......@@ -2214,7 +2239,7 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue {
22142239 if (is_array) {
22152240 // Insert a memcpy to initialize this array. The source operand is always a pointer
22162241 // and thus we only need to know size/type information from the local type/dest.
2217 try writer.writeAll(";");
2242 try writer.writeByte(';');
22182243 try f.object.indent_writer.insertNewline();
22192244 try writer.writeAll("memcpy(");
22202245 try f.writeCValue(writer, local);
......@@ -2278,7 +2303,7 @@ fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue {
22782303 const local = try f.allocLocal(inst_ty, .Const);
22792304 try writer.writeAll(" = (");
22802305 try f.renderTypecast(writer, inst_ty);
2281 try writer.writeAll(")");
2306 try writer.writeByte(')');
22822307 try f.writeCValue(writer, operand);
22832308 try writer.writeAll(";\n");
22842309 return local;
......@@ -2381,7 +2406,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue {
23812406 const new_local = try f.allocLocal(rhs_type, .Const);
23822407 try writer.writeAll(" = ");
23832408 try f.writeCValue(writer, src_val);
2384 try writer.writeAll(";");
2409 try writer.writeByte(';');
23852410 try f.object.indent_writer.insertNewline();
23862411
23872412 break :blk new_local;
......@@ -2605,7 +2630,7 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, op_abbrev: [*:0]const u8) !CV
26052630 const max = intMax(scalar_ty, target, &max_buf);
26062631
26072632 const ret = try f.allocLocal(inst_ty, .Mut);
2608 try w.writeAll(";");
2633 try w.writeByte(';');
26092634 try f.object.indent_writer.insertNewline();
26102635 try f.writeCValue(w, ret);
26112636
......@@ -2654,10 +2679,7 @@ fn airNot(f: *Function, inst: Air.Inst.Index) !CValue {
26542679 const local = try f.allocLocal(inst_ty, .Const);
26552680
26562681 try writer.writeAll(" = ");
2657 if (inst_ty.zigTypeTag() == .Bool)
2658 try writer.writeAll("!")
2659 else
2660 try writer.writeAll("~");
2682 try writer.writeByte(if (inst_ty.tag() == .bool) '!' else '~');
26612683 try f.writeCValue(writer, op);
26622684 try writer.writeAll(";\n");
26632685
......@@ -2868,7 +2890,7 @@ fn airCall(
28682890 try f.writeCValue(writer, callee);
28692891 }
28702892
2871 try writer.writeAll("(");
2893 try writer.writeByte('(');
28722894 var args_written: usize = 0;
28732895 for (args) |arg| {
28742896 const ty = f.air.typeOf(arg);
......@@ -2992,7 +3014,7 @@ fn lowerTry(
29923014 try writer.writeAll("if(");
29933015 }
29943016 try f.writeCValue(writer, err_union);
2995 try writer.writeAll(")");
3017 try writer.writeByte(')');
29963018 break :err;
29973019 }
29983020 if (operand_is_ptr or isByRef(err_union_ty)) {
......@@ -3066,7 +3088,7 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
30663088 try writer.writeAll(" = (");
30673089 try f.renderTypecast(writer, inst_ty);
30683090
3069 try writer.writeAll(")");
3091 try writer.writeByte(')');
30703092 try f.writeCValue(writer, operand);
30713093 try writer.writeAll(";\n");
30723094 return local;
......@@ -3797,7 +3819,7 @@ fn airPtrToInt(f: *Function, inst: Air.Inst.Index) !CValue {
37973819
37983820 try writer.writeAll(" = (");
37993821 try f.renderTypecast(writer, inst_ty);
3800 try writer.writeAll(")");
3822 try writer.writeByte(')');
38013823 try f.writeCValue(writer, operand);
38023824 try writer.writeAll(";\n");
38033825 return local;
......@@ -4212,7 +4234,7 @@ fn airNeg(f: *Function, inst: Air.Inst.Index) !CValue {
42124234 const inst_ty = f.air.typeOfIndex(inst);
42134235 const operand = try f.resolveInst(un_op);
42144236 const local = try f.allocLocal(inst_ty, .Const);
4215 try writer.writeAll("-");
4237 try writer.writeByte('-');
42164238 try f.writeCValue(writer, operand);
42174239 try writer.writeAll(";\n");
42184240 return local;