-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathStatementNestingDepth.ql
More file actions
40 lines (35 loc) · 1.15 KB
/
StatementNestingDepth.ql
File metadata and controls
40 lines (35 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* @name Nesting depth
* @description The maximum number of nested statements (eg. if, for, while ...). Blocks are not counted.
* @kind treemap
* @treemap.warnOn highValues
* @metricType callable
* @metricAggregate avg max
* @tags maintainability
* complexity
* @id cs/statement-nesting-depth-per-function
*/
import csharp
/**
* The parent of a statement, excluding some common cases that don't really make
* sense for nesting depth. An example is: "if (...) { } else if (...) { }: we don't
* consider the second if nested. Blocks are also skipped
*/
predicate realParent(Stmt inner, Stmt outer) {
if skipParent(inner) then realParent(inner.getParent(), outer) else outer = inner.getParent()
}
predicate skipParent(Stmt s) {
exists(Stmt parent | parent = s.getParent() |
s instanceof IfStmt and parent.(IfStmt).getElse() = s
or
parent instanceof BlockStmt
)
}
predicate nestingDepth(Stmt s, int depth) {
depth = count(Stmt enclosing | realParent+(s, enclosing))
}
from Method m, int depth
where
depth =
max(Stmt s, int aDepth | s.getEnclosingCallable() = m and nestingDepth(s, aDepth) | aDepth)
select m, depth order by depth