Repository navigation
Architecture
Version: 0.9.x Last Updated: 2025-10-30 Performance: O(n) single-pass analysis with 99.75% speed improvement
- Overview
- Core Architecture
- UnifiedRuleVisitor Pattern
- Performance Optimizations
- Symbol Table Analysis
- Sensor Execution Pipeline
- Adding New Rules
- Build System
The SonarQube Mathematica Plugin provides comprehensive static analysis for Wolfram Mathematica code with 430+ rules across multiple categories:
- 21 Vulnerability rules
- 7 Security Hotspot rules
- ~80 Bug rules
- ~300 Code Smell rules
- 20 Symbol Table analysis rules
- O(n) Complexity: Single-pass AST analysis (down from O(400n))
- 99.75% Performance Improvement: From never finishing → 8 minutes for 654 files
- Cross-file Analysis: Symbol table tracks variables across packages
- Modular Design: Clean separation of concerns
┌─────────────────────────────────────────────────────────────────┐
│ SonarQube Plugin Core │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌────────────────────┐ ┌──────────────────────┐ │
│ │ MathematicaPlugin │──────│ MathematicaLanguage │ │
│ │ (Entry Point) │ │ (Language Definition)│ │
│ └────────────────────┘ └──────────────────────┘ │
│ │ │
│ │ Registers Sensors ↓ │
│ │ │
│ ┌────────┴─────────────────────────────────────────────┐ │
│ │ Sensor Pipeline │ │
│ │ (Executed in order for each input file) │ │
│ │ │ │
│ │ 1. MathematicaRulesSensor │ │
│ │ ├─ UnifiedRuleVisitor (O(n) single-pass) │ │
│ │ └─ SymbolTableAnalysis (O(n²) separate) │ │
│ │ │ │
│ │ 2. MathematicaMetricsSensor │ │
│ │ └─ ComplexityCalculator │ │
│ │ │ │
│ │ 3. MathematicaCpdTokenizer │ │
│ │ └─ Token-based duplication detection │ │
│ └───────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘
org.sonar.plugins.mathematica/
├── MathematicaPlugin.java # Entry point
├── MathematicaLanguage.java # Language definition
├── MathematicaCpdTokenizer.java # Duplication detection
│
├── ast/ # AST Parser
│ ├── AstNode.java # Base AST node
│ ├── UnifiedRuleVisitor.java # ⭐ CORE: Single-pass analyzer
│ ├── FunctionCallNode.java # Function call AST nodes
│ ├── FunctionDefNode.java # Function definition AST nodes
│ ├── IdentifierNode.java # Variable/symbol nodes
│ └── LiteralNode.java # Literal value nodes
│
├── symboltable/ # Symbol Table Analysis
│ ├── SymbolTable.java # Symbol storage
│ ├── SymbolTableBuilder.java # Builds symbol table
│ └── Scope.java # Scope representation
│
├── metrics/ # Metrics Calculation
│ ├── MathematicaMetricsSensor.java # Metrics sensor
│ └── ComplexityCalculator.java # Complexity metrics
│
└── rules/ # Rule Sensors
├── MathematicaRulesSensor.java # Main rule sensor
├── MathematicaRulesDefinition.java # Rule definitions
└── MathematicaRuleKeys.java # Rule key constants
Old Architecture (Regex-based):
// OLD: O(400n) - Each file scanned 400+ times
for (InputFile file : files) {
String content = read(file);
// Rule 1: Scan entire file
bugDetector.detectDivisionByZero(content); // Scan 1
// Rule 2: Scan entire file AGAIN
bugDetector.detectNullPointer(content); // Scan 2
// Rule 3: Scan entire file AGAIN
codeSmellDetector.detectMagicNumbers(content); // Scan 3
// ... 400+ more scans of the same file!
}
// Result: 90-268 seconds PER FILE on large files
// Result: Scans never completed (hockey stick performance)Performance Characteristics:
- Complexity: O(400n) where n = file size
- Large file: 90-268 seconds for a single 5000-line file
- Analysis time dominated by 400+ regex scans
- Pattern: "Hockey stick" - exponentially slower as files grow
New Architecture (UnifiedRuleVisitor):
// NEW: O(n) - Each file scanned ONCE
for (InputFile file : files) {
String content = read(file);
// Single-pass AST analysis
UnifiedRuleVisitor visitor = new UnifiedRuleVisitor(inputFile, sensor);
List<AstNode> ast = parseToAST(content); // Parse ONCE
for (AstNode node : ast) {
node.accept(visitor); // Apply ALL 400+ rules in ONE PASS
}
}
// Result: 0-18% of previous time (usually <10%)
// Result: 8 minutes for 654 files (was: never finished)Performance Characteristics:
- Complexity: O(n) where n = file size
- Large file: 0.1-1 seconds (vs 90-268 seconds before)
- Analysis time: Linear with file size
- Pattern: Flat performance curve
The visitor pattern processes each AST node exactly once, checking ALL rules at that node:
public class UnifiedRuleVisitor implements AstVisitor {
@Override
public void visit(FunctionCallNode node) {
String funcName = node.getFunctionName();
// Check ALL rules that apply to function calls
// (All executed in a single pass through the AST)
// Vulnerability rules (21 checks)
checkHardcodedCredentials(node);
checkCommandInjection(node);
checkSqlInjection(node);
// ... 18 more vulnerability checks
// Security hotspot rules (7 checks)
checkFileUploadValidation(node);
checkExternalApiSafeguards(node);
checkCryptoKeyGeneration(node);
// ... 4 more security hotspot checks
// Bug rules (~80 checks)
checkDivisionByZero(node);
checkNullPointer(node);
checkInfiniteRecursion(node);
// ... ~77 more bug checks
// Code smell rules (~300 checks)
checkMagicNumbers(node);
checkLongFunctions(node);
checkComplexity(node);
// ... ~297 more code smell checks
}
@Override
public void visit(FunctionDefNode node) {
// Check rules that apply to function definitions
// All executed in same single pass
}
// Other visit methods for different node types...
}| Metric | Old (Regex) | New (UnifiedAST) | Improvement |
|---|---|---|---|
| Scan Complexity | O(400n) | O(n) | 99.75% faster |
| Large File (5000 lines) | 90-268 seconds | 0.1-1 seconds | 99.6% faster |
| 654 Files Total | Never finished | 8.5 minutes | ∞ → finite! |
| Time per File | Exponential | Constant | Linear scaling |
| AST Overhead | N/A | 0-18% | Negligible |
Impact: 99.75% performance improvement
Before: O(400n) - Each file scanned 400+ times After: O(n) - Each file scanned once
Implementation:
-
UnifiedRuleVisitor.java- Visitor pattern processing each node once - All 400+ rules check each node during single traversal
- AST built once, reused for all rules
Impact: 30-50% additional speed boost
Before:
public void detectBug(String content) {
Pattern p = Pattern.compile("regex"); // Compiled EVERY call!
Matcher m = p.matcher(content);
// ...
}After:
private static final Pattern BUG_PATTERN = Pattern.compile("regex"); // Compiled ONCE
public void detectBug(String content) {
Matcher m = BUG_PATTERN.matcher(content); // Reuse compiled pattern
// ...
}Files Optimized:
- 24 patterns pre-compiled across detector classes
- Eliminates pattern compilation overhead
Impact: 20x faster on subsequent scans
Implementation:
if (inputFile.status() == InputFile.Status.SAME) {
return; // Skip unchanged files
}
// Analyze only changed/new filesPerformance:
- First scan: 1000 files = 120 seconds
- Incremental: 50 changed files = 6 seconds (20x faster!)
While UnifiedRuleVisitor is O(n), the Symbol Table analysis is kept separate as O(n²) for accuracy:
┌─────────────────────────────────────┐
│ Symbol Table Analysis (O(n²)) │
├─────────────────────────────────────┤
│ │
│ For each variable: │
│ - Track all definitions │
│ - Track all uses │
│ - Check lifetime/scope │
│ │
│ Cross-reference analysis: │
│ - Variable shadowing │
│ - Unused variables │
│ - Dead stores │
│ - Use before assignment │
│ │
└─────────────────────────────────────┘
Accuracy: Variable lifetime analysis requires comparing every definition against every use Performance: Still manageable - 33 seconds for complex 5000-line file (was 97% of file time) Value: Provides 20+ high-value rules that can't be done in O(n)
Decision: User explicitly wants to keep despite O(n²) cost for the quality of analysis
Sensors execute in order for each file:
Responsibilities:
- Run UnifiedRuleVisitor (single-pass, O(n))
- Run SymbolTable analysis (O(n²), but valuable)
- Report issues to SonarQube
Performance Breakdown:
- UnifiedAST: 0-18% of file time (usually <10%)
- SymbolTable: Up to 97% of file time (on complex files)
- Total: 79 seconds for 654 files
Responsibilities:
- Calculate cyclomatic complexity
- Calculate cognitive complexity
- Count functions
- Count statements
Metrics Reported:
-
COMPLEXITY- Decision points -
COGNITIVE_COMPLEXITY- Understandability -
FUNCTIONS- Function count -
STATEMENTS- Statement count
Responsibilities:
- Tokenize Mathematica code
- Feed to SonarQube's CPD engine
- Duplication detection
Configuration:
sonar.cpd.mathematica.minimumTokens=250
sonar.cpd.mathematica.minimumLines=25| Component | Time | Percentage | Optimizable |
|---|---|---|---|
| Plugin Rules | 79s | 15% | ✅ Already optimized (O(n)) |
| Plugin Metrics | 26s | 5% | ✅ Already fast |
| Plugin CPD | 153s | 30% | 🟡 SonarQube core (limited) |
| SCM Publisher | 200s | 39% | ❌ Git blame (can't optimize) |
| Issue Saving | 30s | 6% | ❌ SonarQube core |
| Other | 24s | 5% | - |
Plugin contribution: 79s + 26s + 153s = 258s (50% of total) Already optimized: Rules + Metrics = 105s (20% of total) SonarQube core: 383s (75% of total - outside plugin control)
Step 1: Add rule definition in MathematicaRulesDefinition.java:
public static final String MY_NEW_RULE_KEY = "MyNewRule";
// In define() method:
NewBuiltInQualityProfilesDefinition.NewBuiltInActiveRule rule = repository.createRule(MY_NEW_RULE_KEY)
.setName("Detect My Pattern")
.setHtmlDescription("Detects dangerous pattern...")
.setSeverity("MAJOR")
.setType(RuleType.BUG);Step 2: Add check method in UnifiedRuleVisitor.java:
private void checkMyNewRule(FunctionCallNode node) {
if (node.getFunctionName().equals("DangerousFunction")) {
reportIssue(node.getStartLine(), MY_NEW_RULE_KEY,
"Avoid using DangerousFunction due to...");
}
}Step 3: Call from appropriate visit method:
@Override
public void visit(FunctionCallNode node) {
// ... existing checks ...
checkMyNewRule(node); // Add your check
}Performance: No overhead! Checked during existing single pass.
Only add here if the rule REQUIRES cross-referencing all variables:
Examples:
- Unused variable detection
- Variable shadowing
- Use before assignment
- Dead store elimination
Performance: Adds to O(n²) analysis time, so use sparingly.
Git Tag-Based:
# Version comes from git tags
git tag v1.0.0
git push origin v1.0.0
# Gradle extracts version automatically
./gradlew build # Creates wolfralyze-1.0.0.jarImplementation (build.gradle):
def getVersionFromGit() {
def process = ['git', 'describe', '--tags', '--exact-match'].execute()
process.waitFor()
if (process.exitValue() == 0) {
def gitTag = process.text.trim()
if (gitTag && gitTag.startsWith('v')) {
return gitTag.substring(1) // v1.0.0 → 1.0.0
}
}
return '0.1.0-SNAPSHOT' // Fallback
}
version = getVersionFromGit()Available Targets:
make # Show help
make build # Build plugin JAR
make clean # Remove all build artifacts
make test # Run unit tests
make version # Show current version
make install # Full automated install to SonarQubeAutomated Install Process:
SONARQUBE_HOME=/path/to/sonarqube make installSteps Executed:
- Stop SonarQube
- Remove ALL old plugin versions
- Copy current version only
- Start SonarQube
- Wait for "SonarQube is operational" in logs
- Display success message
Key Features:
- Removes old versions automatically (prevents duplicate plugin errors)
- Verifies removal succeeded
- Waits for full startup before returning
- Platform-specific restart commands (macOS/Linux)
Automated Releases (.github/workflows/release.yml):
Trigger: Push git tag (v*..)
git tag v1.0.0
git push origin v1.0.0Automated Steps:
- Build plugin with Java 17
- Generate SHA256 hash
- Create GitHub release
- Attach JAR as artifact
- Include installation instructions in release notes
Release Contents:
- JAR file (verified by SHA256)
- Installation instructions (all platforms)
- Version information
- Feature list
Test Framework: JUnit 5
Coverage:
- Symbol table functionality
- AST node parsing
- Language definition
- Plugin initialization
Run Tests:
./gradlew test
# Or via Makefile:
make testManual Test Process:
- Build plugin:
make build - Install to test SonarQube:
make install - Run scanner on test project
- Verify issues detected in UI
- Check performance metrics
Test Projects:
- Small test suite (10-50 files)
- Large codebase (650+ files)
- Known edge cases
SonarQube Logs:
tail -f $SONARQUBE_HOME/logs/ce.log | grep "sensor"Key Metrics to Watch:
- Sensor execution time
- Total analysis time
- Memory usage (metaspace)
Expected Performance (654 files):
- Total: ~8-10 minutes
- Rules sensor: ~80 seconds (15%)
- Metrics sensor: ~26 seconds (5%)
- CPD: ~153 seconds (30%)
Check UnifiedAST overhead:
- Should be 0-18% of file time
- Usually <10% for most files
Check SymbolTable time:
- Can be 60-97% of file time (expected for complex files)
- Still completes in reasonable time
If scan is very slow:
- Check heap/metaspace settings
- Look for memory pressure
- Consider excluding large auto-generated files
Alternatives Considered:
- ❌ Keep regex-based: Too slow (never finished)
- ❌ Multiple AST passes: Still O(kn), less improvement
- ✅ Single-pass visitor: O(n), maximum performance
Trade-offs:
- ✅ 99.75% performance improvement
- ✅ Scales to large codebases
- 🟡 Slightly more complex to add rules
- 🟡 All rules must fit visitor pattern
Could have been integrated, but:
- ✅ Cleaner separation of concerns
- ✅ Symbol analysis is inherently O(n²)
- ✅ User values accuracy over speed for this analysis
- ✅ Still performs well enough (33s for complex 5000-line file)
Decision: Keep separate for code quality
Could implement Hindley-Milner type inference, but:
- 🟡 Mathematica is dynamically typed
- 🟡 Complex to maintain
- 🟡 Diminishing returns
Current approach: Basic type tracking sufficient for most rules
-
Test Coverage Integration
- Parse VerificationTest output
- Import generic coverage XML
- Display in SonarQube UI
-
Quick Fixes
- Add SonarLint integration
- Provide one-click fixes for common issues
-
Custom Rule Templates
- Allow users to define custom rules
- Template-based approach
-
Enhanced Type Inference
- Track types through more complex expressions
- Better nullable/null analysis
Current bottlenecks (from 8:29 scan):
- SCM Publisher (200s / 39%) - Git blame, can't optimize
- CPD (153s / 30%) - SonarQube core, limited optimization
- Symbol Table (part of 79s) - O(n²) but valuable
Potential gains:
- Rules sensor: Already optimal (O(n))
- Metrics sensor: Already fast
- CPD: Minor gains possible
- Realistic target: ~7-8 minutes (current is excellent!)
The SonarQube Mathematica Plugin achieves professional-grade (Tier 2+) language support through:
- Architecture: Clean, modular design with clear separation of concerns
- Performance: O(n) single-pass analysis with 99.75% improvement
- Scale: Handles large codebases (650+ files in 8 minutes)
- Quality: 430+ rules covering all major code quality dimensions
- Automation: Complete build/test/release pipeline
Key Innovation: UnifiedRuleVisitor pattern enabling O(400n) → O(n) transformation
For More Information:
- README.md - User guide and features
- ROADMAP.md - Feature roadmap and tier progression
- IMPROVEMENTS.md - Performance improvements and metrics
- Source code -
/src/main/java/org/sonar/plugins/mathematica/