Skip to content

Commit 227bd4b

Browse files
authored
Improve robustness of framecode_lines (#410)
1 parent 7796df8 commit 227bd4b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/utils.jl

+19
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,25 @@ end
392392

393393
function framecode_lines(src::CodeInfo)
394394
buf = IOBuffer()
395+
if isdefined(Base.IRShow, :show_ir_stmt)
396+
lines = String[]
397+
src = replace_coretypes!(copy_codeinfo(src); rev=true)
398+
reverse_lookup_globalref!(src.code)
399+
io = IOContext(buf, :displaysize=>displaysize(stdout))
400+
used = BitSet()
401+
cfg = Core.Compiler.compute_basic_blocks(src.code)
402+
for stmt in src.code
403+
Core.Compiler.scan_ssa_use!(push!, used, stmt)
404+
end
405+
line_info_preprinter = Base.IRShow.lineinfo_disabled
406+
line_info_postprinter = Base.IRShow.default_expr_type_printer
407+
bb_idx = 1
408+
for idx = 1:length(src.code)
409+
bb_idx = Base.IRShow.show_ir_stmt(io, src, idx, line_info_preprinter, line_info_postprinter, used, cfg, bb_idx)
410+
push!(lines, chomp(String(take!(buf))))
411+
end
412+
return lines
413+
end
395414
show(buf, src)
396415
code = filter!(split(String(take!(buf)), '\n')) do line
397416
!(line == "CodeInfo(" || line == ")" || isempty(line) || occursin("within `", line))

test/core.jl

+17
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,21 @@ using Test
66
@test !JuliaInterpreter.is_quoted_type(QuoteNode(Int32), :Int64)
77
@test !JuliaInterpreter.is_quoted_type(QuoteNode(Int32(0)), :Int32)
88
@test !JuliaInterpreter.is_quoted_type(Int32, :Int32)
9+
10+
function buildexpr()
11+
items = [7, 3]
12+
ex = quote
13+
X = $items
14+
for x in X
15+
println(x)
16+
end
17+
end
18+
return ex
19+
end
20+
frame = JuliaInterpreter.enter_call(buildexpr)
21+
lines = JuliaInterpreter.framecode_lines(frame.framecode.src)
22+
# Test that the :copyast ends up on the same line as the println
23+
if isdefined(Base.IRShow, :show_ir_stmt) # only works on Julia 1.6 and higher
24+
@test any(str->occursin(":copyast", str) && occursin("println", str), lines)
25+
end
926
end

0 commit comments

Comments
 (0)