Skip to content

llcppg: interface & implement file convert #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ jobs:
run: |
# install demo's lib
sudo apt install liblua5.4-dev libsqlite3-dev libgmp-dev libgpg-error-dev zlib1g-dev -y
llcppgtest -demo ./_llcppgtest/cjson
llcppgtest -demo ./_llcppgtest/lua
llcppgtest -demo ./_llcppgtest/sqlite
llcppgtest -demo ./_llcppgtest/gmp
llcppgtest -demo ./_llcppgtest/gpgerror
llcppgtest -demo ./_llcppgtest/zlib
llcppgtest -demo ./_llcppgtest/cjson -conf conf/linux
llcppgtest -demo ./_llcppgtest/lua -conf conf/linux
llcppgtest -demo ./_llcppgtest/sqlite -conf conf/linux
llcppgtest -demo ./_llcppgtest/gmp -conf conf/linux
llcppgtest -demo ./_llcppgtest/gpgerror -conf conf/linux
llcppgtest -demo ./_llcppgtest/zlib -conf conf/linux


- name: Upload coverage reports to Codecov
Expand Down
12 changes: 12 additions & 0 deletions _llcppgtest/cjson/conf/linux/llcppg.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "cjson",
"cflags": "$(pkg-config --cflags libcjson)",
"libs": "$(pkg-config --libs libcjson libcjson_utils)",
"include": [
"cJSON.h",
"cJSON_Utils.h"
],
"trimPrefixes": ["cJSON_", "cJSONUtils_"],
"cplusplus": false,
"mix":true
}
1 change: 1 addition & 0 deletions _llcppgtest/cjson/conf/linux/llcppg.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cJSON JSON
42 changes: 42 additions & 0 deletions _llcppgtest/cjson/conf/linux/llcppg.symb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"mangle": "cJSON_PrintUnformatted",
"c++": "cJSON_PrintUnformatted(const cJSON *)",
"go": "(*CJSON).CStr"
},
{
"mangle": "cJSON_CreateObject",
"c++": "cJSON_CreateObject()",
"go": "Object"
},
{
"mangle": "cJSON_CreateArray",
"c++": "cJSON_free()",
"go": "Array"
},
{
"mangle": "cJSON_CreateString",
"c++": "cJSON_CreateString(const char *)",
"go": "String"
},
{
"mangle": "cJSON_free",
"c++": "cJSON_free(void *)",
"go": "FreeCStr"
},
{
"mangle": "cJSON_AddItemToArray",
"c++": "cJSON_AddItemToArray(cJSON *, cJSON *)",
"go": "(*CJSON).AddItem"
},
{
"mangle": "cJSON_AddItemToObject",
"c++": "cJSON_AddItemToObject(cJSON *, const char *, cJSON *)",
"go": "(*CJSON).SetItem"
},
{
"mangle": "cJSON_free",
"c++": "cJSON_free(void *)",
"go": "FreeCStr"
}
]
12 changes: 12 additions & 0 deletions _llcppgtest/gmp/conf/linux/llcppg.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "gmp",
"cflags": "$(pkg-config --cflags gmp)",
"libs": "$(pkg-config --libs gmp)",
"include": [
"gmp.h",
"gmpxx.h"
],
"trimPrefixes": ["__"],
"cplusplus": false,
"mix":true
}
12 changes: 12 additions & 0 deletions _llcppgtest/gpgerror/conf/linux/llcppg.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "gpgerror",
"cflags": "$(pkg-config --cflags gpg-error)",
"libs": "$(pkg-config --libs gpg-error)",
"include": [
"gpgrt.h",
"gpg-error.h"
],
"trimPrefixes": ["gpg_err_","gpgrt_set_","gpg_","GPG_ERR_"],
"cplusplus": false,
"mix":true
}
14 changes: 14 additions & 0 deletions _llcppgtest/lua/conf/linux/llcppg.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "lua",
"cflags": "$(pkg-config --cflags lua)",
"libs": "$(pkg-config --libs lua)",
"include": [
"lua.h",
"luaconf.h",
"lualib.h",
"lauxlib.h"
],
"trimPrefixes": ["lua_", "luaopen_", "luaL_", "LUA_T", "LUA_"],
"cplusplus": false,
"mix":true
}
12 changes: 12 additions & 0 deletions _llcppgtest/sqlite/conf/linux/llcppg.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "sqlite",
"cflags": "$(pkg-config --cflags sqlite3)",
"libs": "$(pkg-config --libs sqlite3)",
"include": [
"sqlite3ext.h",
"sqlite3.h"
],
"trimPrefixes": ["sqlite3_","SQLITE_"],
"cplusplus": false,
"mix":true
}
7 changes: 7 additions & 0 deletions _llcppgtest/sqlite/conf/linux/llcppg.symb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"mangle": "sqlite3_finalize",
"c++": "sqlite3_finalize(sqlite3_stmt *)",
"go": "(*Stmt).Close"
}
]
12 changes: 12 additions & 0 deletions _llcppgtest/zlib/conf/linux/llcppg.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "zlib",
"cflags": "$(pkg-config --cflags zlib)",
"libs": "$(pkg-config --libs zlib)",
"include": [
"zconf.h",
"zlib.h"
],
"trimPrefixes": ["Z_"],
"cplusplus": false,
"mix":true
}
27 changes: 27 additions & 0 deletions _llcppgtest/zlib/conf/linux/llcppg.symb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"mangle": "compress",
"c++": "compress(Bytef *, uLongf *, const Bytef *, uLong)",
"go": "Compress"
},
{
"mangle": "compress2",
"c++": "compress2(Bytef *, uLongf *, const Bytef *, uLong, int)",
"go": "Compress2"
},
{
"mangle": "uncompress",
"c++": "uncompress(Bytef *, uLongf *, const Bytef *, uLong)",
"go": "Uncompress"
},
{
"mangle": "uncompress2",
"c++": "uncompress2(Bytef *, uLongf *, const Bytef *, uLong *)",
"go": "Uncompress2"
},
{
"mangle": "compressBound",
"c++": "compressBound(uLong)",
"go": "CompressBound"
}
]
47 changes: 20 additions & 27 deletions _xtool/llcppsigfetch/llcppsigfetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import (
"github.com/goplus/llcppg/_xtool/llcppsymg/args"
"github.com/goplus/llcppg/_xtool/llcppsymg/clangutils"
"github.com/goplus/llcppg/_xtool/llcppsymg/config"
"github.com/goplus/llcppg/_xtool/llcppsymg/config/cfgparse"
"github.com/goplus/llcppg/_xtool/llcppsymg/syspath"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/cjson"
)
Expand Down Expand Up @@ -154,41 +152,36 @@ func runFromConfig(cfgFile string, useStdin bool, outputToFile bool, verbose boo
os.Exit(1)
}

