analyzer

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultCapacity = 100_000

DefaultCapacity for small to medium projects.

View Source
const LargeCapacity = 1_000_000

LargeCapacity for enterprise projects.

Variables

This section is empty.

Functions

func ForEachFile

func ForEachFile[T any](files []string, fn func(string) (T, error)) []T

ForEachFile processes files in parallel, calling fn for each file. No parser is provided; use this for non-AST operations (e.g., SATD scanning). Uses 2x NumCPU workers by default.

func ForEachFileWithProgress

func ForEachFileWithProgress[T any](files []string, fn func(string) (T, error), onProgress ProgressFunc) []T

ForEachFileWithProgress processes files in parallel with optional progress callback.

func MapFiles

func MapFiles[T any](files []string, fn func(*parser.Parser, string) (T, error)) []T

MapFiles processes files in parallel, calling fn for each file with a dedicated parser. Results are collected and returned in arbitrary order. Errors from individual files are silently skipped. Uses 2x NumCPU workers by default (optimal for mixed I/O and CGO workloads).

func MapFilesN

func MapFilesN[T any](files []string, maxWorkers int, fn func(*parser.Parser, string) (T, error), onProgress ProgressFunc) []T

MapFilesN processes files with a configurable worker count. If maxWorkers is <= 0, defaults to 2x NumCPU.

func MapFilesWithProgress

func MapFilesWithProgress[T any](files []string, fn func(*parser.Parser, string) (T, error), onProgress ProgressFunc) []T

MapFilesWithProgress processes files in parallel with optional progress callback.

Types

type AstContext added in v1.2.0

type AstContext struct {
	NodeType              AstNodeType
	ParentFunction        string
	Complexity            uint32
	SiblingsCount         int
	NestingDepth          int
	SurroundingStatements []string
}

AstContext provides context about a code location for severity adjustment. Matches PMAT's AstContext struct.

type AstNodeType added in v1.2.0

type AstNodeType int

AstNodeType represents the type of AST context for a code location. Matches PMAT's AstNodeType enum for severity adjustment.

const (
	// AstNodeRegular is a normal code location with no special context.
	AstNodeRegular AstNodeType = iota
	// AstNodeSecurityFunction is code within a security-related function.
	AstNodeSecurityFunction
	// AstNodeDataValidation is code within a data validation function.
	AstNodeDataValidation
	// AstNodeTestFunction is code within a test function.
	AstNodeTestFunction
	// AstNodeMockImplementation is code within a mock/stub implementation.
	AstNodeMockImplementation
)

type ChurnAnalyzer

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

ChurnAnalyzer analyzes git commit history for file churn.

func NewChurnAnalyzer

func NewChurnAnalyzer(days int) *ChurnAnalyzer

NewChurnAnalyzer creates a new churn analyzer.

func (*ChurnAnalyzer) AnalyzeFiles

func (a *ChurnAnalyzer) AnalyzeFiles(repoPath string, files []string) (*models.ChurnAnalysis, error)

AnalyzeFiles analyzes churn for specific files.

func (*ChurnAnalyzer) AnalyzeRepo

func (a *ChurnAnalyzer) AnalyzeRepo(repoPath string) (*models.ChurnAnalysis, error)

AnalyzeRepo analyzes git history for a repository.

func (*ChurnAnalyzer) SetSpinner

func (a *ChurnAnalyzer) SetSpinner(spinner *progress.Tracker)

SetSpinner sets a spinner for progress indication during analysis.

type ComplexityAnalyzer

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

ComplexityAnalyzer computes cyclomatic and cognitive complexity.

func NewComplexityAnalyzer

func NewComplexityAnalyzer() *ComplexityAnalyzer

NewComplexityAnalyzer creates a new complexity analyzer.

func NewComplexityAnalyzerWithOptions added in v1.2.0

func NewComplexityAnalyzerWithOptions(opts ComplexityOptions) *ComplexityAnalyzer

NewComplexityAnalyzerWithOptions creates a new complexity analyzer with options.

func (*ComplexityAnalyzer) AnalyzeFile

