Skip to content

Commit 477f62c

Browse files
authored
ruleguard: use a separate token.FileSet for gogrep parsing (#275)
Also adapt the error tests for go1.17 (error messages changed there).
1 parent e6a9d2d commit 477f62c

File tree

3 files changed

+40
-31
lines changed

3 files changed

+40
-31
lines changed

ruleguard/engine.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"go/ast"
7+
"go/token"
78
"go/types"
89
"io"
910
"io/ioutil"
@@ -55,11 +56,12 @@ func (e *engine) Load(ctx *LoadContext, filename string, r io.Reader) error {
5556
return err
5657
}
5758
config := irLoaderConfig{
58-
state: e.state,
59-
pkg: pkg,
60-
ctx: ctx,
61-
importer: imp,
62-
itab: typematch.NewImportsTab(stdinfo.Packages),
59+
state: e.state,
60+
pkg: pkg,
61+
ctx: ctx,
62+
importer: imp,
63+
itab: typematch.NewImportsTab(stdinfo.Packages),
64+
gogrepFset: token.NewFileSet(),
6365
}
6466
l := newIRLoader(config)
6567
rset, err := l.LoadFile(filename, irfile)
@@ -87,10 +89,11 @@ func (e *engine) LoadFromIR(ctx *LoadContext, filename string, f *ir.File) error
8789
debugPrint: ctx.DebugPrint,
8890
})
8991
config := irLoaderConfig{
90-
state: e.state,
91-
ctx: ctx,
92-
importer: imp,
93-
itab: typematch.NewImportsTab(stdinfo.Packages),
92+
state: e.state,
93+
ctx: ctx,
94+
importer: imp,
95+
itab: typematch.NewImportsTab(stdinfo.Packages),
96+
gogrepFset: token.NewFileSet(),
9497
}
9598
l := newIRLoader(config)
9699
rset, err := l.LoadFile(filename, f)

ruleguard/ir_loader.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ type irLoaderConfig struct {
3030

3131
pkg *types.Package
3232

33+
gogrepFset *token.FileSet
34+
3335
prefix string
3436
importedPkg string
3537
}
@@ -41,7 +43,8 @@ type irLoader struct {
4143

4244
pkg *types.Package
4345

44-
file *ir.File
46+
file *ir.File
47+
gogrepFset *token.FileSet
4548

4649
filename string
4750
res *goRuleSet
@@ -58,12 +61,13 @@ type irLoader struct {
5861

5962
func newIRLoader(config irLoaderConfig) *irLoader {
6063
return &irLoader{
61-
state: config.state,
62-
ctx: config.ctx,
63-
importer: config.importer,
64-
itab: config.itab,
65-
pkg: config.pkg,
66-
prefix: config.prefix,
64+
state: config.state,
65+
ctx: config.ctx,
66+
importer: config.importer,
67+
itab: config.itab,
68+
pkg: config.pkg,
69+
prefix: config.prefix,
70+
gogrepFset: config.gogrepFset,
6771
}
6872
}
6973

@@ -154,6 +158,7 @@ func (l *irLoader) loadExternFile(prefix, pkgPath, filename string) (*goRuleSet,
154158
pkg: pkg,
155159
importedPkg: pkgPath,
156160
itab: l.itab,
161+
gogrepFset: l.gogrepFset,
157162
}
158163
rset, err := newIRLoader(config).LoadFile(filename, irfile)
159164
if err != nil {
@@ -297,7 +302,7 @@ func (l *irLoader) loadCommentRule(resultProto goRule, rule ir.Rule) error {
297302
func (l *irLoader) loadSyntaxRule(resultProto goRule, rule ir.Rule) error {
298303
result := resultProto
299304

300-
pat, err := gogrep.Compile(l.ctx.Fset, rule.SyntaxPattern, false)
305+
pat, err := gogrep.Compile(l.gogrepFset, rule.SyntaxPattern, false)
301306
if err != nil {
302307
return l.errorf(rule.Line, err, "parse match pattern")
303308
}

ruleguard/ruleguard_error_test.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"go/token"
7+
"regexp"
78
"strings"
89
"testing"
910
)
@@ -189,57 +190,57 @@ func TestParseRuleError(t *testing.T) {
189190
}{
190191
{
191192
`m.Match("$x").Where(m["x"].Object.Is("abc")).Report("")`,
192-
`abc is not a valid go/types object name`,
193+
`\Qabc is not a valid go/types object name`,
193194
},
194195

195196
{
196197
`m.Match("$x").MatchComment("").Report("")`,
197-
`Match() and MatchComment() can't be combined`,
198+
`\QMatch() and MatchComment() can't be combined`,
198199
},
199200

200201
{
201202
`m.MatchComment("").Match("$x").Report("")`,
202-
`Match() and MatchComment() can't be combined`,
203+
`\QMatch() and MatchComment() can't be combined`,
203204
},
204205

205206
{
206207
`m.Where(m.File().Imports("strings")).Report("no match call")`,
207-
`missing Match() or MatchComment() call`,
208+
`\Qmissing Match() or MatchComment() call`,
208209
},
209210

210211
{
211212
`m.Match("$x").Where(m["x"].Pure)`,
212-
`missing Report() or Suggest() call`,
213+
`\Qmissing Report() or Suggest() call`,
213214
},
214215

215216
{
216217
`m.Match("$x").Match("$x")`,
217-
`Match() can't be repeated`,
218+
`\QMatch() can't be repeated`,
218219
},
219220

220221
{
221222
`m.MatchComment("").MatchComment("")`,
222-
`MatchComment() can't be repeated`,
223+
`\QMatchComment() can't be repeated`,
223224
},
224225

225226
{
226227
`m.Match().Report("$$")`,
227-
`too few arguments in call to m.Match`,
228+
`(?:too few|not enough) arguments in call to m\.Match`,
228229
},
229230

230231
{
231232
`m.MatchComment().Report("$$")`,
232-
`too few arguments in call to m.MatchComment`,
233+
`(?:too few|not enough) arguments in call to m\.MatchComment`,
233234
},
234235

235236
{
236237
`m.MatchComment("(").Report("")`,
237-
`error parsing regexp: missing closing )`,
238+
`\Qerror parsing regexp: missing closing )`,
238239
},
239240

240241
{
241242
`m.Match("func[]").Report("$$")`,
242-
`parse match pattern: cannot parse expr: 1:5: expected '(', found '['`,
243+
`\Qparse match pattern: cannot parse expr: 1:5: expected '(', found '['`,
243244
},
244245
}
245246

@@ -261,9 +262,9 @@ func TestParseRuleError(t *testing.T) {
261262
continue
262263
}
263264
have := err.Error()
264-
want := test.err
265-
if !strings.Contains(have, want) {
266-
t.Errorf("parse %s: errors mismatch:\nhave: %s\nwant: %s", test.expr, have, want)
265+
wantRE := regexp.MustCompile(test.err)
266+
if !wantRE.MatchString(have) {
267+
t.Errorf("parse %s: errors mismatch:\nhave: %s\nwant: %s", test.expr, have, test.err)
267268
continue
268269
}
269270
}

0 commit comments

Comments
 (0)