mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Added quick for to create local variable for gcc error message
This commit is contained in:
parent
fd46566d60
commit
164eb8af3b
4 changed files with 68 additions and 6 deletions
|
@ -21,6 +21,10 @@
|
|||
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCreateLocalVariable"
|
||||
problemId="org.eclipse.cdt.codan.internal.checkers.VariableResolutionProblem">
|
||||
</resolution>
|
||||
<resolution
|
||||
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCreateLocalVariable"
|
||||
messagePattern="`(.*)' undeclared \(first use in this function\)">
|
||||
</resolution>
|
||||
<resolution
|
||||
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCreateField"
|
||||
problemId="org.eclipse.cdt.codan.internal.checkers.VariableResolutionProblem">
|
||||
|
|
|
@ -12,11 +12,16 @@ package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;
|
|||
|
||||
import org.eclipse.cdt.codan.internal.checkers.ui.CheckersUiActivator;
|
||||
import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
import org.eclipse.cdt.core.index.IIndex;
|
||||
import org.eclipse.core.resources.IMarker;
|
||||
import org.eclipse.core.runtime.CoreException;
|
||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
||||
import org.eclipse.jface.text.BadLocationException;
|
||||
import org.eclipse.jface.text.FindReplaceDocumentAdapter;
|
||||
import org.eclipse.jface.text.IDocument;
|
||||
import org.eclipse.jface.text.IRegion;
|
||||
|
||||
public abstract class AbstractAstRewriteQuickFix extends
|
||||
AbstractCodanCMarkerResolution {
|
||||
|
@ -64,4 +69,38 @@ public abstract class AbstractAstRewriteQuickFix extends
|
|||
public IDocument getDocument() {
|
||||
return document;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param marker
|
||||
* @param ast
|
||||
* @param argumentIndex TODO
|
||||
* @return
|
||||
* @throws BadLocationException
|
||||
*/
|
||||
public IASTName getAstNameFromProblemArgument(IMarker marker,
|
||||
IASTTranslationUnit ast, int argumentIndex) {
|
||||
IASTName astName = null;
|
||||
int pos = getOffset(marker, getDocument());
|
||||
String name = null;
|
||||
try {
|
||||
name = getProblemArgument(marker, argumentIndex);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
if (name == null)
|
||||
return null;
|
||||
FindReplaceDocumentAdapter dad = new FindReplaceDocumentAdapter(
|
||||
getDocument());
|
||||
IRegion region;
|
||||
try {
|
||||
region = dad.find(pos, name,
|
||||
/* forwardSearch */true, /* caseSensitive */true,
|
||||
/* wholeWord */true, /* regExSearch */false);
|
||||
} catch (BadLocationException e) {
|
||||
return null;
|
||||
}
|
||||
astName = getASTNameFromPositions(ast, region.getOffset(),
|
||||
region.getLength());
|
||||
return astName;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,7 +39,6 @@ public class QuickFixCreateLocalVariable extends AbstractAstRewriteQuickFix {
|
|||
*/
|
||||
public void modifyAST(IIndex index, IMarker marker) {
|
||||
CxxAstUtils utils = CxxAstUtils.getInstance();
|
||||
|
||||
IASTTranslationUnit ast;
|
||||
try {
|
||||
ITranslationUnit tu = getTranslationUnitViaEditor(marker);
|
||||
|
@ -48,14 +47,19 @@ public class QuickFixCreateLocalVariable extends AbstractAstRewriteQuickFix {
|
|||
CheckersUiActivator.log(e);
|
||||
return;
|
||||
}
|
||||
|
||||
IASTName astName = getASTNameFromMarker(marker, ast);
|
||||
IASTName astName;
|
||||
if (isCodanProblem()) {
|
||||
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 = utils.createDeclaration(astName, factory, index);
|
||||
IASTDeclaration declaration = utils.createDeclaration(astName, factory,
|
||||
index);
|
||||
IASTDeclarationStatement newStatement = factory
|
||||
.newDeclarationStatement(declaration);
|
||||
IASTNode targetStatement = utils.getEnclosingStatement(astName);
|
||||
|
@ -71,11 +75,19 @@ public class QuickFixCreateLocalVariable extends AbstractAstRewriteQuickFix {
|
|||
CheckersUiActivator.log(e);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
marker.delete();
|
||||
} catch (CoreException e) {
|
||||
CheckersUiActivator.log(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(IMarker marker) {
|
||||
String problemArgument = getProblemArgument(marker, 1);
|
||||
return problemArgument.contains(":func"); //$NON-NLS-1$
|
||||
if (isCodanProblem()) {
|
||||
String problemArgument = getProblemArgument(marker, 1);
|
||||
return problemArgument.contains(":func"); //$NON-NLS-1$
|
||||
}
|
||||
return true; // gcc problem that matched the pattern
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,6 +45,8 @@ import org.eclipse.ui.texteditor.ITextEditor;
|
|||
*/
|
||||
public abstract class AbstractCodanCMarkerResolution implements
|
||||
IMarkerResolution {
|
||||
private boolean codanProblem;
|
||||
|
||||
/**
|
||||
* Get position offset from marker. If CHAR_START attribute is not set for
|
||||
* marker, line and document would be used.
|
||||
|
@ -69,6 +71,10 @@ public abstract class AbstractCodanCMarkerResolution implements
|
|||
return position;
|
||||
}
|
||||
|
||||
public boolean isCodanProblem() {
|
||||
return codanProblem;
|
||||
}
|
||||
|
||||
public String getProblemArgument(IMarker marker, int index) {
|
||||
return CodanProblemMarker.getProblemArgument(marker, index);
|
||||
}
|
||||
|
@ -82,6 +88,7 @@ public abstract class AbstractCodanCMarkerResolution implements
|
|||
public void run(IMarker marker) {
|
||||
IDocument doc = openDocument(marker);
|
||||
if (doc != null) {
|
||||
codanProblem = getProblemId(marker) != null;
|
||||
apply(marker, doc);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue