A high-performance static analysis tool designed to identify redundant code in C/C++ source files. It leverages regex-based pattern matching and basic control flow analysis to detect potential optimizations.
- Unused Variables: Detects variables that are declared but never used on reachable lines.
- Unused Functions: Identifies functions that are never called (excluding
main). - Unreachable Code: Detects code located after
return,break, orcontinuestatements within the same scope. - Smart Parsing: Handles single-line comments and string literals to avoid false positives.
- GCC/G++ Compiler
Build the analyzer using the following command:
g++ main.cpp analyzer.cpp utils.cpp -o analyzerRun the analyzer against any C/C++ source file:
./analyzer sample.c==== Dead Code Analysis Report ====
Unused Variables:
- b
- c
Unused Functions:
- unusedFunction
- add
Unreachable Code:
Line 13: b = b + 1; // unreachable
- Static Program Analysis: Analyzing code without executing it.
- Dead Code Elimination (DCE): A compiler optimization that removes code which does not affect the program results.
- Scope-Aware Reachability: Utilizing brace depth tracking to determine control flow boundaries.
Created as a demonstration of compiler-inspired static analysis techniques.