Skip to content

Commit

Permalink
CxxCognitiveComplexityVisitor: clean-up
Browse files Browse the repository at this point in the history
* minor performance findings while looking at SonarOpenCommunity#1494
  • Loading branch information
ivangalkin committed Jun 19, 2018
1 parent a57ef4e commit f7eb9e7
Showing 1 changed file with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public CxxCognitiveComplexityVisitor<G> build() {
IDENTIFIER
};

private static final AstNodeType[] INCREMENT_TYPES = new AstNodeType[]{
private static final List<AstNodeType> INCREMENT_TYPES = Arrays.asList(
CxxGrammarImpl.handler,
CxxGrammarImpl.iterationStatement,
CxxGrammarImpl.logicalAndExpression,
Expand All @@ -92,22 +92,22 @@ public CxxCognitiveComplexityVisitor<G> build() {
CxxKeyword.ELSE,
CxxKeyword.GOTO,
CxxPunctuator.QUEST
};
);

private static final AstNodeType[] NESTING_LEVEL_TYPES = new AstNodeType[]{
private static final List<AstNodeType> NESTING_LEVEL_TYPES = Arrays.asList(
CxxGrammarImpl.handler,
CxxGrammarImpl.iterationStatement,
CxxGrammarImpl.lambdaExpression,
CxxGrammarImpl.selectionStatement,
CxxPunctuator.QUEST
};
);

private static final AstNodeType[] NESTING_INCREMENTS_TYPES = new AstNodeType[]{
private static final List<AstNodeType> NESTING_INCREMENTS_TYPES = Arrays.asList(
CxxGrammarImpl.handler,
CxxGrammarImpl.iterationStatement,
CxxGrammarImpl.selectionStatement,
CxxPunctuator.QUEST
};
);

private int nesting;
private final Set<AstNode> checkedNodes;
Expand All @@ -132,33 +132,28 @@ public void init() {

@Override
public void visitNode(AstNode node) {
if (checkedNodes.contains(node)) {
if (!checkedNodes.add(node)) {
return;
}
checkedNodes.add(node);

List<AstNode> watchedDescendants = node.getDescendants(DESCENDANT_TYPES);

if (Arrays.asList(NESTING_LEVEL_TYPES).contains(node.getType())
if (NESTING_LEVEL_TYPES.contains(node.getType())
&& !isElseIf(node)) {
nesting++;
}

visitChildren(watchedDescendants);
visitChildren(node.getDescendants(DESCENDANT_TYPES));

if (Arrays.asList(NESTING_LEVEL_TYPES).contains(node.getType())
if (NESTING_LEVEL_TYPES.contains(node.getType())
&& !isElseIf(node)) {
nesting--;
}

checkedNodes.addAll(watchedDescendants);

if (Arrays.asList(INCREMENT_TYPES).contains(node.getType())
if (INCREMENT_TYPES.contains(node.getType())
&& !isElseIf(node)) {
getContext().peekSourceCode().add(metric, 1);
}

if (Arrays.asList(NESTING_INCREMENTS_TYPES).contains(node.getType())
if (NESTING_INCREMENTS_TYPES.contains(node.getType())
&& !isElseIf(node)) {
getContext().peekSourceCode().add(metric, nesting);
}
Expand Down

0 comments on commit f7eb9e7

Please sign in to comment.