-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect.go
625 lines (578 loc) · 12.6 KB
/
inspect.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
package astTool
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/printer"
"go/token"
"io"
"os"
"reflect"
)
type IdentInfo struct {
Name string `description:"ident 名字"`
Pos token.Pos `description:"在代码文件中的位置"`
}
var dummyData []IdentInfo //存放Parse()中的缓存数据,以除去冗余ast.Node
var fset *token.FileSet
func init() {
dummyData = make([]IdentInfo, 0)
}
func (h HappyAst) Position(tp token.Pos) token.Position {
return h.FileSet.PositionFor(tp, true)
}
func (h HappyAst) FindNodeByPos(pos token.Pos) (ret *ast.Node) {
if pos == token.NoPos {
return nil
}
ast.Inspect(h.Ast, func(node ast.Node) bool {
switch node.(type) {
case ast.Node:
if !node.Pos().IsValid() {
return true
}
if node.Pos() == pos {
ret = &node
return false
}
}
return true
})
return ret
}
// find function by name,
// return token.Pos
func (h HappyAst) FindIdent(ident string) (pos []token.Pos) {
ast.Inspect(h.Ast, func(node ast.Node) bool {
switch x := node.(type) {
case *ast.Ident:
if x.Name == ident {
pos = append(pos, x.Pos())
return true
}
}
return true
})
return pos
}
// find value spec node by name,
// return token.Pos
func (h HappyAst) FindValueSpec(ident string) (pos []token.Pos) {
ast.Inspect(h.Ast, func(node ast.Node) bool {
switch x := node.(type) {
case *ast.ValueSpec:
for _, v := range x.Names {
if v.Name == ident {
pos = append(pos, x.Pos())
return true
}
}
}
return true
})
return pos
}
// find value spec node by name,
// return token.Pos
func (h HappyAst) FindValueSpecFromNode(n ast.Node, ident string) (pos []token.Pos) {
ast.Inspect(n, func(node ast.Node) bool {
switch x := node.(type) {
case *ast.ValueSpec:
for _, v := range x.Names {
if v.Name == ident {
pos = append(pos, x.Pos())
return true
}
}
}
return true
})
return pos
}
// find function by name,
// return token.Pos
func (h HappyAst) FindFuncDeclNode(wantName string) (pos token.Pos) {
ast.Inspect(h.Ast, func(node ast.Node) bool {
switch x := node.(type) {
case *ast.FuncDecl:
if x.Name.Name == wantName {
pos = x.Pos()
return false
}
}
return true
})
return
}
// find Struct Decl by name,
// return token.Pos
func (h HappyAst) FindStructDeclNode(wantName string) (pos token.Pos) {
ast.Inspect(h.Ast, func(node ast.Node) bool {
switch g := node.(type) {
case *ast.GenDecl:
if g.Tok == token.TYPE {
for _, v := range g.Specs {
switch x := v.(type) {
case *ast.TypeSpec:
switch x.Type.(type) {
case *ast.StructType:
if x.Name.Name == wantName {
pos = x.Pos()
return false
}
}
}
}
}
}
return true
})
return
}
// find Struct Field by name from given Node,
// return token.Pos
func (h HappyAst) FindStructFieldFromNode(n ast.Node, wantName string) (pos token.Pos) {
ast.Inspect(n, func(node ast.Node) bool {
switch g := node.(type) {
case *ast.StructType:
for _, v := range g.Fields.List {
for _, val := range v.Names {
if val.Name == wantName {
pos = val.Pos()
return false
}
}
}
}
return true
})
return
}
// find function call from given Node,
// return token.Pos
func (h HappyAst) FindCallExprFromNode(n ast.Node, funcName string) (pos token.Pos) {
ast.Inspect(n, func(node ast.Node) bool {
switch x := node.(type) {
case *ast.CallExpr:
switch i := x.Fun.(type) {
case *ast.Ident:
if i.Name == funcName {
pos = x.Pos()
return false
}
}
}
return true
})
return
}
// print HappyAst into string
func (h HappyAst) Output(printConfig *printer.Config) string {
var buf bytes.Buffer
if printConfig == nil {
printConfig = &printer.Config{Mode: printer.TabIndent, Tabwidth: 4}
}
err := printConfig.Fprint(&buf, h.FileSet, h.Ast)
if err != nil {
panic(err)
}
out := buf.Bytes()
out, err = format.Source(out)
if err != nil {
panic(err)
}
return string(out)
}
// print HappyAst into string
func (h HappyAst) OutputNode(node interface{}) string {
var buf bytes.Buffer
printConfig := &printer.Config{Mode: printer.TabIndent, Tabwidth: 4}
err := printConfig.Fprint(&buf, h.FileSet, node)
if err != nil {
panic(err)
}
out := buf.Bytes()
out, err = format.Source(out)
if err != nil {
panic(err)
}
return string(out)
}
//print
func (h HappyAst) Print() {
ast.Fprint(os.Stdout, h.FileSet, h.Ast, nil)
}
//visitor
func (h HappyAst) Visit() {
//visitor
ast.Walk(HappyVisitor(printNode), h.Ast)
}
// node Visitor
type HappyVisitor func(ast.Node) string
func (v HappyVisitor) Visit(node ast.Node) ast.Visitor {
view := v(node)
//_=view
fmt.Print(view)
return v
}
// print node
func printNode(node ast.Node) string {
if node == nil {
return ""
}
//save
ptr := node.Pos()
if line, exists := p.ptrmap[ptr]; exists {
_ = line
//printf("(obj @ %d)", line)
return ""
} else {
p.ptrmap[node.Pos()] = node.Pos()
}
switch n := node.(type) {
case *ast.Comment:
return ""
//code = n.Text
//return printLine(code)
case *ast.CommentGroup:
return ""
//for _,c := range n.List{
// code += printNode(c)
//}
//return printLine(code)
case *ast.Field:
return ""
//for _,v := range n.Names{
// code+=printNode(v)
//}
//return code + string(tab)
case *ast.FieldList:
return ""
//for _,v := range n.List{
// code += printNode(v)
//}
//return code + string(tab)
case *ast.BadExpr:
case *ast.Ident:
inner := ""
if n.Obj != nil {
inner += n.Obj.Kind.String()
}
inner += n.Name
//return ""
return inner + string(blank)
case *ast.BasicLit:
inner := ""
inner += n.Kind.String()
inner += n.Value
return inner
//return n.Value+ string(blank)
case *ast.Ellipsis:
return ""
//return printNode(n)+ string(blank)
case *ast.FuncLit:
return ""
//return printNode(n.Body)+ string(newline)
case *ast.CompositeLit:
return ""
//return printNode(n.Type)+ string(blank)
case *ast.ParenExpr:
return ""
//return printNode(n)+ string(blank)
case *ast.SelectorExpr:
inner := ""
//inner += printNode(n.X)
inner += "."
//inner += printNode(n.Sel)
return inner
case *ast.IndexExpr:
inner := "*"
inner += printNode(n.X)
return inner
case *ast.SliceExpr:
inner := "*"
inner += printNode(n.X)
return inner
case *ast.TypeAssertExpr:
inner := "*"
//inner += printNode(n.Type)
//inner += printNode(n.X)
return inner
case *ast.CallExpr:
case *ast.StarExpr:
inner := "*"
inner += printNode(n.X)
return inner
case *ast.UnaryExpr:
inner := ""
inner += printNode(n.X)
return inner
case *ast.BinaryExpr:
inner := ""
inner += printNode(n.X)
inner += printNode(n.Y)
return inner
case *ast.KeyValueExpr:
case *ast.ArrayType:
case *ast.StructType:
case *ast.FuncType:
case *ast.InterfaceType:
case *ast.MapType:
case *ast.ChanType:
case *ast.BadStmt:
case *ast.DeclStmt:
case *ast.EmptyStmt:
case *ast.LabeledStmt:
case *ast.ExprStmt:
case *ast.SendStmt:
case *ast.IncDecStmt:
case *ast.AssignStmt:
case *ast.GoStmt:
case *ast.DeferStmt:
case *ast.ReturnStmt:
case *ast.BranchStmt:
case *ast.BlockStmt:
case *ast.IfStmt:
case *ast.CaseClause:
case *ast.SwitchStmt:
case *ast.TypeSwitchStmt:
case *ast.CommClause:
case *ast.SelectStmt:
case *ast.ForStmt:
case *ast.RangeStmt:
case *ast.ImportSpec:
return n.Path.Value + string(newline)
case *ast.ValueSpec:
inner := ""
for _, v := range n.Names {
inner += v.Name + " "
}
return inner + string(newline)
case *ast.TypeSpec:
case *ast.BadDecl:
case *ast.GenDecl:
return inspectGenDecl(n)
case *ast.FuncDecl:
return inspectFuncDecl(n)
case *ast.File:
//packageName := n.Name.Name
inner := ""
inner += printNode(n.Name)
return "package " + inner + string(newline)
case *ast.Package:
return ""
default:
_ = n
}
return ""
}
type mprinter struct {
output io.Writer
fset *token.FileSet
filter ast.FieldFilter
ptrmap map[interface{}]interface{} // *T -> line number
//ptrmap map[interface{}]int // *T -> line number
indent int // current indentation level
last byte // the last byte processed by Write
line int // current line number
}
//var p = mprinter{os.Stdout,fset,nil,0,byte(0),0}
var p = mprinter{
output: os.Stdout,
fset: fset,
filter: nil,
ptrmap: make(map[interface{}]interface{}),
last: '\n', // force printing of line number on first line
}
func print(x reflect.Value) {
switch x.Kind() {
case reflect.Interface:
print(x.Elem())
case reflect.Map:
printf("%s (len = %d) {", x.Type(), x.Len())
if x.Len() > 0 {
p.indent++
printf("\n")
for _, key := range x.MapKeys() {
print(key)
printf(": ")
print(x.MapIndex(key))
printf("\n")
}
p.indent--
}
printf("}")
case reflect.Ptr:
printf("*")
// type-checked ASTs may contain cycles - use ptrmap
// to keep track of objects that have been printed
// already and print the respective line number instead
ptr := x.Interface()
if line, exists := p.ptrmap[ptr]; exists {
printf("(obj @ %d)", line)
} else {
p.ptrmap[ptr] = p.line
print(x.Elem())
}
case reflect.Array:
printf("%s {", x.Type())
if x.Len() > 0 {
p.indent++
printf("\n")
for i, n := 0, x.Len(); i < n; i++ {
printf("%d: ", i)
print(x.Index(i))
printf("\n")
}
p.indent--
}
printf("}")
case reflect.Slice:
if s, ok := x.Interface().([]byte); ok {
printf("%#q", s)
return
}
printf("%s (len = %d) {", x.Type(), x.Len())
if x.Len() > 0 {
p.indent++
printf("\n")
for i, n := 0, x.Len(); i < n; i++ {
printf("%d: ", i)
print(x.Index(i))
printf("\n")
}
p.indent--
}
printf("}")
case reflect.Struct:
t := x.Type()
printf("%s {", t)
p.indent++
first := true
for i, n := 0, t.NumField(); i < n; i++ {
// exclude non-exported fields because their
// values cannot be accessed via reflection
if name := t.Field(i).Name; ast.IsExported(name) {
value := x.Field(i)
if first {
printf("\n")
first = false
}
printf("%s: ", name)
print(value)
printf("\n")
}
}
p.indent--
printf("}")
////test
//xxxy := x.Interface().(ast.Node)
//switch xxxy.(type){
//case *ast.File:
// fmt.Println("=================================file")
//default:
//
//}
////test end
default:
v := x.Interface()
switch v := v.(type) {
case string:
// print strings in quotes
printf("%q", v)
return
case token.Pos:
// position values can be printed nicely if we have a file set
if p.fset != nil {
printf("%s", p.fset.Position(v))
return
}
}
// default
printf("%v", v)
}
}
// printf is a convenience wrapper that takes care of print errors.
func printf(format string, args ...interface{}) {
if _, err := fmt.Fprintf(p.output, format, args...); err != nil {
panic(err)
}
}
//从ast.Node中根据函数名查找指定函数
func findFuncByName(node ast.Node) bool {
//ast.Print(fset,node)
//ast.Fprint(os.Stdout,fset,node,nil)
var wantName string
wantName = "registerBrandcustomerClientConn"
switch x := node.(type) {
case *ast.FuncDecl:
for _, v := range x.Body.List {
switch decl := v.(type) {
case *ast.DeclStmt:
findFuncByName(decl.Decl)
case *ast.ExprStmt:
switch exp := decl.X.(type) {
case *ast.CallExpr:
funcName, pos := parseCallExpr(exp.Fun)
if funcName == wantName {
dummyData = append(dummyData, IdentInfo{
Name: funcName,
Pos: pos,
})
}
}
}
}
}
return true
}
func parseCallExpr(cexpr ast.Expr) (string, token.Pos) {
switch x := cexpr.(type) {
case *ast.FuncLit:
case *ast.Ident:
return x.Name, x.Pos()
}
return "", cexpr.Pos()
}
// 分析函数声明
func inspectFuncDecl(n *ast.FuncDecl) (declStr string) {
funcName := fmt.Sprintf("%s", n.Name)
if n.Type.Params != nil {
paramList := n.Type.Params.List
declStr = fmt.Sprintf("Func: %s\n", funcName)
for k, v := range paramList {
for _, val := range v.Names {
declStr += fmt.Sprintf("param%d %s %s\n", k, val.Obj.Name, val.Obj.Decl.(*ast.Field).Type)
}
}
}
if n.Type.Results != nil {
resultList := n.Type.Results.List
for k, v := range resultList {
for _, val := range v.Names {
declStr += fmt.Sprintf("return%d %s %s\n", k, val.Obj.Name, val.Obj.Decl.(*ast.Field).Type)
}
}
}
return
}
// 分析通用声明
func inspectGenDecl(n *ast.GenDecl) (declStr string) {
declStr += n.Tok.String() + " "
for _, v := range n.Specs {
declStr += printNode(v)
}
return
}
func inspectNameOfSpec(n ast.Spec) []*ast.Ident {
switch x := n.(type) {
case *ast.ValueSpec:
return x.Names
case *ast.TypeSpec:
ret := make([]*ast.Ident, 0)
ret = append(ret, x.Name)
return ret
}
return make([]*ast.Ident, 0)
}