golang

package
v0.0.0-...-f39ceb6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package golang implements the model to load and represent syntax and semantic information from source code in the .go files.

Specifically, this file defines SrcFile which provides interface to access the syntactic, type and semantic information from source code of one single file.

Package golang implements the model to load and represent syntax and semantic information from source code in the .go files.

Specifically, this file defines Package, which provides interfaces to load and access the type, syntax and semantic information of every source files under the package of being analyzed.

Package can be seen as the basic element taken into account for static analyzers in go-linters.

Package golang implements the model to load and represent syntax and semantic information from source code in the .go files.

Specifically, this file implements the top-level model Program that provides the packages and source files for static analyzers to consume.

Index

Constants

View Source
const (
	GoFileSuffix  = ".go"      // GoFileSuffix defines the suffix of go source files
	PackagePrefix = "package"  // PackagePrefix is the prefix of code line in package declaration
	GoModFileName = "go.mod"   // GoModFileName is the name of `go.mod` file to find module name
	GoModIndirect = "indirect" // GoModIndirect is the 'indirect' flag to specify dependency one
	ModulePrefix  = "module "  // ModulePrefix is the prefix of code line in `go.mod` with module
	VersionPrefix = "go "      // VersionPrefix is the prefix of code line in go.mod with version

	NewLine   = "\n" // NewLine is the string used to split code into lines
	TabString = "\t" // TabString is the prefix of \t
	SpaceChar = " "  // SpaceChar is a space ' '
)

Variables

This section is empty.

Functions

func LoadAllPkg

func LoadAllPkg(srcDir string) ([]*packages.Package, error)

LoadAllPkg will parse the AST of all source files under the directory and load the type & package information.

func LoadOneFile

func LoadOneFile(srcFile string) (*ast.File, *packages.Package, error)

LoadOneFile parses the AST of source file and its corresponding package info.

func LoadOnePkg

func LoadOnePkg(srcDir string) (*packages.Package, error)

LoadOnePkg simply load the syntax tree and type info of source files in the directory and return its package (as object of packages.Package).

Note that: this

Types

type LoadInfo

type LoadInfo struct {
	LoadTime     time.Time // LoadTime is the time this loading is executed
	LoadedFiles  []string  // LoadedFiles are paths of source files loaded
	IgnoredFiles []string  // IgnoredFiles are paths of those not be loaded
	IllTyped     bool      // IllTyped is true if any type error occurs in parsing
	FileErrors   []error   // FileErrors are a set of errors when parsing the file
	TypeErrors   []error   // TypeErrors are a set of errors in checking the types
	DepsErrors   []error   // DepsErrors are a set of errors in dependency imports
}

LoadInfo records the information of the last loading a package, including the syntactic, types and the other error information that might be used for debugging and analyzing.

type Module

type Module struct {
	RootPath     string            // RootPath is the absolute path of root directory of repository
	GoVersion    string            // GoVersion is the version of go language required in `go.mod`
	GoModFile    string            // GoModFile is the absolute path of go.mod file of the project
	ModuleName   string            // ModuleName is the name declared in go.mod file
	DirectDeps   map[string]string // DirectDeps map from dependency packages to required versions
	IndirectDeps map[string]string // IndirectDeps model those indirectly dependency packages info
}

Module gives the information in `go.mod` file that defines the module of project be analyzed.

type Package

type Package struct {
	// contains filtered or unexported fields
}

Package represents a package with its source files (modeled as SrcFile) being loaded from code.

Package is the basic element for static analyzer to taken as inputs (concurrently). The Package could be loaded with the syntactic, type's and semantic information of code at different levels.

func (*Package) DirPath

func (pkg *Package) DirPath() string

DirPath is the absolute path of directory of this package's source file

func (*Package) FileSet

func (pkg *Package) FileSet() *token.FileSet

FileSet positions the syntax and semantic element in its source files

func (*Package) GoFiles

func (pkg *Package) GoFiles() []string

GoFiles are the set of absolute paths of source files in this package

func (*Package) Imports

func (pkg *Package) Imports() []string

Imports are the set of logical paths of packages imported in this package

func (*Package) IsLoaded

func (pkg *Package) IsLoaded() bool

IsLoaded check whether this package is loaded with any syntax, type and semantic information of its source files.

func (*Package) LoadInfo

func (pkg *Package) LoadInfo() *LoadInfo

LoadInfo records the information of the latest loading for this package

func (*Package) PkgName

func (pkg *Package) PkgName() string

PkgName is the short name to refer this from the code of other packages

func (*Package) PkgPath

func (pkg *Package) PkgPath() string

PkgPath is logical path to import this package in file of other package

func (*Package) Program

func (pkg *Package) Program() *Program

Program is the parent object where this Package is created

func (*Package) SrcFile

func (pkg *Package) SrcFile(path string) *SrcFile

SrcFile returns the source file w.r.t. the absolute file in this package

func (*Package) TypeInfo

func (pkg *Package) TypeInfo() *types.Info

TypeInfo records the types and declarations of any variable and expression

func (*Package) TypePkg

func (pkg *Package) TypePkg() *types.Package

TypePkg declares the package and its types

func (*Package) TypeSize

func (pkg *Package) TypeSize() *types.Sizes

TypeSize records the size of bytes hold by any type in this package

type Program

type Program struct {
	// contains filtered or unexported fields
}

Program defines the top-level model of packages that will be taken as input by static analyzers.

func (*Program) AllPackages

func (prog *Program) AllPackages() []*Package

AllPackages return the set of all loaded packages in the program.

func (*Program) Module

func (prog *Program) Module() *Module

Module records the module information of go.mod from the program.

func (*Program) Package

func (prog *Program) Package(pkgPath string) *Package

Package return the unique package in program w.r.t. the unique path

type SrcFile

type SrcFile struct {
	// contains filtered or unexported fields
}

SrcFile represents a source code file in go program (ending with '.go' suffix).

It is the smallest unit for analyzer, and implements interfaces to access code text, syntactic tree along with the static single assignment IR members loaded from this source during parsing.

The syntax and semantic info of SrcFile can be updated by invoking SrcFile.update, which is an internal method that will (only) be used by Package when loading its source files from outside.

func LoadBaseFile

func LoadBaseFile(srcFile string) (*SrcFile, error)

func (*SrcFile) Code

func (file *SrcFile) Code() string

Code is the text in the source file being analyzed

func (*SrcFile) Contain

func (file *SrcFile) Contain(pos token.Pos) bool

Contain checks whether the position is included by this source file.

func (*SrcFile) Members

func (file *SrcFile) Members() []ssa.Member

Members return the static single assignment (SSA) members in the file or none if SSA form is not loaded.

func (*SrcFile) Package

func (file *SrcFile) Package() *Package

Package refers to the Package in which this source file is contained

func (*SrcFile) Path

func (file *SrcFile) Path() string

Path is the absolute path of the source file that it represents

func (*SrcFile) Syntax

func (file *SrcFile) Syntax() *ast.File

Syntax is the abstract syntax tree of source file (AST) or nil if it has not been loaded

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL