go-clang / cmd / go-clang-dump / main.go

// 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 (


var fname *string = flag.String("fname", "", "the file to analyze")

func main() {
	fmt.Printf(":: go-clang-dump...\n")
	fmt.Printf(":: fname: %s\n", *fname)
	fmt.Printf(":: args: %v\n", flag.Args())
	if *fname == "" {
		fmt.Printf("please provide a file name to analyze\n")
	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, 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


	fmt.Printf(":: bye.\n")

// EOF
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.