Skip to content

Commit

Permalink
gogensig:mv from goplus/llgo#813
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Nov 27, 2024
1 parent 6444304 commit c8d278a
Show file tree
Hide file tree
Showing 82 changed files with 13,699 additions and 1 deletion.
79 changes: 79 additions & 0 deletions cmd/gogensig/cmp/cmp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package cmp

import (
"bufio"
"bytes"
"strings"
"unicode"

"github.com/google/go-cmp/cmp"

Check failure on line 9 in cmd/gogensig/cmp/cmp.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/google/go-cmp/cmp; to add it:
)

func skipSpace(data []byte, from int) int {
dataBuf := bytes.NewBuffer(data[from:])
index := from
for {
rn, sz, err := dataBuf.ReadRune()
if err != nil {
break
}
if !unicode.IsSpace(rn) {
break
}
index = index + sz
}
return index
}

func SplitLineIgnoreSpace(s string) []string {
buf := bytes.NewBufferString(s)
scan := bufio.NewScanner(buf)
results := make([]string, 0)
for scan.Scan() {
lineText := scan.Text()
lineTextBuf := bytes.NewBufferString(lineText)
lineTextScan := bufio.NewScanner(lineTextBuf)
lineTextScan.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.IndexAny(data, " \t"); i >= 0 {
ii := skipSpace(data, i+1)
return ii, data[0:i], nil
}
if atEOF {
return len(data), data, nil
}
return 0, nil, nil
})

strBuilder := strings.Builder{}
writeSpace := false
total := 0
for lineTextScan.Scan() {
word := lineTextScan.Text()
if writeSpace {
strBuilder.WriteRune(' ')
}
n, err := strBuilder.WriteString(word)
if err != nil {
break
}
total += n
writeSpace = total > 0
}
if total > 0 {
results = append(results, strBuilder.String())
}
}
return results
}

func EqualStringIgnoreSpace(s1 string, s2 string) (bool, string) {
arr1 := SplitLineIgnoreSpace(s1)
arr2 := SplitLineIgnoreSpace(s2)
if !cmp.Equal(arr1, arr2) {
return false, cmp.Diff(arr1, arr2)
}
return true, ""
}
4 changes: 4 additions & 0 deletions cmd/gogensig/config/_testinput/llcppg.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
file FILE
err Err