func (a *ComplexityAnalyzer) AnalyzeFile(path string) (*models.FileComplexity, error)

AnalyzeFile analyzes complexity for a single file.

func (*ComplexityAnalyzer) AnalyzeProject

func (a *ComplexityAnalyzer) AnalyzeProject(files []string) (*models.ComplexityAnalysis, error)

AnalyzeProject analyzes all files in a project using parallel processing.

func (*ComplexityAnalyzer) AnalyzeProjectWithProgress

func (a *ComplexityAnalyzer) AnalyzeProjectWithProgress(files []string, onProgress ProgressFunc) (*models.ComplexityAnalysis, error)

AnalyzeProjectWithProgress analyzes all files with optional progress callback.

func (*ComplexityAnalyzer) Close

func (a *ComplexityAnalyzer) Close()

Close releases analyzer resources.

type ComplexityOptions added in v1.2.0

type ComplexityOptions struct {
	IncludeHalstead bool // Calculate Halstead software science metrics
}

ComplexityOptions configures complexity analysis behavior.

func DefaultComplexityOptions added in v1.2.0

func DefaultComplexityOptions() ComplexityOptions

DefaultComplexityOptions returns default complexity analysis options.

type CoverageData added in v1.3.0

type CoverageData struct {
	CoveredLines    map[string]map[uint32]bool  // file -> line -> covered
	ExecutionCounts map[string]map[uint32]int64 // file -> line -> count
	// contains filtered or unexported fields
}

CoverageData integrates test coverage data from external tools (go test -cover, llvm-cov, etc.). This matches PMAT's CoverageData architecture for improved dead code detection accuracy.

func NewCoverageData added in v1.3.0

func NewCoverageData() *CoverageData

NewCoverageData creates an empty coverage data structure.

func (*CoverageData) GetExecutionCount added in v1.3.0

func (c *CoverageData) GetExecutionCount(file string, line uint32) int64

GetExecutionCount returns how many times a line was executed.

func (*CoverageData) IsLineCovered added in v1.3.0

func (c *CoverageData) IsLineCovered(file string, line uint32) bool

IsLineCovered checks if a line was covered during test execution.

type CrossLangReferenceGraph added in v1.3.0

type CrossLangReferenceGraph struct {
	Edges     []models.ReferenceEdge
	Nodes     map[uint32]*models.ReferenceNode
	EdgeIndex map[uint32][]int // node -> edge indices (outgoing)
	// contains filtered or unexported fields
}

CrossLangReferenceGraph tracks references across language boundaries. This matches PMAT's CrossLangReferenceGraph architecture.

func NewCrossLangReferenceGraph added in v1.3.0

func NewCrossLangReferenceGraph() *CrossLangReferenceGraph

NewCrossLangReferenceGraph creates an initialized reference graph.

func (*CrossLangReferenceGraph) AddEdge added in v1.3.0

func (g *CrossLangReferenceGraph) AddEdge(edge models.ReferenceEdge)

AddEdge adds an edge to the reference graph with indexing.

func (*CrossLangReferenceGraph) AddNode added in v1.3.0

func (g *CrossLangReferenceGraph) AddNode(node *models.ReferenceNode)

AddNode adds a node to the reference graph.

func (*CrossLangReferenceGraph) GetOutgoingEdges added in v1.3.0

func (g *CrossLangReferenceGraph) GetOutgoingEdges(nodeID uint32) []models.ReferenceEdge

GetOutgoingEdges returns all edges originating from a node.

type DeadCodeAnalyzer

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

DeadCodeAnalyzer detects unused functions, variables, and unreachable code. Architecture matches PMAT's DeadCodeAnalyzer with: - HierarchicalBitSet for efficient reachability tracking - CrossLangReferenceGraph for cross-language references - VTableResolver for dynamic dispatch resolution - CoverageData for test coverage integration - 4-phase analysis: build_reference_graph -> resolve_dynamic_calls -> mark_reachable -> classify_dead_code

func NewDeadCodeAnalyzer

func NewDeadCodeAnalyzer(confidence float64) *DeadCodeAnalyzer

