-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.go
60 lines (47 loc) · 1.13 KB
/
unit.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
package libclang
// #include <stdlib.h>
// #include <clang-c/Index.h>
import "C"
import (
"fmt"
"path"
"unsafe"
)
type Unit struct {
c C.CXTranslationUnit
}
func ParseUnit(sourceFilepath string) (Unit, error) {
var unit Unit
cFilepath := C.CString(sourceFilepath)
defer C.free(unsafe.Pointer(cFilepath))
resourceDir, err := clangResourceDir()
if err != nil {
return unit, err
}
parseArgs := []*C.char{
C.CString("-I"),
C.CString(path.Join(resourceDir, "include")),
}
defer func() {
for _, cString := range parseArgs {
C.free(unsafe.Pointer(cString))
}
}()
index := C.clang_createIndex(0, 1)
defer C.clang_disposeIndex(index)
parseRes := C.clang_parseTranslationUnit2(
index, cFilepath, &parseArgs[0], C.int(len(parseArgs)), nil, 0,
C.CXTranslationUnit_SkipFunctionBodies, &unit.c,
)
if parseRes != C.CXError_Success {
return unit, fmt.Errorf("non-zero error %d from clang_parseTranslationUnit2 for %s\n",
parseRes, sourceFilepath)
}
return unit, nil
}
func (u Unit) Cursor() Cursor {
return Cursor(C.clang_getTranslationUnitCursor(u.c))
}
func (u Unit) Destroy() {
C.clang_disposeTranslationUnit(u.c)
}