Skip to content

Commit 18e558f

Browse files
authored
Handle empty backtraces (#185)
Fixes #184
1 parent 1737fbb commit 18e558f

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/backedges.jl

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# A lightly-modified version of the same function in Base
22
# Highlights argument types with color specified by highlighter(typ)
3-
function show_tuple_as_call(@nospecialize(highlighter), io::IO, name::Symbol, @nospecialize(sig::Type), demangle=false, kwargs=nothing)
3+
function show_tuple_as_call(@nospecialize(highlighter), io::IO, name::Symbol, @nospecialize(sig::Type), demangle=false #=, kwargs=nothing =#)
44
if sig === Tuple
55
print(io, demangle ? Base.demangle_function_name(name) : name, "(...)")
66
return
@@ -34,16 +34,16 @@ function show_tuple_as_call(@nospecialize(highlighter), io::IO, name::Symbol, @n
3434
first = false
3535
printstyled(env_io, "::", sig[i], color=highlighter(sig[i]))
3636
end
37-
if kwargs !== nothing
38-
print(io, "; ")
39-
first = true
40-
for (k, t) in kwargs
41-
first || print(io, ", ")
42-
first = false
43-
print(io, k, "::")
44-
show(io, t)
45-
end
46-
end
37+
# if kwargs !== nothing
38+
# print(io, "; ")
39+
# first = true
40+
# for (k, t) in kwargs
41+
# first || print(io, ", ")
42+
# first = false
43+
# print(io, k, "::")
44+
# show(io, t)
45+
# end
46+
# end
4747
print(io, ")")
4848
Base.show_method_params(io, tv)
4949
nothing
@@ -89,22 +89,23 @@ nextnode(mi, edge) = edge
8989
instance(sfs::Vector{StackTraces.StackFrame}) = sfs[end].linfo
9090
method(sfs::Vector{StackTraces.StackFrame}) = method(instance(sfs))
9191

92-
instance(ipframes::Vector{IPFrames}) = instance(ipframes[1].sfs)
92+
instance(ipframes::Vector{IPFrames}) = isempty(ipframes) ? nothing : instance(ipframes[1].sfs)
9393
backedges(ipframes::Vector{IPFrames}) = (ret = ipframes[2:end]; isempty(ret) ? () : (ret,))
9494

9595
function callstring(io, mi)
9696
show_tuple_as_call(nonconcrete_red, IOContext(io, :color=>true), method(mi).name, specTypes(mi))
9797
return String(take!(io))
9898
end
9999
function callstring(io, sfs::Vector{StackTraces.StackFrame})
100+
isempty(sfs) && return ""
100101
for i = 1:length(sfs)-1
101102
sf = sfs[i]
102103
print(io, sf.func, " at ", sf.file, ':', sf.line, " => ")
103104
end
104105
sf = sfs[end]
105106
return callstring(io, instance(sfs)) * string(" at ", sf.file, ':', sf.line)
106107
end
107-
callstring(io, ipframes::Vector{IPFrames}) = callstring(io, ipframes[1].sfs)
108+
callstring(io, ipframes::Vector{IPFrames}) = isempty(ipframes) ? "" : callstring(io, ipframes[1].sfs)
108109

109110
struct Data{T}
110111
callstr::String

test/runtests.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,27 @@ fst5(x) = fst4(x)
411411
root = Cthulhu.treelist(mi)
412412
@test occursin("Vararg", root.data.callstr)
413413

414+
# Test highlighting and other printing
415+
mi = Cthulhu.get_specialization(:, Tuple{T, T} where T<:Integer)
416+
root = Cthulhu.treelist(mi)
417+
@test occursin("\e[31m::T\e[39m", root.data.callstr)
418+
mi = Cthulhu.get_specialization(Vector{Int}, Tuple{typeof(undef), Int})
419+
io = IOBuffer()
420+
@test Cthulhu.callstring(io, mi) == "Vector{$Int}(::UndefInitializer, ::$Int)"
421+
mi = Cthulhu.get_specialization(similar, Tuple{Type{Vector{T}}, Dims{1}} where T)
422+
@test occursin(r"31m::Type", Cthulhu.callstring(io, mi))
423+
414424
# treelist for stacktraces
415425
tree = Cthulhu.treelist(fst5(1.0))
416426
@test match(r"fst1 at .*:\d+ => fst2 at .*:\d+ => fst3\(::Float64\) at .*:\d+", tree.data.callstr) !== nothing
417427
@test length(tree.children) == 1
418428
child = tree.children[1]
419429
@test match(r" fst4 at .*:\d+ => fst5\(::Float64\) at .*:\d+", child.data.callstr) !== nothing
430+
431+
# issue #184
432+
tree = Cthulhu.treelist(similar(fst5(1.0), 0))
433+
@test isempty(tree.data.callstr)
434+
@test isempty(Cthulhu.callstring(io, similar(stacktrace(fst5(1.0)), 0)))
420435
end
421436

422437
@testset "ascend" begin

0 commit comments

Comments
 (0)