cflag := cfgparse.ParseCFlags(conf.CFlags)
files, notFounds, err := cflag.GenHeaderFilePaths(conf.Include, syspath.GetIncludePaths())
check(err)

if verbose {
fmt.Fprintln(os.Stderr, "runFromConfig: header file paths", files)
if len(notFounds) > 0 {
fmt.Fprintln(os.Stderr, "runFromConfig: not found header files", notFounds)
}
}

// Generate include directory flags (-I flags)
incFlags := make([]string, 0, len(cflag.Paths))
for _, path := range cflag.Paths {
incFlags = append(incFlags, "-I"+path)
}

context := parse.NewContext(&parse.ContextConfig{
Conf: conf.Config,
IncFlags: incFlags,
})
err = context.ProcessFiles(files)
context, err := parse.Do(conf.Config)
check(err)

outputInfo(context, outputToFile)
}

func runExtract(file string, isTemp bool, isCpp bool, outToFile bool, otherArgs []string, verbose bool) {
func runExtract(content string, isTemp bool, isCpp bool, outToFile bool, otherArgs []string, verbose bool) {
var file string
if isTemp {
temp, err := os.Create(clangutils.TEMP_FILE)
if err != nil {
panic(err)
}
defer temp.Close()
defer os.Remove(file)
temp.Write([]byte(content))
file = temp.Name()
} else {
file = content
}
cfg := &clangutils.Config{
File: file,
Args: otherArgs,
IsCpp: isCpp,
Temp: isTemp,
Temp: false,
}
converter, err := parse.NewConverter(cfg)

converter, err := parse.NewConverter(cfg, &config.PkgHfilesInfo{
Inters: []string{file},
})
check(err)
_, err = converter.Convert()
check(err)
Expand Down
42 changes: 34 additions & 8 deletions _xtool/llcppsigfetch/parse/cvt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/goplus/llcppg/_xtool/llcppsigfetch/dbg"
"github.com/goplus/llcppg/_xtool/llcppsymg/clangutils"
"github.com/goplus/llcppg/_xtool/llcppsymg/config"
"github.com/goplus/llcppg/ast"
"github.com/goplus/llcppg/llcppg"
"github.com/goplus/llcppg/token"
Expand Down Expand Up @@ -41,7 +42,7 @@ type Config struct {
IsCpp bool
}

func NewConverter(config *clangutils.Config) (*Converter, error) {
func NewConverter(config *clangutils.Config, pkgFileInfo *config.PkgHfilesInfo) (*Converter, error) {
if dbg.GetDebugParse() {
fmt.Fprintln(os.Stderr, "NewConverter: config")
fmt.Fprintln(os.Stderr, "config.File", config.File)
Expand All @@ -54,8 +55,7 @@ func NewConverter(config *clangutils.Config) (*Converter, error) {
if err != nil {
return nil, err
}

files := initFileEntries(unit)
files := initFileEntries(unit, pkgFileInfo)

return &Converter{
Files: files,
Expand All @@ -71,7 +71,22 @@ func (ct *Converter) Dispose() {
ct.unit.Dispose()
}

func initFileEntries(unit *clang.TranslationUnit) []*llcppg.FileEntry {
func initFileEntries(unit *clang.TranslationUnit, pkgHfileInfo *config.PkgHfilesInfo) []*llcppg.FileEntry {
inters := make(map[string]struct{})
impls := make(map[string]struct{})
thirds := make(map[string]struct{})
if pkgHfileInfo != nil {
for _, file := range pkgHfileInfo.Inters {
inters[file] = struct{}{}
}
for _, file := range pkgHfileInfo.Impls {
impls[file] = struct{}{}
}
for _, file := range pkgHfileInfo.Thirds {
thirds[file] = struct{}{}
}
}

files := make([]*llcppg.FileEntry, 0)
clangutils.GetInclusions(unit, func(inced clang.File, incins []clang.SourceLocation) {
loc := unit.GetLocation(inced, 1, 1)
Expand All @@ -81,11 +96,22 @@ func initFileEntries(unit *clang.TranslationUnit) []*llcppg.FileEntry {
cur := unit.GetCursor(&incins[0])
incPath = toStr(cur.String())
}
var fileType llcppg.FileType
if _, ok := inters[incedFile]; ok {
fileType = llcppg.Inter
} else if _, ok := impls[incedFile]; ok {
fileType = llcppg.Impl
} else if _, ok := thirds[incedFile]; ok {
fileType = llcppg.Third
} else {
panic("unknown file type: " + incedFile)
}
files = append(files, &llcppg.FileEntry{
Path: incedFile,
IncPath: incPath,
IsSys: loc.IsInSystemHeader() != 0,
Doc: &ast.File{},
Path: incedFile,
IncPath: incPath,
IsSys: loc.IsInSystemHeader() != 0,
Doc: &ast.File{},
FileType: fileType,
})
})
return files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
test "github.com/goplus/llcppg/_xtool/llcppsigfetch/parse/cvt_test"
"github.com/goplus/llcppg/_xtool/llcppsymg/clangutils"
"github.com/goplus/llcppg/llcppg"
)

func main() {
Expand All @@ -11,17 +11,15 @@ func main() {
}

func TestForwardDecl() {
test.RunTestWithConfig(&clangutils.Config{
File: "./hfile/forwarddecl.h",
Temp: false,
IsCpp: false,
test.RunTestWithConfig(&llcppg.Config{
Include: []string{"forwarddecl.h"},
CFlags: "-I./hfile",
})
}

func TestForwardDeclCrossFile() {
test.RunTestWithConfig(&clangutils.Config{
File: "./hfile/def.h",
Temp: false,
IsCpp: false,
test.RunTestWithConfig(&llcppg.Config{
Include: []string{"def.h"},
CFlags: "-I./hfile",
})
}
Loading
Loading