c/clang: castdump

This commit is contained in:
xushiwei
2024-07-15 01:07:26 +08:00
parent cbe190fa70
commit 0b0cecc2a9
5 changed files with 55 additions and 7 deletions

View File

@@ -1,6 +1,9 @@
package main package main
import ( import (
"fmt"
"os"
"github.com/goplus/llgo/c" "github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/clang" "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) { func printAST(cursor clang.Cursor, depth c.Uint) {
cursorKind := cursor.Kind.String() 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++ { for i := c.Uint(0); i < depth; i++ {
println("c.Fputs start")
c.Fputs(c.Str(" "), c.Stdout) 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()) c.Printf(c.Str("%s: %s\n"), cursorKind.CStr(), cursorSpelling.CStr())
println("c.Printf end")
cursorKind.Dispose() cursorKind.Dispose()
cursorSpelling.Dispose() cursorSpelling.Dispose()
@@ -28,13 +39,22 @@ func printAST(cursor clang.Cursor, depth c.Uint) {
} }
func main() { func main() {
if c.Argc != 2 {
fmt.Fprintln(os.Stderr, "Usage: castdump <headerFile>")
return
}
sourceFile := *c.Advance(c.Argv, 1)
index := clang.CreateIndex(0, 0) index := clang.CreateIndex(0, 0)
println("clang.CreateIndex done")
unit := index.ParseTranslationUnit( unit := index.ParseTranslationUnit(
c.Str("todo"), sourceFile,
nil, 0, nil, 0,
nil, 0, nil, 0,
clang.TranslationUnit_None, clang.TranslationUnit_None,
) )
println("index.ParseTranslationUnit done")
if unit == nil { if unit == nil {
println("Unable to parse translation unit. Quitting.") println("Unable to parse translation unit. Quitting.")
@@ -42,6 +62,8 @@ func main() {
} }
cursor := unit.Cursor() cursor := unit.Cursor()
println("unit.Cursor done")
printAST(cursor, 0) printAST(cursor, 0)
unit.Dispose() unit.Dispose()

View File

@@ -0,0 +1,7 @@
#include <stdio.h>
#include <clang-c/Index.h>
int main() {
printf("sizeof(clang.Cursor) = %lu\n", sizeof(CXCursor));
return 0;
}

View File

@@ -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{}))
}

View File

@@ -38,13 +38,13 @@ type String struct {
/** /**
* Retrieve the character data associated with the given string. * 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 } func (String) CStr() *c.Char { return nil }
/** /**
* Free the given string. * Free the given string.
*/ */
// llgo:link C.clang_disposeString // llgo:link String.Dispose C.clang_disposeString
func (String) Dispose() {} func (String) Dispose() {}
type StringSet struct { type StringSet struct {
@@ -55,5 +55,5 @@ type StringSet struct {
/** /**
* Free the given string set. * Free the given string set.
*/ */
// llgo:link C.clang_disposeStringSet // llgo:link (*StringSet).Dispose C.clang_disposeStringSet
func (*StringSet) Dispose() {} func (*StringSet) Dispose() {}

View File

@@ -152,7 +152,7 @@ type TranslationUnit struct {
/** /**
* Destroy the specified CXTranslationUnit object. * Destroy the specified CXTranslationUnit object.
*/ */
// llgo:linke (*TranslationUnit).Dispose C.clang_disposeTranslationUnit // llgo:link (*TranslationUnit).Dispose C.clang_disposeTranslationUnit
func (*TranslationUnit) Dispose() {} func (*TranslationUnit) Dispose() {}
/** /**
@@ -204,11 +204,14 @@ type Cursor struct {
/** /**
* Retrieve a name for the entity referenced by this cursor. * 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) { func (Cursor) String() (ret String) {
return return
} }
//go:linkname GetCursorSpelling C.clang_getCursorSpelling
func GetCursorSpelling(cursor Cursor) String
/** /**
* Describes how the traversal of the children of a particular * Describes how the traversal of the children of a particular
* cursor should proceed after visiting a particular child cursor. * cursor should proceed after visiting a particular child cursor.