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
- Variables
- func DecodeBaseType(t int32) int32
- func DecodeSemanticHash(t int32) int32
- func Parse(filename string) (*syntax.Node, error)
- func ParseWithConfig(filename string, cfg ParseConfig) (*syntax.Node, error)
- func ParseWithLineCount(filename string) (*syntax.Node, int, error)
- func ParseWithLineCountConfig(filename string, cfg ParseConfig) (*syntax.Node, int, error)
- func TypeName(nodeType int32) string
- type DetectionMode
- type ParseConfig
- type PreloadedAST
- type TypeAwareData
Constants ¶
const ( DetectionModeExact = domain.DetectionModeExact DetectionModeSemantic = domain.DetectionModeSemantic DetectionModeStructural = domain.DetectionModeStructural )
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 ¶
var ErrInvalidDetectionMode = errors.New("invalid detection mode")
ErrInvalidDetectionMode is returned when an invalid DetectionMode is used.
Functions ¶
func DecodeBaseType ¶
DecodeBaseType extracts the base AST node type from a semantic-encoded type.
func DecodeSemanticHash ¶
DecodeSemanticHash extracts the identifier hash from a semantic-encoded type.
func Parse ¶
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 ¶
ParseWithLineCount parses the given file and returns the syntax tree along with the line count. Uses default configuration (semantic mode).
func ParseWithLineCountConfig ¶
ParseWithLineCountConfig parses the given file with the specified configuration and returns the syntax tree along with the line count.
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
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.