Source file src/cmd/vendor/golang.org/x/tools/internal/analysis/analyzerutil/readfile.go

     1  // Copyright 2025 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package analyzerutil
     6  
     7  // This file defines helpers for calling [analysis.Pass.ReadFile].
     8  
     9  import (
    10  	"go/token"
    11  	"os"
    12  
    13  	"golang.org/x/tools/go/analysis"
    14  )
    15  
    16  // ReadFile reads a file and adds it to the FileSet in pass
    17  // so that we can report errors against it using lineStart.
    18  func ReadFile(pass *analysis.Pass, filename string) ([]byte, *token.File, error) {
    19  	readFile := pass.ReadFile
    20  	if readFile == nil {
    21  		readFile = os.ReadFile
    22  	}
    23  	content, err := readFile(filename)
    24  	if err != nil {
    25  		return nil, nil, err
    26  	}
    27  	tf := pass.Fset.AddFile(filename, -1, len(content))
    28  	tf.SetLinesForContent(content)
    29  	return content, tf, nil
    30  }
    31  

View as plain text