forked from SonarOpenCommunity/sonar-cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionComplexityCheck.java
More file actions
77 lines (70 loc) · 2.63 KB
/
FunctionComplexityCheck.java
File metadata and controls
77 lines (70 loc) · 2.63 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Sonar C++ Plugin (Community)
* Copyright (C) 2010-2016 SonarOpenCommunity
* http://github.com/SonarOpenCommunity/sonar-cxx
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.cxx.checks;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.check.RuleProperty;
import org.sonar.cxx.api.CxxMetric;
import org.sonar.cxx.parser.CxxGrammarImpl;
import org.sonar.squidbridge.api.SourceFunction;
import org.sonar.squidbridge.checks.ChecksHelper;
import org.sonar.squidbridge.checks.SquidCheck;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.Grammar;
import org.sonar.squidbridge.annotations.ActivatedByDefault;
import org.sonar.squidbridge.annotations.SqaleLinearWithOffsetRemediation;
import org.sonar.cxx.tag.Tag;
@Rule(
key = "FunctionComplexity",
name = "Functions should not be too complex",
tags = {Tag.BRAIN_OVERLOAD},
priority = Priority.MAJOR)
@ActivatedByDefault
@SqaleLinearWithOffsetRemediation(
coeff = "1min",
offset = "10min",
effortToFixDescription = "per complexity point above the threshold")
public class FunctionComplexityCheck extends SquidCheck<Grammar> {
private static final int DEFAULT_MAX = 10;
@RuleProperty(
key = "max",
description = "Maximum complexity allowed",
defaultValue = "" + DEFAULT_MAX)
private int max = DEFAULT_MAX;
@Override
public void init() {
subscribeTo(CxxGrammarImpl.functionDefinition);
}
@Override
public void leaveNode(AstNode node) {
SourceFunction sourceFunction = (SourceFunction) getContext().peekSourceCode();
int complexity = ChecksHelper.getRecursiveMeasureInt(sourceFunction, CxxMetric.COMPLEXITY);
if (complexity > max) {
getContext().createLineViolation(this,
"The Cyclomatic Complexity of this function is {0,number,integer} which is greater than {1,number,integer} authorized.",
node,
complexity,
max);
}
}
public void setMax(int max) {
this.max = max;
}
}