NewDeadCodeAnalyzer creates a new dead code analyzer with PMAT-compatible architecture.

func (*DeadCodeAnalyzer) AnalyzeFile

func (a *DeadCodeAnalyzer) AnalyzeFile(path string) (*fileDeadCode, error)

AnalyzeFile analyzes a single file for dead code.

func (*DeadCodeAnalyzer) AnalyzeProject

func (a *DeadCodeAnalyzer) AnalyzeProject(files []string) (*models.DeadCodeAnalysis, error)

AnalyzeProject analyzes dead code across a project using the 4-phase PMAT architecture.

func (*DeadCodeAnalyzer) AnalyzeProjectWithProgress

func (a *DeadCodeAnalyzer) AnalyzeProjectWithProgress(files []string, onProgress ProgressFunc) (*models.DeadCodeAnalysis, error)

AnalyzeProjectWithProgress analyzes dead code with optional progress callback. Implements PMAT's 4-phase analysis: Phase 1: Build reference graph from AST Phase 2: Resolve dynamic dispatch Phase 3: Mark reachable from entry points Phase 4: Classify dead code by type

func (*DeadCodeAnalyzer) Close

func (a *DeadCodeAnalyzer) Close()

Close releases analyzer resources.

func (*DeadCodeAnalyzer) WithCallGraph added in v1.3.0

func (a *DeadCodeAnalyzer) WithCallGraph(enabled bool) *DeadCodeAnalyzer

WithCallGraph enables or disables call graph-based analysis.

func (*DeadCodeAnalyzer) WithCapacity added in v1.3.0

func (a *DeadCodeAnalyzer) WithCapacity(capacity uint32) *DeadCodeAnalyzer

WithCapacity sets the initial node capacity.

func (*DeadCodeAnalyzer) WithCoverage added in v1.3.0

func (a *DeadCodeAnalyzer) WithCoverage(coverage *CoverageData) *DeadCodeAnalyzer

WithCoverage adds test coverage data for improved accuracy.

type DefectAnalyzer

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

DefectAnalyzer predicts defect probability using PMAT weights.

func NewDefectAnalyzer

func NewDefectAnalyzer(churnDays int) *DefectAnalyzer

NewDefectAnalyzer creates a new defect analyzer with default PMAT weights.

func (*DefectAnalyzer) AnalyzeProject

func (a *DefectAnalyzer) AnalyzeProject(repoPath string, files []string) (*models.DefectAnalysis, error)

AnalyzeProject predicts defects across a project.

func (*DefectAnalyzer) Close

func (a *DefectAnalyzer) Close()

Close releases analyzer resources.

type DuplicateAnalyzer

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

DuplicateAnalyzer detects code clones using MinHash.

func NewDuplicateAnalyzer

func NewDuplicateAnalyzer(minLines int, threshold float64) *DuplicateAnalyzer

NewDuplicateAnalyzer creates a new duplicate analyzer.

func (*DuplicateAnalyzer) AnalyzeProject

func (a *DuplicateAnalyzer) AnalyzeProject(files []string) (*models.CloneAnalysis, error)

AnalyzeProject detects code clones across a project.

func (*DuplicateAnalyzer) AnalyzeProjectWithProgress

func (a *DuplicateAnalyzer) AnalyzeProjectWithProgress(files []string, onProgress ProgressFunc) (*models.CloneAnalysis, error)

AnalyzeProjectWithProgress detects code clones with optional progress callback.

func (*DuplicateAnalyzer) Close

func (a *DuplicateAnalyzer) Close()

Close releases analyzer resources.

type GraphAnalyzer

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

GraphAnalyzer builds dependency graphs from source code.

func NewGraphAnalyzer

func NewGraphAnalyzer(scope GraphScope) *GraphAnalyzer

NewGraphAnalyzer creates a new graph analyzer.

func (*GraphAnalyzer) AnalyzeProject

func (a *GraphAnalyzer) AnalyzeProject(files []string) (*models.DependencyGraph, error)

AnalyzeProject builds a dependency graph for a project.

