Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves #3081: add analyzer_flags to nogo config #3082

Merged
merged 6 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Resolves #3801: add analyzer_flags to nogo config
  • Loading branch information
navijation committed Mar 8, 2022
commit 1e2c9a76bd7ae31affb4f81b4fea36046683867a
19 changes: 14 additions & 5 deletions go/tools/builders/generate_nogo_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ var analyzers = []*analysis.Analyzer{
var configs = map[string]config{
{{- range $name, $config := .Configs}}
{{printf "%q" $name}}: config{
{{- if $config.AnalyzerFlags }}
analyzerFlags: map[string]string {
{{- range $flagKey, $flagValue := $config.AnalyzerFlags}}
{{printf "%q: %q" $flagKey $flagValue}},
{{- end}}
},
{{- end -}}
{{- if $config.OnlyFiles}}
onlyFiles: []*regexp.Regexp{
{{- range $path, $comment := $config.OnlyFiles}}
Expand Down Expand Up @@ -171,8 +178,9 @@ func buildConfig(path string) (Configs, error) {
}
configs[name] = Config{
// Description is currently unused.
OnlyFiles: config.OnlyFiles,
ExcludeFiles: config.ExcludeFiles,
OnlyFiles: config.OnlyFiles,
ExcludeFiles: config.ExcludeFiles,
AnalyzerFlags: config.AnalyzerFlags,
}
}
return configs, nil
Expand All @@ -181,7 +189,8 @@ func buildConfig(path string) (Configs, error) {
type Configs map[string]Config

type Config struct {
Description string
OnlyFiles map[string]string `json:"only_files"`
ExcludeFiles map[string]string `json:"exclude_files"`
Description string
OnlyFiles map[string]string `json:"only_files"`
ExcludeFiles map[string]string `json:"exclude_files"`
AnalyzerFlags map[string]string `json:"analyzer_flags"`
}
8 changes: 8 additions & 0 deletions go/tools/builders/nogo_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ func checkPackage(analyzers []*analysis.Analyzer, packagePath string, packageFil

roots := make([]*action, 0, len(analyzers))
for _, a := range analyzers {
if cfg, ok := configs[a.Name]; ok {
for flagKey, flagVal := range cfg.analyzerFlags {
a.Flags.Set(flagKey, flagVal)
}
}
roots = append(roots, visit(a))
}

Expand Down Expand Up @@ -457,6 +462,9 @@ type config struct {
// excludeFiles is a list of regular expressions that match files that an
// analyzer will not emit diagnostics for.
excludeFiles []*regexp.Regexp

// analyzerFlags is a map of
analyzerFlags map[string]string
navijation marked this conversation as resolved.
Show resolved Hide resolved
}

// importer is an implementation of go/types.Importer that imports type
Expand Down
89 changes: 89 additions & 0 deletions tests/core/nogo/custom/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ nogo(
":foofuncname",
":importfmt",
":visibility",
":flagger",
],
# config = "",
visibility = ["//visibility:public"],
Expand Down Expand Up @@ -71,6 +72,16 @@ go_library(
visibility = ["//visibility:public"],
)

go_library(
name = "flagger",
srcs = ["flagger.go"],
importpath = "flaggeranalyzer",
deps = [
"@org_golang_x_tools//go/analysis",
],
visibility = ["//visibility:public"],
)

go_library(
name = "has_errors",
srcs = ["has_errors.go"],
Expand Down Expand Up @@ -280,6 +291,48 @@ func run(pass *analysis.Pass) (interface{}, error) {
return nil, nil
}

-- flagger.go --
// flagger crashes when three flags are set in the config or else it no-ops
package flagger

import (
"errors"
"fmt"

"golang.org/x/tools/go/analysis"
)

var (
boolSwitch bool
stringSwitch string
intSwitch int
)

var Analyzer = &analysis.Analyzer{
Name: "flagger",
Run: run,
Doc: "Dummy analyzer that requires some flags to be set true in order to pass",
}

func init() {
Analyzer.Flags.BoolVar(&boolSwitch, "bool-switch", false, "Bool must be set to true to run")
Analyzer.Flags.StringVar(&stringSwitch, "string-switch", "no", "String must be set to \"yes\" to run")
Analyzer.Flags.IntVar(&intSwitch, "int-switch", 0, "Int must be set to 1 to run")
}

func run(pass *analysis.Pass) (interface{}, error) {
if !boolSwitch {
return nil, nil
}
if stringSwitch != "yes" {
return nil, nil
}
if intSwitch != 1 {
return nil, nil
}
return nil, errors.New("all switches were set -> fail")
}
navijation marked this conversation as resolved.
Show resolved Hide resolved

-- config.json --
{
"importfmt": {
Expand All @@ -297,6 +350,36 @@ func run(pass *analysis.Pass) (interface{}, error) {
}
}

-- flagger_config.json --
{
"importfmt": {
"only_files": {
"no_errors\\.go": ""
}
},
"foofuncname": {
"only_files": {
"no_errors\\.go": ""
}
},
"flagger": {
"description": "this will crash on the specified file",
"only_files": {
"has_errors\\.go": ""
},
"analyzer_flags": {
"bool-switch": "true",
"int-switch": "1",
"string-switch": "yes"
}
},
"visibility": {
"only_files": {
"no_errors\\.go": ""
}
}
}

-- has_errors.go --
package haserrors

Expand Down Expand Up @@ -427,6 +510,12 @@ func Test(t *testing.T) {
// note the cross platform regex :)
`.*[\\/]cgo[\\/]examplepkg[\\/]pure_src_with_err_calling_native.go:.*function must not be named Foo \(foofuncname\)`,
},
}, {
desc: "config_flags_triggering_error",
target: "//:has_errors",
wantSuccess: false,
config: "flagger_config.json",
includes: []string{"all switches were set -> fail"},
navijation marked this conversation as resolved.
Show resolved Hide resolved
}, {
desc: "no_errors",
target: "//:no_errors",
Expand Down