Skip to content

Commit

Permalink
Merge pull request goplus#514 from xushiwei/q
Browse files Browse the repository at this point in the history
c/clang: castdump
  • Loading branch information
xushiwei authored Jul 14, 2024
2 parents 3b5b9c9 + 0b0cecc commit f85aa09
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
26 changes: 24 additions & 2 deletions c/clang/_demo/castdump/astdump.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"fmt"
"os"

"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/clang"
)
Expand All @@ -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()
Expand All @@ -28,20 +39,31 @@ func printAST(cursor clang.Cursor, depth c.Uint) {
}

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)
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.")
c.Exit(1)
}

cursor := unit.Cursor()
println("unit.Cursor done")

printAST(cursor, 0)

unit.Dispose()
Expand Down
7 changes: 7 additions & 0 deletions c/clang/_wrap/llgo_check.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>
#include <clang-c/Index.h>

int main() {
printf("sizeof(clang.Cursor) = %lu\n", sizeof(CXCursor));
return 0;
}
16 changes: 16 additions & 0 deletions c/clang/_wrap/llgo_check.go
Original file line number Diff line number Diff line change
@@ -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{}))
}
6 changes: 3 additions & 3 deletions c/clang/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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() {}
7 changes: 5 additions & 2 deletions c/clang/clang.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

/**
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit f85aa09

Please sign in to comment.