func (*GraphAnalyzer) AnalyzeProjectWithProgress

func (a *GraphAnalyzer) AnalyzeProjectWithProgress(files []string, onProgress ProgressFunc) (*models.DependencyGraph, error)

AnalyzeProjectWithProgress builds a dependency graph with optional progress callback.

func (*GraphAnalyzer) CalculateMetrics

func (a *GraphAnalyzer) CalculateMetrics(graph *models.DependencyGraph) *models.GraphMetrics

CalculateMetrics computes comprehensive graph metrics.

func (*GraphAnalyzer) Close

func (a *GraphAnalyzer) Close()

Close releases analyzer resources.

func (*GraphAnalyzer) DetectCycles added in v1.4.0

func (a *GraphAnalyzer) DetectCycles(graph *models.DependencyGraph) [][]string

DetectCycles uses gonum's Tarjan SCC to find cycles.

func (*GraphAnalyzer) PruneGraph added in v1.4.0

func (a *GraphAnalyzer) PruneGraph(graph *models.DependencyGraph, maxNodes, maxEdges int) *models.DependencyGraph

PruneGraph reduces graph size while preserving important nodes using PageRank.

type GraphScope

type GraphScope string

GraphScope determines the granularity of graph nodes.

const (
	ScopeFile     GraphScope = "file"
	ScopeFunction GraphScope = "function"
	ScopeModule   GraphScope = "module"
	ScopePackage  GraphScope = "package"
)

type HalsteadAnalyzer added in v1.2.0

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

HalsteadAnalyzer calculates Halstead software science metrics.

func NewHalsteadAnalyzer added in v1.2.0

func NewHalsteadAnalyzer() *HalsteadAnalyzer

NewHalsteadAnalyzer creates a new Halstead analyzer.

func (*HalsteadAnalyzer) AnalyzeNode added in v1.2.0

func (h *HalsteadAnalyzer) AnalyzeNode(node *sitter.Node, source []byte, lang parser.Language) *models.HalsteadMetrics

AnalyzeNode analyzes a tree-sitter node for Halstead metrics.

func (*HalsteadAnalyzer) Reset added in v1.2.0

func (h *HalsteadAnalyzer) Reset()

Reset clears the analyzer state for a new analysis.

type HierarchicalBitSet added in v1.3.0

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

HierarchicalBitSet provides efficient bitmap-based reachability tracking using Roaring bitmaps. This matches PMAT's HierarchicalBitSet architecture for memory-efficient sparse bitset operations.

func NewHierarchicalBitSet added in v1.3.0

func NewHierarchicalBitSet(capacity uint32) *HierarchicalBitSet

NewHierarchicalBitSet creates a new hierarchical bitset with the given capacity.

func (*HierarchicalBitSet) CountSet added in v1.3.0

func (h *HierarchicalBitSet) CountSet() uint64

CountSet returns the number of reachable nodes.

func (*HierarchicalBitSet) IsSet added in v1.3.0

func (h *HierarchicalBitSet) IsSet(index uint32) bool

IsSet checks if a node is reachable.

func (*HierarchicalBitSet) Set added in v1.3.0

func (h *HierarchicalBitSet) Set(index uint32)

Set marks a node as reachable.

func (*HierarchicalBitSet) SetBatch added in v1.3.0

func (h *HierarchicalBitSet) SetBatch(indices []uint32)

SetBatch marks multiple nodes as reachable efficiently.

type ProgressFunc

type ProgressFunc func()

ProgressFunc is called after each file is processed.

type SATDAnalyzer

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

SATDAnalyzer detects self-admitted technical debt markers.

func NewSATDAnalyzer

func NewSATDAnalyzer() *SATDAnalyzer

NewSATDAnalyzer creates a new SATD analyzer with default patterns.

func NewSATDAnalyzerStrict added in v1.2.0

func NewSATDAnalyzerStrict() *SATDAnalyzer