stdio
61 changes: 61 additions & 0 deletions cmd/gogensig/config/_testinput/llcppg.symb.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[{
"mangle": "_ZN9INIReader12ValueHandlerEPvPKcS2_S2_",
"c++": "INIReader::ValueHandler(void*, char const*, char const*, char const*)",
"go": "(*Reader).ValueHandler"
}, {
"mangle": "_ZN9INIReader7MakeKeyERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_",
"c++": "INIReader::MakeKey(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&)",
"go": "(*Reader).MakeKey"
}, {
"mangle": "_ZN9INIReaderC1EPKcm",
"c++": "INIReader::INIReader(char const*, unsigned long)",
"go": "(*Reader).Init__1"
}, {
"mangle": "_ZN9INIReaderC1ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE",
"c++": "INIReader::INIReader(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&)",
"go": "(*Reader).Init"
}, {
"mangle": "_ZNK9INIReader10GetBooleanERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_b",
"c++": "INIReader::GetBoolean(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, bool) const",
"go": "(*Reader).GetBoolean"
}, {
"mangle": "_ZNK9INIReader10GetIntegerERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_l",
"c++": "INIReader::GetInteger(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, long) const",
"go": "(*Reader).GetInteger"
}, {
"mangle": "_ZNK9INIReader10HasSectionERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE",
"c++": "INIReader::HasSection(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&) const",
"go": "(*Reader).HasSection"
}, {
"mangle": "_ZNK9INIReader10ParseErrorEv",
"c++": "INIReader::ParseError() const",
"go": "(*Reader).ParseError"
}, {
"mangle": "_ZNK9INIReader11GetUnsignedERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_m",
"c++": "INIReader::GetUnsigned(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, unsigned long) const",
"go": "(*Reader).GetUnsigned"
}, {
"mangle": "_ZNK9INIReader12GetInteger64ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_x",
"c++": "INIReader::GetInteger64(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, long long) const",
"go": "(*Reader).GetInteger64"
}, {
"mangle": "_ZNK9INIReader13GetUnsigned64ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_y",
"c++": "INIReader::GetUnsigned64(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, unsigned long long) const",
"go": "(*Reader).GetUnsigned64"
}, {
"mangle": "_ZNK9INIReader3GetERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_S8_",
"c++": "INIReader::Get(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&) const",
"go": "(*Reader).Get"
}, {
"mangle": "_ZNK9INIReader7GetRealERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_d",
"c++": "INIReader::GetReal(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, double) const",
"go": "(*Reader).GetReal"
}, {
"mangle": "_ZNK9INIReader8HasValueERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_",
"c++": "INIReader::HasValue(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&) const",
"go": "(*Reader).HasValue"
}, {
"mangle": "_ZNK9INIReader9GetStringERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_S8_",
"c++": "INIReader::GetString(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&) const",
"go": "(*Reader).GetString"
}]
61 changes: 61 additions & 0 deletions cmd/gogensig/config/_testinput/llcppg.symb.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[{
: "_ZN9INIReader12ValueHandlerEPvPKcS2_S2_",
"c++": "INIReader::ValueHandler(void*, char const*, char const*, char const*)",
"go": "(*Reader).ValueHandler"
}, {
"mangle": "_ZN9INIReader7MakeKeyERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_",
"c++": "INIReader::MakeKey(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&)",
"go": "(*Reader).MakeKey"
}, {
"mangle": "_ZN9INIReaderC1EPKcm",
"c++": "INIReader::INIReader(char const*, unsigned long)",
"go": "(*Reader).Init__1"
}, {
"mangle": "_ZN9INIReaderC1ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE",
"c++": "INIReader::INIReader(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&)",
"go": "(*Reader).Init"
}, {
"mangle": "_ZNK9INIReader10GetBooleanERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_b",
"c++": "INIReader::GetBoolean(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, bool) const",
"go": "(*Reader).GetBoolean"
}, {
"mangle": "_ZNK9INIReader10GetIntegerERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_l",
"c++": "INIReader::GetInteger(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, long) const",
"go": "(*Reader).GetInteger"
}, {
"mangle": "_ZNK9INIReader10HasSectionERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEE",
"c++": "INIReader::HasSection(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&) const",
"go": "(*Reader).HasSection"
}, {
"mangle": "_ZNK9INIReader10ParseErrorEv",
"c++": "INIReader::ParseError() const",
"go": "(*Reader).ParseError"
}, {
"mangle": "_ZNK9INIReader11GetUnsignedERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_m",
"c++": "INIReader::GetUnsigned(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, unsigned long) const",
"go": "(*Reader).GetUnsigned"
}, {
"mangle": "_ZNK9INIReader12GetInteger64ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_x",
"c++": "INIReader::GetInteger64(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, long long) const",
"go": "(*Reader).GetInteger64"
}, {
"mangle": "_ZNK9INIReader13GetUnsigned64ERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_y",
"c++": "INIReader::GetUnsigned64(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, unsigned long long) const",
"go": "(*Reader).GetUnsigned64"
}, {
"mangle": "_ZNK9INIReader3GetERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_S8_",
"c++": "INIReader::Get(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&) const",
"go": "(*Reader).Get"
}, {
"mangle": "_ZNK9INIReader7GetRealERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_d",
"c++": "INIReader::GetReal(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, double) const",
"go": "(*Reader).GetReal"
}, {
"mangle": "_ZNK9INIReader8HasValueERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_",
"c++": "INIReader::HasValue(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&) const",
"go": "(*Reader).HasValue"
}, {
"mangle": "_ZNK9INIReader9GetStringERKNSt3__112basic_stringIcNS0_11char_traitsIcEENS0_9allocatorIcEEEES8_S8_",
"c++": "INIReader::GetString(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&) const",
"go": "(*Reader).GetString"
}]
160 changes: 160 additions & 0 deletions cmd/gogensig/config/conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package config

