Skip to content

llcppg:direct config the funct & pub type name in llcppg.cfg #217

@luoliwoshang

Description

@luoliwoshang

Direct config the func & type name in llcppg.cfg. instead config in llcppg.pub & llcppg.symb.json

To control function names and convert functions to methods, you need to edit the llcppg.symb.json file.

llcppg/README.md

Lines 173 to 219 in 8405fed

#### Function Customization
When you run llcppg directly with the above configuration, it will generate function names according to the configuration. After execution, you'll find a `llcppg.symb.json` file in the current directory.
```json
[
{
"mangle": "cJSON_CreateObject",
"c++": "cJSON_CreateObject()",
"go": "CreateObject"
},
{
"mangle": "cJSON_AddItemToObject",
"c++": "cJSON_AddItemToObject(cJSON *, const char *, cJSON *)",
"go": "(*CJSON).AddItemToObject"
},
{
"mangle": "cJSON_PrintUnformatted",
"c++": "cJSON_PrintUnformatted(const cJSON *)",
"go": "(*CJSON).PrintUnformatted"
},
]
```
- `mangle` field contains the symbol name of function
- `c++` field shows the function prototype from the header file
- `go` field displays the function name that will be generated in LLGo binding.
You can customize this field to:
1. Change function names (e.g. "CreateObject" to "Object" for simplicity)
2. Remove the method receiver prefix to generate a function instead of a method
For example, to convert `(*CJSON).PrintUnformatted` from a method to a function, simply remove the `(*CJSON).` prefix in the configuration file:
```json
[
{
"mangle": "cJSON_PrintUnformatted",
"c++": "cJSON_PrintUnformatted(const cJSON *)",
"go": "PrintUnformatted"
},
]
```
This will generate a function instead of a method in the Go code:
```go
//go:linkname PrintUnformatted C.cJSON_PrintUnformatted
func PrintUnformatted(item *CJSON) *int8
```

if we want control the type name we need edit the llcppg.pub as a input

llcppg/README.md

Lines 237 to 252 in 8405fed

#### Type Customization
The `llcppg.pub` file maintains type mapping relationships between C and Go. You can customize these mappings to better suit your needs.
For instance, if you prefer to use `JSON` instead of `cJSON` as the Go type name, simply modify the `llcppg.pub` file as follows:
```
cJSON JSON
```
After running llcppg again, all generated code will use the new type name. The struct definition and its methods will be automatically updated:
```go
type JSON struct {
// .....
}
// llgo:link (*JSON).PrintBuffered C.cJSON_PrintBuffered
func (recv_ *JSON) PrintBuffered(prebuffer c.Int, fmt Bool) *int8 {
return nil
}
```

We need to move the name mapping functionality back to the configuration file llcppg.cfg, so that the responsibility of llcppg.symb.json is only to instruct gogensig about which functions should be generated, and the responsibility of llcppg.pub is only to serve as part of the output, describing public information in the package when it's being depended on.

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions