1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: -autofree fixes for []string{} having literals, comming from defailt string stuct field values

This commit is contained in:
Delyan Angelov 2021-03-23 18:54:37 +02:00
parent 7101d53d74
commit 8abfe2f5a2
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 33 additions and 13 deletions

View File

@ -62,10 +62,13 @@ fn new_aints(ovals []int, extreme_mins int, extreme_maxs int) Aints {
mut imin := math.max_i32
mut imax := -math.max_i32
// discard the extremes:
mut vals := ovals.clone()
mut vals := []int{}
for x in ovals {
vals << x
}
vals.sort()
if vals.len > extreme_mins + extreme_maxs {
vals = vals[extreme_mins..vals.len - extreme_maxs]
vals = vals[extreme_mins..vals.len - extreme_maxs].clone()
} else {
vals = []
}
@ -152,7 +155,10 @@ fn (mut context Context) parse_options() {
exit(1)
}
context.commands = context.expand_all_commands(commands)
context.results = []CmdResult{len: context.commands.len, init: CmdResult{}}
context.results = []CmdResult{len: context.commands.len, cap: 100, init: CmdResult{
outputs: []string{cap: 1000}
timings: []int{cap: 1000}
}}
if context.use_newline {
context.cline = '\n'
context.cgoback = '\n'
@ -170,7 +176,8 @@ fn (mut context Context) expand_all_commands(commands []string) []string {
mut all_commands := []string{}
for cmd in commands {
maincmd := context.cmd_template.replace('{T}', cmd)
mut substituted_commands := [maincmd]
mut substituted_commands := []string{}
substituted_commands << maincmd
for paramk, paramlist in context.cmd_params {
for paramv in paramlist {
mut new_substituted_commands := []string{}
@ -178,10 +185,14 @@ fn (mut context Context) expand_all_commands(commands []string) []string {
scmd := cscmd.replace(paramk, paramv)
new_substituted_commands << scmd
}
substituted_commands << new_substituted_commands
for sc in new_substituted_commands {
substituted_commands << sc
}
}
}
all_commands << substituted_commands
for sc in substituted_commands {
all_commands << sc
}
}
mut unique := map[string]int{}
for x in all_commands {
@ -228,8 +239,10 @@ fn (mut context Context) run() {
eprintln('${i:10} non 0 exit code for cmd: $cmd')
continue
}
context.results[icmd].outputs << res.output.trim_right('\r\n').replace('\r\n',
'\n').split('\n')
trimed_output := res.output.trim_right('\r\n')
trimed_normalized := trimed_output.replace('\r\n', '\n')
lines := trimed_normalized.split('\n')
context.results[icmd].outputs << lines
context.results[icmd].timings << duration
sum += duration
runs++
@ -296,10 +309,12 @@ fn (mut context Context) show_diff_summary() {
println('Summary (commands are ordered by ascending mean time), after $context.series series of $context.count repetitions:')
base := context.results[0].atiming.average
mut first_cmd_percentage := f64(100.0)
mut first_marker := ''
for i, r in context.results {
first_marker = ' '
cpercent := (r.atiming.average / base) * 100 - 100
first_marker := if r.icmd == 0 { util.bold('>') } else { ' ' }
if r.icmd == 0 {
first_marker = util.bold('>')
first_cmd_percentage = cpercent
}
println(' $first_marker${(i + 1):3} | ${cpercent:5.1f}% slower | ${r.cmd:-57s} | $r.atiming')

View File

@ -5715,7 +5715,7 @@ fn (mut g Gen) type_default(typ_ table.Type) string {
}
*/
match sym.name {
'string' { return '(string){.str=(byteptr)""}' }
'string' { return '(string){.str=(byteptr)"", .is_lit=1}' }
'rune' { return '0' }
else {}
}

View File

@ -478,9 +478,10 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
if node.receiver_type == 0 {
g.checker_bug('CallExpr.receiver_type is 0 in method_call', node.pos)
}
typ_sym := g.table.get_type_symbol(g.unwrap_generic(node.receiver_type))
mut receiver_type_name := util.no_dots(g.cc_type(g.unwrap_generic(node.receiver_type),
false))
unwrapped_rec_type := g.unwrap_generic(node.receiver_type)
typ_sym := g.table.get_type_symbol(unwrapped_rec_type)
rec_cc_type := g.cc_type(unwrapped_rec_type, false)
mut receiver_type_name := util.no_dots(rec_cc_type)
if typ_sym.kind == .interface_ && (typ_sym.info as table.Interface).defines_method(node.name) {
// Speaker_name_table[s._interface_idx].speak(s._object)
$if debug_interface_method_call ? {
@ -578,6 +579,10 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
// && rec_sym.name == 'array' && receiver_name.starts_with('array') {
// `array_byte_clone` => `array_clone`
receiver_type_name = 'array'
if false && node.name == 'free' && typ_sym.has_method(node.name) {
// TODO: allow for more specific overrides of array .free() like `pub fn (x []string) free() {`
receiver_type_name = g.typ(unwrapped_rec_type).trim('*')
}
if node.name in ['last', 'first', 'pop'] {
return_type_str := g.typ(node.return_type)
has_cast = true