llcppsigfetch:func pointer

This commit is contained in:
luoliwoshang
2024-08-27 15:14:44 +08:00
parent a897683272
commit e57ee17532
7 changed files with 186 additions and 3 deletions

View File

@@ -217,8 +217,7 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr {
expr = &ast.LvalueRefType{X: ct.ProcessType(t.NonReferenceType())}
case clang.TypeFunctionProto:
// function type will only collect return type, params will be collected in ProcessFuncDecl
ret := ct.ProcessType(t.ResultType())
expr = &ast.FuncType{Ret: ret}
expr = ct.ProcessFunctionType(t)
case clang.TypeConstantArray, clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray:
if t.Kind == clang.TypeConstantArray {
len := (*c.Char)(c.Malloc(unsafe.Sizeof(c.Char(0)) * 20))
@@ -238,6 +237,30 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr {
return expr
}
// For function types, we can only obtain the parameter types, but not the parameter names.
// This is because we cannot reverse-lookup the corresponding declaration node from a function type.
// Note: For function declarations, parameter names are collected in the ProcessFuncDecl method.
func (ct *Converter) ProcessFunctionType(t clang.Type) *ast.FuncType {
// Note: Attempting to get the type declaration for a function type will result in CursorNoDeclFound
// cursor := t.TypeDeclaration()
// This would return CursorNoDeclFound
ret := ct.ProcessType(t.ResultType())
params := &ast.FieldList{List: make([]*ast.Field, 0)}
numArgs := t.NumArgTypes()
for i := 0; i < int(numArgs); i++ {
argType := t.ArgType(c.Uint(i))
params.List = append(params.List, &ast.Field{
Type: ct.ProcessType(argType),
})
}
return &ast.FuncType{
Ret: ret,
Params: params,
}
}
func (ct *Converter) ProcessTypeDefDecl(cursor clang.Cursor) *ast.TypedefDecl {
name := cursor.String()
defer name.Dispose()
@@ -260,7 +283,6 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl {
fmt.Println("failed to process function type")
return nil
}
params := ct.ProcessFieldList(cursor)
funcType.Params = params
fn := &ast.FuncDecl{

View File

@@ -148,6 +148,84 @@ TestStructDecl Case 3:
}
}
TestStructDecl Case 4:
{
"temp.h": {
"decls": [{
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "A"
},
"Type": {
"Tag": 0,
"Fields": {
"List": [{
"Type": {
"Kind": 6,
"Flags": 0
},
"Doc": {
"List": []
},
"Comment": {
"List": []
},
"Names": [{
"Name": "a"
}]
}, {
"Type": {
"X": {
"Params": {
"List": [{
"Type": {
"Kind": 6,
"Flags": 0
},
"Doc": null,
"Comment": null,
"Names": []
}, {
"Type": {
"Kind": 6,
"Flags": 0
},
"Doc": null,
"Comment": null,
"Names": []
}]
},
"Ret": {
"Kind": 6,
"Flags": 0
}
}
},
"Doc": {
"List": []
},
"Comment": {
"List": []
},
"Names": [{
"Name": "Foo"
}]
}]
},
"Methods": []
}
}],
"includes": [],
"macros": []
}
}
#stderr

View File

@@ -18,6 +18,10 @@ func TestStructDecl() {
`struct A {
int a, b;
};`,
`struct A {
int a;
int (*Foo)(int, int);
};`,
}
test.RunTest("TestStructDecl", testCases)
}

View File

@@ -61,6 +61,53 @@ TestTypeDefDecl Case 2:
}
}
TestTypeDefDecl Case 3:
{
"temp.h": {
"decls": [{
"Loc": {
"File": "temp.h"
},
"Doc": {
"List": []
},
"Parent": null,
"Name": {
"Name": "Foo"
},
"Type": {
"X": {
"Params": {
"List": [{
"Type": {
"Kind": 6,
"Flags": 0
},
"Doc": null,
"Comment": null,
"Names": []
}, {
"Type": {
"Kind": 6,
"Flags": 0
},
"Doc": null,
"Comment": null,
"Names": []
}]
},
"Ret": {
"Kind": 6,
"Flags": 0
}
}
}
}],
"includes": [],
"macros": []
}
}
#stderr

View File

@@ -12,6 +12,8 @@ func TestTypeDefDecl() {
`typedef int INT;
typedef INT STANDARD_INT;`,
`typedef int (*Foo)(int, int);`,
}
test.RunTest("TestTypeDefDecl", testCases)
}

View File

@@ -169,6 +169,34 @@ Type: class a::b::c:
},
"Tag": 3
}
Type: int (*)(int, char):
{
"X": {
"Params": {
"List": [{
"Type": {
"Kind": 6,
"Flags": 0
},
"Doc": null,
"Comment": null,
"Names": []
}, {
"Type": {
"Kind": 2,
"Flags": 1
},
"Doc": null,
"Comment": null,
"Names": []
}]
},
"Ret": {
"Kind": 6,
"Flags": 0
}
}
}
#stderr
todo: unknown builtin type: Ibm128

View File

@@ -111,6 +111,8 @@ func TestNonBuiltinTypes() {
}
}
class a::b::c`,
`int (*p)(int, char);`,
}
for _, t := range tests {