NewSATDAnalyzerStrict creates a strict SATD analyzer that only matches explicit markers with colons (e.g., // TODO:, // FIXME:).

func NewSATDAnalyzerWithOptions added in v1.2.0

func NewSATDAnalyzerWithOptions(opts SATDOptions) *SATDAnalyzer

NewSATDAnalyzerWithOptions creates a SATD analyzer with custom options.

func (*SATDAnalyzer) AddPattern

func (a *SATDAnalyzer) AddPattern(pattern string, category models.DebtCategory, severity models.Severity) error

AddPattern adds a custom SATD detection pattern.

func (*SATDAnalyzer) AdjustSeverityWithContext added in v1.2.0

func (a *SATDAnalyzer) AdjustSeverityWithContext(base models.Severity, ctx *AstContext) models.Severity

AdjustSeverityWithContext modifies severity based on AST context. Matches PMAT's adjust_severity method: - SecurityFunction/DataValidation: escalate severity - TestFunction/MockImplementation: reduce severity - Regular with complexity > 20: escalate severity

func (*SATDAnalyzer) AnalyzeFile

func (a *SATDAnalyzer) AnalyzeFile(path string) ([]models.TechnicalDebt, error)

AnalyzeFile scans a file for SATD markers.

func (*SATDAnalyzer) AnalyzeProject

func (a *SATDAnalyzer) AnalyzeProject(files []string) (*models.SATDAnalysis, error)

AnalyzeProject scans all files in a project for SATD using parallel processing.

func (*SATDAnalyzer) AnalyzeProjectWithProgress

func (a *SATDAnalyzer) AnalyzeProjectWithProgress(files []string, onProgress ProgressFunc) (*models.SATDAnalysis, error)

AnalyzeProjectWithProgress scans all files with optional progress callback.

type SATDOptions added in v1.2.0

type SATDOptions struct {
	IncludeTests      bool // Include test files in analysis
	IncludeVendor     bool // Include vendor/third-party files
	AdjustSeverity    bool // Adjust severity based on context
	GenerateContextID bool // Generate context hash for identity tracking
	StrictMode        bool // Only match explicit markers with colons (e.g., // TODO:)
	ExcludeTestBlocks bool // Exclude SATD in Rust #[cfg(test)] blocks
}

SATDOptions configures SATD analysis behavior.

func DefaultSATDOptions added in v1.2.0

func DefaultSATDOptions() SATDOptions

DefaultSATDOptions returns the default options.

type TDGAnalyzer

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

TDGAnalyzer calculates Technical Debt Gradient scores.

func NewTDGAnalyzer

func NewTDGAnalyzer(churnDays int) *TDGAnalyzer

NewTDGAnalyzer creates a new TDG analyzer with default weights.

func (*TDGAnalyzer) AnalyzeProject

func (a *TDGAnalyzer) AnalyzeProject(repoPath string, files []string) (*models.TDGAnalysis, error)

AnalyzeProject calculates TDG scores for a project.

func (*TDGAnalyzer) Close

func (a *TDGAnalyzer) Close()

Close releases analyzer resources.

type VTable added in v1.3.0

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

VTable represents a virtual method table for dynamic dispatch resolution.

type VTableResolver added in v1.3.0

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

VTableResolver resolves virtual method calls and interface implementations. This matches PMAT's VTableResolver architecture for accurate dead code detection in OOP languages.

func NewVTableResolver added in v1.3.0

func NewVTableResolver() *VTableResolver

NewVTableResolver creates a new VTable resolver.

func (*VTableResolver) RegisterImplementation added in v1.3.0

func (v *VTableResolver) RegisterImplementation(interfaceName, typeName string)

RegisterImplementation records that a type implements an interface.

func (*VTableResolver) RegisterType added in v1.3.0

func (v *VTableResolver) RegisterType(typeName, baseType string, methods map[string]uint32)

RegisterType registers a type's virtual method table.

func (*VTableResolver) ResolveDynamicCall added in v1.3.0

func (v *VTableResolver) ResolveDynamicCall(interfaceName, methodName string) []uint32

ResolveDynamicCall returns all possible targets for a dynamic method call.

Jump to

Keyboard shortcuts

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