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.

WARNING - DEPRECATED

WARNING: This repository is DEPRECATED. Please use instead one of the clang bindings under github.com/go-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

doppioandante avatar pwaller avatar quarnster avatar sbinet avatar zimmski avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-clang's Issues

Better default LD path for darwin.

On all (developer) OSX systems I think this would be a more sane default;

#cgo darwin LDFLAGS: -L/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib

The previous value would be the one to use when you install clang yourself I think?

use go-generate to generate most of the go-clang code

it should be possible to use go-clang (or python-cindex) to generate most of the go-clang code, starting from a parsed representation of a given CIndex API version.

This would tremendously reduce the maintenance load of keeping up with the LLVM/CIndex API churn.

Ideally, just running go generate in go-clang repository would generate the whole set of stubs (and possibly the complete bindings?) for each CIndex version.

Check if a variable declaration is static

I am wondering how I can check if a variable declaration (Type == VarDecl) is static. There is method called "isStaticLocal" in the Clang documentation http://clang.llvm.org/doxygen/classclang_1_1VarDecl.html#a513c56596b4d12e99dd42a4f3ff646d4 but I do not see it in go-clang.

How can I add and use isStaticLocal in go-clang? Can you please document the steps of enhancing go-clang so that I can do it on my own, and send pull request. I am currently a little baffled on how to do that.

handle multiple clang versions

consider using gopkg.in for go get-able multiple versions of the clang C-API, instead of "just" git branches.
the alternative being the creation and curation of a Makefile to discover the user's clang version, creating go build tags...

Broken for non-default clang/llvm libdir

Gentoo installs libclang, etc in /usr/lib/llvm. This means that when you try and build go-clang you get:

/usr/lib/gcc/x86_64-pc-linux-gnu/4.5.4/../../../../x86_64-pc-linux-gnu/bin/ld: warning: libLLVM-3.1.so, needed by /usr/lib64/llvm/libclang.so, not found (try using -rpath or -rpath-link)
/usr/lib64/llvm/libclang.so: undefined reference to `llvm::sys::fs::make_absolute(llvm::SmallVectorImpl<char>&)'
/usr/lib64/llvm/libclang.so: undefined reference to `llvm::EnableStatistics()'
... [and many more]

.. this is sort of fixed by doing:

CGO_LDFLAGS="-Wl,-rpath,$(llvm-config --libdir) $(llvm-config --ldflags)" go get

... in that you get a binary, but then:

$ ldd ./goclang 
linux-vdso.so.1 (0x00007fff15bd3000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f01d0a2b000)
libffi.so.6 => /usr/lib64/libffi.so.6 (0x00007f01d0823000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f01d061f000)
libm.so.6 => /lib64/libm.so.6 (0x00007f01d032a000)
libclang.so => not found
libc.so.6 => /lib64/libc.so.6 (0x00007f01cff80000)
/lib64/ld-linux-x86-64.so.2 (0x00007f01d0c48000)

Will update if I find a solution.

Tests are failing on travis with undeclared identifiers

I hope you don't mind me creating this issue. I'd like to track the test failure.

1: error: 'clang_Cursor_getNumArguments' undeclared (first use in this function)
1: note: each undeclared identifier is reported only once for each function it appears in
1: error: 'clang_Cursor_getArgument' undeclared (first use in this function)
1: error: 'clang_getEnumConstantDeclValue' undeclared (first use in this function)
1: error: 'clang_getEnumConstantDeclUnsignedValue' undeclared (first use in this function)
1: error: 'clang_getEnumDeclIntegerType' undeclared (first use in this function)
1: error: 'clang_getTypedefDeclUnderlyingType' undeclared (first use in this function)

Go 1.6 - panic: runtime error: cgo argument has Go pointer to Go pointer

Hi,

https://github.com/sbinet/go-clang/blob/master/cursor.go#L798

it seems like this line causes issues when passed a closure if building with go 1.6. Using GODEBUG=cgocheck=0 when running works but that's not really a good idea. Passing it a closure is the only way to actually pass arguments to your visitor I think, e.g.

// collectMatchKind collects all or one element name(s) in the sub AST-tree rooted at
// the provided cursor.
func collectMatchKind(entityCursor clang.Cursor, kind clang.CursorKind,
        collectAll bool) (names []string) {

        // Invoke the visitor with this closure.
        entityCursor.Visit(func(cursor, parent clang.Cursor) (
                status clang.ChildVisitResult) {

                if cursor.Kind() == kind {
                        names = append(names, cursor.DisplayName())
                        if !collectAll {
                                return clang.CVR_Break
                        }
                }
                return clang.CVR_Continue
        })
        return // named
}
panic: runtime error: cgo argument has Go pointer to Go pointer

goroutine 1 [running]:
panic(0x42247c0, 0xc82000eae0)
        /usr/local/Cellar/go/1.6/libexec/src/runtime/panic.go:464 +0x3e6
github.com/sbinet/go-clang._cgoCheckPointer0(0x41c8c80, 0xc82002c050, 0xc82000ead0, 0x1, 0x1, 0xc82002c048)
        ??:0 +0x4d
github.com/sbinet/go-clang.Cursor.Visit(0xb, 0x6e24080, 0x1, 0x6095c30, 0xc820014ec0, 0x4236f20)
        /Users/dzan/Source/go/src/github.com/sbinet/go-clang/cursor.go:798 +0x12f
main.collectMatchKind(0xb, 0x6e24080, 0x1, 0x6095c30, 0x28, 0x0, 0x0, 0x0)

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.