-
Notifications
You must be signed in to change notification settings - Fork 43
/
irconv.go
630 lines (569 loc) · 16.5 KB
/
irconv.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
626
627
628
629
630
package irconv
import (
"fmt"
"go/ast"
"go/constant"
"go/token"
"go/types"
"path"
"strconv"
"strings"
"github.com/quasilyte/go-ruleguard/ruleguard/goutil"
"github.com/quasilyte/go-ruleguard/ruleguard/ir"
"golang.org/x/tools/go/ast/astutil"
)
type Context struct {
Pkg *types.Package
Types *types.Info
Fset *token.FileSet
Src []byte
}
func ConvertFile(ctx *Context, f *ast.File) (result *ir.File, err error) {
defer func() {
if err != nil {
return
}
rv := recover()
if rv == nil {
return
}
if convErr, ok := rv.(convError); ok {
err = convErr.err
return
}
panic(rv) // not our panic
}()
conv := &converter{
types: ctx.Types,
pkg: ctx.Pkg,
fset: ctx.Fset,
src: ctx.Src,
}
result = conv.ConvertFile(f)
return result, nil
}
type convError struct {
err error
}
type converter struct {
types *types.Info
pkg *types.Package
fset *token.FileSet
src []byte
group *ir.RuleGroup
dslPkgname string // The local name of the "ruleguard/dsl" package (usually its just "dsl")
}
func (conv *converter) errorf(n ast.Node, format string, args ...interface{}) convError {
loc := conv.fset.Position(n.Pos())
msg := fmt.Sprintf(format, args...)
return convError{err: fmt.Errorf("%s:%d: %s", loc.Filename, loc.Line, msg)}
}
func (conv *converter) ConvertFile(f *ast.File) *ir.File {
result := &ir.File{
PkgPath: conv.pkg.Path(),
}
conv.dslPkgname = "dsl"
for _, imp := range f.Imports {
importPath, err := strconv.Unquote(imp.Path.Value)
if err != nil {
panic(conv.errorf(imp, "unquote %s import path: %s", imp.Path.Value, err))
}
if importPath == "github.com/quasilyte/go-ruleguard/dsl" {
if imp.Name != nil {
conv.dslPkgname = imp.Name.Name
}
}
}
for _, decl := range f.Decls {
funcDecl, ok := decl.(*ast.FuncDecl)
if !ok {
genDecl := decl.(*ast.GenDecl)
if genDecl.Tok != token.IMPORT {
conv.addCustomDecl(result, decl)
}
continue
}
if funcDecl.Name.String() == "init" {
conv.convertInitFunc(result, funcDecl)
continue
}
if conv.isMatcherFunc(funcDecl) {
result.RuleGroups = append(result.RuleGroups, *conv.convertRuleGroup(funcDecl))
} else {
conv.addCustomDecl(result, funcDecl)
}
}
return result
}
func (conv *converter) convertInitFunc(dst *ir.File, decl *ast.FuncDecl) {
for _, stmt := range decl.Body.List {
exprStmt, ok := stmt.(*ast.ExprStmt)
if !ok {
panic(conv.errorf(stmt, "unsupported statement"))
}
call, ok := exprStmt.X.(*ast.CallExpr)
if !ok {
panic(conv.errorf(stmt, "unsupported expr"))
}
fn, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
panic(conv.errorf(stmt, "unsupported call"))
}
pkg, ok := fn.X.(*ast.Ident)
if !ok || pkg.Name != conv.dslPkgname {
panic(conv.errorf(stmt, "unsupported call"))
}
switch fn.Sel.Name {
case "ImportRules":
prefix := conv.parseStringArg(call.Args[0])
bundleSelector, ok := call.Args[1].(*ast.SelectorExpr)
if !ok {
panic(conv.errorf(call.Args[1], "expected a `pkgname.Bundle` argument"))
}
bundleObj := conv.types.ObjectOf(bundleSelector.Sel)
dst.BundleImports = append(dst.BundleImports, ir.BundleImport{
Prefix: prefix,
PkgPath: bundleObj.Pkg().Path(),
Line: conv.fset.Position(exprStmt.Pos()).Line,
})
default:
panic(conv.errorf(stmt, "unsupported %s call", fn.Sel.Name))
}
}
}
func (conv *converter) addCustomDecl(dst *ir.File, decl ast.Decl) {
begin := conv.fset.Position(decl.Pos())
end := conv.fset.Position(decl.End())
src := conv.src[begin.Offset:end.Offset]
dst.CustomDecls = append(dst.CustomDecls, string(src))
}
func (conv *converter) isMatcherFunc(f *ast.FuncDecl) bool {
typ := conv.types.ObjectOf(f.Name).Type().(*types.Signature)
return typ.Results().Len() == 0 &&
typ.Params().Len() == 1 &&
typ.Params().At(0).Type().String() == "github.com/quasilyte/go-ruleguard/dsl.Matcher"
}
func (conv *converter) convertRuleGroup(decl *ast.FuncDecl) *ir.RuleGroup {
result := &ir.RuleGroup{
Line: conv.fset.Position(decl.Name.Pos()).Line,
}
conv.group = result
result.Name = decl.Name.String()
result.MatcherName = decl.Type.Params.List[0].Names[0].String()
if decl.Doc != nil {
conv.convertDocComments(decl.Doc)
}
seenRules := false
for _, stmt := range decl.Body.List {
if _, ok := stmt.(*ast.DeclStmt); ok {
continue
}
stmtExpr, ok := stmt.(*ast.ExprStmt)
if !ok {
panic(conv.errorf(stmt, "expected a %s method call, found %s", result.MatcherName, goutil.SprintNode(conv.fset, stmt)))
}
call, ok := stmtExpr.X.(*ast.CallExpr)
if !ok {
panic(conv.errorf(stmt, "expected a %s method call, found %s", result.MatcherName, goutil.SprintNode(conv.fset, stmt)))
}
switch conv.matcherMethodName(call) {
case "Import":
if seenRules {
panic(conv.errorf(call, "Import() should be used before any rules definitions"))
}
conv.doMatcherImport(call)
default:
seenRules = true
conv.convertRuleExpr(call)
}
}
return result
}
func (conv *converter) doMatcherImport(call *ast.CallExpr) {
pkgPath := conv.parseStringArg(call.Args[0])
pkgName := path.Base(pkgPath)
conv.group.Imports = append(conv.group.Imports, ir.PackageImport{
Path: pkgPath,
Name: pkgName,
})
}
func (conv *converter) matcherMethodName(call *ast.CallExpr) string {
selector, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
return ""
}
id, ok := selector.X.(*ast.Ident)
if !ok || id.Name != conv.group.MatcherName {
return ""
}
return selector.Sel.Name
}
func (conv *converter) convertDocComments(comment *ast.CommentGroup) {
knownPragmas := []string{
"tags",
"summary",
"before",
"after",
"note",
}
for _, c := range comment.List {
if !strings.HasPrefix(c.Text, "//doc:") {
continue
}
s := strings.TrimPrefix(c.Text, "//doc:")
var pragma string
for i := range knownPragmas {
if strings.HasPrefix(s, knownPragmas[i]) {
pragma = knownPragmas[i]
break
}
}
if pragma == "" {
panic(conv.errorf(c, "unrecognized 'doc' pragma in comment"))
}
s = strings.TrimPrefix(s, pragma)
s = strings.TrimSpace(s)
switch pragma {
case "summary":
conv.group.DocSummary = s
case "before":
conv.group.DocBefore = s
case "after":
conv.group.DocAfter = s
case "note":
conv.group.DocNote = s
case "tags":
conv.group.DocTags = strings.Fields(s)
default:
panic("unhandled 'doc' pragma: " + pragma) // Should never happen
}
}
}
func (conv *converter) convertRuleExpr(call *ast.CallExpr) {
origCall := call
var (
matchArgs *[]ast.Expr
matchCommentArgs *[]ast.Expr
whereArgs *[]ast.Expr
suggestArgs *[]ast.Expr
reportArgs *[]ast.Expr
atArgs *[]ast.Expr
)
for {
chain, ok := call.Fun.(*ast.SelectorExpr)
if !ok {
break
}
switch chain.Sel.Name {
case "Match":
if matchArgs != nil {
panic(conv.errorf(chain.Sel, "Match() can't be repeated"))
}
if matchCommentArgs != nil {
panic(conv.errorf(chain.Sel, "Match() and MatchComment() can't be combined"))
}
matchArgs = &call.Args
case "MatchComment":
if matchCommentArgs != nil {
panic(conv.errorf(chain.Sel, "MatchComment() can't be repeated"))
}
if matchArgs != nil {
panic(conv.errorf(chain.Sel, "Match() and MatchComment() can't be combined"))
}
matchCommentArgs = &call.Args
case "Where":
if whereArgs != nil {
panic(conv.errorf(chain.Sel, "Where() can't be repeated"))
}
whereArgs = &call.Args
case "Suggest":
if suggestArgs != nil {
panic(conv.errorf(chain.Sel, "Suggest() can't be repeated"))
}
suggestArgs = &call.Args
case "Report":
if reportArgs != nil {
panic(conv.errorf(chain.Sel, "Report() can't be repeated"))
}
reportArgs = &call.Args
case "At":
if atArgs != nil {
panic(conv.errorf(chain.Sel, "At() can't be repeated"))
}
atArgs = &call.Args
default:
panic(conv.errorf(chain.Sel, "unexpected %s method", chain.Sel.Name))
}
call, ok = chain.X.(*ast.CallExpr)
if !ok {
break
}
}
// AST patterns for Match() or regexp patterns for MatchComment().
var alternatives []string
var alternativeLines []int
if matchArgs == nil && matchCommentArgs == nil {
panic(conv.errorf(origCall, "missing Match() or MatchComment() call"))
}
if matchArgs != nil {
for _, arg := range *matchArgs {
alternatives = append(alternatives, conv.parseStringArg(arg))
alternativeLines = append(alternativeLines, conv.fset.Position(arg.Pos()).Line)
}
} else {
for _, arg := range *matchCommentArgs {
alternatives = append(alternatives, conv.parseStringArg(arg))
alternativeLines = append(alternativeLines, conv.fset.Position(arg.Pos()).Line)
}
}
rule := ir.Rule{Line: conv.fset.Position(origCall.Pos()).Line}
if atArgs != nil {
index, ok := (*atArgs)[0].(*ast.IndexExpr)
if !ok {
panic(conv.errorf((*atArgs)[0], "expected %s[`varname`] expression", conv.group.MatcherName))
}
rule.LocationVar = conv.parseStringArg(index.Index)
}
if whereArgs != nil {
rule.WhereExpr = conv.convertFilterExpr((*whereArgs)[0])
}
if suggestArgs != nil {
rule.SuggestTemplate = conv.parseStringArg((*suggestArgs)[0])
}
if suggestArgs == nil && reportArgs == nil {
panic(conv.errorf(origCall, "missing Report() or Suggest() call"))
}
if reportArgs == nil {
rule.ReportTemplate = "suggestion: " + rule.SuggestTemplate
} else {
rule.ReportTemplate = conv.parseStringArg((*reportArgs)[0])
}
for i, alt := range alternatives {
pat := ir.PatternString{
Line: alternativeLines[i],
Value: alt,
}
if matchArgs != nil {
rule.SyntaxPatterns = append(rule.SyntaxPatterns, pat)
} else {
rule.CommentPatterns = append(rule.CommentPatterns, pat)
}
}
conv.group.Rules = append(conv.group.Rules, rule)
}
func (conv *converter) convertFilterExpr(e ast.Expr) ir.FilterExpr {
result := conv.convertFilterExprImpl(e)
result.Src = goutil.SprintNode(conv.fset, e)
result.Line = conv.fset.Position(e.Pos()).Line
if !result.IsValid() {
panic(conv.errorf(e, "unsupported expr: %s (%T)", result.Src, e))
}
return result
}
func (conv *converter) convertFilterExprImpl(e ast.Expr) ir.FilterExpr {
if cv := conv.types.Types[e].Value; cv != nil {
switch cv.Kind() {
case constant.String:
v := constant.StringVal(cv)
return ir.FilterExpr{Op: ir.FilterStringOp, Value: v}
case constant.Int:
v, ok := constant.Int64Val(cv)
if ok {
return ir.FilterExpr{Op: ir.FilterIntOp, Value: v}
}
}
}
convertExprList := func(list []ast.Expr) []ir.FilterExpr {
if len(list) == 0 {
return nil
}
result := make([]ir.FilterExpr, len(list))
for i, e := range list {
result[i] = conv.convertFilterExpr(e)
}
return result
}
switch e := e.(type) {
case *ast.ParenExpr:
return conv.convertFilterExpr(e.X)
case *ast.UnaryExpr:
x := conv.convertFilterExpr(e.X)
args := []ir.FilterExpr{x}
switch e.Op {
case token.NOT:
return ir.FilterExpr{Op: ir.FilterNotOp, Args: args}
}
case *ast.BinaryExpr:
x := conv.convertFilterExpr(e.X)
y := conv.convertFilterExpr(e.Y)
args := []ir.FilterExpr{x, y}
switch e.Op {
case token.LAND:
return ir.FilterExpr{Op: ir.FilterAndOp, Args: args}
case token.LOR:
return ir.FilterExpr{Op: ir.FilterOrOp, Args: args}
case token.NEQ:
return ir.FilterExpr{Op: ir.FilterNeqOp, Args: args}
case token.EQL:
return ir.FilterExpr{Op: ir.FilterEqOp, Args: args}
case token.GTR:
return ir.FilterExpr{Op: ir.FilterGtOp, Args: args}
case token.LSS:
return ir.FilterExpr{Op: ir.FilterLtOp, Args: args}
case token.GEQ:
return ir.FilterExpr{Op: ir.FilterGtEqOp, Args: args}
case token.LEQ:
return ir.FilterExpr{Op: ir.FilterLtEqOp, Args: args}
default:
panic(conv.errorf(e, "unexpected binary op: %s", e.Op.String()))
}
case *ast.SelectorExpr:
op := conv.inspectFilterSelector(e)
switch op.path {
case "Text":
return ir.FilterExpr{Op: ir.FilterVarTextOp, Value: op.varName}
case "Line":
return ir.FilterExpr{Op: ir.FilterVarLineOp, Value: op.varName}
case "Pure":
return ir.FilterExpr{Op: ir.FilterVarPureOp, Value: op.varName}
case "Const":
return ir.FilterExpr{Op: ir.FilterVarConstOp, Value: op.varName}
case "ConstSlice":
return ir.FilterExpr{Op: ir.FilterVarConstSliceOp, Value: op.varName}
case "Addressable":
return ir.FilterExpr{Op: ir.FilterVarAddressableOp, Value: op.varName}
case "Type.Size":
return ir.FilterExpr{Op: ir.FilterVarTypeSizeOp, Value: op.varName}
}
case *ast.CallExpr:
op := conv.inspectFilterSelector(e)
switch op.path {
case "Deadcode":
return ir.FilterExpr{Op: ir.FilterDeadcodeOp}
case "GoVersion.Eq":
return ir.FilterExpr{Op: ir.FilterGoVersionEqOp, Value: conv.parseStringArg(e.Args[0])}
case "GoVersion.LessThan":
return ir.FilterExpr{Op: ir.FilterGoVersionLessThanOp, Value: conv.parseStringArg(e.Args[0])}
case "GoVersion.GreaterThan":
return ir.FilterExpr{Op: ir.FilterGoVersionGreaterThanOp, Value: conv.parseStringArg(e.Args[0])}
case "GoVersion.LessEqThan":
return ir.FilterExpr{Op: ir.FilterGoVersionLessEqThanOp, Value: conv.parseStringArg(e.Args[0])}
case "GoVersion.GreaterEqThan":
return ir.FilterExpr{Op: ir.FilterGoVersionGreaterEqThanOp, Value: conv.parseStringArg(e.Args[0])}
case "File.Imports":
return ir.FilterExpr{Op: ir.FilterFileImportsOp, Value: conv.parseStringArg(e.Args[0])}
case "File.PkgPath.Matches":
return ir.FilterExpr{Op: ir.FilterFilePkgPathMatchesOp, Value: conv.parseStringArg(e.Args[0])}
case "File.Name.Matches":
return ir.FilterExpr{Op: ir.FilterFileNameMatchesOp, Value: conv.parseStringArg(e.Args[0])}
case "Filter":
funcName, ok := e.Args[0].(*ast.Ident)
if !ok {
panic(conv.errorf(e.Args[0], "only named function args are supported"))
}
args := []ir.FilterExpr{
{Op: ir.FilterFilterFuncRefOp, Value: funcName.String()},
}
return ir.FilterExpr{Op: ir.FilterVarFilterOp, Value: op.varName, Args: args}
}
args := convertExprList(e.Args)
switch op.path {
case "Value.Int":
return ir.FilterExpr{Op: ir.FilterVarValueIntOp, Value: op.varName, Args: args}
case "Text.Matches":
return ir.FilterExpr{Op: ir.FilterVarTextMatchesOp, Value: op.varName, Args: args}
case "Node.Is":
return ir.FilterExpr{Op: ir.FilterVarNodeIsOp, Value: op.varName, Args: args}
case "Node.Parent.Is":
if op.varName != "$$" {
// TODO: remove this restriction.
panic(conv.errorf(e.Args[0], "only $$ parent nodes are implemented"))
}
return ir.FilterExpr{Op: ir.FilterRootNodeParentIsOp, Args: args}
case "Object.Is":
return ir.FilterExpr{Op: ir.FilterVarObjectIsOp, Value: op.varName, Args: args}
case "Type.Is":
return ir.FilterExpr{Op: ir.FilterVarTypeIsOp, Value: op.varName, Args: args}
case "Type.Underlying.Is":
return ir.FilterExpr{Op: ir.FilterVarTypeUnderlyingIsOp, Value: op.varName, Args: args}
case "Type.ConvertibleTo":
return ir.FilterExpr{Op: ir.FilterVarTypeConvertibleToOp, Value: op.varName, Args: args}
case "Type.AssignableTo":
return ir.FilterExpr{Op: ir.FilterVarTypeAssignableToOp, Value: op.varName, Args: args}
case "Type.Implements":
return ir.FilterExpr{Op: ir.FilterVarTypeImplementsOp, Value: op.varName, Args: args}
}
}
return ir.FilterExpr{}
}
func (conv *converter) parseStringArg(e ast.Expr) string {
s, ok := conv.toStringValue(e)
if !ok {
panic(conv.errorf(e, "expected a string literal argument"))
}
return s
}
func (conv *converter) toStringValue(x ast.Node) (string, bool) {
switch x := x.(type) {
case *ast.BasicLit:
if x.Kind != token.STRING {
return "", false
}
s, err := strconv.Unquote(x.Value)
if err != nil {
return "", false
}
return s, true
case ast.Expr:
typ, ok := conv.types.Types[x]
if !ok || typ.Type.String() != "string" {
return "", false
}
str := constant.StringVal(typ.Value)
return str, true
}
return "", false
}
func (conv *converter) inspectFilterSelector(e ast.Expr) filterExprSelector {
var o filterExprSelector
if call, ok := e.(*ast.CallExpr); ok {
o.args = call.Args
e = call.Fun
}
var path string
for {
if call, ok := e.(*ast.CallExpr); ok {
e = call.Fun
continue
}
selector, ok := e.(*ast.SelectorExpr)
if !ok {
break
}
if path == "" {
path = selector.Sel.Name
} else {
path = selector.Sel.Name + "." + path
}
e = astutil.Unparen(selector.X)
}
o.path = path
indexing, ok := astutil.Unparen(e).(*ast.IndexExpr)
if !ok {
return o
}
mapIdent, ok := astutil.Unparen(indexing.X).(*ast.Ident)
if !ok {
return o
}
o.mapName = mapIdent.Name
indexString, _ := conv.toStringValue(indexing.Index)
o.varName = indexString
return o
}
type filterExprSelector struct {
mapName string
varName string
path string
args []ast.Expr
}