1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

extracted comment manupulation login to an abstract checker

This commit is contained in:
Alena Laskavaia 2011-03-17 20:17:17 +00:00
parent fac7fe1bb1
commit b8f814df23
5 changed files with 113 additions and 71 deletions

View file

@ -10,10 +10,8 @@
*******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers;
import org.eclipse.cdt.codan.checkers.CodanCheckersActivator;
import org.eclipse.cdt.codan.core.cxx.CxxAstUtils;
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
import org.eclipse.cdt.codan.core.cxx.model.CxxModelsCache;
import org.eclipse.cdt.codan.core.model.ICheckerWithPreferences;
import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
@ -120,12 +118,7 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke
_prev_normal_stmnt_offset = 0;
_prev_case_stmnt_offset = 0;
_prev_case_stmnt = null;
//initilize cache
try {
CxxModelsCache.getInstance().getAst(getFile());
} catch (Exception e) {
CodanCheckersActivator.log(e);
}
getCommentMap();
}
/**
@ -215,7 +208,7 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke
* @return
*/
public IASTComment getLeadingComment(IASTStatement statement) {
return CxxAstUtils.getInstance().getLeadingComment(statement);
return getCommentMap().getLastLeadingCommentForNode(statement);
}
@ -224,7 +217,7 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke
* @return
*/
public IASTComment getFreestandingComment(IASTStatement statement) {
return CxxAstUtils.getInstance().getFreestandingComment(statement);
return getCommentMap().getLastFreestandingCommentForNode(statement);
}
@Override
@ -265,6 +258,9 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke
_checkEmptyCase = (Boolean) getPreference(getProblemById(ER_ID, getFile()), PARAM_EMPTY_CASE);
_noBreakComment = (String) getPreference(getProblemById(ER_ID, getFile()), PARAM_NO_BREAK_COMMENT);
SwitchFindingVisitor visitor = new SwitchFindingVisitor();
ast.accept(visitor);
}
}

View file

@ -14,14 +14,10 @@ package org.eclipse.cdt.codan.core.cxx;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.cdt.codan.core.cxx.model.CxxModelsCache;
import org.eclipse.cdt.codan.core.cxx.model.ICodanCommentMap;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
@ -395,51 +391,5 @@ public final class CxxAstUtils {
return functionNameExpression.getRawSignature().equals("exit"); //$NON-NLS-1$
}
/**
* @param statement
* @return
*/
public IASTComment getLeadingComment(IASTStatement statement) {
IASTComment comment = null;
ICodanCommentMap map = CxxModelsCache.getInstance().getCommentedNodeMap(statement.getTranslationUnit());
if (map != null) {
List<IASTComment> comms = map.getLeadingCommentsForNode(statement);
if (comms.size() > 0) {
comment = comms.get(comms.size() - 1);
}
}
return comment;
}
/**
* @param statement
* @return
*/
public IASTComment getTrailingComment(IASTStatement statement) {
IASTComment comment = null;
ICodanCommentMap map = CxxModelsCache.getInstance().getCommentedNodeMap(statement.getTranslationUnit());
if (map != null) {
List<IASTComment> comms = map.getTrailingCommentsForNode(statement);
if (comms.size() > 0) {
comment = comms.get(0);
}
}
return comment;
}
/**
* @param statement
* @return
*/
public IASTComment getFreestandingComment(IASTStatement statement) {
IASTComment comment = null;
ICodanCommentMap map = CxxModelsCache.getInstance().getCommentedNodeMap(statement.getTranslationUnit());
if (map != null) {
List<IASTComment> comms = map.getFreestandingForNode(statement);
if (comms.size() > 0) {
comment = comms.get(comms.size() - 1);
}
}
return comment;
}
}

View file

