Skip to content

Commit

Permalink
Short and Elaborated Enums (#519)
Browse files Browse the repository at this point in the history
* Add missing enum types: CXType_Short, CXType_UShort

* Elaborated Cursor enums

* Add test
  • Loading branch information
christiangnrd authored Jan 18, 2025
1 parent 5fa691d commit 1ba01c1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/cursor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ Return the integer value of an enum constant declaration.
"""
function value(c::CLEnumConstantDecl)::Integer
typeKind = kind(getCursorType(c))

# PR 519
if typeKind == CXType_Elaborated
typeKind = getCursorType(c) |>
get_elaborated_cursor |>
getTypedefDeclUnderlyingType |>
getCanonicalType |>
kind
end

# issue #404
if typeKind == CXType_Typedef
typeKind = getCursorType(c) |>
Expand All @@ -242,12 +252,14 @@ function value(c::CLEnumConstantDecl)::Integer
getCanonicalType |>
kind
end
if typeKind == CXType_Int ||
if typeKind == CXType_Short ||
typeKind == CXType_Int ||
typeKind == CXType_Long ||
typeKind == CXType_LongLong ||
typeKind == CXType_Char_S # enum : char
return clang_getEnumConstantDeclValue(c)
elseif typeKind == CXType_UInt ||
elseif typeKind == CXType_UShort ||
typeKind == CXType_UInt ||
typeKind == CXType_ULong ||
typeKind == CXType_ULongLong ||
typeKind == CXType_UChar
Expand Down
7 changes: 7 additions & 0 deletions src/generator/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,13 @@ const ENUM_SYMBOL2TYPE = Dict(
function emit!(dag::ExprDAG, node::ExprNode{<:AbstractEnumNodeType}, options::Dict; args...)
cursor = node.cursor
ty = getEnumDeclIntegerType(cursor)

if ty isa Clang.CLElaborated
ty = get_elaborated_cursor(ty) |>
getTypedefDeclUnderlyingType |>
getCanonicalType
end

if ty isa Clang.CLTypedef
ty = getTypeDeclaration(ty) |>
getTypedefDeclUnderlyingType |>
Expand Down
5 changes: 5 additions & 0 deletions test/generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ end
@test_logs (:info, "Done!") match_mode = :any build!(ctx)
end

@testset "PR 519 - Elaborated Enum" begin
ctx = create_context([joinpath(@__DIR__, "include/elaborateEnum.h")], get_default_args())
@test_logs (:info, "Done!") match_mode = :any build!(ctx)
end

@testset "Issue 452 - StructMutualRef" begin
ctx = create_context([joinpath(@__DIR__, "include/struct-mutual-ref.h")], get_default_args())
@test_logs (:info, "Done!") match_mode = :any build!(ctx)
Expand Down
7 changes: 7 additions & 0 deletions test/include/elaborateEnum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdint.h>

enum X : uint32_t {
A = 2,
B = 4,
C = 6,
};

0 comments on commit 1ba01c1

Please sign in to comment.