blob: 3c9713e29913efccc939e7d4feec12d33c1cc162 (
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
|
/*******************************************************************************
* Copyright (c) 2010 Tomasz Wesolowski
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Tomasz Wesolowski - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;
import org.eclipse.cdt.codan.core.cxx.CxxAstUtils;
import org.eclipse.cdt.codan.internal.checkers.ui.CheckersUiActivator;
import org.eclipse.cdt.codan.ui.AbstractAstRewriteQuickFix;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
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 QuickFixCreateLocalVariable extends AbstractAstRewriteQuickFix {
@Override
public String getLabel() {
return QuickFixMessages.QuickFixCreateLocalVariable_create_local_variable;
}
@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;
}
IASTName astName;
if (isCodanProblem(marker)) {
astName = getASTNameFromMarker(marker, ast);
} else {
astName = getAstNameFromProblemArgument(marker, ast, 0);
}
if (astName == null) {
return;
}
ASTRewrite r = ASTRewrite.create(ast);
INodeFactory factory = ast.getASTNodeFactory();
IASTDeclaration declaration = CxxAstUtils.createDeclaration(astName, factory, index);
IASTDeclarationStatement newStatement = factory.newDeclarationStatement(declaration);
IASTNode targetStatement = CxxAstUtils.getEnclosingStatement(astName);
if (targetStatement == null) {
return;
}
r.insertBefore(targetStatement.getParent(), targetStatement, newStatement, 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);
}
}
@Override
public boolean isApplicable(IMarker marker) {
if (isCodanProblem(marker)) {
String problemArgument = getProblemArgument(marker, 1);
return problemArgument.contains(":func"); //$NON-NLS-1$
}
return true; // gcc problem that matched the pattern
}
}
|