Skip to content

Commit

Permalink
Add a pass for linking enum aliases in the no_audit mode (#413)
Browse files Browse the repository at this point in the history
fix #412
  • Loading branch information
Gnimuc authored Jan 2, 2023
1 parent 14a90e9 commit c95896f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
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

0 comments on commit c95896f

Please sign in to comment.