golang

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package golang provides Go AST parsing and transformation for clone detection.

Implementation split across: - nodetypes.go: AST node type constants - parse.go: Parse functions and transformer struct - transform.go: AST transformation logic - identifier_hash.go: semantic identifier/operator hashing - detection_mode.go: detection mode types - parse_config.go: parse configuration

Index

Constants

View Source
const (
	DetectionModeExact      = domain.DetectionModeExact
	DetectionModeSemantic   = domain.DetectionModeSemantic
	DetectionModeStructural = domain.DetectionModeStructural
)
View Source
const (
	BadNode = iota
	File
	ArrayType
	AssignStmt
	BasicLit
	BinaryExpr
	BlockStmt
	BranchStmt
	CallExpr
	CaseClause
	ChanType
	CommClause
	CompositeLit
	DeclStmt
	DeferStmt
	Ellipsis
	EmptyStmt
	ExprStmt
	Field
	FieldList
	ForStmt
	FuncDecl
	FuncLit
	FuncType
	GenDecl
	GoStmt
	Ident
	IfStmt
	IncDecStmt
	IndexExpr
	IndexListExpr
	InterfaceType
	KeyValueExpr
	LabeledStmt
	MapType
	ParenExpr
	RangeStmt
	ReturnStmt
	SelectStmt
	SelectorExpr
	SendStmt
	SliceExpr
	StarExpr
	StructType
	SwitchStmt
	TypeAssertExpr
	TypeSpec
	TypeSwitchStmt
	UnaryExpr
	ValueSpec
)

Variables

View Source
var ErrInvalidDetectionMode = errors.New("invalid detection mode")

ErrInvalidDetectionMode is returned when an invalid DetectionMode is used.

Functions

func DecodeBaseType

func DecodeBaseType(t int32) int32

DecodeBaseType extracts the base AST node type from a semantic-encoded type.

func DecodeSemanticHash

func DecodeSemanticHash(t int32) int32

DecodeSemanticHash extracts the identifier hash from a semantic-encoded type.

func Parse

func Parse(filename string) (*syntax.Node, error)

Parse the given file and return uniform syntax tree using default configuration. For custom configuration, use ParseWithConfig.

func ParseWithConfig

func ParseWithConfig(filename string, cfg ParseConfig) (*syntax.Node, error)

ParseWithConfig parses the given file with the specified configuration.

func ParseWithLineCount

func ParseWithLineCount(filename string) (*syntax.Node, int, error)

ParseWithLineCount parses the given file and returns the syntax tree along with the line count. Uses default configuration (semantic mode).

func ParseWithLineCountConfig

func ParseWithLineCountConfig(filename string, cfg ParseConfig) (*syntax.Node, int, error)

ParseWithLineCountConfig parses the given file with the specified configuration and returns the syntax tree along with the line count.

func TypeName added in v0.3.0

func TypeName(nodeType int32) string

TypeName returns the human-readable name for a Go AST node type constant. Returns "Unknown" for unrecognized types.

Types

type DetectionMode

type DetectionMode = domain.DetectionMode

type ParseConfig

type ParseConfig struct {
	// Mode determines whether semantic matching is enabled.
	// SemanticMode: Clones matched by structure AND identifier semantics (default).
	// StructuralMode: Clones matched by AST structure only.
	Mode DetectionMode

	// Preloaded, when non-nil, supplies a pre-parsed AST and type-checking
	// results. The transformer uses this AST instead of re-parsing and encodes
	// variable types into identifier hashes to reduce false positives where
	// different types share a method name (e.g. time.Time.String vs *big.Int.String).
	Preloaded *PreloadedAST
}

ParseConfig holds configuration for the Go source code parser. It controls how AST nodes are transformed and matched.

func DefaultParseConfig

func DefaultParseConfig() ParseConfig

DefaultParseConfig returns a ParseConfig with sensible defaults. By default, semantic matching is enabled to reduce false positives.

func MustParseConfig

func MustParseConfig(mode DetectionMode) ParseConfig

MustParseConfig returns a ParseConfig with the given mode, panicking if invalid. Use this for compile-time constants where you know the mode is valid.

func (ParseConfig) IsSemantic

func (cfg ParseConfig) IsSemantic() bool

IsSemantic returns true if semantic matching is enabled.

func (ParseConfig) Validate

func (cfg ParseConfig) Validate() error

Validate returns nil if the config is valid, otherwise an error describing the problem.

type PreloadedAST added in v0.4.0

type PreloadedAST struct {
	File     *ast.File
	TypeInfo *types.Info
	Fset     *token.FileSet
}

PreloadedAST holds a pre-parsed AST file together with its type-checking results. When provided to the transformer, the pre-loaded AST is used instead of re-parsing the file, and the TypeInfo is consulted to encode variable types into identifier hashes.

type TypeAwareData added in v0.4.0

type TypeAwareData map[string]*PreloadedAST

TypeAwareData is a map from absolute file path to its pre-loaded AST and type info.

func LoadTypeAwareData added in v0.4.0

func LoadTypeAwareData(files []string) (TypeAwareData, error)

LoadTypeAwareData loads type information for the given Go files using go/packages. Returns a map from absolute file path to PreloadedAST.

This is significantly slower than parsing alone (10-100x) because it runs the full Go type checker with import resolution. Only call when --type-aware is enabled.

func (TypeAwareData) LookupPreloaded added in v0.4.0

func (td TypeAwareData) LookupPreloaded(file string) *PreloadedAST

LookupPreloaded returns the pre-loaded data for the given file path, or nil if type-aware data was not loaded for this file.

Jump to

Keyboard shortcuts

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