From 762ed994c14d78f944139456f8a85c4071936e0a Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Thu, 15 Aug 2024 16:08:50 +0800 Subject: [PATCH] llcppsigfetch:Decl Comment --- chore/_xtool/llcppsigfetch/parse/cvt.go | 19 + .../parse/cvt_test/decl_test/decl.go | 46 +++ .../parse/cvt_test/decl_test/llgo.expect | 340 +++++++++++++++++- 3 files changed, 394 insertions(+), 11 deletions(-) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index dd9b282e..19169be3 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "strings" "unsafe" "github.com/goplus/llgo/c" @@ -126,12 +127,30 @@ func (ct *Converter) UpdateCurFile(cursor clang.Cursor) { } func (ct *Converter) CreateDeclBase(cursor clang.Cursor) ast.DeclBase { + rawComment := cursor.RawCommentText() + defer rawComment.Dispose() + + commentGroup := &ast.CommentGroup{} + if rawComment.CStr() != nil { + commentGroup = ct.ParseComment(c.GoString(rawComment.CStr())) + } + return ast.DeclBase{ Loc: &ct.curLoc, Parent: ct.GetCurScope(), + Doc: commentGroup, } } +func (ct *Converter) ParseComment(rawComment string) *ast.CommentGroup { + lines := strings.Split(rawComment, "\n") + commentGroup := &ast.CommentGroup{} + for _, line := range lines { + commentGroup.List = append(commentGroup.List, &ast.Comment{Text: line}) + } + return commentGroup +} + // visit top decls (struct,class,function,enum & marco,include) func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVisitResult { ct := (*Converter)(clientData) 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 2391b8dc..4decd928 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/decl.go @@ -8,6 +8,7 @@ import ( func main() { TestFuncDecl() TestScope() + TestComment() } func TestFuncDecl() { @@ -72,3 +73,48 @@ func TestScope() { converter.Dispose() } } +func TestComment() { + testCases := []string{ + `// not read comment 1 + void foo();`, + `/* not read comment 2 */ + void foo();`, + `/// comment + void foo();`, + `/** comment */ + void foo();`, + `/*! comment */ + void foo();`, + `/// comment 1 +/// comment 2 +void foo();`, + `/*! comment 1 */ +/*! comment 2 */ +void foo();`, + `/** comment 1 */ +/** comment 1 */ +void foo();`, + `/** + * comment 1 + * comment 2 + */ +void foo();`, + } + + 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.GetFilesJSON() + c.Printf(c.Str("TestComment Case %d:\n%s\n\n"), c.Int(i+1), json.Print()) + + converter.Dispose() + } +} 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 206886c3..87b9e634 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/llgo.expect @@ -7,7 +7,9 @@ TestFuncDecl Case 1: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -35,7 +37,9 @@ TestFuncDecl Case 2: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -73,7 +77,9 @@ TestFuncDecl Case 3: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -121,7 +127,9 @@ TestFuncDecl Case 4: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -171,7 +179,9 @@ TestFuncDecl Case 5: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -223,7 +233,9 @@ TestFuncDecl Case 6: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -279,7 +291,9 @@ TestFuncDecl Case 7: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -332,7 +346,9 @@ TestFuncDecl Case 8: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -384,7 +400,9 @@ TestScope Case 1: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": null, "Name": { "Name": "foo" @@ -412,7 +430,9 @@ TestScope Case 2: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": { "Name": "a" }, @@ -442,7 +462,9 @@ TestScope Case 3: "Loc": { "File": "temp.h" }, - "Doc": null, + "Doc": { + "List": [] + }, "Parent": { "X": { "Name": "b" @@ -469,6 +491,302 @@ TestScope Case 3: } } +TestComment Case 1: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 2: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 3: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/// comment" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 4: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/** comment */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 5: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/*! comment */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 6: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/// comment 1" + }, { + "Text": "/// comment 2" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 7: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/*! comment 1 */" + }, { + "Text": "/*! comment 2 */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 8: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/** comment 1 */" + }, { + "Text": "/** comment 1 */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + +TestComment Case 9: +{ + "temp.h": { + "path": "temp.h", + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [{ + "Text": "/**" + }, { + "Text": " * comment 1" + }, { + "Text": " * comment 2" + }, { + "Text": " */" + }] + }, + "Parent": null, + "Name": { + "Name": "foo" + }, + "Type": { + "Params": { + "List": [] + }, + "Ret": { + "Kind": 0, + "Flags": 0 + } + } + }], + "includes": [], + "macros": [] + } +} + #stderr