Commit 531e5676
531e56766672Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): cast away volatile when passing main() locals to typed params
Closes #487. Top-level begin/rescue sets @needs_setjmp = 1, which
makes main()'s pointer locals get declared as volatile T *. When
those locals are then passed to a function whose param is plain
T *, gcc's -Wdiscarded-qualifiers (clang's
-Wincompatible-pointer-types-discards-qualifiers) flags the call.
The program still ran correctly because volatile is strictly
stronger than the callee asks for, but clean -Wall builds tripped
the warning.
Generalises the existing bigint-side cast-away-volatile pattern.
A new helper cast_away_volatile_arg(arg_id, val) returns
(T *)val when the enclosing scope uses setjmp AND the arg is a
LocalVariableReadNode whose declared type is a pointer; otherwise
returns val untouched. The helper is wrapped around compile_expr
results in compile_call_args, compile_gc_safe_args, and the
positional-arg paths of compile_call_args_with_defaults.
The cast is cosmetic: same C semantics, just discards the stronger qualifier the callee doesn't need.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 531e56766672
- Parent
- ab2c59d09229
spinel_codegen.rbmodifiedView file
# the caller is more general than the callee) is unboxed via # the matching union field. Mirrors the #438 IntStrHash-key # unbox and extends it to user-method args. result = result + compile_expr_for_expected_type(positional_ids[k], ptypes[k]) result = result + cast_away_volatile_arg(positional_ids[k], compile_expr_for_expected_type(positional_ids[k], ptypes[k])) else result = result + compile_expr(positional_ids[k]) result = result + cast_away_volatile_arg(positional_ids[k], compile_expr(positional_ids[k])) end else # Use default value if k > 0 result = result + ", " end result = result + compile_expr(arg_ids[k]) result = result + cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k])) k = k + 1 end result end # When the enclosing function uses setjmp (top-level begin/rescue), # every pointer LV in main() is declared `volatile T *`. Passing # such a local to a function that takes plain `T *` discards the # volatile qualifier — gcc -Wdiscarded-qualifiers and clang's # -Wincompatible-pointer-types-discards-qualifiers flag it. Casting # away volatile is safe because the qualifier is strictly stronger # than what the callee asks for. Mirrors the bigint-side cast at # the operator dispatch (search for `(sp_Bigint *)`). def cast_away_volatile_arg(arg_id, val) if @needs_setjmp != 1 return val end if arg_id < 0 return val end if @nd_type[arg_id] != "LocalVariableReadNode" return val end vt = find_var_declared_type(@nd_name[arg_id]) if vt == "" || type_is_pointer(vt) != 1 return val end "(" + c_type(vt) + ")" + val end def compile_proc_call_args(nid) args_id = @nd_arguments[nid] if args_id < 0 tmp = new_temp at = infer_type(arg_ids[k]) ct = c_type(at) emit(" " + ct + " " + tmp + " = " + compile_expr(arg_ids[k]) + ";") emit(" " + ct + " " + tmp + " = " + cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k])) + ";") if type_is_pointer(at) == 1 emit(" SP_GC_ROOT(" + tmp + ");") end temps.push(tmp) else temps.push(compile_expr(arg_ids[k])) temps.push(cast_away_volatile_arg(arg_ids[k], compile_expr(arg_ids[k]))) end k = k + 1 endtest/volatile_discarded_at_call.rbaddedView file
Loading diff…
test/volatile_discarded_at_call.rb.expectedaddedView file
Loading diff…