mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
Java 1.5-style loops, etc.
This commit is contained in:
parent
6322c0691d
commit
da28cc19fc
6 changed files with 50 additions and 106 deletions
|
@ -33,7 +33,7 @@ import org.eclipse.ltk.core.refactoring.Change;
|
|||
public class CaseBreakQuickFixBreak extends AbstractAstRewriteQuickFix {
|
||||
@Override
|
||||
public boolean isApplicable(IMarker marker) {
|
||||
int line = marker.getAttribute(IMarker.LINE_NUMBER, -1) - 1;
|
||||
int line = marker.getAttribute(IMarker.LINE_NUMBER, 0) - 1;
|
||||
if (line < 0)
|
||||
return false;
|
||||
return true;
|
||||
|
@ -45,17 +45,18 @@ public class CaseBreakQuickFixBreak extends AbstractAstRewriteQuickFix {
|
|||
}
|
||||
|
||||
protected IASTStatement getStmtBeforeBreak(IMarker marker, IASTTranslationUnit ast) throws BadLocationException {
|
||||
int line = marker.getAttribute(IMarker.LINE_NUMBER, -1) - 1;
|
||||
int line = marker.getAttribute(IMarker.LINE_NUMBER, 0) - 1;
|
||||
if (line < 0)
|
||||
return null;
|
||||
IRegion lineInformation = getDocument().getLineInformation(line);
|
||||
IASTNodeSelector nodeSelector = ast.getNodeSelector(null);
|
||||
IASTNode containedNode = nodeSelector.findFirstContainedNode(lineInformation.getOffset(), lineInformation.getLength());
|
||||
IASTNode beforeBreakNode = null;
|
||||
if (containedNode != null)
|
||||
if (containedNode != null) {
|
||||
beforeBreakNode = CxxAstUtils.getEnclosingStatement(containedNode);
|
||||
else
|
||||
} else {
|
||||
beforeBreakNode = nodeSelector.findEnclosingNode(lineInformation.getOffset(), lineInformation.getLength());
|
||||
}
|
||||
if (beforeBreakNode instanceof IASTCompoundStatement) {
|
||||
while (beforeBreakNode != null) {
|
||||
if (beforeBreakNode.getParent() instanceof IASTCompoundStatement
|
||||
|
@ -79,7 +80,7 @@ public class CaseBreakQuickFixBreak extends AbstractAstRewriteQuickFix {
|
|||
try {
|
||||
IASTTranslationUnit ast = getTranslationUnitViaEditor(marker).getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
|
||||
IASTStatement beforeBreak = getStmtBeforeBreak(marker, ast);
|
||||
if (beforeBreak.getParent() instanceof IASTCompoundStatement) {
|
||||
if (beforeBreak != null && beforeBreak.getParent() instanceof IASTCompoundStatement) {
|
||||
IASTCompoundStatement enclosingStatement = (IASTCompoundStatement) beforeBreak.getParent();
|
||||
IASTStatement after = getAfterStatement(beforeBreak);
|
||||
ASTRewrite r = ASTRewrite.create(enclosingStatement.getTranslationUnit());
|
||||
|
|
|
@ -43,12 +43,9 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke
|
|||
public static final String PARAM_EMPTY_CASE = "empty_case_param"; //$NON-NLS-1$
|
||||
public static final String PARAM_NO_BREAK_COMMENT = "no_break_comment"; //$NON-NLS-1$
|
||||
public static final String DEFAULT_NO_BREAK_COMMENT = "no break"; //$NON-NLS-1$
|
||||
private Boolean _checkLastCase; // Should we check the last case in the switch?
|
||||
private Boolean _checkEmptyCase; // Should we check an empty case (a case without any statements within it)
|
||||
private String _noBreakComment; // The comment suppressing this warning
|
||||
|
||||
public CaseBreakChecker() {
|
||||
}
|
||||
private boolean fCheckLastCase; // Should we check the last case in the switch?
|
||||
private boolean fCheckEmptyCase; // Should we check an empty case (a case without any statements within it)
|
||||
private String fNoBreakComment; // The comment suppressing this warning
|
||||
|
||||
/**
|
||||
* This visitor looks for "switch" statements and invokes "SwitchVisitor" on them.
|
||||
|
@ -99,10 +96,10 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke
|
|||
// Next is case or end of switch - means this one is the last
|
||||
if (prevCase != null && (isCaseStatement(next) || next == null)) {
|
||||
// Check that current statement end with break or any other exit statement
|
||||
if (!_checkEmptyCase && isCaseStatement(curr) && next != null) {
|
||||
if (!fCheckEmptyCase && isCaseStatement(curr) && next != null) {
|
||||
continue; // Empty case and we don't care
|
||||
}
|
||||
if (!_checkLastCase && next == null) {
|
||||
if (!fCheckLastCase && next == null) {
|
||||
continue; // Last case and we don't care
|
||||
}
|
||||
if (!isProducedByMacroExpansion(prevCase) && isFallThroughStamement(curr)) {
|
||||
|
@ -116,7 +113,7 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke
|
|||
}
|
||||
if (comment != null) {
|
||||
String str = getTrimmedComment(comment);
|
||||
if (str.toLowerCase().contains(_noBreakComment.toLowerCase()))
|
||||
if (str.toLowerCase().contains(fNoBreakComment.toLowerCase()))
|
||||
continue;
|
||||
}
|
||||
reportProblem(curr, prevCase);
|
||||
|
@ -162,7 +159,7 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke
|
|||
}
|
||||
}
|
||||
|
||||
public void reportProblem(IASTStatement curr, IASTStatement prevCase) {
|
||||
private void reportProblem(IASTStatement curr, IASTStatement prevCase) {
|
||||
reportProblem(ER_ID, getProblemLocationAtEndOfNode(curr));
|
||||
}
|
||||
|
||||
|
@ -227,9 +224,9 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke
|
|||
|
||||
@Override
|
||||
public void processAst(IASTTranslationUnit ast) {
|
||||
_checkLastCase = (Boolean) getPreference(getProblemById(ER_ID, getFile()), PARAM_LAST_CASE);
|
||||
_checkEmptyCase = (Boolean) getPreference(getProblemById(ER_ID, getFile()), PARAM_EMPTY_CASE);
|
||||
_noBreakComment = (String) getPreference(getProblemById(ER_ID, getFile()), PARAM_NO_BREAK_COMMENT);
|
||||
fCheckLastCase = (Boolean) getPreference(getProblemById(ER_ID, getFile()), PARAM_LAST_CASE);
|
||||
fCheckEmptyCase = (Boolean) getPreference(getProblemById(ER_ID, getFile()), PARAM_EMPTY_CASE);
|
||||
fNoBreakComment = (String) getPreference(getProblemById(ER_ID, getFile()), PARAM_NO_BREAK_COMMENT);
|
||||
SwitchFindingVisitor visitor = new SwitchFindingVisitor();
|
||||
ast.accept(visitor);
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ public abstract class AbstractProblemReporter implements IProblemReporter {
|
|||
IProblem problem = CheckersRegistry.getInstance().getResourceProfile(file).findProblem(id);
|
||||
if (problem == null)
|
||||
throw new IllegalArgumentException("Id is not registered:" + id); //$NON-NLS-1$
|
||||
if (problem.isEnabled() == false)
|
||||
if (!problem.isEnabled())
|
||||
return; // skip
|
||||
ICodanProblemMarker codanProblemMarker = new CodanProblemMarker(problem, loc, args);
|
||||
reportProblem(codanProblemMarker);
|
||||
|
|
|
@ -13,7 +13,6 @@ package org.eclipse.cdt.codan.internal.core.model;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.eclipse.cdt.codan.core.CodanCorePlugin;
|
||||
import org.eclipse.cdt.codan.core.CodanRuntime;
|
||||
|
@ -36,16 +35,15 @@ import org.eclipse.core.runtime.IProgressMonitor;
|
|||
/**
|
||||
* Problem reported that created eclipse markers
|
||||
*/
|
||||
public class CodanMarkerProblemReporter extends AbstractProblemReporter implements
|
||||
IProblemReporterPersistent, IProblemReporterSessionPersistent {
|
||||
public class CodanMarkerProblemReporter extends AbstractProblemReporter
|
||||
implements IProblemReporterPersistent, IProblemReporterSessionPersistent {
|
||||
private IResource resource;
|
||||
private IChecker checker;
|
||||
private ArrayList<ICodanProblemMarker> toAdd = new ArrayList<ICodanProblemMarker>();
|
||||
|
||||
/**
|
||||
* Create instance, which can be use as factory for
|
||||
* IProblemReporterSessionPersistent or
|
||||
* as IProblemReporterPersistent.
|
||||
* IProblemReporterSessionPersistent or as IProblemReporterPersistent.
|
||||
*/
|
||||
public CodanMarkerProblemReporter() {
|
||||
super();
|
||||
|
@ -117,9 +115,8 @@ public class CodanMarkerProblemReporter extends AbstractProblemReporter implemen
|
|||
@Override
|
||||
public void run(IProgressMonitor monitor) throws CoreException {
|
||||
Collection<IMarker> markers = findResourceMarkers(file, checker);
|
||||
for (Iterator<IMarker> iterator = markers.iterator(); iterator.hasNext();) {
|
||||
IMarker iMarker = iterator.next();
|
||||
iMarker.delete();
|
||||
for (IMarker marker : markers) {
|
||||
marker.delete();
|
||||
}
|
||||
}
|
||||
}, null, IWorkspace.AVOID_UPDATE, null);
|
||||
|
@ -146,9 +143,8 @@ public class CodanMarkerProblemReporter extends AbstractProblemReporter implemen
|
|||
for (int i = 0; i < markers.length; i++) {
|
||||
IMarker m = markers[i];
|
||||
String id = m.getAttribute(ICodanProblemMarker.ID, ""); //$NON-NLS-1$
|
||||
for (Iterator<IProblem> iterator = problems.iterator(); iterator.hasNext();) {
|
||||
IProblem iProblem = iterator.next();
|
||||
if (iProblem.getId().equals(id)) {
|
||||
for (IProblem problem : problems) {
|
||||
if (problem.getId().equals(id)) {
|
||||
res.add(m);
|
||||
}
|
||||
}
|
||||
|
@ -176,10 +172,11 @@ public class CodanMarkerProblemReporter extends AbstractProblemReporter implemen
|
|||
@Override
|
||||
public void done() {
|
||||
if (checker != null) {
|
||||
if (toAdd.size() == 0)
|
||||
if (toAdd.isEmpty()) {
|
||||
deleteProblems(false);
|
||||
else
|
||||
} else {
|
||||
reconcileMarkers();
|
||||
}
|
||||
toAdd.clear();
|
||||
}
|
||||
}
|
||||
|
@ -190,8 +187,7 @@ public class CodanMarkerProblemReporter extends AbstractProblemReporter implemen
|
|||
@Override
|
||||
public void run(IProgressMonitor monitor) throws CoreException {
|
||||
Collection<IMarker> markers = findResourceMarkers(resource, checker);
|
||||
for (Iterator<IMarker> iterator = markers.iterator(); iterator.hasNext();) {
|
||||
IMarker m = iterator.next();
|
||||
for (IMarker m : markers) {
|
||||
ICodanProblemMarker cm = similarMarker(m);
|
||||
if (cm == null) {
|
||||
m.delete();
|
||||
|
@ -200,8 +196,7 @@ public class CodanMarkerProblemReporter extends AbstractProblemReporter implemen
|
|||
toAdd.remove(cm);
|
||||
}
|
||||
}
|
||||
for (Iterator<ICodanProblemMarker> iterator = toAdd.iterator(); iterator.hasNext();) {
|
||||
ICodanProblemMarker cm = iterator.next();
|
||||
for (ICodanProblemMarker cm : toAdd) {
|
||||
cm.createMarker();
|
||||
}
|
||||
}
|
||||
|
@ -244,8 +239,7 @@ public class CodanMarkerProblemReporter extends AbstractProblemReporter implemen
|
|||
protected ICodanProblemMarker similarMarker(IMarker m) {
|
||||
ICodanProblemMarker mcm = CodanProblemMarker.createCodanProblemMarkerFromResourceMarker(m);
|
||||
ArrayList<ICodanProblemMarker> cand = new ArrayList<ICodanProblemMarker>();
|
||||
for (Iterator<ICodanProblemMarker> iterator = toAdd.iterator(); iterator.hasNext();) {
|
||||
ICodanProblemMarker cm = iterator.next();
|
||||
for (ICodanProblemMarker cm : toAdd) {
|
||||
if (mcm.equals(cm))
|
||||
return cm;
|
||||
if (markersAreSimilar(mcm, cm)) {
|
||||
|
@ -276,11 +270,6 @@ public class CodanMarkerProblemReporter extends AbstractProblemReporter implemen
|
|||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see IProblemReporterSessionPersistent#deleteProblems(boolean)
|
||||
*/
|
||||
@Override
|
||||
public void deleteProblems(boolean all) {
|
||||
if (all)
|
||||
|
|
|
@ -40,11 +40,6 @@ public class CodanProblemMarker implements ICodanProblemMarker {
|
|||
private IProblem problem;
|
||||
private Object args[];
|
||||
|
||||
@Override
|
||||
public Object[] getArgs() {
|
||||
return args;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param problem
|
||||
* @param loc
|
||||
|
@ -56,41 +51,26 @@ public class CodanProblemMarker implements ICodanProblemMarker {
|
|||
this.args = args;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.eclipse.cdt.codan.core.model.ICodanProblemMarker#getLocation()
|
||||
*/
|
||||
@Override
|
||||
public Object[] getArgs() {
|
||||
return args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IProblemLocation getLocation() {
|
||||
return loc;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.eclipse.cdt.codan.core.model.ICodanProblemMarker#getProblem()
|
||||
*/
|
||||
@Override
|
||||
public IProblem getProblem() {
|
||||
return problem;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.eclipse.cdt.codan.core.model.ICodanProblemMarker#getResource()
|
||||
*/
|
||||
@Override
|
||||
public IResource getResource() {
|
||||
return loc.getFile();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.eclipse.cdt.codan.core.model.ICodanProblemMarker#createMarker()
|
||||
*/
|
||||
@Override
|
||||
public IMarker createMarker() throws CoreException {
|
||||
IResource file = loc.getFile();
|
||||
|
@ -112,11 +92,6 @@ public class CodanProblemMarker implements ICodanProblemMarker {
|
|||
return marker;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.eclipse.cdt.codan.core.model.ICodanProblemMarker#createMessage()
|
||||
*/
|
||||
@Override
|
||||
public String createMessage() {
|
||||
String messagePattern = problem.getMessagePattern();
|
||||
|
@ -130,10 +105,6 @@ public class CodanProblemMarker implements ICodanProblemMarker {
|
|||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param args2
|
||||
* @return
|
||||
*/
|
||||
private static String serializeArgs(Object[] args) {
|
||||
if (args != null) {
|
||||
Properties prop = new Properties();
|
||||
|
@ -155,7 +126,7 @@ public class CodanProblemMarker implements ICodanProblemMarker {
|
|||
}
|
||||
|
||||
/**
|
||||
* Return the argument of a problem that checker passed to "reportProblem"
|
||||
* Returns the argument of a problem that checker passed to "reportProblem"
|
||||
* method
|
||||
*
|
||||
* @param marker - problem marker
|
||||
|
@ -169,7 +140,7 @@ public class CodanProblemMarker implements ICodanProblemMarker {
|
|||
}
|
||||
|
||||
/**
|
||||
* Return the arguments of a problem that checker passed to "reportProblem"
|
||||
* Returns the arguments of a problem that checker passed to "reportProblem"
|
||||
* method
|
||||
*
|
||||
* @param marker - problem marker
|
||||
|
@ -194,7 +165,7 @@ public class CodanProblemMarker implements ICodanProblemMarker {
|
|||
}
|
||||
|
||||
/**
|
||||
* Return problemId from marker
|
||||
* Returns problemId from marker
|
||||
*
|
||||
* @param marker
|
||||
* @return codan problemId
|
||||
|
@ -225,7 +196,7 @@ public class CodanProblemMarker implements ICodanProblemMarker {
|
|||
}
|
||||
|
||||
/**
|
||||
* Attempt to restore CodamProblemMaker from the resource marker
|
||||
* Attempts to restore CodamProblemMaker from the resource marker
|
||||
*
|
||||
* @param marker
|
||||
* @return new instanceof of ICodanProblemMarker or null if marker is not
|
||||
|
@ -239,10 +210,6 @@ public class CodanProblemMarker implements ICodanProblemMarker {
|
|||
return new CodanProblemMarker(problem, loc, getProblemArguments(marker));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param marker
|
||||
* @return
|
||||
*/
|
||||
public static CodanProblem getProblem(IMarker marker) {
|
||||
String id = getProblemId(marker);
|
||||
if (id == null)
|
||||
|
@ -255,10 +222,6 @@ public class CodanProblemMarker implements ICodanProblemMarker {
|
|||
return problem;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource
|
||||
* @return
|
||||
*/
|
||||
public static IProblemProfile getProfile(IResource resource) {
|
||||
IProblemProfile profile = CheckersRegistry.getInstance().getResourceProfile(resource);
|
||||
return profile;
|
||||
|
@ -276,11 +239,6 @@ public class CodanProblemMarker implements ICodanProblemMarker {
|
|||
return loc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param marker
|
||||
* @param res
|
||||
* @throws CoreException
|
||||
*/
|
||||
public static void setProblemArguments(IMarker marker, String[] args) throws CoreException {
|
||||
String propArgs = serializeArgs(args);
|
||||
marker.setAttribute(PROBLEM_ARGS, propArgs);
|
||||
|
|
|
@ -241,7 +241,7 @@ class LocationCtxContainer extends LocationCtx {
|
|||
if (idx < 0) {
|
||||
return -idx;
|
||||
}
|
||||
return idx+1;
|
||||
return idx + 1;
|
||||
}
|
||||
|
||||
private int[] computeLineOffsets() {
|
||||
|
@ -255,7 +255,6 @@ class LocationCtxContainer extends LocationCtx {
|
|||
int[] result= new int[offsets.size()];
|
||||
for (int i = 0; i < result.length; i++) {
|
||||
result[i]= offsets.get(i).intValue();
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue