dfa

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DFABuilder

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

DFABuilder builds DFA information from a CFG using a language-specific RefExtractor.

func NewDFABuilder

func NewDFABuilder(extractor RefExtractor) *DFABuilder

NewDFABuilder creates a new DFABuilder with the given reference extractor.

func (*DFABuilder) Build

func (b *DFABuilder) Build(c *cfg.CFG) (*DFAInfo, error)

Build performs data flow analysis on the given CFG. It collects definitions, collects uses, then links def-use pairs.

type DFAFeatures

type DFAFeatures struct {
	PairCount       int
	AvgChainLength  float64
	CrossBlockRatio float64
	DefKindDist     map[DefUseKind]int
	UseKindDist     map[DefUseKind]int
}

DFAFeatures holds extracted DFA feature values for similarity comparison.

func ExtractDFAFeatures

func ExtractDFAFeatures(info *DFAInfo) *DFAFeatures

ExtractDFAFeatures extracts DFA features from DFAInfo for similarity comparison.

func NewDFAFeatures

func NewDFAFeatures() *DFAFeatures

NewDFAFeatures creates an empty DFAFeatures.

type DFAInfo

type DFAInfo struct {
	CFG       *cfg.CFG
	Chains    map[string]*DefUseChain
	BlockDefs map[string][]*VarReference // block ID -> definitions
	BlockUses map[string][]*VarReference // block ID -> uses
}

DFAInfo holds the complete data flow analysis results for a CFG.

func NewDFAInfo

func NewDFAInfo(c *cfg.CFG) *DFAInfo

NewDFAInfo creates a new DFAInfo for the given CFG.

func (*DFAInfo) AddDef

func (d *DFAInfo) AddDef(ref *VarReference)

AddDef records a definition in the DFA info.

func (*DFAInfo) AddUse

func (d *DFAInfo) AddUse(ref *VarReference)

AddUse records a use in the DFA info.

func (*DFAInfo) GetChain

func (d *DFAInfo) GetChain(variable string) *DefUseChain

GetChain returns the def-use chain for the given variable, creating it if needed.

func (*DFAInfo) TotalDefs

func (d *DFAInfo) TotalDefs() int

TotalDefs returns the total number of definitions across all chains.

func (*DFAInfo) TotalPairs

func (d *DFAInfo) TotalPairs() int

TotalPairs returns the total number of def-use pairs across all chains.

func (*DFAInfo) TotalUses

func (d *DFAInfo) TotalUses() int

TotalUses returns the total number of uses across all chains.

func (*DFAInfo) UniqueVariables

func (d *DFAInfo) UniqueVariables() int

UniqueVariables returns the number of unique variables.

type DefUseChain

type DefUseChain struct {
	Variable string
	Defs     []*VarReference
	Uses     []*VarReference
	Pairs    []*DefUsePair
}

DefUseChain represents a chain of definitions and uses for a single variable.

func NewDefUseChain

func NewDefUseChain(variable string) *DefUseChain

NewDefUseChain creates a new def-use chain for the given variable.

func (*DefUseChain) AddDef

func (c *DefUseChain) AddDef(ref *VarReference)

AddDef adds a definition to the chain.

func (*DefUseChain) AddPair

func (c *DefUseChain) AddPair(pair *DefUsePair)

AddPair adds a def-use pair to the chain.

func (*DefUseChain) AddUse

func (c *DefUseChain) AddUse(ref *VarReference)

AddUse adds a use to the chain.

type DefUseKind

type DefUseKind int

DefUseKind represents the kind of definition or use of a variable.

const (
	DefKindAssign    DefUseKind = iota // Assignment (x = ...)
	DefKindAugAssign                   // Augmented assignment (x += ...)
	DefKindParam                       // Function parameter
	DefKindImport                      // Import statement
	DefKindFor                         // For-loop variable
	DefKindWith                        // With-statement variable
	DefKindExcept                      // Exception handler variable
	DefKindGlobal                      // Global declaration
	DefKindNonlocal                    // Nonlocal declaration
	DefKindDelete                      // Delete statement
	DefKindPattern                     // Match/case capture pattern (case x:)
	UseKindLoad                        // Simple name load
	UseKindAttribute                   // Attribute access
	UseKindCall                        // Function call
	UseKindSubscript                   // Subscript access
)

func (DefUseKind) IsDef

func (k DefUseKind) IsDef() bool

IsDef returns true if this kind represents a definition.

func (DefUseKind) IsUse

func (k DefUseKind) IsUse() bool

IsUse returns true if this kind represents a use.

func (DefUseKind) String

func (k DefUseKind) String() string

String returns the string representation of a DefUseKind.

type DefUsePair

type DefUsePair struct {
	Def *VarReference
	Use *VarReference
}

DefUsePair represents a definition-use pair.

func NewDefUsePair

func NewDefUsePair(def, use *VarReference) *DefUsePair

NewDefUsePair creates a new def-use pair.

func (*DefUsePair) IsCrossBlock

func (p *DefUsePair) IsCrossBlock() bool

IsCrossBlock returns true if the def and use are in different blocks.

type ParamExtractor

type ParamExtractor interface {
	ExtractParameterDefs(functionNode any, entry *cfg.BasicBlock) []*VarReference
}

ParamExtractor is an optional RefExtractor extension that seeds function parameter definitions from the CFG's FunctionNode into the entry block, so parameter uses inside the body link back to a definition.

type RefExtractor

type RefExtractor interface {
	ExtractDefinitions(stmt any, block *cfg.BasicBlock, pos int) []*VarReference
	ExtractUses(stmt any, block *cfg.BasicBlock, pos int) []*VarReference
}

RefExtractor is the language-specific interface for extracting variable references. Language adapters (e.g. pyscn, jscan) implement this to extract definitions and uses from their AST nodes stored in BasicBlock.Statements.

type VarReference

type VarReference struct {
	Name      string
	Kind      DefUseKind
	Block     *cfg.BasicBlock
	Statement any
	Position  int
}

VarReference represents a reference to a variable (definition or use).

func NewVarReference

func NewVarReference(name string, kind DefUseKind, block *cfg.BasicBlock, stmt any, pos int) *VarReference

NewVarReference creates a new variable reference.

Jump to

Keyboard shortcuts

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