Skip to content

Commit cecf599

Browse files
authored
add option to hide type annotations (#234)
* add option to hide type annotations The motivating case here is that Diffractor often generates complex and deeply nested lattice elements, so hiding them can make the code easier to read. * fix toggle * improve coverage
1 parent ba4a116 commit cecf599

File tree

70 files changed

+929
-36
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+929
-36
lines changed

src/Cthulhu.jl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
231231
override::Union{Nothing,InferenceResult}=nothing, debuginfo::Union{Symbol,DebugInfo}=DInfo.compact, # default is compact debuginfo
232232
params=current_params(), optimize::Bool=true, interruptexc::Bool=true,
233233
iswarn::Bool=false, hide_type_stable::Union{Nothing,Bool}=nothing, verbose::Union{Nothing,Bool}=nothing,
234-
remarks::Bool=false, inline_cost::Bool=false)
234+
remarks::Bool=false, inline_cost::Bool=false, type_annotations::Bool=true)
235235
if isnothing(hide_type_stable)
236236
hide_type_stable = something(verbose, false)
237237
end
@@ -287,11 +287,11 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
287287
if debuginfo == DInfo.compact
288288
str = let debuginfo=debuginfo, codeinf=codeinf, rt=rt, mi=mi,
289289
iswarn=iswarn, hide_type_stable=hide_type_stable,
290-
remarks=_remarks, inline_cost=inline_cost
290+
remarks=_remarks, inline_cost=inline_cost, type_annotations=type_annotations
291291
stringify() do io # eliminate trailing indentation (see first item in bullet list in PR #189)
292292
cthulhu_typed(io, debuginfo, codeinf, rt, mi;
293293
iswarn, hide_type_stable,
294-
remarks, inline_cost)
294+
remarks, inline_cost, type_annotations)
295295
end
296296
end
297297
rmatch = findfirst(r"\u001B\[90m\u001B\[(\d+)G( *)\u001B\[1G\u001B\[39m\u001B\[90m( *)\u001B\[39m$", str)
@@ -302,15 +302,15 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
302302
else
303303
cthulhu_typed(term.out_stream::IO, debuginfo, codeinf, rt, mi;
304304
iswarn, hide_type_stable,
305-
remarks=_remarks, inline_cost)
305+
remarks=_remarks, inline_cost, type_annotations)
306306
end
307307
view_cmd = cthulhu_typed
308308
end
309309
display_CI = true
310310
end
311311

312312
menu = CthulhuMenu(callsites, optimize, iswarn&get(term.out_stream::IO, :color, false)::Bool; menu_options...)
313-
usg = usage(view_cmd, optimize, iswarn, hide_type_stable, debuginfo, remarks, inline_cost, CONFIG.enable_highlighter)
313+
usg = usage(view_cmd, optimize, iswarn, hide_type_stable, debuginfo, remarks, inline_cost, type_annotations, CONFIG.enable_highlighter)
314314
cid = request(term, usg, menu)
315315
toggle = menu.toggle
316316

@@ -354,7 +354,7 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
354354
debuginfo,
355355
params, optimize, interruptexc,
356356
iswarn, hide_type_stable,
357-
remarks, inline_cost)
357+
remarks, inline_cost, type_annotations)
358358
continue
359359
end
360360

@@ -372,7 +372,7 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
372372
override = isa(info, ConstPropCallInfo) ? info.result : nothing, debuginfo,
373373
params,optimize, interruptexc,
374374
iswarn, hide_type_stable,
375-
remarks, inline_cost)
375+
remarks, inline_cost, type_annotations)
376376

