Commit 690abf09

690abf0987ccMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified

Description

fix: remove unused variables and dead code in generated C

  • Remove unused method: new_label
  • Remove unused variables: actual_init_idx, struct_kw_init, mi (3 places), changed, has_reader, saved, cname (2 places), defval, arg, is_splat_call, at_guess, bp1
  • Guard sp_last_status with @needs_system flag (separate from @needs_file_io)

Generated C warnings: 106 → 46 (only unused-function remains). return-type: 0, unused-variable: 0, unused-but-set: 0.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
690abf0987cc
Parent
be009f4f03d7

spinel_codegen.rbmodifiedView file

@@ -122,6 +122,7 @@
122122
123123 # Feature flags
124124 @needs_gc = 0
125+ @needs_system = 0
125126 @needs_int_array = 0
126127 @needs_float_array = 0
127128 @needs_str_array = 0
@@ -198,11 +199,6 @@
198199 def new_temp
199200 @temp_counter = @temp_counter + 1
200201 "_t" + @temp_counter.to_s
201- end
202-
203- def new_label
204- @label_counter = @label_counter + 1
205- "_L" + @label_counter.to_s
206202 end
207203
208204 # ---- AST reader ----
@@ -2371,7 +2367,6 @@
23712367 # Add as top-level method with prefix
23722368 mname = @nd_name[sid]
23732369 # Store with special naming for lookup
2374- mi = @meth_names.length
23752370 @meth_names.push("__oc_" + cname + "_" + mname)
23762371 params = collect_params_str(sid)
23772372 @meth_param_names.push(params)
@@ -2389,7 +2384,6 @@
23892384 parent = ""
23902385 sp = @nd_superclass[nid]
23912386 struct_fields = "".split(",")
2392- struct_kw_init = 0
23932387 if sp >= 0
23942388 if @nd_type[sp] == "CallNode"
23952389 if @nd_name[sp] == "new"
@@ -2411,7 +2405,7 @@
24112405 struct_fields.push(fname)
24122406 end
24132407 if @nd_type[sarg_ids[sk]] == "KeywordHashNode"
2414- struct_kw_init = 1
2408+ # keyword_init detected
24152409 end
24162410 sk = sk + 1
24172411 end
@@ -3254,7 +3248,6 @@
32543248 if @nd_type[@nd_receiver[sid]] == "SelfNode"
32553249 dmname = @nd_name[sid]
32563250 # Create as top-level method with module prefix for dispatch
3257- mi = @meth_names.length
32583251 @meth_names.push(mname + "_cls_" + dmname)
32593252 @meth_param_names.push(collect_params_str(sid))
32603253 @meth_param_types.push(collect_ptypes_str(sid, -1))
@@ -3837,7 +3830,6 @@
38373830 all_params = @cls_meth_params[oci].split("|")
38383831 all_ptypes = @cls_meth_ptypes[oci].split("|")
38393832 bodies = @cls_meth_bodies[oci].split(";")
3840- changed = 0
38413833 j = 0
38423834 while j < mnames.length
38433835 if mnames[j] != "initialize"
@@ -3899,7 +3891,6 @@
38993891 ptypes[pk] = "obj_" + @cls_names[ci2]
39003892 all_ptypes[j] = ptypes.join(",")
39013893 @cls_meth_ptypes[oci] = all_ptypes.join("|")
3902- changed = 1
39033894 end
39043895 end
39053896 ci2 = ci2 + 1
@@ -5345,6 +5336,7 @@
53455336 end
53465337 if mname == "system"
53475338 @needs_file_io = 1
5339+ @needs_system = 1
53485340 end
53495341 if @nd_receiver[nid] >= 0
53505342 if @nd_type[@nd_receiver[nid]] == "ConstantReadNode"
@@ -6137,7 +6129,9 @@
61376129 emit_raw("static const char*sp_int_chr(mrb_int n){char*s=(char*)malloc(2);s[0]=(char)n;s[1]=0;return s;}")
61386130 emit_raw("typedef struct{mrb_int first;mrb_int last;}sp_Range;")
61396131 emit_raw("static sp_Range sp_range_new(mrb_int f,mrb_int l){sp_Range r;r.first=f;r.last=l;return r;}")
6140- emit_raw("static int sp_last_status = 0;")
6132+ if @needs_system == 1
6133+ emit_raw("static int sp_last_status = 0;")
6134+ end
61416135 emit_raw("")
61426136 end
61436137
@@ -6758,16 +6752,6 @@
67586752 bid = bodies[mi].to_i
67596753 if is_simple_writer_method(mn, bid) == 1
67606754 append_attr_writer(i, bname)
6761- # Also ensure the corresponding reader exists
6762- readers = @cls_attr_readers[i].split(";")
6763- has_reader = 0
6764- ri = 0
6765- while ri < readers.length
6766- if readers[ri] == bname
6767- has_reader = 1
6768- end
6769- ri = ri + 1
6770- end
67716755 end
67726756 end
67736757 end
@@ -6991,8 +6975,6 @@
69916975
69926976 def recalc_needs_gc
69936977 # Recalculate @needs_gc: only needed if non-value-type classes are used
6994- # Save and reset, then re-enable if needed
6995- saved = @needs_gc
69966978 @needs_gc = 0
69976979 # Non-value-type class exists → GC needed
69986980 i = 0
@@ -7664,10 +7646,6 @@
76647646 end
76657647
76667648 init_ci = find_init_class(ci)
7667- actual_init_idx = -1
7668- if init_ci >= 0
7669- actual_init_idx = cls_find_method_direct(init_ci, "initialize")
7670- end
76717649
76727650 if init_idx >= 0
76737651 bodies = @cls_meth_bodies[ci].split(";")
@@ -9560,7 +9538,6 @@
95609538 cidx = cls_find_method(@current_class_idx, mname)
95619539 if cidx >= 0
95629540 ca = compile_call_args(nid)
9563- cname = @cls_names[@current_class_idx]
95649541 owner = find_method_owner(@current_class_idx, mname)
95659542 if ca != ""
95669543 return "sp_" + owner + "_" + sanitize_name(mname) + "(self, " + ca + ")"
@@ -9884,7 +9861,6 @@
98849861 aargs = get_args(args_id)
98859862 if aargs.length >= 1
98869863 # Hash.new(default_val) - check type
9887- defval = compile_expr(aargs.first)
98889864 dt = infer_type(aargs.first)
98899865 if dt == "string"
98909866 @needs_str_str_hash = 1
@@ -10426,7 +10402,6 @@
1042610402 if mname == "zip"
1042710403 # zip returns array of pairs; for length-only usage, return array with same length
1042810404 tmp = new_temp
10429- arg = compile_arg0(nid)
1043010405 emit(" sp_IntArray *" + tmp + " = sp_IntArray_new();")
1043110406 emit(" for (mrb_int _i = 0; _i < sp_IntArray_length(" + rc + "); _i++) sp_IntArray_push(" + tmp + ", sp_IntArray_get(" + rc + ", _i));")
1043210407 return tmp
@@ -11135,7 +11110,6 @@
1113511110 i = 0
1113611111 while i < @cls_names.length
1113711112 cname = @cls_names[i]
11138- mi = cls_find_method(@cls_names.length - 1 - i, mname)
1113911113 # Check if this class has the method
1114011114 midx = cls_find_method_direct(i, mname)
1114111115 if midx >= 0
@@ -11245,7 +11219,6 @@
1124511219 kw_names = "".split(",")
1124611220 kw_vals = "".split(",")
1124711221 positional_ids = []
11248- is_splat_call = 0
1124911222 ak = 0
1125011223 while ak < arg_ids.length
1125111224 if @nd_type[arg_ids[ak]] == "KeywordHashNode"
@@ -11287,7 +11260,6 @@
1128711260 if k < ptypes.length
1128811261 if ptypes[k] == "poly"
1128911262 # Need to box - kw_vals[ki] is already compiled
11290- at_guess = "string"
1129111263 result = result + "sp_box_str(" + kw_vals[ki] + ")"
1129211264 else
1129311265 result = result + kw_vals[ki]
@@ -14071,8 +14043,6 @@
1407114043 # catch(:tag) do ... end
1407214044 tag = compile_arg0(nid)
1407314045 blk = @nd_block[nid]
14074- bp1 = get_block_param(nid, 0)
14075-
1407614046 emit(" sp_catch_tag[sp_catch_top] = " + tag + ";")
1407714047 emit(" sp_catch_top++;")
1407814048 emit(" if (setjmp(sp_catch_stack[sp_catch_top-1]) == 0) {")
@@ -14624,7 +14594,6 @@
1462414594 saved_ci = @current_class_idx
1462514595 @current_class_idx = cci
1462614596
14627- cname = @cls_names[cci]
1462814597 @block_counter = @block_counter + 1
1462914598 suffix = "_y" + @block_counter.to_s
1463014599