@@ -2,6 +2,7 @@ package ruleguard
2
2
3
3
import (
4
4
"bytes"
5
+ "context"
5
6
"fmt"
6
7
"go/ast"
7
8
"go/build"
@@ -13,13 +14,16 @@ import (
13
14
"strings"
14
15
15
16
"github.com/quasilyte/go-ruleguard/ruleguard/goutil"
17
+ "github.com/quasilyte/go-ruleguard/ruleguard/profiling"
16
18
"github.com/quasilyte/gogrep"
17
19
"github.com/quasilyte/gogrep/nodetag"
18
20
)
19
21
20
22
type rulesRunner struct {
21
23
state * engineState
22
24
25
+ bgContext context.Context
26
+
23
27
ctx * RunContext
24
28
rules * goRuleSet
25
29
@@ -60,6 +64,7 @@ func newRulesRunner(ctx *RunContext, buildContext *build.Context, state *engineS
60
64
gogrepState := gogrep .NewMatcherState ()
61
65
gogrepState .Types = ctx .Types
62
66
rr := & rulesRunner {
67
+ bgContext : context .Background (),
63
68
ctx : ctx ,
64
69
importer : importer ,
65
70
rules : rules ,
@@ -209,12 +214,25 @@ func (rr *rulesRunner) runCommentRules(comment *ast.Comment) {
209
214
}
210
215
211
216
func (rr * rulesRunner ) runRules (n ast.Node ) {
217
+ // profiling.LabelsEnabled is constant, so labels-related
218
+ // code should be a no-op inside normal build.
219
+ // To enable labels, use "-tags pproflabels" build tag.
220
+
212
221
tag := nodetag .FromNode (n )
213
222
for _ , rule := range rr .rules .universal .rulesByTag [tag ] {
223
+ if profiling .LabelsEnabled {
224
+ profiling .EnterWithLabels (rr .bgContext , rule .group .Name )
225
+ }
226
+
214
227
matched := false
215
228
rule .pat .MatchNode (& rr .gogrepState , n , func (m gogrep.MatchData ) {
216
229
matched = rr .handleMatch (rule , m )
217
230
})
231
+
232
+ if profiling .LabelsEnabled {
233
+ profiling .Leave (rr .bgContext )
234
+ }
235
+
218
236
if matched {
219
237
break
220
238
}
0 commit comments