Skip to content

Commit

Permalink
support s[x..y] used in the gcc/clang backtrace expansion in builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman committed Jan 5, 2025
1 parent 503876e commit 5cd2aa6
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
9 changes: 3 additions & 6 deletions vlib/builtin/backtraces_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,10 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
return false
}
nr_actual_frames := nr_ptrs - skipframes
mut sframes := []string{}
//////csymbols := backtrace_symbols(*voidptr(&buffer[skipframes]), nr_actual_frames)
csymbols := C.backtrace_symbols(voidptr(&buffer[skipframes]), nr_actual_frames)
for i in 0 .. nr_actual_frames {
sframes << unsafe { tos2(&u8(csymbols[i])) }
}
for sframe in sframes {
sframe := unsafe { tos2(&u8(csymbols[i])) }
executable := sframe.all_before('(')
addr := sframe.all_after('[').all_before(']')
beforeaddr := sframe.all_before('[')
Expand All @@ -92,7 +89,7 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
output += tos(bp, vstrlen(bp))
}
}
output = output.trim_space() + ':'
output = output.trim_chars(' \t\n', .trim_both) + ':'
if C.pclose(f) != 0 {
eprintln(sframe)
continue
Expand All @@ -111,7 +108,7 @@ fn print_backtrace_skipping_top_frames_linux(skipframes int) bool {
eprint(' | ')
eprintln(beforeaddr)
}
if sframes.len > 0 {
if nr_actual_frames > 0 {
unsafe { C.free(csymbols) }
}
}
Expand Down
1 change: 1 addition & 0 deletions vlib/builtin/sorted_map.v
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ fn (mut n mapnode) split_child(child_index int, mut y mapnode) {
n.len++
}

@[direct_array_access]
fn (m SortedMap) get(key string, out voidptr) bool {
mut node := m.root
for {
Expand Down
3 changes: 2 additions & 1 deletion vlib/builtin/string.v
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,8 @@ pub fn (s string) substr_unsafe(start int, _end int) string {
pub fn (s string) substr_with_check(start int, _end int) !string {
end := if _end == max_int { s.len } else { _end } // max_int
if start > end || start > s.len || end > s.len || start < 0 || end < 0 {
return error('substr(${start}, ${end}) out of bounds (len=${s.len})')
return error('substr(' + start.str() + ', ' + end.str() + ') out of bounds (len=' +
s.len.str() + ')')
}
len := end - start
if len == s.len {
Expand Down
9 changes: 5 additions & 4 deletions vlib/builtin/utf8.v
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,15 @@ pub fn utf32_decode_to_buffer(code u32, mut buf &u8) int {
// it is used in vlib/builtin/string.v,
// and also in vlib/v/gen/c/cgen.v
pub fn (_rune string) utf32_code() int {
return int(_rune.bytes().utf8_to_utf32() or {
// error('more than one utf-8 rune found in this string')
rune(0)
})
if res := _rune.bytes().utf8_to_utf32() {
return int(res)
}
return 0
}

// convert array of utf8 bytes to single utf32 value
// will error if more than 4 bytes are submitted
@[direct_array_access]
pub fn (_bytes []u8) utf8_to_utf32() !rune {
if _bytes.len == 0 {
return 0
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/markused/markused.v
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,10 @@ pub fn mark_used(mut table ast.Table, mut pref_ pref.Preferences, ast_files []&a
}
}

if table.used_features.range_index {
walker.fn_by_name(string_idx_str + '.substr')
}

table.used_features.used_fns = walker.used_fns.move()
table.used_features.used_consts = walker.used_consts.move()
table.used_features.used_globals = walker.used_globals.move()
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/markused/walker.v
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,10 @@ fn (mut w Walker) expr(node_ ast.Expr) {
w.features.used_maps++
} else if sym.kind == .array {
w.features.used_arrays++
} else if sym.kind == .string {
if node.index is ast.RangeExpr {
w.features.range_index = true
}
}
}
ast.InfixExpr {
Expand Down

0 comments on commit 5cd2aa6

Please sign in to comment.