This repository was archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathclang.go
182 lines (167 loc) · 4.76 KB
/
clang.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
170
171
172
173
174
175
176
177
178
179
180
181
182
package clang
import (
"github.com/limetext/log4go"
"fmt"
cp "github.com/quarnster/completion/clang/parser"
"github.com/quarnster/completion/content"
"github.com/quarnster/completion/util/expand_path"
"github.com/quarnster/parser"
"io/ioutil"
"os/exec"
"reflect"
"regexp"
"strconv"
)
func init() {
if err := content.RegisterType("compiler_flags", reflect.TypeOf([]string{})); err != nil {
panic(err)
}
}
func RunClang(stdin string, args ...string) ([]byte, []byte, error) {
cmd := exec.Command("clang", args...)
log4go.Debug("Running clang command: %v", cmd)
if in, err := cmd.StdinPipe(); err != nil {
return nil, nil, err
} else if e, err := cmd.StderrPipe(); err != nil {
return nil, nil, err
} else if s, err := cmd.StdoutPipe(); err != nil {
return nil, nil, err
} else if err := cmd.Start(); err != nil {
return nil, nil, err
} else {
in.Write([]byte(stdin))
in.Close()
so, serr := ioutil.ReadAll(s)
eo, eerr := ioutil.ReadAll(e)
// We ignore the output error here as a non-zero exit
// code doesn't necessarily mean that the output isn't
// useful
cmd.Wait()
log4go.Fine("stdout: %s\n", string(so))
log4go.Fine("stderr: %s\n", string(eo))
if serr != nil {
return so, eo, serr
} else if eerr != nil {
return so, eo, eerr
}
return so, eo, nil
}
}
func data(n *parser.Node) string {
switch n.Name {
default:
return n.Data()
case "InheritedName":
return data(n.Children[1])
case "OptionalArgument":
return data(n.Children[0])
}
}
func parseresult(in string) (ret content.CompletionResult, err error) {
var p cp.PARSER
p.Parse(in)
n := p.RootNode()
for i := range n.Children {
child := n.Children[i]
switch child.Name {
case "Variable":
v := content.Field{}
v.Type.Name.Relative = child.Children[0].Data()
v.Name.Relative = data(child.Children[1])
ret.Fields = append(ret.Fields, v)
case "CFunction":
f := content.Method{}
f.Returns = append(f.Returns, content.Variable{Type: content.Type{Name: content.FullyQualifiedName{Relative: child.Children[0].Data()}}})
f.Name.Relative = data(child.Children[1])
args := child.Children[2:]
for j := range args {
if args[j].Name == "ConstQualifier" {
// TODO
break
}
p := content.Variable{}
p.Type.Name.Relative = data(args[j])
f.Parameters = append(f.Parameters, p)
}
ret.Methods = append(ret.Methods, f)
case "ObjCFunction":
f := content.Method{}
f.Returns = append(f.Returns, content.Variable{Type: content.Type{Name: content.FullyQualifiedName{Relative: child.Children[1].Data()}}})
args := child.Children[2:]
for j := range args {
p := content.Variable{}
p.Type.Name.Relative = data(args[j].Children[1])
p.Name.Relative = data(args[j].Children[0])
f.Parameters = append(f.Parameters, p)
}
ret.Methods = append(ret.Methods, f)
}
}
return
}
type Clang struct {
}
func (c *Clang) prepare(a *content.CompleteAtArgs) (fn string, args []string, err error) {
origargs, _ := a.Settings().Get("compiler_flags").([]string)
args = make([]string, len(origargs))
for i := range origargs {
args[i] = expand_path.ExpandPath(origargs[i])
}
fn = a.Location.File.Name
if a.Location.File.Contents != "" {
// File is unsaved, so use stdin as the filename
fn = "-"
}
return fn, args, nil
}
func (c *Clang) GetDefinition(a *content.GetDefinitionArgs, ret *content.SourceLocation) error {
fn, args, err := c.prepare(&a.CompleteAtArgs)
if err != nil {
return err
}
args = append([]string{"-fsyntax-only", "-Xclang", "-ast-dump", "-Xclang", "-ast-dump-filter", "-Xclang", a.Identifier}, args...)
args = append(args, fn)
out, oute, err := RunClang(a.Location.File.Contents, args...)
if len(out) == 0 {
if err != nil {
return err
} else {
return fmt.Errorf("%s", oute)
}
}
re, err := regexp.Compile(`\w+Decl[^<]+<(..[^:,]+):?(\d+)?:?(\d+)?.*?\s` + a.Identifier + `\s`)
if err != nil {
return err
}
res := re.FindAllStringSubmatch(string(out), -1)
if len(res) == 0 {
return fmt.Errorf("Not found")
}
ret.File.Name = res[0][1]
i, _ := strconv.ParseInt(res[0][2], 10, 32)
ret.Line = uint(i)
i, _ = strconv.ParseInt(res[0][3], 10, 32)
ret.Column = uint(i)
return nil
}
func (c *Clang) CompleteAt(a *content.CompleteAtArgs, ret *content.CompletionResult) error {
fn, args, err := c.prepare(a)
if err != nil {
return err
}
args = append([]string{"-fsyntax-only", "-Xclang", fmt.Sprintf("-code-completion-at=%s:%d:%d", fn, a.Location.Line, a.Location.Column)}, args...)
args = append(args, fn)
if out, oute, err := RunClang(a.Location.File.Contents, args...); len(out) == 0 {
if err != nil {
return err
} else {
return fmt.Errorf("%s", oute)
}
} else if r, err := parseresult(string(out)); err != nil {
return err
} else {
*ret = r
ret.Messages = string(oute)
return nil
}
}