import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"

"github.com/goplus/llgo/chore/gogensig/unmarshal"
cppgtypes "github.com/goplus/llgo/chore/llcppg/types"
)

// llcppg.cfg
func GetCppgCfgFromPath(filePath string) (*cppgtypes.Config, error) {
bytes, err := ReadFile(filePath)
if err != nil {
return nil, err
}
conf := &cppgtypes.Config{}
err = json.Unmarshal(bytes, &conf)
if err != nil {
return nil, err
}
return conf, nil
}

// llcppg.pub
func GetPubFromPath(filePath string) (map[string]string, error) {
return ReadPubFile(filePath)
}

func GetCppgSigfetchFromByte(data []byte) (unmarshal.FileSet, error) {
return unmarshal.UnmarshalFileSet(data)
}

func SigfetchExtract(file string, isTemp bool, isCPP bool, dir string) ([]byte, error) {
args := []string{"--extract", file}

if isTemp {
args = append(args, "-temp=true")
}

if isCPP {
args = append(args, "-cpp=true")
} else {
args = append(args, "-cpp=false")
}

return executeSigfetch(args, dir)
}

func SigfetchConfig(configFile string, dir string) ([]byte, error) {
args := []string{configFile}
return executeSigfetch(args, dir)
}

func executeSigfetch(args []string, dir string) ([]byte, error) {
cmd := exec.Command("llcppsigfetch", args...)
if dir != "" {
cmd.Dir = dir
}

var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr

err := cmd.Run()
if err != nil {
return nil, fmt.Errorf("error running llcppsigfetch: %v\nStderr: %s\nArgs: %s", err, stderr.String(), strings.Join(args, " "))
}

return out.Bytes(), nil
}

func ReadFile(filePath string) ([]byte, error) {
jsonFile, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer jsonFile.Close()
return io.ReadAll(jsonFile)
}

func ReadPubFile(pubfile string) (ret map[string]string, err error) {
b, err := os.ReadFile(pubfile)
if err != nil {
if os.IsNotExist(err) {
return make(map[string]string), nil
}
return
}

text := string(b)
lines := strings.Split(text, "\n")
ret = make(map[string]string, len(lines))
for i, line := range lines {
flds := strings.Fields(line)
goName := ""
switch len(flds) {
case 1:
case 2:
goName = flds[1]
case 0:
continue
default:
err = fmt.Errorf("%s:%d: too many fields", pubfile, i+1)
return
}
ret[flds[0]] = goName
}
return
}

func WritePubFile(file string, public map[string]string) (err error) {
if len(public) == 0 {
return
}
f, err := os.Create(file)
if err != nil {
return
}
defer f.Close()
ret := make([]string, 0, len(public))
for name, goName := range public {
if goName == "" {
ret = append(ret, name)
} else {
ret = append(ret, name+" "+goName)
}
}
sort.Strings(ret)
_, err = f.WriteString(strings.Join(ret, "\n"))
return
}

func RunCommand(dir, cmdName string, args ...string) error {
execCmd := exec.Command(cmdName, args...)
execCmd.Stdout = os.Stdout
execCmd.Stderr = os.Stderr
execCmd.Dir = dir
return execCmd.Run()
}

func CreateJSONFile(filename string, data interface{}) (string, error) {
filePath := filepath.Join(os.TempDir(), filename)
file, err := os.Create(filePath)
if err != nil {
return "", err
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
return filePath, encoder.Encode(data)
}
Loading

0 comments on commit c8d278a

Please sign in to comment.