c-for-go icon indicating copy to clipboard operation
c-for-go copied to clipboard

typedef of user-defined enum in C header file causes syntax error in types.go

Open enr0n opened this issue 6 years ago • 3 comments

While working with c-for-go, I've encountered the following issue.

Suppose you have a header file that looks something like this:

// example.h
typedef enum my_type {
    FOO = 1,
    BAR = 2,
} my_type;

typedef enum my_type my_aliased_type;

and an example.yml configuration file:

---
GENERATOR:
    PackageName: example
    PackageDescription: "Package example shows an issue in c-for-go"
    Includes:
        - example/example.h
PARSER:
    IncludePaths:
        - example/
    SourcesPaths:
        - example/example.h 
TRANSLATOR:
    Rules:
        global:
            - {action: accept}
            - {load: snakecase}

When I run c-for-go, I get the following error:

$ c-for-go example.yml 
  processing example.yml ⠋[WARN] cannot gofmt example/types.go: example/types.go:16:22: expected type, found newline (and 1 more errors)
  processing example.yml done.

The generated example/types.go file looks like this:


// WARNING: This file has automatically been generated on Thu, 13 Jun 2019 13:25:38 EDT.
// Code generated by https://git.io/c-for-go. DO NOT EDIT.

package example

/*
#include "example/example.h"
#include <stdlib.h>
#include "cgo_helpers.h"
*/
import "C"


// myAliasedType as declared in example/example.h:6
type myAliasedType 

Where I would expect line 16 to look like this:

type myAliasedType myType

I'm aware there may be some uncertainty in how to define the constants for the aliased type, since Go will not allow implicit type conversion here, whereas C would.

enr0n avatar Jun 13 '19 17:06 enr0n

Closing since no response.

enr0n avatar Oct 09 '19 02:10 enr0n

Sorry, just missed that issue. Probably a renaming scheme would help avoiding a missing type name, but that will still require a type cast.

However, a type cast requirement can be avoided by introducing Go aliases and adding that feature into c-for-go. I expect this issue might be a good starting point for such improvement.

xlab avatar Oct 09 '19 10:10 xlab

I'm facing this same issue

elamre avatar Jun 22 '21 09:06 elamre