Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5f02f1fc152558fd0a9c40337fafa66882eae651 (plain) (blame)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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;
	}
}

Back to the top