1
1
/*******************************************************************************
2
* Copyright (c) 2009, 2010 Andrew Gvozdev
2
* Copyright (c) 2009, 2013 Andrew Gvozdev and others
3
3
* All rights reserved. This program and the accompanying materials
4
4
* are made available under the terms of the Eclipse Public License v1.0
5
5
* which accompanies this distribution, and is available at
6
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Andrew Gvozdev - initial API and implementation
9
* Andrew Gvozdev - initial API and implementation
10
11
*******************************************************************************/
11
12
package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;
13
14
import org.eclipse.cdt.codan.internal.checkers.ui.CheckersUiActivator;
14
15
import org.eclipse.cdt.codan.internal.checkers.ui.Messages;
15
import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
16
import org.eclipse.cdt.codan.ui.AbstractAstRewriteQuickFix;
17
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
18
import org.eclipse.cdt.core.dom.ast.IASTNode;
19
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
20
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
21
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
22
import org.eclipse.cdt.core.index.IIndex;
23
import org.eclipse.cdt.core.model.ITranslationUnit;
16
24
import org.eclipse.core.resources.IMarker;
25
import org.eclipse.core.runtime.CoreException;
17
26
import org.eclipse.jface.text.BadLocationException;
18
27
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
19
import org.eclipse.jface.text.IDocument;
22
30
* quick fix for assignment in condition
24
public class QuickFixAssignmentInCondition extends AbstractCodanCMarkerResolution {
32
public class QuickFixAssignmentInCondition extends AbstractAstRewriteQuickFix {
26
34
public String getLabel() {
27
35
return Messages.QuickFixAssignmentInCondition_Message;
31
public void apply(IMarker marker, IDocument document) {
32
int pos = getOffset(marker, document);
39
public void modifyAST(IIndex index, IMarker marker) {
34
FindReplaceDocumentAdapter dad = new FindReplaceDocumentAdapter(document);
35
dad.find(pos, "=", /* forwardSearch *///$NON-NLS-1$
36
true, /* caseSensitive */false,
37
/* wholeWord */false, /* regExSearch */false);
38
dad.replace("==", /* regExReplace */false); //$NON-NLS-1$
41
IASTTranslationUnit ast = getTranslationUnitViaEditor(marker).getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
42
int markerStart = marker.getAttribute(IMarker.CHAR_START, -1);
43
int markerEnd = marker.getAttribute(IMarker.CHAR_END, -1);
44
if (markerStart == -1 || markerEnd == -1 || markerStart >= markerEnd)
46
IASTNodeSelector nodeSelector = ast.getNodeSelector(null);
47
IASTNode containedNode = nodeSelector.findEnclosingNode(markerStart, markerEnd - markerStart);
48
if (containedNode instanceof IASTBinaryExpression) {
49
IASTBinaryExpression expr = (IASTBinaryExpression) containedNode;
50
IASTNodeLocation[] leftSubexprLocations = expr.getOperand1().getNodeLocations();
51
if (leftSubexprLocations.length != 1) // don't handle expressions in macro expansions
53
IASTNodeLocation leftSubexprLocation = leftSubexprLocations[0];
54
int leftSubexprEnd = leftSubexprLocation.getNodeOffset() + leftSubexprLocation.getNodeLength();
56
// Assignment operator will be following the end of the left subexpression.
57
FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(getDocument());
58
adapter.find(leftSubexprEnd,
60
true, /* forwardSearch */
61
false, /* caseSensitive */
62
false, /* wholeWord */
63
false); /* regExSearch */
64
adapter.replace("==", false /* regExReplace */); ///$NON-NLS-1$
66
} catch (CoreException e) {
67
CheckersUiActivator.log(e);
39
68
} catch (BadLocationException e) {
40
69
CheckersUiActivator.log(e);