Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a pass for linking enum aliases in the no_audit mode #413

Merged
merged 1 commit into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/generator/context.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ function create_context(headers::Vector, args::Vector=String[], options::Dict=Di
if get(general_options, "smart_de_anonymize", true)
push!(ctx.passes, DeAnonymize())
end
if !get(general_options, "no_audit", false)
@error "The generator is running in `no_audit` mode. It could generate invalid Julia code. Please DO NOT submit issues only occur in this mode. You can remove the `no_audit` entry in the `.toml` file to exit this mode."
if get(general_options, "no_audit", false)
@error "The generator is running in `no_audit` mode. It could generate invalid Julia code. You can remove the `no_audit` entry in the `.toml` file to exit this mode."
get(general_options, "link_enum_alias", true) && push!(ctx.passes, LinkEnumAlias())
else
push!(ctx.passes, Audit())
end
push!(ctx.passes, Codegen())
Expand Down
35 changes: 35 additions & 0 deletions src/generator/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,41 @@ function (x::CatchDuplicatedAnonymousTags)(dag::ExprDAG, options::Dict)
end
end

"""
LinkEnumAlias <: AbstractPass
Link hard-coded enum types to the corresponding enums. This pass only works in `no_audit` mode.
"""
mutable struct LinkEnumAlias <: AbstractPass
show_info::Bool
end
LinkEnumAlias(; info=false) = LinkEnumAlias(info)

function (x::LinkEnumAlias)(dag::ExprDAG, options::Dict)
general_options = get(options, "general", Dict())
log_options = get(general_options, "log", Dict())
show_info = get(log_options, "LinkEnumAlias_log", x.show_info)

for (id,i) in dag.ids
node = dag.nodes[i]
node.type isa AbstractTypedefNodeType || continue

ty = getTypedefDeclUnderlyingType(node.cursor) |> getCanonicalType
typeKind = kind(ty)
typeKind == CXType_Int || typeKind == CXType_Short || typeKind == CXType_Long || typeKind == CXType_LongLong ||
typeKind == CXType_UInt || typeKind == CXType_UShort || typeKind == CXType_ULong || typeKind == CXType_ULongLong ||
continue

for (tagid, j) in dag.tags
dag.nodes[j].type isa AbstractEnumNodeType || continue
tagid == id || continue

dag.nodes[i] = ExprNode(id, SoftSkip(), node.cursor, node.exprs, node.adj)
show_info &&
@info "[LinkEnumAlias]: skip $id at dag.nodes[$i]."
end
end
end

"""
DeAnonymize <: AbstractPass
In this pass, naive anonymous tag-types are de-anonymized and the correspoding typedefs
Expand Down
6 changes: 6 additions & 0 deletions test/generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,9 @@ end
ctx = create_context([joinpath(@__DIR__, "include/enum.h")], get_default_args())
@test_throws Exception build!(ctx)
end

@testset "Issue 412 - no audit" begin
options = Dict("general" => Dict{String,Any}("no_audit" => true))
ctx = create_context([joinpath(@__DIR__, "include/enum.h")], get_default_args(), options)
@test_logs (:info, "Done!") match_mode = :any build!(ctx)
end