Skip to content

Commit

Permalink
Fix FAM rendering by removing FAM field early (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
keynmol authored Jul 10, 2023
1 parent 19e2196 commit d2948b9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
34 changes: 28 additions & 6 deletions modules/bindgen/src/main/scala/analysis/visitStruct.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,14 @@ def visitStruct(cursor: CXCursor, name: String)(using
val nestedName = "Struct" + builder.anonymous.size
val str = visitStruct(cursor, builder.name.value + "." + nestedName)
builder.anonymous.addOne(
Def.Struct(
fields = str.fields,
name = StructName(nestedName),
anonymous = str.anonymous,
meta = extractMetadata(cursor)
removeFAM(
Def.Struct(
fields = str.fields,
name = StructName(nestedName),
anonymous = str.anonymous,
meta = extractMetadata(cursor)
),
Some(cursor.spelling)
)
)
CXChildVisitResult.CXChildVisit_Continue
Expand All @@ -125,8 +128,27 @@ def visitStruct(cursor: CXCursor, name: String)(using
CXClientData.wrap(ptr)
)

(!ptr)._1.struct.build
removeFAM((!ptr)._1.struct.build)
finally memory.deallocate()
end try
}
end visitStruct

def removeFAM(model: Def.Struct, name: Option[String] = None)(using
LoggingConfig
): Def.Struct =

val hasFlexibleArrayMember = model.fields.lastOption.collectFirst {
case (_, CType.IncompleteArray(_)) => true
}.isDefined

if hasFlexibleArrayMember then
warning(
s"Struct '${name.getOrElse(model.name)}' has a Flexible Array Member, so it was dropped from the definition. " +
"See https://github.com/indoorvivants/sn-bindgen/issues/62 for details"
)

model.copy(fields = model.fields.dropRight(1))
else model

end removeFAM
19 changes: 2 additions & 17 deletions modules/bindgen/src/main/scala/render/struct.scala
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
package bindgen
package rendering

def struct(model: Def.Struct, line: Appender)(using
def struct(struct: Def.Struct, line: Appender)(using
c: Config,
ar: AliasResolver
): Exported =
val hasFlexibleArrayMember = model.fields.lastOption.collectFirst {
case (_, CType.IncompleteArray(_)) => true
}.isDefined

val struct: Def.Struct =
if hasFlexibleArrayMember then
model.copy(fields = model.fields.dropRight(1))
else model

if hasFlexibleArrayMember then
warning(
s"Struct '${model.name}' has a Flexible Array Member, so it was dropped from the definition. " +
"See https://github.com/indoorvivants/sn-bindgen/issues/62 for details"
)

val rewriteRules = hack_recursive_structs(struct)
val structName = struct.name
val structType: CType.Struct = CType.Struct(struct.fields.map(_._2).toList)
Expand Down Expand Up @@ -84,7 +69,7 @@ def struct(model: Def.Struct, line: Appender)(using
.map(p => p.name.value -> p.newRawType)
)

renderComment(line, model.meta)
renderComment(line, struct.meta)
line(s"opaque type $structName = ${scalaType(finalStructType)}")
line(s"object ${sanitiseBeforeColon(structName.value)}:")
nest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,13 @@ typedef struct FAM {
int len;
char data[];
} FAM;

typedef union {
struct
{
int va_header;
char va_data[];
} va_compressed;
} FAM_union;


0 comments on commit d2948b9

Please sign in to comment.