From 6b1bc15f37bf7f7bbdca7f890031a86d5dc943b2 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 14 Aug 2024 18:22:14 +0800 Subject: [PATCH] llcppsigfetch:Constant & Incomplete Array --- chore/_xtool/llcppsigfetch/parse/cvt.go | 17 ++- .../parse/cvt_test/decl_test/decl.go | 3 + .../parse/cvt_test/decl_test/llgo.expect | 101 ++++++++++++++++++ chore/_xtool/llcppsigfetch/parse/dump.go | 6 ++ 4 files changed, 123 insertions(+), 4 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index cd9fb2e2..5e5a84c0 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -147,11 +147,20 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { expr = &ast.FuncType{Ret: ret} case clang.TypeTypedef: expr = ct.ProcessType(t.CanonicalType()) - case clang.TypeConstantArray, clang.TypeVariableArray, clang.TypeIncompleteArray, clang.TypeDependentSizedArray: - expr = &ast.ArrayType{ - Elt: ct.ProcessType(t.ArrayElementType()), + case clang.TypeConstantArray, clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray: + if t.Kind == clang.TypeConstantArray { + valueStr := make([]c.Char, 20) + c.Sprintf(unsafe.SliceData(valueStr), c.Str("%d"), t.ArraySize()) + expr = &ast.ArrayType{ + Elt: ct.ProcessType(t.ArrayElementType()), + Len: &ast.BasicLit{Kind: ast.IntLit, Value: c.GoString(unsafe.SliceData(valueStr))}, + } + } else if t.Kind == clang.TypeIncompleteArray { + // incomplete array havent len expr + expr = &ast.ArrayType{ + Elt: ct.ProcessType(t.ArrayElementType()), + } } - // todo(zzy):array length } return expr } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go index 848cdf77..d76bd84f 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -18,6 +18,9 @@ func TestFuncDecl() { `void foo(char* str, double x);`, `float* foo(char* str, double x);`, `float* foo(char*** str, double x);`, + + `float* foo(char str[], double x);`, + `float* foo(int arr[3][4]);`, } for i, content := range testCases { diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect index 244091c2..f39f47d7 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -259,6 +259,107 @@ Test Case 6: } } +Test Case 7: +{ + "temp.h": { + "Path": "temp.h", + "decls": [{ + "Loc": null, + "Doc": null, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Elt": { + "Kind": 2, + "Flags": 1 + }, + "Len": null + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "str" + }] + }, { + "Type": { + "Kind": 8, + "Flags": 16 + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "x" + }] + }] + }, + "Ret": { + "X": { + "Kind": 8, + "Flags": 0 + } + } + } + }], + "includes": [], + "macros": [] + } +} + +Test Case 8: +{ + "temp.h": { + "Path": "temp.h", + "decls": [{ + "Loc": null, + "Doc": null, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [{ + "Type": { + "Elt": { + "Elt": { + "Kind": 6, + "Flags": 0 + }, + "Len": { + "Kind": 0, + "Value": "4" + } + }, + "Len": { + "Kind": 0, + "Value": "3" + } + }, + "Doc": null, + "Comment": null, + "Names": [{ + "Name": "arr" + }] + }] + }, + "Ret": { + "X": { + "Kind": 8, + "Flags": 0 + } + } + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go index 7c1dc9d7..5b0ce8ac 100644 --- a/chore/_xtool/llcppsigfetch/parse/dump.go +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -87,8 +87,14 @@ func (ct *Converter) TypeJSON(t ast.Expr) *cjson.JSON { root.SetItem(c.Str("Names"), names) case *ast.Ident: root.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(d.Name))) + case *ast.BasicLit: + root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) + root.SetItem(c.Str("Value"), cjson.String(c.AllocaCStr(d.Value))) case *ast.PointerType: root.SetItem(c.Str("X"), ct.TypeJSON(d.X)) + case *ast.ArrayType: + root.SetItem(c.Str("Elt"), ct.TypeJSON(d.Elt)) + root.SetItem(c.Str("Len"), ct.TypeJSON(d.Len)) case *ast.BuiltinType: root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) root.SetItem(c.Str("Flags"), cjson.Number(float64(d.Flags)))