@ -18,7 +18,7 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
/**
* Implementation of ICodanCommentMap.
* Implementation of ICodanCommentMap.
*/
public class CodanCommentMap implements ICodanCommentMap {
private NodeCommentMap commentedNodeMap;
@ -50,10 +50,53 @@ public class CodanCommentMap implements ICodanCommentMap {
return commentedNodeMap.getLeadingCommentsForNode(node);
}
/* (non-Javadoc)
* @see org.eclipse.cdt.codan.core.cxx.model.ICodanCommentMap#getFreestandingForNode(org.eclipse.cdt.core.dom.ast.IASTStatement)
/*
* (non-Javadoc)
*
* @see
* org.eclipse.cdt.codan.core.cxx.model.ICodanCommentMap#getFreestandingForNode
* (org.eclipse.cdt.core.dom.ast.IASTStatement)
*/
public List<IASTComment> getFreestandingForNode(IASTNode node) {
return commentedNodeMap.getFreestandingCommentsForNode(node);
}
/**
* @param node
* @return
*/
public IASTComment getLastLeadingCommentForNode(IASTNode node) {
IASTComment comment = null;
List<IASTComment> comms = getLeadingCommentsForNode(node);
if (comms.size() > 0) {
comment = comms.get(comms.size() - 1);
}
return comment;
}
/**
* @param node
* @return
*/
public IASTComment getFirstTrailingCommentForNode(IASTNode node) {
IASTComment comment = null;
List<IASTComment> comms = getTrailingCommentsForNode(node);
if (comms.size() > 0) {
comment = comms.get(0);
}
return comment;
}
/**
* @param node
* @return
*/
public IASTComment getLastFreestandingCommentForNode(IASTNode node) {
IASTComment comment = null;
List<IASTComment> comms = getFreestandingForNode(node);
if (comms.size() > 0) {
comment = comms.get(comms.size() - 1);
}
return comment;
}
}

View file

@ -36,6 +36,7 @@ import org.eclipse.core.runtime.Path;
public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblemPreferences implements ICAstChecker,
IRunnableInEditorChecker {
private IFile file;
private ICodanCommentMap commentmap;
protected IFile getFile() {
return file;
@ -46,6 +47,7 @@ public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblem
}
void processFile(IFile file) throws CoreException, InterruptedException {
commentmap = null;
IASTTranslationUnit ast = CxxModelsCache.getInstance().getAst(file);
if (ast == null)
return;
@ -121,7 +123,29 @@ public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblem
IPath location = new Path(ast.getFilePath());
IFile astFile = ResourceLookup.selectFileForLocation(location, getProject());
file = astFile;
commentmap = null;
processAst(ast);
}
}
/**
* @return
*
*/
protected ICodanCommentMap getCommentMap() {
if (commentmap == null) {
try {
CxxModelsCache cxxcache = CxxModelsCache.getInstance();
synchronized (cxxcache) {
IASTTranslationUnit ast = cxxcache.getAst(getFile());
commentmap = cxxcache.getCommentedNodeMap(ast);
return commentmap;
}
} catch (Exception e) {
Activator.log(e);
}
}
return commentmap;
}
}

View file

@ -7,8 +7,7 @@
*
* Contributors:
* QNX Software Systems (Alena Laskavaia) - initial API and implementation
*******************************************************************************/
*******************************************************************************/
package org.eclipse.cdt.codan.core.cxx.model;
import java.util.List;
@ -21,24 +20,54 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
*/
public interface ICodanCommentMap {
/**
* Returns an Collection of comments for the given node (after the node). This list contains all the comments
* which are assigned to this specific node. If no comments are available an empty
* Returns an Collection of comments for the given node (after the node).
* This list contains all the comments
* which are assigned to this specific node. If no comments are available an
* empty
* collection is returned.
*
* @param node The key to fetch the associated comments.
* @return list of comments
*/
public List<IASTComment> getTrailingCommentsForNode(IASTNode node);
/**
* Returns an Collection of comments for the given node (before the node). This list contains all the comments
* which are assigned to this specific node. If no comments are available an empty
* Returns an Collection of comments for the given node (before the node).
* This list contains all the comments
* which are assigned to this specific node. If no comments are available an
* empty
* collection is returned.
*
* @param node The key to fetch the associated comments.
* @return list of comments
*/
public List<IASTComment> getLeadingCommentsForNode(IASTNode node);
/**
* @param statement
* @return
* Returns an ArrayList for the given node. This ArrayList contains all the comments
* which are assigned to this specific node. If no comments are available an empty
* ArrayList is returned.
* @param node The key to fetch the associated comments.
* @return ArrayList
*/
public List<IASTComment> getFreestandingForNode(IASTNode node);
/**
* @param node
* @return
*/
public IASTComment getLastLeadingCommentForNode(IASTNode node);
/**
* @param node
* @return
*/
public IASTComment getFirstTrailingCommentForNode(IASTNode node);
/**
* @param node
* @return
*/
public IASTComment getLastFreestandingCommentForNode(IASTNode node);
}