mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-07 17:56: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"
|
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCreateLocalVariable"
|
||||||
problemId="org.eclipse.cdt.codan.internal.checkers.VariableResolutionProblem">
|
problemId="org.eclipse.cdt.codan.internal.checkers.VariableResolutionProblem">
|
||||||
</resolution>
|
</resolution>
|
||||||
|
<resolution
|
||||||
|
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCreateLocalVariable"
|
||||||
|
messagePattern="`(.*)' undeclared \(first use in this function\)">
|
||||||
|
</resolution>
|
||||||
<resolution
|
<resolution
|
||||||
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCreateField"
|
class="org.eclipse.cdt.codan.internal.checkers.ui.quickfix.QuickFixCreateField"
|
||||||
problemId="org.eclipse.cdt.codan.internal.checkers.VariableResolutionProblem">
|
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.internal.checkers.ui.CheckersUiActivator;
|
||||||
import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
|
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.cdt.core.index.IIndex;
|
||||||
import org.eclipse.core.resources.IMarker;
|
import org.eclipse.core.resources.IMarker;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
import org.eclipse.core.runtime.NullProgressMonitor;
|
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.IDocument;
|
||||||
|
import org.eclipse.jface.text.IRegion;
|
||||||
|
|
||||||
public abstract class AbstractAstRewriteQuickFix extends
|
public abstract class AbstractAstRewriteQuickFix extends
|
||||||
AbstractCodanCMarkerResolution {
|
AbstractCodanCMarkerResolution {
|
||||||
|
@ -64,4 +69,38 @@ public abstract class AbstractAstRewriteQuickFix extends
|
||||||
public IDocument getDocument() {
|
public IDocument getDocument() {
|
||||||
return document;
|
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) {
|
public void modifyAST(IIndex index, IMarker marker) {
|
||||||
CxxAstUtils utils = CxxAstUtils.getInstance();
|
CxxAstUtils utils = CxxAstUtils.getInstance();
|
||||||
|
|
||||||
IASTTranslationUnit ast;
|
IASTTranslationUnit ast;
|
||||||
try {
|
try {
|
||||||
ITranslationUnit tu = getTranslationUnitViaEditor(marker);
|
ITranslationUnit tu = getTranslationUnitViaEditor(marker);
|
||||||
|
@ -48,14 +47,19 @@ public class QuickFixCreateLocalVariable extends AbstractAstRewriteQuickFix {
|
||||||
CheckersUiActivator.log(e);
|
CheckersUiActivator.log(e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
IASTName astName;
|
||||||
IASTName astName = getASTNameFromMarker(marker, ast);
|
if (isCodanProblem()) {
|
||||||
|
astName = getASTNameFromMarker(marker, ast);
|
||||||
|
} else {
|
||||||
|
astName = getAstNameFromProblemArgument(marker, ast, 0);
|
||||||
|
}
|
||||||
if (astName == null) {
|
if (astName == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
ASTRewrite r = ASTRewrite.create(ast);
|
ASTRewrite r = ASTRewrite.create(ast);
|
||||||
INodeFactory factory = ast.getASTNodeFactory();
|
INodeFactory factory = ast.getASTNodeFactory();
|
||||||
IASTDeclaration declaration = utils.createDeclaration(astName, factory, index);
|
IASTDeclaration declaration = utils.createDeclaration(astName, factory,
|
||||||
|
index);
|
||||||
IASTDeclarationStatement newStatement = factory
|
IASTDeclarationStatement newStatement = factory
|
||||||
.newDeclarationStatement(declaration);
|
.newDeclarationStatement(declaration);
|
||||||
IASTNode targetStatement = utils.getEnclosingStatement(astName);
|
IASTNode targetStatement = utils.getEnclosingStatement(astName);
|
||||||
|
@ -71,11 +75,19 @@ public class QuickFixCreateLocalVariable extends AbstractAstRewriteQuickFix {
|
||||||
CheckersUiActivator.log(e);
|
CheckersUiActivator.log(e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
marker.delete();
|
||||||
|
} catch (CoreException e) {
|
||||||
|
CheckersUiActivator.log(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isApplicable(IMarker marker) {
|
public boolean isApplicable(IMarker marker) {
|
||||||
|
if (isCodanProblem()) {
|
||||||
String problemArgument = getProblemArgument(marker, 1);
|
String problemArgument = getProblemArgument(marker, 1);
|
||||||
return problemArgument.contains(":func"); //$NON-NLS-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
|
public abstract class AbstractCodanCMarkerResolution implements
|
||||||
IMarkerResolution {
|
IMarkerResolution {
|
||||||
|
private boolean codanProblem;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get position offset from marker. If CHAR_START attribute is not set for
|
* Get position offset from marker. If CHAR_START attribute is not set for
|
||||||
* marker, line and document would be used.
|
* marker, line and document would be used.
|
||||||
|
@ -69,6 +71,10 @@ public abstract class AbstractCodanCMarkerResolution implements
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isCodanProblem() {
|
||||||
|
return codanProblem;
|
||||||
|
}
|
||||||
|
|
||||||
public String getProblemArgument(IMarker marker, int index) {
|
public String getProblemArgument(IMarker marker, int index) {
|
||||||
return CodanProblemMarker.getProblemArgument(marker, index);
|
return CodanProblemMarker.getProblemArgument(marker, index);
|
||||||
}
|
}
|
||||||
|
@ -82,6 +88,7 @@ public abstract class AbstractCodanCMarkerResolution implements
|
||||||
public void run(IMarker marker) {
|
public void run(IMarker marker) {
|
||||||
IDocument doc = openDocument(marker);
|
IDocument doc = openDocument(marker);
|
||||||
if (doc != null) {
|
if (doc != null) {
|
||||||
|
codanProblem = getProblemId(marker) != null;
|
||||||
apply(marker, doc);
|
apply(marker, doc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue