From 0b0cecc2a983e457d8fe00940471b83559b0a76f Mon Sep 17 00:00:00 2001 From: xushiwei Date: Mon, 15 Jul 2024 01:07:26 +0800 Subject: [PATCH] c/clang: castdump --- c/clang/_demo/castdump/astdump.go | 26 ++++++++++++++++++++++++-- c/clang/_wrap/llgo_check.cpp | 7 +++++++ c/clang/_wrap/llgo_check.go | 16 ++++++++++++++++ c/clang/basic.go | 6 +++--- c/clang/clang.go | 7 +++++-- 5 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 c/clang/_wrap/llgo_check.cpp create mode 100644 c/clang/_wrap/llgo_check.go diff --git a/c/clang/_demo/castdump/astdump.go b/c/clang/_demo/castdump/astdump.go index 336c8d6f5..91f3ee5f7 100644 --- a/c/clang/_demo/castdump/astdump.go +++ b/c/clang/_demo/castdump/astdump.go @@ -1,6 +1,9 @@ package main import ( + "fmt" + "os" + "github.com/goplus/llgo/c" "github.com/goplus/llgo/c/clang" ) @@ -13,13 +16,21 @@ func visit(cursor, parent clang.Cursor, clientData c.Pointer) clang.ChildVisitRe func printAST(cursor clang.Cursor, depth c.Uint) { cursorKind := cursor.Kind.String() - cursorSpelling := cursor.String() + println("cursor.Kind.String done") + c.Printf(c.Str("cursorKind = %s\n"), cursorKind.CStr()) + + cursorSpelling := clang.GetCursorSpelling(cursor) // cursor.String() + println("clang.GetCursorSpelling done") for i := c.Uint(0); i < depth; i++ { + println("c.Fputs start") c.Fputs(c.Str(" "), c.Stdout) + println("c.Fputs end") } + println("c.Printf start") c.Printf(c.Str("%s: %s\n"), cursorKind.CStr(), cursorSpelling.CStr()) + println("c.Printf end") cursorKind.Dispose() cursorSpelling.Dispose() @@ -28,13 +39,22 @@ func printAST(cursor clang.Cursor, depth c.Uint) { } func main() { + if c.Argc != 2 { + fmt.Fprintln(os.Stderr, "Usage: castdump ") + return + } + sourceFile := *c.Advance(c.Argv, 1) + index := clang.CreateIndex(0, 0) + println("clang.CreateIndex done") + unit := index.ParseTranslationUnit( - c.Str("todo"), + sourceFile, nil, 0, nil, 0, clang.TranslationUnit_None, ) + println("index.ParseTranslationUnit done") if unit == nil { println("Unable to parse translation unit. Quitting.") @@ -42,6 +62,8 @@ func main() { } cursor := unit.Cursor() + println("unit.Cursor done") + printAST(cursor, 0) unit.Dispose() diff --git a/c/clang/_wrap/llgo_check.cpp b/c/clang/_wrap/llgo_check.cpp new file mode 100644 index 000000000..0eecbb9f2 --- /dev/null +++ b/c/clang/_wrap/llgo_check.cpp @@ -0,0 +1,7 @@ +#include +#include + +int main() { + printf("sizeof(clang.Cursor) = %lu\n", sizeof(CXCursor)); + return 0; +} diff --git a/c/clang/_wrap/llgo_check.go b/c/clang/_wrap/llgo_check.go new file mode 100644 index 000000000..3a96ba2de --- /dev/null +++ b/c/clang/_wrap/llgo_check.go @@ -0,0 +1,16 @@ +package main + +import ( + "unsafe" + + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/clang" +) + +const ( + LLGoCFlags = "-I$(llvm-config --includedir)" +) + +func main() { + c.Printf(c.Str("sizeof(clang.Cursor) = %lu\n"), unsafe.Sizeof(clang.Cursor{})) +} diff --git a/c/clang/basic.go b/c/clang/basic.go index 1d33d1fe9..078bb3f2d 100644 --- a/c/clang/basic.go +++ b/c/clang/basic.go @@ -38,13 +38,13 @@ type String struct { /** * Retrieve the character data associated with the given string. */ -// llgo:link C.clang_getCString +// llgo:link String.CStr C.clang_getCString func (String) CStr() *c.Char { return nil } /** * Free the given string. */ -// llgo:link C.clang_disposeString +// llgo:link String.Dispose C.clang_disposeString func (String) Dispose() {} type StringSet struct { @@ -55,5 +55,5 @@ type StringSet struct { /** * Free the given string set. */ -// llgo:link C.clang_disposeStringSet +// llgo:link (*StringSet).Dispose C.clang_disposeStringSet func (*StringSet) Dispose() {} diff --git a/c/clang/clang.go b/c/clang/clang.go index 6a49ce271..f0a3ccd3c 100644 --- a/c/clang/clang.go +++ b/c/clang/clang.go @@ -152,7 +152,7 @@ type TranslationUnit struct { /** * Destroy the specified CXTranslationUnit object. */ -// llgo:linke (*TranslationUnit).Dispose C.clang_disposeTranslationUnit +// llgo:link (*TranslationUnit).Dispose C.clang_disposeTranslationUnit func (*TranslationUnit) Dispose() {} /** @@ -204,11 +204,14 @@ type Cursor struct { /** * Retrieve a name for the entity referenced by this cursor. */ -// llgo:link C.clang_getCursorSpelling +// llgo:link Cursor.String C.clang_getCursorSpelling func (Cursor) String() (ret String) { return } +//go:linkname GetCursorSpelling C.clang_getCursorSpelling +func GetCursorSpelling(cursor Cursor) String + /** * Describes how the traversal of the children of a particular * cursor should proceed after visiting a particular child cursor.