-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
90 lines (66 loc) · 1.94 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// 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/go-clang/bootstrap/clang"
)
var fname = flag.String("fname", "", "the file to analyze")
func main() {
os.Exit(cmd(os.Args[1:]))
}
func cmd(args []string) int {
fmt.Printf(":: go-clang-dump...\n")
if err := flag.CommandLine.Parse(args); err != nil {
fmt.Printf("ERROR: %s", err)
return 1
}
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")
return 1
}
idx := clang.NewIndex(0, 1)
defer idx.Dispose()
tuArgs := []string{}
if len(flag.Args()) > 0 && flag.Args()[0] == "-" {
tuArgs = make([]string, len(flag.Args()[1:]))
copy(tuArgs, flag.Args()[1:])
}
tu := idx.ParseTranslationUnit(*fname, tuArgs, nil, 0)
defer tu.Dispose()
fmt.Printf("tu: %s\n", tu.Spelling())
diagnostics := tu.Diagnostics()
for _, d := range diagnostics {
fmt.Println("PROBLEM:", d.Spelling())
}
cursor := tu.TranslationUnitCursor()
fmt.Printf("cursor-isnull: %v\n", cursor.IsNull())
fmt.Printf("cursor: %s\n", cursor.Spelling())
fmt.Printf("cursor-kind: %s\n", cursor.Kind().Spelling())
fmt.Printf("tu-fname: %s\n", tu.File(*fname).Name())
cursor.Visit(func(cursor, parent clang.Cursor) clang.ChildVisitResult {
if cursor.IsNull() {
fmt.Printf("cursor: <none>\n")
return clang.ChildVisit_Continue
}
fmt.Printf("%s: %s (%s)\n", cursor.Kind().Spelling(), cursor.Spelling(), cursor.USR())
switch cursor.Kind() {
case clang.Cursor_ClassDecl, clang.Cursor_EnumDecl, clang.Cursor_StructDecl, clang.Cursor_Namespace:
return clang.ChildVisit_Recurse
}
return clang.ChildVisit_Continue
})
if len(diagnostics) > 0 {
fmt.Println("NOTE: There were problems while analyzing the given file")
}
fmt.Printf(":: bye.\n")
return 0
}