xlab / c-for-go

Automatic C-Go Bindings Generator for Go Programming Language
https://c.for-go.com
MIT License
1.5k stars 119 forks source link

Generator uses underlying type of typedef when used as a parameter #146

Open waj334 opened 1 year ago

waj334 commented 1 year ago

Consider the following C:

typedef struct LLVMOpaqueContext *LLVMContextRef;
void *LLVMContextGetDiagnosticContext(LLVMContextRef C);

and the following config:

GENERATOR:
  PackageName: llvm
  PackageDescription: "LLVM bindings for Go"
  PackageLicense: "Apache License v2.0 with LLVM Exceptions"
  Options:
      SafeStrings: true
PARSER:
  IncludePaths:
    - ./thirdparty/llvm-project/llvm/include
    - ./build/llvm-build/include
  SourcesPaths:
    - ./test.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/Analysis.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/BitReader.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/BitWriter.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/blake3.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/Comdat.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/Core.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/DataTypes.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/DebugInfo.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/Deprecated.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/Disassembler.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/DisassemblerTypes.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/Error.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/ErrorHandling.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/ExecutionEngine.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/ExternC.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/Initialization.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/IRReader.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/Linker.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/LLJIT.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/lto.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/Object.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/Orc.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/OrcEE.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/Remarks.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/Support.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/Target.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/TargetMachine.h
    #- ./thirdparty/llvm-project/llvm/include/llvm-c/Types.h
TRANSLATOR:
  ConstCharIsString: true
  ConstUCharIsString: true
  ConstRules:
    defines: eval
  Rules:
    global:
      - {action: accept, from: "^LLVM"}
      - {transform: export}
    const:
      - {action: accept, from: "^LLVM"}
      - {transform: export}
    type:
      - {action: accept, from: "^LLVM"}
      - {transform: export}
    private:
      - {transform: unexport}

The produces the following: types.go

// Apache License v2.0 with LLVM Exceptions

// WARNING: This file has automatically been generated
// Code generated by https://git.io/c-for-go. DO NOT EDIT.

package llvm

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

// LLVMContextRef as declared in sigo/test.h:1
type LLVMContextRef C.struct_LLVMOpaqueContext

llvm.go

// Apache License v2.0 with LLVM Exceptions

// WARNING: This file has automatically been generated
// Code generated by https://git.io/c-for-go. DO NOT EDIT.

package llvm

/*
#include <stdlib.h>
#include "cgo_helpers.h"
*/
import "C"
import (
    "runtime"
    "unsafe"
)

// LLVMContextGetDiagnosticContext function as declared in sigo/test.h:3
func LLVMContextGetDiagnosticContext(c LLVMOpaqueContext) unsafe.Pointer {
    cc, ccAllocMap := *(*C.struct_LLVMOpaqueContext)(unsafe.Pointer(&c)), cgoAllocsUnknown
    __ret := C.LLVMContextGetDiagnosticContext(cc)
    runtime.KeepAlive(ccAllocMap)
    __v := *(*unsafe.Pointer)(unsafe.Pointer(&__ret))
    return __v
}

In LLVMContextGetDiagnosticContext I expected the parameter to be of type LLVMContextRef, not LLVMOpaqueContext which is not directly defined.