github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/tools/nogo/analyzers.go (about)

     1  // Copyright 2019 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package nogo
    16  
    17  import (
    18  	"encoding/gob"
    19  
    20  	"golang.org/x/tools/go/analysis"
    21  	"golang.org/x/tools/go/analysis/passes/asmdecl"
    22  	"golang.org/x/tools/go/analysis/passes/assign"
    23  	"golang.org/x/tools/go/analysis/passes/atomic"
    24  	"golang.org/x/tools/go/analysis/passes/bools"
    25  	"golang.org/x/tools/go/analysis/passes/buildtag"
    26  	"golang.org/x/tools/go/analysis/passes/cgocall"
    27  	"golang.org/x/tools/go/analysis/passes/composite"
    28  	"golang.org/x/tools/go/analysis/passes/copylock"
    29  	"golang.org/x/tools/go/analysis/passes/errorsas"
    30  	"golang.org/x/tools/go/analysis/passes/httpresponse"
    31  	"golang.org/x/tools/go/analysis/passes/loopclosure"
    32  	"golang.org/x/tools/go/analysis/passes/lostcancel"
    33  	"golang.org/x/tools/go/analysis/passes/nilfunc"
    34  	"golang.org/x/tools/go/analysis/passes/nilness"
    35  	"golang.org/x/tools/go/analysis/passes/printf"
    36  	"golang.org/x/tools/go/analysis/passes/shadow"
    37  	"golang.org/x/tools/go/analysis/passes/shift"
    38  	"golang.org/x/tools/go/analysis/passes/stdmethods"
    39  	"golang.org/x/tools/go/analysis/passes/stringintconv"
    40  	"golang.org/x/tools/go/analysis/passes/structtag"
    41  	"golang.org/x/tools/go/analysis/passes/tests"
    42  	"golang.org/x/tools/go/analysis/passes/unmarshal"
    43  	"golang.org/x/tools/go/analysis/passes/unreachable"
    44  	"golang.org/x/tools/go/analysis/passes/unsafeptr"
    45  	"golang.org/x/tools/go/analysis/passes/unusedresult"
    46  	"honnef.co/go/tools/staticcheck"
    47  	"honnef.co/go/tools/stylecheck"
    48  
    49  	"github.com/SagerNet/gvisor/tools/checkescape"
    50  	"github.com/SagerNet/gvisor/tools/checklocks"
    51  	"github.com/SagerNet/gvisor/tools/checkunsafe"
    52  )
    53  
    54  // AllAnalyzers is a list of all available analyzers.
    55  var AllAnalyzers = []*analysis.Analyzer{
    56  	asmdecl.Analyzer,
    57  	assign.Analyzer,
    58  	atomic.Analyzer,
    59  	bools.Analyzer,
    60  	buildtag.Analyzer,
    61  	cgocall.Analyzer,
    62  	composite.Analyzer,
    63  	copylock.Analyzer,
    64  	errorsas.Analyzer,
    65  	httpresponse.Analyzer,
    66  	loopclosure.Analyzer,
    67  	lostcancel.Analyzer,
    68  	nilfunc.Analyzer,
    69  	nilness.Analyzer,
    70  	printf.Analyzer,
    71  	shift.Analyzer,
    72  	stdmethods.Analyzer,
    73  	stringintconv.Analyzer,
    74  	shadow.Analyzer,
    75  	structtag.Analyzer,
    76  	tests.Analyzer,
    77  	unmarshal.Analyzer,
    78  	unreachable.Analyzer,
    79  	unsafeptr.Analyzer,
    80  	unusedresult.Analyzer,
    81  	checkescape.Analyzer,
    82  	checkunsafe.Analyzer,
    83  	checklocks.Analyzer,
    84  }
    85  
    86  func register(all []*analysis.Analyzer) {
    87  	// Register all fact types.
    88  	//
    89  	// N.B. This needs to be done recursively, because there may be
    90  	// analyzers in the Requires list that do not appear explicitly above.
    91  	registered := make(map[*analysis.Analyzer]struct{})
    92  	var registerOne func(*analysis.Analyzer)
    93  	registerOne = func(a *analysis.Analyzer) {
    94  		if _, ok := registered[a]; ok {
    95  			return
    96  		}
    97  
    98  		// Register dependencies.
    99  		for _, da := range a.Requires {
   100  			registerOne(da)
   101  		}
   102  
   103  		// Register local facts.
   104  		for _, f := range a.FactTypes {
   105  			gob.Register(f)
   106  		}
   107  
   108  		registered[a] = struct{}{} // Done.
   109  	}
   110  	for _, a := range all {
   111  		registerOne(a)
   112  	}
   113  }
   114  
   115  func init() {
   116  	// Add all staticcheck analyzers.
   117  	for _, a := range staticcheck.Analyzers {
   118  		AllAnalyzers = append(AllAnalyzers, a)
   119  	}
   120  	// Add all stylecheck analyzers.
   121  	for _, a := range stylecheck.Analyzers {
   122  		AllAnalyzers = append(AllAnalyzers, a)
   123  	}
   124  
   125  	// Register lists.
   126  	register(AllAnalyzers)
   127  }