From 5bd7b202fb0c85e8b48d68043d5f1343dc8c13f0 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com> Date: Mon, 20 Nov 2023 16:05:39 +0900 Subject: [PATCH] pass `world::UInt` instead of `interp::CthulhuInterpreter` to codeview functions (#516) Making it a bit easier to use them standalone. --- src/Cthulhu.jl | 9 +++++---- src/codeview.jl | 44 +++++++++++++++++++++---------------------- test/test_codeview.jl | 2 +- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/src/Cthulhu.jl b/src/Cthulhu.jl index 6223c508..2095cbbc 100644 --- a/src/Cthulhu.jl +++ b/src/Cthulhu.jl @@ -521,7 +521,7 @@ function _descend(term::AbstractTerminal, interp::AbstractInterpreter, curs::Abs shown_callsites = annotate_source ? sourcenodes : callsites menu = CthulhuMenu(shown_callsites, with_effects, optimize & !annotate_source, iswarn&get(iostream, :color, false)::Bool, hide_type_stable, custom_toggles; menu_options...) - usg = usage(view_cmd, annotate_source, optimize, iswarn, hide_type_stable, debuginfo, remarks, with_effects, inline_cost, type_annotations, + usg = usage(view_cmd, annotate_source, optimize, iswarn, hide_type_stable, debuginfo, remarks, with_effects, inline_cost, type_annotations, CONFIG.enable_highlighter, inlay_types_vscode, diagnostics_vscode, jump_always, custom_toggles) cid = request(term, usg, menu) toggle = menu.toggle @@ -611,8 +611,8 @@ function _descend(term::AbstractTerminal, interp::AbstractInterpreter, curs::Abs override = get_override(info), debuginfo, optimize, interruptexc, iswarn, hide_type_stable, - remarks, with_effects, inline_cost, - type_annotations, annotate_source, + remarks, with_effects, inline_cost, + type_annotations, annotate_source, inlay_types_vscode, diagnostics_vscode, jump_always) @@ -696,8 +696,9 @@ function _descend(term::AbstractTerminal, interp::AbstractInterpreter, curs::Abs display_CI = true elseif toggle === :ast || toggle === :llvm || toggle === :native view_cmd = CODEVIEWS[toggle] + world = get_world_counter(interp) println(iostream) - view_cmd(iostream, mi, optimize, debuginfo, interp, CONFIG) + view_cmd(iostream, mi, optimize, debuginfo, world, CONFIG) display_CI = false else local i = findfirst(ct->ct.toggle === toggle, custom_toggles) diff --git a/src/codeview.jl b/src/codeview.jl index e4d00fb4..11137d2b 100644 --- a/src/codeview.jl +++ b/src/codeview.jl @@ -20,17 +20,17 @@ function highlight(io, x, lexer, config::CthulhuConfig) end end -function cthulhu_llvm(io::IO, mi, optimize, debuginfo, interp::CthulhuInterpreter, +function cthulhu_llvm(io::IO, mi, optimize::Bool, debuginfo, world::UInt, config::CthulhuConfig, dump_module::Bool=false, raw::Bool=false) dump = @static if VERSION ≥ v"1.10.0-DEV.1386" InteractiveUtils._dump_function_llvm( - mi, get_world_counter(interp), + mi, world, #=wrapper=# false, !raw, dump_module, optimize, debuginfo != DInfo.none ? :source : :none, Base.CodegenParams()) else InteractiveUtils._dump_function_linfo_llvm( - mi, get_world_counter(interp), + mi, world, #=wrapper=# false, #=strip_ir_metadata=# true, dump_module, optimize, debuginfo != DInfo.none ? :source : :none, Base.CodegenParams()) @@ -38,26 +38,26 @@ function cthulhu_llvm(io::IO, mi, optimize, debuginfo, interp::CthulhuInterprete highlight(io, dump, "llvm", config) end -function cthulhu_native(io::IO, mi, optimize, debuginfo, interp::CthulhuInterpreter, +function cthulhu_native(io::IO, mi, ::Bool, debuginfo, world::UInt, config::CthulhuConfig, dump_module::Bool=false, raw::Bool=false) dump = @static if VERSION ≥ v"1.10.0-DEV.1386" if dump_module InteractiveUtils._dump_function_native_assembly( - mi, get_world_counter(interp), + mi, world, #=wrapper=# false, #=syntax=# config.asm_syntax, debuginfo != DInfo.none ? :source : :none, #=binary=# false, raw, Base.CodegenParams()) else InteractiveUtils._dump_function_native_disassembly( - mi, get_world_counter(interp), + mi, world, #=wrapper=# false, #=syntax=# config.asm_syntax, debuginfo != DInfo.none ? :source : :none, #=binary=# false) end else InteractiveUtils._dump_function_linfo_native( - mi, get_world_counter(interp), + mi, world, #=wrapper=# false, #=syntax=# config.asm_syntax, debuginfo != DInfo.none ? :source : :none, #=binary=# false) @@ -65,7 +65,7 @@ function cthulhu_native(io::IO, mi, optimize, debuginfo, interp::CthulhuInterpre highlight(io, dump, "asm", config) end -function cthulhu_ast(io::IO, mi, optimize, debuginfo, ::CthulhuInterpreter, config::CthulhuConfig) +function cthulhu_ast(io::IO, mi, ::Bool, debuginfo, ::UInt, config::CthulhuConfig) meth = mi.def::Method ast = definition(Expr, meth) if ast!==nothing @@ -155,10 +155,10 @@ function cthulhu_typed(io::IO, debuginfo::Symbol, if !TypedSyntax.inlay_hints_available_vscode() || isnothing(functionloc(mi)[1]) inlay_types_vscode = false end - + vscode_io = IOContext( jump_always && inlay_types_vscode ? devnull : lambda_io, - :inlay_hints => inlay_types_vscode ? Dict{String,Vector{TypedSyntax.InlayHint}}() : nothing , + :inlay_hints => inlay_types_vscode ? Dict{String,Vector{TypedSyntax.InlayHint}}() : nothing , :diagnostics => diagnostics_vscode ? TypedSyntax.Diagnostic[] : nothing ) @@ -167,7 +167,7 @@ function cthulhu_typed(io::IO, debuginfo::Symbol, else printstyled(vscode_io, tsn; type_annotations, iswarn, hide_type_stable, idxend) end - + callsite_diagnostics = TypedSyntax.Diagnostic[] if (diagnostics_vscode || inlay_types_vscode) vscode_io = IOContext(devnull, :inlay_hints=>vscode_io[:inlay_hints], :diagnostics=>vscode_io[:diagnostics]) @@ -184,7 +184,7 @@ function cthulhu_typed(io::IO, debuginfo::Symbol, !isnothing(vscode_io[:diagnostics]) && append!(callsite_diagnostics, vscode_io[:diagnostics]) TypedSyntax.display_diagnostics_vscode(callsite_diagnostics) TypedSyntax.display_inlay_hints_vscode(vscode_io) - + (jump_always && inlay_types_vscode) || println(lambda_io) istruncated && @info "This method only fills in default arguments; descend into the body method to see the full source." return nothing @@ -328,7 +328,7 @@ function cthulhu_typed(io::IO, debuginfo::Symbol, return nothing end -function descend_into_callsite!(io::IO, tsn::TypedSyntaxNode; +function descend_into_callsite!(io::IO, tsn::TypedSyntaxNode; iswarn::Bool, hide_type_stable::Bool, type_annotations::Bool) sig, body = children(tsn) # We empty the body when filling kwargs @@ -340,10 +340,10 @@ function descend_into_callsite!(io::IO, tsn::TypedSyntaxNode; end function add_callsites!(d::AbstractDict, visited_mis::AbstractSet, diagnostics::AbstractVector, - mi::MethodInstance, source_mi::MethodInstance=mi; + mi::MethodInstance, source_mi::MethodInstance=mi; optimize::Bool=true, annotate_source::Bool=false, interp::AbstractInterpreter=CthulhuInterpreter()) - + callsites, src, rt = try (; src, rt, infos, slottypes, effects, codeinf) = lookup(interp, mi, optimize & !annotate_source) @@ -354,7 +354,7 @@ function add_callsites!(d::AbstractDict, visited_mis::AbstractSet, diagnostics:: @assert length(src.code) == length(infos) end - # We pass false as it doesn't affect callsites and skips fetching the method definition + # We pass false as it doesn't affect callsites and skips fetching the method definition # using CodeTracking which is slow callsites, _ = find_callsites(interp, src, infos, mi, slottypes, optimize & !annotate_source, false) callsites, src, rt @@ -376,7 +376,7 @@ function add_callsites!(d::AbstractDict, visited_mis::AbstractSet, diagnostics:: return nothing end tsn, _ = get_typed_sourcetext(mi, src, rt; warn=false) - isnothing(tsn) && return nothing + isnothing(tsn) && return nothing sig, body = children(tsn) # We empty the body when filling kwargs istruncated = isempty(children(body)) @@ -387,10 +387,10 @@ function add_callsites!(d::AbstractDict, visited_mis::AbstractSet, diagnostics:: if haskey(d, key) if !isnothing(d[key]) && mi != d[key].mi d[key] = nothing - push!(diagnostics, + push!(diagnostics, TypedSyntax.Diagnostic( - isnothing(functionloc(mi)[1]) ? string(mi.file) : functionloc(mi)[1], mi.def.line, - TypedSyntax.DiagnosticKinds.Information, + isnothing(functionloc(mi)[1]) ? string(mi.file) : functionloc(mi)[1], mi.def.line, + TypedSyntax.DiagnosticKinds.Information, "Cthulhu disabled: This function was called multiple times with different argument types" ) ) @@ -524,7 +524,7 @@ InteractiveUtils.code_llvm( b.mi, optimize, debuginfo === :source, - b.interp, + get_world_counter(b.interp), config, dump_module, raw, @@ -545,7 +545,7 @@ InteractiveUtils.code_native( b.mi, optimize, debuginfo === :source, - b.interp, + get_world_counter(b.interp), config, dump_module, raw, diff --git a/test/test_codeview.jl b/test/test_codeview.jl index 09957287..02058fde 100644 --- a/test/test_codeview.jl +++ b/test/test_codeview.jl @@ -22,7 +22,7 @@ Revise.track(TestCodeViewSandbox, normpath(@__DIR__, "TestCodeViewSandbox.jl")) config = Cthulhu.CONFIG io = IOBuffer() - codeview(io, mi, optimize, debuginfo, interp, config) + codeview(io, mi, optimize, debuginfo, Cthulhu.get_world_counter(interp), config) @test !isempty(String(take!(io))) # just check it works end end