gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/github.com/securego/gosec/call_list.go (about)

     1  //
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  //     http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package gosec
    15  
    16  import (
    17  	"go/ast"
    18  )
    19  
    20  type set map[string]bool
    21  
    22  // CallList is used to check for usage of specific packages
    23  // and functions.
    24  type CallList map[string]set
    25  
    26  // NewCallList creates a new empty CallList
    27  func NewCallList() CallList {
    28  	return make(CallList)
    29  }
    30  
    31  // AddAll will add several calls to the call list at once
    32  func (c CallList) AddAll(selector string, idents ...string) {
    33  	for _, ident := range idents {
    34  		c.Add(selector, ident)
    35  	}
    36  }
    37  
    38  // Add a selector and call to the call list
    39  func (c CallList) Add(selector, ident string) {
    40  	if _, ok := c[selector]; !ok {
    41  		c[selector] = make(set)
    42  	}
    43  	c[selector][ident] = true
    44  }
    45  
    46  // Contains returns true if the package and function are
    47  /// members of this call list.
    48  func (c CallList) Contains(selector, ident string) bool {
    49  	if idents, ok := c[selector]; ok {
    50  		_, found := idents[ident]
    51  		return found
    52  	}
    53  	return false
    54  }
    55  
    56  // ContainsCallExpr resolves the call expression name and type
    57  /// or package and determines if it exists within the CallList
    58  func (c CallList) ContainsCallExpr(n ast.Node, ctx *Context) *ast.CallExpr {
    59  	selector, ident, err := GetCallInfo(n, ctx)
    60  	if err != nil {
    61  		return nil
    62  	}
    63  
    64  	// Use only explicit path to reduce conflicts
    65  	if path, ok := GetImportPath(selector, ctx); ok && c.Contains(path, ident) {
    66  		return n.(*ast.CallExpr)
    67  	}
    68  
    69  	/*
    70  		// Try direct resolution
    71  		if c.Contains(selector, ident) {
    72  			log.Printf("c.Contains == true, %s, %s.", selector, ident)
    73  			return n.(*ast.CallExpr)
    74  		}
    75  	*/
    76  
    77  	return nil
    78  }