Giter VIP home page Giter VIP logo

go-clang's Introduction

go-clang

Build Status Build Status

Naive Go bindings to the C-API of CLang.

Installation

As there is no pkg-config entry for clang, you may have to tinker a bit the various CFLAGS and LDFLAGS options, or pass them via the shell:

$ CGO_CFLAGS="-I`llvm-config --includedir`" \
  CGO_LDFLAGS="-L`llvm-config --libdir`" \
  go get github.com/sbinet/go-clang

Example

An example on how to use the AST visitor of CLang is provided here:

https://github.com/sbinet/go-clang/blob/master/go-clang-dump/main.go

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")
}

which can be installed like so:

$ go get github.com/sbinet/go-clang/go-clang-dump

Limitations

  • Only a subset of the C-API of CLang has been provided yet. More will come as patches flow in and time goes by.

  • Go-doc documentation is lagging (but the doxygen docs from the C-API of CLang are in the .go files)

Documentation

Is available at godoc:

http://godoc.org/github.com/sbinet/go-clang

go-clang's People

Contributors

pwaller avatar quarnster avatar sbinet avatar sridhar-surul avatar zimmski avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

oandrew

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.