-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfunction.go
169 lines (143 loc) · 3.54 KB
/
function.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
// #cgo LDFLAGS: -lclang
// #cgo CFLAGS: -std=c11
// #include <stdlib.h>
// #include <errno.h>
// #include "function.h"
import "C"
import (
"fmt"
"path"
"strings"
"unsafe"
"github.com/libgit2/git2go"
"github.com/sbinet/go-clang"
)
var DisableFunctionAnalysis = false
type Function struct {
Id int64 `db:"id" table:"functions"`
CommitId int64 `db:"commit_id"`
Name string `db:"name"`
FileName string `db:"file_name"`
StartLine uint `db:"start_line"`
EndLine uint `db:"end_line"`
State string `db:"state"` // can be "added", "modified", "deleted"
}
type Functions struct {
data map[string](*Function)
functions [](*Function)
}
type EmtyFunctionIterator func(line int) (f *Function, ok bool)
func NewFunctions() *Functions {
fs := &Functions{
data: make(map[string]*Function),
functions: make([](*Function), 0, 10),
}
return fs
}
func NewFunctionsFromC(fa *C.functions_array) *Functions {
fs := NewFunctions()
faLen := int(fa.len)
for i := 0; i < faLen; i++ {
f := C.fa_at(fa, C.size_t(i))
if f.start_line+1 >= f.end_line { // ignore declarations
continue
}
fs.Add(&Function{
Name: C.GoString(f.name),
StartLine: uint(f.start_line),
EndLine: uint(f.end_line),
})
}
return fs
}
func (fs *Functions) NewEmptyFunctionIterator() EmtyFunctionIterator {
var (
i = 0
)
return func(line int) (f *Function, ok bool) {
for ; i < len(fs.functions); i++ {
f := fs.functions[i]
if f.State == "" && line <= int(f.EndLine) {
return f, true
}
}
return nil, false
}
}
func (fs *Functions) Functions() (fa []*Function) {
return fs.functions
}
func (fs *Functions) Add(f *Function) {
fs.data[f.Name] = f
fs.functions = append(fs.functions, f)
}
func (fs *Functions) Data() map[string]*Function {
return fs.data
}
func AddedAndDeletedFunctions(newFs, oldFs *Functions) []*Function {
fs := make([]*Function, 0, len(newFs.functions))
for name, f := range newFs.data {
if _, found := oldFs.data[name]; !found {
f.State = "added"
fs = append(fs, f)
}
}
for name, f := range oldFs.data {
if _, found := newFs.data[name]; !found {
f.State = "deleted"
fs = append(fs, f)
}
}
return fs
}
func (fs *Functions) String() string {
var ss []string
for _, f := range fs.functions {
ss = append(ss, f.String())
}
return "\n" + strings.Join(ss, "\n")
}
func (f *Function) GetId() int64 {
return f.Id
}
func (f *Function) String() string {
return fmt.Sprintf("%s (%s): %s:%d:%d", f.Name, f.State, f.FileName, f.StartLine, f.EndLine)
}
func (f *Function) ContainsLine(line int) bool {
return int(f.StartLine) <= line && line <= int(f.EndLine)
}
func functionsForFilename(fname string, unsaved clang.UnsavedFiles) (functions *Functions, err error) {
cfname := C.CString(fname)
defer C.free(unsafe.Pointer(cfname))
contents := C.CString(unsaved[fname])
defer C.free(unsafe.Pointer(contents))
fa, err := C.get_functions(cfname, contents, C.ulong(len(unsaved[fname])))
if err != nil {
return nil, err
}
defer C.fa_free(fa)
functions = NewFunctionsFromC(fa)
return
}
func FunctionsForFile(repo *git.Repository, file *git.DiffFile) (functions *Functions, err error) {
if DisableFunctionAnalysis {
return NewFunctions(), nil
}
blob, err := repo.LookupBlob(file.Oid)
if err != nil {
return
}
fname := path.Join(repo.Workdir(), file.Path)
functions, err = functionsForFilename(
fname,
map[string]string{fname: string(blob.Contents())},
)
if err != nil {
return nil, err
}
for _, f := range functions.functions {
f.FileName = file.Path
}
return
}