llcppsigfetch:reuse clangutil
This commit is contained in:
@@ -27,6 +27,7 @@ import (
|
|||||||
"github.com/goplus/llgo/c"
|
"github.com/goplus/llgo/c"
|
||||||
"github.com/goplus/llgo/c/cjson"
|
"github.com/goplus/llgo/c/cjson"
|
||||||
"github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse"
|
"github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse"
|
||||||
|
"github.com/goplus/llgo/chore/_xtool/llcppsymg/clangutils"
|
||||||
"github.com/goplus/llgo/chore/_xtool/llcppsymg/config"
|
"github.com/goplus/llgo/chore/_xtool/llcppsymg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -116,7 +117,7 @@ func runExtract() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := &parse.Config{
|
cfg := &clangutils.Config{
|
||||||
File: os.Args[2],
|
File: os.Args[2],
|
||||||
Args: []string{},
|
Args: []string{},
|
||||||
IsCpp: true,
|
IsCpp: true,
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package parse
|
package parse
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -10,6 +9,7 @@ import (
|
|||||||
"github.com/goplus/llgo/c"
|
"github.com/goplus/llgo/c"
|
||||||
"github.com/goplus/llgo/c/cjson"
|
"github.com/goplus/llgo/c/cjson"
|
||||||
"github.com/goplus/llgo/c/clang"
|
"github.com/goplus/llgo/c/clang"
|
||||||
|
"github.com/goplus/llgo/chore/_xtool/llcppsymg/clangutils"
|
||||||
"github.com/goplus/llgo/chore/llcppg/ast"
|
"github.com/goplus/llgo/chore/llcppg/ast"
|
||||||
"github.com/goplus/llgo/chore/llcppg/token"
|
"github.com/goplus/llgo/chore/llcppg/token"
|
||||||
)
|
)
|
||||||
@@ -63,8 +63,8 @@ type Config struct {
|
|||||||
IsCpp bool
|
IsCpp bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConverter(config *Config) (*Converter, error) {
|
func NewConverter(config *clangutils.Config) (*Converter, error) {
|
||||||
index, unit, err := CreateTranslationUnit(config)
|
index, unit, err := clangutils.CreateTranslationUnit(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -78,56 +78,6 @@ func NewConverter(config *Config) (*Converter, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit, error) {
|
|
||||||
// default use the c/c++ standard of clang; c:gnu17 c++:gnu++17
|
|
||||||
// https://clang.llvm.org/docs/CommandGuide/clang.html
|
|
||||||
defaultArgs := []string{"-x", "c"}
|
|
||||||
if config.IsCpp {
|
|
||||||
defaultArgs = []string{"-x", "c++"}
|
|
||||||
}
|
|
||||||
allArgs := append(defaultArgs, config.Args...)
|
|
||||||
|
|
||||||
cArgs := make([]*c.Char, len(allArgs))
|
|
||||||
for i, arg := range allArgs {
|
|
||||||
cArgs[i] = c.AllocaCStr(arg)
|
|
||||||
}
|
|
||||||
|
|
||||||
index := clang.CreateIndex(0, 0)
|
|
||||||
|
|
||||||
var unit *clang.TranslationUnit
|
|
||||||
|
|
||||||
if config.Temp {
|
|
||||||
content := c.AllocaCStr(config.File)
|
|
||||||
tempFile := &clang.UnsavedFile{
|
|
||||||
Filename: c.Str("temp.h"),
|
|
||||||
Contents: content,
|
|
||||||
Length: c.Ulong(c.Strlen(content)),
|
|
||||||
}
|
|
||||||
|
|
||||||
unit = index.ParseTranslationUnit(
|
|
||||||
tempFile.Filename,
|
|
||||||
unsafe.SliceData(cArgs), c.Int(len(cArgs)),
|
|
||||||
tempFile, 1,
|
|
||||||
clang.DetailedPreprocessingRecord,
|
|
||||||
)
|
|
||||||
|
|
||||||
} else {
|
|
||||||
cFile := c.AllocaCStr(config.File)
|
|
||||||
unit = index.ParseTranslationUnit(
|
|
||||||
cFile,
|
|
||||||
unsafe.SliceData(cArgs), c.Int(len(cArgs)),
|
|
||||||
nil, 0,
|
|
||||||
clang.DetailedPreprocessingRecord,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if unit == nil {
|
|
||||||
return nil, nil, errors.New("failed to parse translation unit")
|
|
||||||
}
|
|
||||||
|
|
||||||
return index, unit, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ct *Converter) Dispose() {
|
func (ct *Converter) Dispose() {
|
||||||
ct.index.Dispose()
|
ct.index.Dispose()
|
||||||
ct.unit.Dispose()
|
ct.unit.Dispose()
|
||||||
@@ -853,23 +803,10 @@ func (ct *Converter) ProcessBuiltinType(t clang.Type) *ast.BuiltinType {
|
|||||||
// Constructs a complete scoping expression by traversing the semantic parents, starting from the given clang.Cursor
|
// Constructs a complete scoping expression by traversing the semantic parents, starting from the given clang.Cursor
|
||||||
// For anonymous decl of typedef references, use their anonymous name
|
// For anonymous decl of typedef references, use their anonymous name
|
||||||
func (ct *Converter) BuildScopingExpr(cursor clang.Cursor) ast.Expr {
|
func (ct *Converter) BuildScopingExpr(cursor clang.Cursor) ast.Expr {
|
||||||
parts := ct.BuildScopingParts(cursor)
|
parts := clangutils.BuildScopingParts(cursor)
|
||||||
return buildScopingFromParts(parts)
|
return buildScopingFromParts(parts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ct *Converter) BuildScopingParts(cursor clang.Cursor) []string {
|
|
||||||
var parts []string
|
|
||||||
// Traverse up the semantic parents
|
|
||||||
for cursor.IsNull() != 1 && cursor.Kind != clang.CursorTranslationUnit {
|
|
||||||
name := cursor.String()
|
|
||||||
qualified := c.GoString(name.CStr())
|
|
||||||
parts = append([]string{qualified}, parts...)
|
|
||||||
cursor = cursor.SemanticParent()
|
|
||||||
name.Dispose()
|
|
||||||
}
|
|
||||||
return parts
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ct *Converter) MarshalASTFiles() *cjson.JSON {
|
func (ct *Converter) MarshalASTFiles() *cjson.JSON {
|
||||||
return MarshalASTFiles(ct.Files)
|
return MarshalASTFiles(ct.Files)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,11 +8,12 @@ import (
|
|||||||
"github.com/goplus/llgo/c/cjson"
|
"github.com/goplus/llgo/c/cjson"
|
||||||
"github.com/goplus/llgo/c/clang"
|
"github.com/goplus/llgo/c/clang"
|
||||||
"github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse"
|
"github.com/goplus/llgo/chore/_xtool/llcppsigfetch/parse"
|
||||||
|
"github.com/goplus/llgo/chore/_xtool/llcppsymg/clangutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RunTest(testName string, testCases []string) {
|
func RunTest(testName string, testCases []string) {
|
||||||
for i, content := range testCases {
|
for i, content := range testCases {
|
||||||
converter, err := parse.NewConverter(&parse.Config{
|
converter, err := parse.NewConverter(&clangutils.Config{
|
||||||
File: content,
|
File: content,
|
||||||
Temp: true,
|
Temp: true,
|
||||||
IsCpp: true,
|
IsCpp: true,
|
||||||
@@ -63,7 +64,7 @@ type GetTypeOptions struct {
|
|||||||
// e.g. index.Dispose(), unit.Dispose()
|
// e.g. index.Dispose(), unit.Dispose()
|
||||||
func GetType(option *GetTypeOptions) (clang.Type, *clang.Index, *clang.TranslationUnit) {
|
func GetType(option *GetTypeOptions) (clang.Type, *clang.Index, *clang.TranslationUnit) {
|
||||||
code := fmt.Sprintf("%s placeholder;", option.TypeCode)
|
code := fmt.Sprintf("%s placeholder;", option.TypeCode)
|
||||||
index, unit, err := parse.CreateTranslationUnit(&parse.Config{
|
index, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{
|
||||||
File: code,
|
File: code,
|
||||||
Temp: true,
|
Temp: true,
|
||||||
Args: option.Args,
|
Args: option.Args,
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/goplus/llgo/c/cjson"
|
"github.com/goplus/llgo/c/cjson"
|
||||||
|
"github.com/goplus/llgo/chore/_xtool/llcppsymg/clangutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Context struct {
|
type Context struct {
|
||||||
@@ -49,7 +50,7 @@ func (p *Context) processFile(path string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Context) parseFile(path string) ([]*FileEntry, error) {
|
func (p *Context) parseFile(path string) ([]*FileEntry, error) {
|
||||||
converter, err := NewConverter(&Config{
|
converter, err := NewConverter(&clangutils.Config{
|
||||||
File: path,
|
File: path,
|
||||||
Temp: false,
|
Temp: false,
|
||||||
IsCpp: p.IsCpp,
|
IsCpp: p.IsCpp,
|
||||||
|
|||||||
Reference in New Issue
Block a user