llcppsigfetch:include

This commit is contained in:
luoliwoshang
2024-08-18 11:16:43 +08:00
parent 02651c93a7
commit 9d16df5f25
4 changed files with 104 additions and 4 deletions

View File

@@ -160,9 +160,8 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi
switch cursor.Kind { switch cursor.Kind {
case clang.CursorInclusionDirective: case clang.CursorInclusionDirective:
// todo(zzy) ct.ProcessInclude(cursor)
case clang.CursorMacroDefinition: case clang.CursorMacroDefinition:
// todo(zzy)
ct.ProcessMarco(cursor) ct.ProcessMarco(cursor)
case clang.CursorEnumDecl: case clang.CursorEnumDecl:
// todo(zzy) // todo(zzy)
@@ -277,6 +276,12 @@ func (ct *Converter) ProcessMarco(cursor clang.Cursor) {
ct.curFile.Macros = append(ct.curFile.Macros, macro) ct.curFile.Macros = append(ct.curFile.Macros, macro)
} }
func (ct *Converter) ProcessInclude(cursor clang.Cursor) {
name := cursor.String()
defer name.Dispose()
ct.curFile.Includes = append(ct.curFile.Includes, &ast.Include{Path: c.GoString(name.CStr())})
}
type visitFieldContext struct { type visitFieldContext struct {
params *ast.FieldList params *ast.FieldList
converter *Converter converter *Converter

View File

@@ -8,7 +8,7 @@ TestDefine Case 1:
"macros": [{ "macros": [{
"Name": { "Name": {
"Token": 2, "Token": 2,
"Lit": "foo" "Lit": "OK"
}, },
"Body": [{ "Body": [{
"Token": 3, "Token": 3,
@@ -18,6 +18,70 @@ TestDefine Case 1:
} }
} }
TestDefine Case 2:
{
"temp.h": {
"path": "temp.h",
"decls": [],
"includes": [],
"macros": [{
"Name": {
"Token": 2,
"Lit": "SQUARE"
},
"Body": [{
"Token": 0,
"Lit": "("
}, {
"Token": 2,
"Lit": "x"
}, {
"Token": 0,
"Lit": ")"
}, {
"Token": 0,
"Lit": "("
}, {
"Token": 0,
"Lit": "("
}, {
"Token": 2,
"Lit": "x"
}, {
"Token": 0,
"Lit": ")"
}, {
"Token": 0,
"Lit": "*"
}, {
"Token": 0,
"Lit": "("
}, {
"Token": 2,
"Lit": "x"
}, {
"Token": 0,
"Lit": ")"
}, {
"Token": 0,
"Lit": ")"
}]
}]
}
}
TestInclude Case 1:
{
"temp.h": {
"path": "temp.h",
"decls": [],
"includes": [{
"Path": "foo.h"
}],
"macros": []
}
}
#stderr #stderr

View File

@@ -7,11 +7,13 @@ import (
func main() { func main() {
TestDefine() TestDefine()
TestInclude()
} }
func TestDefine() { func TestDefine() {
testCases := []string{ testCases := []string{
`#define foo 1`, `#define OK 1`,
`#define SQUARE(x) ((x) * (x))`,
} }
for i, content := range testCases { for i, content := range testCases {
@@ -31,3 +33,27 @@ func TestDefine() {
converter.Dispose() converter.Dispose()
} }
} }
func TestInclude() {
testCases := []string{
`#include "foo.h"`,
// `#include <limits.h>`, // Standard libraries are mostly platform-dependent
}
for i, content := range testCases {
converter, err := parse.NewConverter(content, true)
if err != nil {
panic(err)
}
_, err = converter.Convert()
if err != nil {
panic(err)
}
json := converter.MarshalASTFiles()
c.Printf(c.Str("TestInclude Case %d:\n%s\n\n"), c.Int(i+1), json.Print())
converter.Dispose()
}
}

View File

@@ -28,6 +28,11 @@ func MarshalASTFile(file *ast.File) *cjson.JSON {
// json:includes,omitempty // json:includes,omitempty
if file.Includes != nil { if file.Includes != nil {
includes := cjson.Array() includes := cjson.Array()
for _, i := range file.Includes {
include := cjson.Object()
include.SetItem(c.Str("Path"), cjson.String(c.AllocaCStr(i.Path)))
includes.AddItem(include)
}
root.SetItem(c.Str("includes"), includes) root.SetItem(c.Str("includes"), includes)
} }