377377
elseif toggle === :warn
378378
iswarn ⊻= true
@@ -396,6 +396,8 @@ function _descend(term::AbstractTerminal, interp::CthulhuInterpreter, mi::Method
396396
if inline_cost && !optimize
397397
@warn "enable optimization to see the inlining costs"
398398
end
399+
elseif toggle === :type_annotations
400+
type_annotations ⊻= true
399401
elseif toggle === :highlighter
400402
CONFIG.enable_highlighter ⊻= true
401403
if CONFIG.enable_highlighter

src/codeview.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ cthulhu_typed(io::IO, debuginfo::DebugInfo, args...; kwargs...) =
107107
function cthulhu_typed(io::IO, debuginfo::Symbol,
108108
src::Union{CodeInfo,IRCode}, @nospecialize(rt), mi::Union{Nothing,MethodInstance};
109109
iswarn::Bool=false, hide_type_stable::Bool=false,
110-
remarks::Union{Nothing,Remarks}=nothing, inline_cost::Bool=false)
110+
remarks::Union{Nothing,Remarks}=nothing, inline_cost::Bool=false,
111+
type_annotations::Bool=true)
112+
111113
debuginfo = IRShow.debuginfo(debuginfo)
112114
lineprinter = __debuginfo[debuginfo]
113115
rettype = ignorelimited(rt)
@@ -159,7 +161,11 @@ function cthulhu_typed(io::IO, debuginfo::Symbol,
159161
preprinter = lineprinter(src)
160162
end
161163
# postprinter configuration
162-
_postprinter = iswarn ? InteractiveUtils.warntype_type_printer : IRShow.default_expr_type_printer
164+
_postprinter = if type_annotations
165+
iswarn ? InteractiveUtils.warntype_type_printer : IRShow.default_expr_type_printer
166+
else
167+
Returns(nothing)
168+
end
163169
if !isnothing(remarks)
164170
function postprinter(io::IO, @nospecialize(typ), used::Bool)
165171
_postprinter(io, typ, used)

src/ui.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function stringify(@nospecialize(f), io::IO=IOBuffer())
4646
end
4747

4848
const debugcolors = (:nothing, :light_black, :yellow)
49-
function usage(@nospecialize(view_cmd), optimize, iswarn, hide_type_stable, debuginfo, remarks, inline_cost, highlight)
49+
function usage(@nospecialize(view_cmd), optimize, iswarn, hide_type_stable, debuginfo, remarks, inline_cost, type_annotations, highlight)
5050
colorize(iotmp, use_color::Bool, c::Char) = stringify(iotmp) do io
5151
use_color ? printstyled(io, c; color=:cyan) : print(io, c)
5252
end
@@ -64,6 +64,7 @@ function usage(@nospecialize(view_cmd), optimize, iswarn, hide_type_stable, debu
6464
end, "]ebuginfo, [",
6565
colorize(iotmp, remarks, 'r'), "]emarks, [",
6666
colorize(iotmp, inline_cost, 'i'), "]nlining costs, [",
67+
colorize(iotmp, type_annotations, 't'), "]ype annotations, [",
6768
colorize(iotmp, highlight, 's'), "]yntax highlight for Source/LLVM/Native.")
6869
println(ioctx, "Show: [",
6970
colorize(iotmp, view_cmd === cthulhu_source, 'S'), "]ource code, [",
@@ -98,6 +99,9 @@ function TerminalMenus.keypress(m::CthulhuMenu, key::UInt32)
9899
elseif key == UInt32('i')
99100
m.toggle = :inline_cost
100101
return true
102+
elseif key == UInt32('t')
103+
m.toggle = :type_annotations
104+
return true
101105
elseif key == UInt32('s')
102106
m.toggle = :highlighter
103107
return true

test/codeview.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@ Revise.track(CthulhuTestSandbox, normpath(@__DIR__, "sandbox.jl"))
2727
@testset "iswarn: $iswarn" for iswarn in tf
2828
@testset "hide_type_stable: $hide_type_stable" for hide_type_stable in tf
2929
@testset "inline_cost: $inline_cost" for inline_cost in tf
30-
io = IOBuffer()
31-
Cthulhu.cthulhu_typed(io, debuginfo,
32-
src, rt, mi;
33-
iswarn, hide_type_stable, inline_cost)
34-
@test !isempty(String(take!(io))) # just check it works
30+
@testset "type_annotations: $type_annotations" for type_annotations in tf
31+
io = IOBuffer()
32+
Cthulhu.cthulhu_typed(io, debuginfo,
33+
src, rt, mi;
34+
iswarn, hide_type_stable, inline_cost, type_annotations)
35+
@test !isempty(String(take!(io))) # just check it works
36+
end
3537
end
3638
end
3739
end

test/irshow.jl

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,24 @@ end
2525
@testset "iswarn: $iswarn" for iswarn in tf
2626
@testset "hide_type_stable: $hide_type_stable" for hide_type_stable in tf
2727
@testset "inline_cost: $inline_cost" for inline_cost in tf
28-
!optimize && debuginfo === Cthulhu.DInfo.compact && continue
29-
!optimize && inline_cost && continue
28+
@testset "type_annotations: $type_annotations" for type_annotations in tf
29+
!optimize && debuginfo === Cthulhu.DInfo.compact && continue
30+
!optimize && inline_cost && continue
3031

31-
s = sprint(; context=:color=>true) do io
32-
Cthulhu.cthulhu_typed(io, debuginfo,
33-
src, rt, mi;
34-
iswarn, hide_type_stable, inline_cost)
35-
end
36-
s = strip_base_linenums(s)
32+
s = sprint(; context=:color=>true) do io
33+
Cthulhu.cthulhu_typed(io, debuginfo,
34+
src, rt, mi;
35+
iswarn, hide_type_stable, inline_cost, type_annotations)
36+
end
37+
s = strip_base_linenums(s)
3738

38-
ground_truth = read(irshow_filename(optimize, debuginfo, iswarn, hide_type_stable, inline_cost), String)
39-
if Sys.iswindows()
40-
ground_truth = replace(ground_truth, "\r\n" => "\n")
39+
ground_truth = read(irshow_filename(optimize, debuginfo, iswarn, hide_type_stable, inline_cost, type_annotations), String)
40+
if Sys.iswindows()
41+
ground_truth = replace(ground_truth, "\r\n" => "\n")
42+
end
43+
@test s == ground_truth
44+
s != ground_truth && println(deepdiff(s, ground_truth))
4145
end
42-
@test s == ground_truth
43-
s != ground_truth && println(deepdiff(s, ground_truth))
4446
end
4547
end
4648
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
│ ─ %-1 = invoke foo(::Int64,::Int64)::Any
2+
││ │  0 %2 = %new(%1)
3+
│  4 ┄─ 0 %9 = Core.getfield(%2, :contents)
4+
│  │  1000 %10 = (%9 < 4)
5+
│  8 ┄─ 0 %16 = Core.getfield(%2, :contents)
6+
│  │  1000 %17 = (%16 + 1)
7+
│  │  20 %20 = invoke %19(_2::Int64)
8+
│  14 ┄ 0 %30 = φ (#10 => $(QuoteNode(missing)), #12 => %26)
9+
│ 8 │  1000 %31 = (%20 + %30)
10+
  
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
│ ─ %-1 = invoke foo(::Int64,::Int64)::Any
2+
││ │ %2 = %new(%1)
3+
│  4 ┄─ %9 = Core.getfield(%2, :contents)
4+
│  │ %10 = (%9 < 4)
5+
│  8 ┄─ %16 = Core.getfield(%2, :contents)
6+
│  │ %17 = (%16 + 1)
7+
│  │ %20 = invoke %19(_2::Int64)
8+
│  14 ┄ %30 = φ (#10 => $(QuoteNode(missing)), #12 => %26)
9+
│ 8 │ %31 = (%20 + %30)
10+
  
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
│ ─ %-1 = invoke foo(::Int64,::Int64)::Any
2+
│╻ Box2 1 ── 0 %1 = Core.Box
3+
││ │  0 %2 = %new(%1)
4+
│╻ + │  1 %3 = Base.add_int(_2, _3)
5+
│  │  3 Core.setfield!(%2, :contents, %3)
6+
│ 3 │  1 %5 = Core.isdefined(%2, :contents)
7+
│  └─── 0 goto #3 if not %5
8+
│  2 ── 0 goto #4
9+
│  3 ── 0 $(Expr(:throw_undef_if_not, :z, false))
10+
│  4 ┄─ 0 %9 = Core.getfield(%2, :contents)
11+
│  │  1000 %10 = (%9 < 4)
12+
│  └─── 0 goto #9 if not %10
13+
│ 4 5 ── 1 %12 = Core.isdefined(%2, :contents)
14+
│  └─── 0 goto #7 if not %12
15+
│  6 ── 0 goto #8
16+
│  7 ── 0 $(Expr(:throw_undef_if_not, :z, false))
17+
│  8 ┄─ 0 %16 = Core.getfield(%2, :contents)
18+
│  │  1000 %17 = (%16 + 1)
19+
│  └─── 3 Core.setfield!(%2, :contents, %17)
20+
│ 6 9 ┄─ 0 %19 = %new(Main.anonymous.:(var"#1#2"), %2)
21+
│  │  20 %20 = invoke %19(_2::Int64)
22+
│ 7 │  1 %21 = (isa)(_2, Missing)
23+
│  └─── 0 goto #11 if not %21
24+
│  10 ─ 0 goto #14
25+
│  11 ─ 1 %24 = (isa)(_2, Int64)
26+
│  └─── 0 goto #13 if not %24
27+
│╻ + 12 ─ 1 %26 = Base.add_int(_2, _3)
28+
│  └─── 0 goto #14
29+
│  13 ─ 0 Core.throw(ErrorException("fatal error in type inference (type bound)"))
30+
│  └─── 0 unreachable
31+
│  14 ┄ 0 %30 = φ (#10 => $(QuoteNode(missing)), #12 => %26)
32+
│ 8 │  1000 %31 = (%20 + %30)
33+
│  └─── 0 return %31
34+
  
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
│ ─ %-1 = invoke foo(::Int64,::Int64)::Any
2+
│╻ Box2 1 ── %1 = Core.Box
3+
││ │ %2 = %new(%1)
4+
│╻ + │ %3 = Base.add_int(_2, _3)
5+
│  │ Core.setfield!(%2, :contents, %3)
6+
│ 3 │ %5 = Core.isdefined(%2, :contents)
7+
│  └─── goto #3 if not %5
8+
│  2 ── goto #4
9+
│  3 ── $(Expr(:throw_undef_if_not, :z, false))
10+
│  4 ┄─ %9 = Core.getfield(%2, :contents)
11+
│  │ %10 = (%9 < 4)
12+
│  └─── goto #9 if not %10
13+
│ 4 5 ── %12 = Core.isdefined(%2, :contents)
14+
│  └─── goto #7 if not %12
15+
│  6 ── goto #8
16+
│  7 ── $(Expr(:throw_undef_if_not, :z, false))
17+
│  8 ┄─ %16 = Core.getfield(%2, :contents)
18+
│  │ %17 = (%16 + 1)
19+
│  └─── Core.setfield!(%2, :contents, %17)
20+
│ 6 9 ┄─ %19 = %new(Main.anonymous.:(var"#1#2"), %2)
21+
│  │ %20 = invoke %19(_2::Int64)
22+
│ 7 │ %21 = (isa)(_2, Missing)
23+
│  └─── goto #11 if not %21
24+
│  10 ─ goto #14
25+
│  11 ─ %24 = (isa)(_2, Int64)
26+
│  └─── goto #13 if not %24
27+
│╻ + 12 ─ %26 = Base.add_int(_2, _3)
28+
│  └─── goto #14
29+
│  13 ─ Core.throw(ErrorException("fatal error in type inference (type bound)"))
30+
│  └─── unreachable
31+
│  14 ┄ %30 = φ (#10 => $(QuoteNode(missing)), #12 => %26)
32+
│ 8 │ %31 = (%20 + %30)
33+
│  └─── return %31
34+
  
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Body::Any
2+
││ │  0 %2 = %new(%1)
3+
│  4 ┄─ 0 %9 = Core.getfield(%2, :contents)
4+
│  │  1000 %10 = (%9 < 4)
5+
│  8 ┄─ 0 %16 = Core.getfield(%2, :contents)
6+
│  │  1000 %17 = (%16 + 1)
7+
│  │  20 %20 = invoke %19(_2::Int64)
8+
│  14 ┄ 0 %30 = φ (#10 => $(QuoteNode(missing)), #12 => %26)
9+
│ 8 │  1000 %31 = (%20 + %30)
10+
  
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Body::Any
2+
││ │ %2 = %new(%1)
3+
│  4 ┄─ %9 = Core.getfield(%2, :contents)
4+
│  │ %10 = (%9 < 4)
5+
│  8 ┄─ %16 = Core.getfield(%2, :contents)
6+
│  │ %17 = (%16 + 1)
7+
│  │ %20 = invoke %19(_2::Int64)
8+
│  14 ┄ %30 = φ (#10 => $(QuoteNode(missing)), #12 => %26)
9+
│ 8 │ %31 = (%20 + %30)
10+
  

0 commit comments

Comments
 (0)