/
main.go
468 lines (401 loc) · 11.4 KB
/
main.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
package cmd
import (
"encoding/json"
"fmt"
"log"
"net/http"
_ "net/http/pprof" // it is ok for actually main package
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"
"sync/atomic"
"time"
"github.com/VKCOM/noverify/src/baseline"
"github.com/VKCOM/noverify/src/cmd/stubs"
"github.com/VKCOM/noverify/src/lintdebug"
"github.com/VKCOM/noverify/src/linter"
"github.com/VKCOM/noverify/src/linter/lintapi"
"github.com/VKCOM/noverify/src/workspace"
)
// Line below implies that we have `https://github.com/VKCOM/phpstorm-stubs.git` cloned
// to the `./src/cmd/stubs/phpstorm-stubs`.
//
//go:generate go-bindata -pkg stubs -nometadata -o ./stubs/phpstorm_stubs.go -ignore=\.idea -ignore=\.git ./stubs/phpstorm-stubs/...
func registerMainApp() *App {
return &App{
CLIName: "noverify",
Name: "NoVerify",
Description: "Pretty fast linter (static analysis tool) for PHP",
Commands: []*Command{
{
Name: "check",
Description: "The command to lint files",
Action: Check,
Arguments: []*Argument{
{
Name: "folders/files",
Description: "Folders and/or files for check",
},
},
RegisterFlags: RegisterCheckFlags,
},
{
Name: "checkers",
Description: "The command to show list of checkers",
Arguments: []*Argument{
{
Name: "checker-name",
Description: "Show info for a certain <checker-name> checker ",
},
},
Action: Checkers,
},
{
Name: "checkers-doc",
Description: "The command to generate markdown checkers documentation",
Action: CheckersDocumentation,
Examples: []Example{
{
Line: "noverify checkers-doc > checkers.md",
Description: "Creates a 'checkers.md' file with documentation for all checkers.",
},
},
},
{
Name: "version",
Description: "The command to output the tool version",
Action: func(ctx *AppContext) (int, error) {
printVersion()
return 0, nil
},
},
{
Name: "test-rules",
Description: "The command to test the dynamic rules",
Action: TestRules,
RegisterFlags: RegisterTestRulesFlags,
},
},
}
}
// Run executes linter main function.
//
// It is separate from linter so that you can insert your own hooks
// before running Run().
//
// It returns a status code to be used for os.Exit() and
// initialization error (if any).
//
// Optionally, non-nil config can be passed to customize function behavior.
func Run(cfg *MainConfig) (int, error) {
if cfg == nil {
cfg = &MainConfig{}
}
if cfg.LinterVersion == "" {
cfg.LinterVersion = BuildCommit
}
config := cfg.LinterConfig
if config == nil {
config = linter.NewConfig("8.1")
cfg.LinterConfig = config
}
cfg.linter = linter.NewLinter(config)
ruleSets, err := ParseEmbeddedRules()
if err != nil {
return 1, fmt.Errorf("preload embedded rules: %v", err)
}
for _, rset := range ruleSets {
config.Checkers.DeclareRules(rset)
}
cfg.rulesSets = append(cfg.rulesSets, ruleSets...)
if cfg.RegisterCheckers != nil {
for _, checker := range cfg.RegisterCheckers() {
cfg.linter.Config().Checkers.DeclareChecker(checker)
}
}
app := registerMainApp()
if cfg.ModifyApp != nil {
cfg.ModifyApp(app)
}
return app.Run(cfg)
}
// Main is like Run(), but it calls os.Exit() and does not return.
func Main(cfg *MainConfig) {
status, err := Run(cfg)
if err != nil {
log.Fatal(err)
}
os.Exit(status)
}
// mainNoExit implements main, but instead of doing log.Fatal or os.Exit it
// returns error or non-zero integer status code to be passed to os.Exit by the caller.
// Note that if error is not nil, integer code will be discarded, so it can be 0.
//
// We don't want os.Exit to be inserted randomly to avoid defer cancellation.
func mainNoExit(ctx *AppContext) (status int, err error) {
if ctx.ParsedFlags.PprofHost != "" {
go func() {
err := http.ListenAndServe(ctx.ParsedFlags.PprofHost, nil)
if err != nil {
log.Printf("pprof listen and serve: %v", err)
}
}()
}
// Since this function is expected to be exit-free, it's OK
// to defer calls here to make required flushes/cleanup.
if ctx.ParsedFlags.CPUProfile != "" {
f, err := os.Create(ctx.ParsedFlags.CPUProfile)
if err != nil {
return 0, fmt.Errorf("Could not create CPU profile: %v", err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
return 0, fmt.Errorf("Could not start CPU profile: %v", err)
}
defer pprof.StopCPUProfile()
}
if ctx.ParsedFlags.MemProfile != "" {
defer func() {
f, err := os.Create(ctx.ParsedFlags.MemProfile)
if err != nil {
log.Printf("could not create memory profile: %v", err)
return
}
defer f.Close()
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Printf("could not write memory profile: %v", err)
}
}()
}
lint := ctx.MainConfig.linter
ruleSets := ctx.MainConfig.rulesSets
runner := NewLinterRunner(lint, linter.NewCheckersFilter())
err = runner.Init(ruleSets, &ctx.ParsedFlags)
if err != nil {
return 1, fmt.Errorf("init: %v", err)
}
lintdebug.Register(func(msg string) {
if lint.Config().Debug {
log.Print(msg)
}
})
go linter.MemoryLimiterThread(ctx.ParsedFlags.MaxFileSize)
log.Printf("Started")
if err := InitStubs(runner.linter); err != nil {
return 0, fmt.Errorf("Init stubs: %v", err)
}
if ctx.ParsedFlags.GitRepo != "" {
return gitMain(runner, ctx)
}
filenames := ctx.ParsedArgs
log.Printf("Indexing %+v", filenames)
runner.linter.AnalyzeFiles(workspace.ReadFilenames(filenames, nil, lint.Config().PhpExtensions))
parseIndexOnlyFiles(runner)
runner.linter.MetaInfo().SetIndexingComplete(true)
if ctx.ParsedFlags.FullAnalysisFiles != "" {
filenames = strings.Split(ctx.ParsedFlags.FullAnalysisFiles, ",")
}
log.Printf("Linting")
reports := runner.linter.AnalyzeFiles(workspace.ReadFilenames(filenames, runner.filenameFilter, lint.Config().PhpExtensions))
if ctx.ParsedFlags.OutputBaseline {
if err := createBaseline(runner, ctx.MainConfig, reports); err != nil {
return 1, fmt.Errorf("write baseline: %v", err)
}
return 0, nil
}
stat := processReports(runner, ctx.MainConfig, reports)
status = processReportsStat(ctx, stat)
return status, nil
}
func processReportsStat(ctx *AppContext, stat ReportsStat) (status int) {
if stat.critical > 0 {
status = 2
}
if !ctx.MainConfig.DisableAfterReportsLog {
if stat.autofixed > 0 {
if stat.autofixed == stat.all {
log.Printf("All issues have been fixed.\n")
} else {
log.Printf("Automatically fixed %d issues out of %d.\n", stat.autofixed, stat.all)
}
}
switch {
case stat.critical > 0:
log.Printf("Found %d critical and %d minor issues.", stat.critical, stat.minor)
case stat.minor > 0:
log.Printf("Found %d minor issues.", stat.minor)
default:
log.Printf("No issues found. Your code is perfect.")
}
if stat.autofixable > 0 {
log.Printf("%s can automatically fix %d issues.\n", ctx.App.Name, stat.autofixable)
log.Printf("Run %s again with --fix flag to fix them.\n", ctx.App.Name)
}
}
return status
}
func createBaseline(l *LinterRunner, cfg *MainConfig, reports []*linter.Report) error {
var stats baseline.Stats
stats.CountPerCheck = make(map[string]int)
files := make(map[string]baseline.FileProfile)
for _, r := range reports {
if cfg.BeforeReport != nil && !cfg.BeforeReport(r) {
continue
}
if !l.checkersFilter.IsEnabledReport(r.CheckName, r.Filename) {
continue
}
stats.CountTotal++
stats.CountPerCheck[r.CheckName]++
filename := filepath.Base(r.Filename)
f, ok := files[filename]
if !ok {
f.Filename = filename
f.Reports = make(map[uint64]baseline.Report)
}
info := f.Reports[r.Hash]
info.Count++
info.Hash = r.Hash
f.Reports[r.Hash] = info
files[filename] = f
}
profile := &baseline.Profile{
LinterVersion: cfg.LinterVersion,
CreatedAt: time.Now().Unix(),
Files: files,
}
return baseline.WriteProfile(l.outputFp, profile, &stats)
}
func FormatReport(r *linter.Report) string {
msg := r.Message
if r.CheckName != "" {
msg = r.CheckName + ": " + msg
}
// No context line for security-level warnings.
if r.Level == lintapi.LevelSecurity {
return fmt.Sprintf("%-7s %s at %s:%d", r.Severity(), msg, r.Filename, r.Line)
}
cursor := strings.Builder{}
for i, ch := range r.Context {
if i == r.StartChar {
break
}
if ch == '\t' {
cursor.WriteRune('\t')
} else {
cursor.WriteByte(' ')
}
}
if r.EndChar > r.StartChar {
cursor.WriteString(strings.Repeat("^", r.EndChar-r.StartChar))
}
return fmt.Sprintf("%-7s %s at %s:%d\n%s\n%s",
r.Severity(), msg, r.Filename, r.Line, r.Context, cursor.String())
}
type ReportsStat struct {
all int
critical int
minor int
autofixed int
autofixable int
}
func processReports(runner *LinterRunner, cfg *MainConfig, diff []*linter.Report) (stat ReportsStat) {
filtered := make([]*linter.Report, 0, len(diff))
for _, r := range diff {
if cfg.BeforeReport != nil && !cfg.BeforeReport(r) {
continue
}
stat.all++
if runner.linter.Config().ApplyQuickFixes && runner.config.Checkers.Autofixable(r.CheckName) {
stat.autofixed++
continue
}
if runner.config.Checkers.Autofixable(r.CheckName) {
stat.autofixable++
}
if runner.checkersFilter.IsCriticalReport(r) {
stat.critical++
} else {
stat.minor++
}
filtered = append(filtered, r)
}
if runner.flags.OutputJSON {
type reportList struct {
Reports []*linter.Report
Errors []string
}
list := &reportList{
Reports: filtered,
}
d := json.NewEncoder(runner.outputFp)
if err := d.Encode(list); err != nil {
// Should never fail to marshal our own reports.
panic(fmt.Sprintf("report list marshaling failed: %v", err))
}
} else {
for _, report := range filtered {
format := ""
if runner.checkersFilter.IsCriticalReport(report) {
format += "<critical> "
}
if runner.config.Checkers.Autofixable(report.CheckName) {
format += "<autofixable> "
}
format += "%s\n"
fmt.Fprintf(runner.outputFp, format, FormatReport(report))
}
}
return stat
}
func InitStubs(l *linter.Linter) error {
if l.Config().StubsDir != "" {
l.InitStubsFromDir(l.Config().StubsDir)
return nil
}
// Try to use embedded stubs (from stubs/phpstorm_stubs.go).
if err := loadEmbeddedStubs(l); err != nil {
return fmt.Errorf("failed to load embedded stubs: %v", err)
}
return nil
}
func LoadEmbeddedStubs(l *linter.Linter, filenames []string) error {
var errorsCount int64
readStubs := func(ch chan workspace.FileInfo) {
for _, filename := range filenames {
contents, err := stubs.Asset(filename)
if err != nil {
log.Printf("Failed to read embedded %q file: %v", filename, err)
atomic.AddInt64(&errorsCount, 1)
continue
}
ch <- workspace.FileInfo{
Name: filename,
Contents: contents,
}
}
}
l.InitStubs(readStubs)
// Using atomic here for consistency.
if atomic.LoadInt64(&errorsCount) != 0 {
return fmt.Errorf("failed to load %d embedded files", errorsCount)
}
return nil
}
func loadEmbeddedStubs(l *linter.Linter) error {
var filenames []string
// NOVERIFYDEBUG_LOAD_STUBS is used in golden tests to specify
// the test dependencies that need to be loaded.
if list := os.Getenv("NOVERIFYDEBUG_LOAD_STUBS"); list != "" {
filenames = strings.Split(list, ",")
} else {
filenames = stubs.AssetNames()
}
if len(filenames) == 0 {
return fmt.Errorf("empty file list")
}
return LoadEmbeddedStubs(l, filenames)
}