Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Stornelli2019-05-26 18:38:03 +0000
committerMarco Stornelli2019-05-28 17:48:59 +0000
commit3890eec7b714ef20d7f6410c32dcdabf7d49ff37 (patch)
tree2b5dd49e83c52d9ad99f105fa3c4ad5e89aa5b31 /codan/org.eclipse.cdt.codan.checkers.ui/src
parenta009b41021849d65c940e87112951cb4064723a3 (diff)
downloadorg.eclipse.cdt-3890eec7b714ef20d7f6410c32dcdabf7d49ff37.tar.gz
org.eclipse.cdt-3890eec7b714ef20d7f6410c32dcdabf7d49ff37.tar.xz
org.eclipse.cdt-3890eec7b714ef20d7f6410c32dcdabf7d49ff37.zip
Bug 355174 - Added quickfix for miss cases/default
Change-Id: I4c815bd55e55d81456efa796453dd2f69a7c876a Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
Diffstat (limited to 'codan/org.eclipse.cdt.codan.checkers.ui/src')
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseSwitch.java163
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultSwitch.java84
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java2
-rw-r--r--codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties4
4 files changed, 252 insertions, 1 deletions
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseSwitch.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseSwitch.java
new file mode 100644
index 00000000000..5f02f1fc152
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddCaseSwitch.java
@@ -0,0 +1,163 @@
+/*******************************************************************************
+ * Copyright (c) 2019 Marco Stornelli
+ *
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ *******************************************************************************/
+package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.eclipse.cdt.codan.internal.checkers.ui.CheckersUiActivator;
+import org.eclipse.cdt.codan.ui.AbstractAstRewriteQuickFix;
+import org.eclipse.cdt.core.dom.ast.IASTBreakStatement;
+import org.eclipse.cdt.core.dom.ast.IASTCaseStatement;
+import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
+import org.eclipse.cdt.core.dom.ast.IASTExpression;
+import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
+import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
+import org.eclipse.cdt.core.dom.ast.IASTName;
+import org.eclipse.cdt.core.dom.ast.IASTNode;
+import org.eclipse.cdt.core.dom.ast.IASTStatement;
+import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
+import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
+import org.eclipse.cdt.core.dom.ast.IBinding;
+import org.eclipse.cdt.core.dom.ast.IEnumeration;
+import org.eclipse.cdt.core.dom.ast.IEnumerator;
+import org.eclipse.cdt.core.dom.ast.INodeFactory;
+import org.eclipse.cdt.core.dom.ast.IType;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPEnumeration;
+import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
+import org.eclipse.cdt.core.index.IIndex;
+import org.eclipse.cdt.core.model.ITranslationUnit;
+import org.eclipse.cdt.internal.core.dom.parser.ValueFactory;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
+import org.eclipse.core.resources.IMarker;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.ltk.core.refactoring.Change;
+
+@SuppressWarnings("restriction")
+public class QuickFixAddCaseSwitch extends AbstractAstRewriteQuickFix {
+
+ @Override
+ public String getLabel() {
+ return QuickFixMessages.QuickFixAddCaseSwitch_add_cases_to_switch;
+ }
+
+ @Override
+ public void modifyAST(IIndex index, IMarker marker) {
+ IASTTranslationUnit ast;
+ try {
+ ITranslationUnit tu = getTranslationUnitViaEditor(marker);
+ ast = tu.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
+ } catch (CoreException e) {
+ CheckersUiActivator.log(e);
+ return;
+ }
+ IASTNode astNode = null;
+ if (isCodanProblem(marker)) {
+ astNode = getASTNodeFromMarker(marker, ast);
+ }
+ if (astNode == null || !(astNode instanceof IASTSwitchStatement)) {
+ return;
+ }
+ ASTRewrite r = ASTRewrite.create(ast);
+ INodeFactory factory = ast.getASTNodeFactory();
+ Map<String, Number> missingEnums = getMissingCases((IASTSwitchStatement) astNode);
+ Set<Number> existing = new HashSet<>();
+ missingEnums = missingEnums.entrySet().stream().filter(entry -> existing.add(entry.getValue()))
+ .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+ List<IASTCaseStatement> caseStatements = new ArrayList<>();
+ for (Map.Entry<String, Number> e : missingEnums.entrySet()) {
+ IASTName newName = factory.newName(e.getKey());
+ caseStatements.add(factory.newCaseStatement(factory.newIdExpression(newName)));
+ }
+ IASTBreakStatement breakStatement = factory.newBreakStatement();
+ IASTNode[] children = astNode.getChildren();
+ IASTCompoundStatement compound = null;
+ for (int i = 0; i < children.length; ++i) {
+ if (children[i] instanceof IASTCompoundStatement) {
+ compound = (IASTCompoundStatement) children[i];
+ break;
+ }
+ }
+ if (compound == null)
+ return;
+ for (IASTCaseStatement caseStatement : caseStatements)
+ r.insertBefore(compound, null, caseStatement, null);
+ r.insertBefore(compound, null, breakStatement, null);
+ Change c = r.rewriteAST();
+ try {
+ c.perform(new NullProgressMonitor());
+ } catch (CoreException e) {
+ CheckersUiActivator.log(e);
+ return;
+ }
+ try {
+ marker.delete();
+ } catch (CoreException e) {
+ CheckersUiActivator.log(e);
+ }
+ }
+
+ private Map<String, Number> getMissingCases(IASTSwitchStatement statement) {
+ IASTExpression controller = statement.getControllerExpression();
+ IASTStatement bodyStmt = statement.getBody();
+ IType type = SemanticUtil.getUltimateType(controller.getExpressionType(), true);
+ Map<String, Number> enumValues = new HashMap<>();
+ if (type instanceof IEnumeration) {
+ IEnumerator[] enums = ((IEnumeration) type).getEnumerators();
+ String prefix = "";
+ if (type instanceof ICPPEnumeration) {
+ String[] qualName = CPPVisitor.getQualifiedName((IEnumeration) type);
+ StringBuilder builder = new StringBuilder();
+ for (int i = 0; i < qualName.length - 1; ++i) {
+ builder.append(qualName[i]);
+ builder.append("::");
+ }
+ prefix = builder.toString();
+ }
+ for (IEnumerator e : enums) {
+ enumValues.put(prefix + e.getName(), e.getValue().numberValue());
+ }
+ } else
+ return enumValues;
+ final List<IASTStatement> statements;
+ if (bodyStmt instanceof IASTCompoundStatement) {
+ statements = Arrays.asList(((IASTCompoundStatement) bodyStmt).getStatements());
+ } else {
+ statements = Collections.singletonList(bodyStmt);
+ }
+ for (IASTStatement s : statements) {
+ if (s instanceof IASTCaseStatement && ((IASTCaseStatement) s).getExpression() instanceof IASTIdExpression) {
+ IASTName name = ((IASTIdExpression) ((IASTCaseStatement) s).getExpression()).getName();
+ IBinding binding = name.resolveBinding();
+ if (binding instanceof IEnumerator) {
+ enumValues.entrySet().removeIf(
+ entry -> entry.getValue().equals(((IEnumerator) binding).getValue().numberValue()));
+ }
+ } else if (s instanceof IASTCaseStatement
+ && ((IASTCaseStatement) s).getExpression() instanceof IASTLiteralExpression) {
+ Number value = ValueFactory.getConstantNumericalValue(((IASTCaseStatement) s).getExpression());
+ if (value != null)
+ enumValues.entrySet().removeIf(entry -> entry.getValue().equals(value));
+ }
+ }
+ return enumValues;
+ }
+}
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultSwitch.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultSwitch.java
new file mode 100644
index 00000000000..468dc3eed2b
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixAddDefaultSwitch.java
@@ -0,0 +1,84 @@
+/*******************************************************************************
+ * Copyright (c) 2019 Marco Stornelli
+ *
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ *******************************************************************************/
+package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;
+
+import org.eclipse.cdt.codan.internal.checkers.ui.CheckersUiActivator;
+import org.eclipse.cdt.codan.ui.AbstractAstRewriteQuickFix;
+import org.eclipse.cdt.core.dom.ast.IASTBreakStatement;
+import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
+import org.eclipse.cdt.core.dom.ast.IASTDefaultStatement;
+import org.eclipse.cdt.core.dom.ast.IASTNode;
+import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
+import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
+import org.eclipse.cdt.core.dom.ast.INodeFactory;
+import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
+import org.eclipse.cdt.core.index.IIndex;
+import org.eclipse.cdt.core.model.ITranslationUnit;
+import org.eclipse.core.resources.IMarker;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.ltk.core.refactoring.Change;
+
+public class QuickFixAddDefaultSwitch extends AbstractAstRewriteQuickFix {
+
+ @Override
+ public String getLabel() {
+ return QuickFixMessages.QuickFixAddDefaultSwitch_add_default_to_switch;
+ }
+
+ @Override
+ public void modifyAST(IIndex index, IMarker marker) {
+ IASTTranslationUnit ast;
+ try {
+ ITranslationUnit tu = getTranslationUnitViaEditor(marker);
+ ast = tu.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
+ } catch (CoreException e) {
+ CheckersUiActivator.log(e);
+ return;
+ }
+ IASTNode astNode = null;
+ if (isCodanProblem(marker)) {
+ astNode = getASTNodeFromMarker(marker, ast);
+ }
+ if (astNode == null || !(astNode instanceof IASTSwitchStatement)) {
+ return;
+ }
+ ASTRewrite r = ASTRewrite.create(ast);
+ INodeFactory factory = ast.getASTNodeFactory();
+ IASTDefaultStatement defStatement = factory.newDefaultStatement();
+ IASTBreakStatement breakStatement = factory.newBreakStatement();
+ IASTNode[] children = astNode.getChildren();
+ IASTCompoundStatement compound = null;
+ for (int i = 0; i < children.length; ++i) {
+ if (children[i] instanceof IASTCompoundStatement) {
+ compound = (IASTCompoundStatement) children[i];
+ break;
+ }
+ }
+ if (compound == null)
+ return;
+ r.insertBefore(compound, null, defStatement, null);
+ r.insertBefore(compound, null, breakStatement, null);
+ Change c = r.rewriteAST();
+ try {
+ c.perform(new NullProgressMonitor());
+ } catch (CoreException e) {
+ CheckersUiActivator.log(e);
+ return;
+ }
+ try {
+ marker.delete();
+ } catch (CoreException e) {
+ CheckersUiActivator.log(e);
+ }
+ }
+}
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java
index a323dfa34b5..444a565aa6a 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.java
@@ -29,6 +29,8 @@ public class QuickFixMessages extends NLS {
public static String QuickFixUseDotOperator_replace_ptr;
public static String QuickFixForFixit_apply_fixit;
public static String QuickFixSuppressProblem_Label;
+ public static String QuickFixAddDefaultSwitch_add_default_to_switch;
+ public static String QuickFixAddCaseSwitch_add_cases_to_switch;
static {
NLS.initializeMessages(QuickFixMessages.class.getName(), QuickFixMessages.class);
diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties
index 9371535ca80..9c5486487c4 100644
--- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties
+++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixMessages.properties
@@ -23,4 +23,6 @@ QuickFixAddSemicolon_add_semicolon=Add semicolon
QuickFixUsePointer_replace_dot=Replace '.' with '->'
QuickFixUseDotOperator_replace_ptr=Replace '->' with '.'
QuickFixForFixit_apply_fixit=Apply compiler recommended fix-it
-QuickFixSuppressProblem_Label=Suppress problem "%s" \ No newline at end of file
+QuickFixSuppressProblem_Label=Suppress problem "%s"
+QuickFixAddDefaultSwitch_add_default_to_switch=Add default to switch
+QuickFixAddCaseSwitch_add_cases_to_switch=Add missing cases to switch

Back to the top