From f7eb9e75d800160f658ae2c6081fe59d694ed148 Mon Sep 17 00:00:00 2001 From: Ivan Galkin Date: Tue, 19 Jun 2018 02:29:36 +0200 Subject: [PATCH] CxxCognitiveComplexityVisitor: clean-up * minor performance findings while looking at #1494 --- .../CxxCognitiveComplexityVisitor.java | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/cxx-squid/src/main/java/org/sonar/cxx/visitors/CxxCognitiveComplexityVisitor.java b/cxx-squid/src/main/java/org/sonar/cxx/visitors/CxxCognitiveComplexityVisitor.java index 50a7a7b443..f6ada6373c 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/visitors/CxxCognitiveComplexityVisitor.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/visitors/CxxCognitiveComplexityVisitor.java @@ -83,7 +83,7 @@ public CxxCognitiveComplexityVisitor build() { IDENTIFIER }; - private static final AstNodeType[] INCREMENT_TYPES = new AstNodeType[]{ + private static final List INCREMENT_TYPES = Arrays.asList( CxxGrammarImpl.handler, CxxGrammarImpl.iterationStatement, CxxGrammarImpl.logicalAndExpression, @@ -92,22 +92,22 @@ public CxxCognitiveComplexityVisitor build() { CxxKeyword.ELSE, CxxKeyword.GOTO, CxxPunctuator.QUEST - }; + ); - private static final AstNodeType[] NESTING_LEVEL_TYPES = new AstNodeType[]{ + private static final List 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 NESTING_INCREMENTS_TYPES = Arrays.asList( CxxGrammarImpl.handler, CxxGrammarImpl.iterationStatement, CxxGrammarImpl.selectionStatement, CxxPunctuator.QUEST - }; + ); private int nesting; private final Set checkedNodes; @@ -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 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); }