-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
72 lines (59 loc) · 1.57 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// go-clang-dump shows how to dump the AST of a C/C++ file via the Cursor
// visitor API.
//
// ex:
// $ go-clang-dump -fname=foo.cxx
package main
import (
"flag"
"fmt"
"os"
"github.com/sbinet/go-clang"
)
var fname *string = flag.String("fname", "", "the file to analyze")
func main() {
fmt.Printf(":: go-clang-dump...\n")
flag.Parse()
fmt.Printf(":: fname: %s\n", *fname)
fmt.Printf(":: args: %v\n", flag.Args())
if *fname == "" {
flag.Usage()
fmt.Printf("please provide a file name to analyze\n")
os.Exit(1)
}
idx := clang.NewIndex(0, 1)
defer idx.Dispose()
nidx := 0
args := []string{}
if len(flag.Args()) > 0 && flag.Args()[0] == "-" {
nidx = 1
args = make([]string, len(flag.Args()[nidx:]))
copy(args, flag.Args()[nidx:])
}
tu := idx.Parse(*fname, args, nil, 0)
defer tu.Dispose()
fmt.Printf("tu: %s\n", tu.Spelling())
cursor := tu.ToCursor()
fmt.Printf("cursor-isnull: %v\n", cursor.IsNull())
fmt.Printf("cursor: %s\n", cursor.Spelling())
fmt.Printf("cursor-kind: %s\n", cursor.Kind().Spelling())
tu_fname := tu.File(*fname).Name()
fmt.Printf("tu-fname: %s\n", tu_fname)
fct := func(cursor, parent clang.Cursor) clang.ChildVisitResult {
if cursor.IsNull() {
fmt.Printf("cursor: <none>\n")
return clang.CVR_Continue
}
fmt.Printf("%s: %s (%s)\n",
cursor.Kind().Spelling(), cursor.Spelling(), cursor.USR())
switch cursor.Kind() {
case clang.CK_ClassDecl, clang.CK_EnumDecl,
clang.CK_StructDecl, clang.CK_Namespace:
return clang.CVR_Recurse
}
return clang.CVR_Continue
}
cursor.Visit(fct)
fmt.Printf(":: bye.\n")
}
// EOF