-
Notifications
You must be signed in to change notification settings - Fork 979
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1895 from crytic/ck-printer
feat: ck printer
- Loading branch information
Showing
13 changed files
with
1,145 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
from .state_variable import StateVariable | ||
from .variable import Variable | ||
from .local_variable_init_from_tuple import LocalVariableInitFromTuple | ||
from .local_variable import LocalVariable | ||
from .top_level_variable import TopLevelVariable | ||
from .event_variable import EventVariable | ||
from .function_type_variable import FunctionTypeVariable | ||
from .structure_variable import StructureVariable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
CK Metrics are a suite of six software metrics proposed by Chidamber and Kemerer in 1994. | ||
These metrics are used to measure the complexity of a class. | ||
https://en.wikipedia.org/wiki/Programming_complexity | ||
- Response For a Class (RFC) is a metric that measures the number of unique method calls within a class. | ||
- Number of Children (NOC) is a metric that measures the number of children a class has. | ||
- Depth of Inheritance Tree (DIT) is a metric that measures the number of parent classes a class has. | ||
- Coupling Between Object Classes (CBO) is a metric that measures the number of classes a class is coupled to. | ||
Not implemented: | ||
- Lack of Cohesion of Methods (LCOM) is a metric that measures the lack of cohesion in methods. | ||
- Weighted Methods per Class (WMC) is a metric that measures the complexity of a class. | ||
During the calculation of the metrics above, there are a number of other intermediate metrics that are calculated. | ||
These are also included in the output: | ||
- State variables: total number of state variables | ||
- Constants: total number of constants | ||
- Immutables: total number of immutables | ||
- Public: total number of public functions | ||
- External: total number of external functions | ||
- Internal: total number of internal functions | ||
- Private: total number of private functions | ||
- Mutating: total number of state mutating functions | ||
- View: total number of view functions | ||
- Pure: total number of pure functions | ||
- External mutating: total number of external mutating functions | ||
- No auth or onlyOwner: total number of functions without auth or onlyOwner modifiers | ||
- No modifiers: total number of functions without modifiers | ||
- Ext calls: total number of external calls | ||
""" | ||
from slither.printers.abstract_printer import AbstractPrinter | ||
from slither.utils.ck import CKMetrics | ||
from slither.utils.output import Output | ||
|
||
|
||
class CK(AbstractPrinter): | ||
ARGUMENT = "ck" | ||
HELP = "Chidamber and Kemerer (CK) complexity metrics and related function attributes" | ||
|
||
WIKI = "https://github.com/trailofbits/slither/wiki/Printer-documentation#ck" | ||
|
||
def output(self, _filename: str) -> Output: | ||
if len(self.contracts) == 0: | ||
return self.generate_output("No contract found") | ||
|
||
ck = CKMetrics(self.contracts) | ||
|
||
res = self.generate_output(ck.full_text) | ||
res.add_pretty_table(ck.auxiliary1.pretty_table, ck.auxiliary1.title) | ||
res.add_pretty_table(ck.auxiliary2.pretty_table, ck.auxiliary2.title) | ||
res.add_pretty_table(ck.auxiliary3.pretty_table, ck.auxiliary3.title) | ||
res.add_pretty_table(ck.auxiliary4.pretty_table, ck.auxiliary4.title) | ||
res.add_pretty_table(ck.core.pretty_table, ck.core.title) | ||
self.info(ck.full_text) | ||
|
||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
Halstead complexity metrics | ||
https://en.wikipedia.org/wiki/Halstead_complexity_measures | ||
12 metrics based on the number of unique operators and operands: | ||
Core metrics: | ||
n1 = the number of distinct operators | ||
n2 = the number of distinct operands | ||
N1 = the total number of operators | ||
N2 = the total number of operands | ||
Extended metrics1: | ||
n = n1 + n2 # Program vocabulary | ||
N = N1 + N2 # Program length | ||
S = n1 * log2(n1) + n2 * log2(n2) # Estimated program length | ||
V = N * log2(n) # Volume | ||
Extended metrics2: | ||
D = (n1 / 2) * (N2 / n2) # Difficulty | ||
E = D * V # Effort | ||
T = E / 18 seconds # Time required to program | ||
B = (E^(2/3)) / 3000 # Number of delivered bugs | ||
""" | ||
from slither.printers.abstract_printer import AbstractPrinter | ||
from slither.utils.halstead import HalsteadMetrics | ||
from slither.utils.output import Output | ||
|
||
|
||
class Halstead(AbstractPrinter): | ||
ARGUMENT = "halstead" | ||
HELP = "Computes the Halstead complexity metrics for each contract" | ||
|
||
WIKI = "https://github.com/trailofbits/slither/wiki/Printer-documentation#halstead" | ||
|
||
def output(self, _filename: str) -> Output: | ||
if len(self.contracts) == 0: | ||
return self.generate_output("No contract found") | ||
|
||
halstead = HalsteadMetrics(self.contracts) | ||
|
||
res = self.generate_output(halstead.full_text) | ||
res.add_pretty_table(halstead.core.pretty_table, halstead.core.title) | ||
res.add_pretty_table(halstead.extended1.pretty_table, halstead.extended1.title) | ||
res.add_pretty_table(halstead.extended2.pretty_table, halstead.extended2.title) | ||
self.info(halstead.full_text) | ||
|
||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Robert "Uncle Bob" Martin - Agile software metrics | ||
https://en.wikipedia.org/wiki/Software_package_metrics | ||
Efferent Coupling (Ce): Number of contracts that the contract depends on | ||
Afferent Coupling (Ca): Number of contracts that depend on a contract | ||
Instability (I): Ratio of efferent coupling to total coupling (Ce / (Ce + Ca)) | ||
Abstractness (A): Number of abstract contracts / total number of contracts | ||
Distance from the Main Sequence (D): abs(A + I - 1) | ||
""" | ||
from slither.printers.abstract_printer import AbstractPrinter | ||
from slither.utils.martin import MartinMetrics | ||
from slither.utils.output import Output | ||
|
||
|
||
class Martin(AbstractPrinter): | ||
ARGUMENT = "martin" | ||
HELP = "Martin agile software metrics (Ca, Ce, I, A, D)" | ||
|
||
WIKI = "https://github.com/trailofbits/slither/wiki/Printer-documentation#martin" | ||
|
||
def output(self, _filename: str) -> Output: | ||
if len(self.contracts) == 0: | ||
return self.generate_output("No contract found") | ||
|
||
martin = MartinMetrics(self.contracts) | ||
|
||
res = self.generate_output(martin.full_text) | ||
res.add_pretty_table(martin.core.pretty_table, martin.core.title) | ||
self.info(martin.full_text) | ||
return res |
Oops, something went wrong.