-
I'm trying to wrap a header that has definitions like:
The definition for |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
There is no trivial way to avoid this. The following workaround ignores all macros:
But it's not easy to add support for both non-macro identifiers and macro identifiers. I'm wondering why people use macros like this. Is it generated by AI? |
Beta Was this translation helpful? Give feedback.
-
I finally got to implementing this: # a pass that removes macro definitions that are also function definitions.
#
# this sometimes happens with NVIDIA's headers, either because of typos, or because they are
# reserving identifiers for future use:
# #define cuStreamGetCaptureInfo_v2 __CUDA_API_PTSZ(cuStreamGetCaptureInfo_v2)
mutable struct AvoidDuplicates <: Clang.AbstractPass end
function (x::AvoidDuplicates)(dag::ExprDAG, options::Dict)
# collect macro definitions
macro_definitions = Dict()
for (i, node) in enumerate(dag.nodes)
if node isa ExprNode{<:AbstractMacroNodeType}
macro_definitions[node.id] = (i, node)
end
end
# scan function definitions
for (i, node) in enumerate(dag.nodes)
if Generators.is_function(node) && !Generators.is_variadic_function(node)
if haskey(macro_definitions, node.id)
@info "Removing macro definition for $(node.id)"
j, duplicate_node = macro_definitions[node.id]
dag.nodes[j] = ExprNode(node.id, Clang.Generators.Skip(), duplicate_node.cursor, duplicate_node.exprs, duplicate_node.adj)
end
end
end
return dag
end This pass needs to be registered early (before the function definition is marked as a duplicate), so: insert!(ctx.passes, 2, AvoidDuplicates()) |
Beta Was this translation helpful? Give feedback.
There is no trivial way to avoid this.
The following workaround ignores all macros: