mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-23 00:45:28 +02:00
extracted comment manupulation login to an abstract checker
This commit is contained in:
parent
fac7fe1bb1
commit
b8f814df23
5 changed files with 113 additions and 71 deletions
codan
org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers
org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx
|
@ -10,10 +10,8 @@
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.codan.internal.checkers;
|
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.CxxAstUtils;
|
||||||
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
|
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.ICheckerWithPreferences;
|
||||||
import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
|
import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
|
||||||
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
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_normal_stmnt_offset = 0;
|
||||||
_prev_case_stmnt_offset = 0;
|
_prev_case_stmnt_offset = 0;
|
||||||
_prev_case_stmnt = null;
|
_prev_case_stmnt = null;
|
||||||
//initilize cache
|
getCommentMap();
|
||||||
try {
|
|
||||||
CxxModelsCache.getInstance().getAst(getFile());
|
|
||||||
} catch (Exception e) {
|
|
||||||
CodanCheckersActivator.log(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -215,7 +208,7 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public IASTComment getLeadingComment(IASTStatement statement) {
|
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
|
* @return
|
||||||
*/
|
*/
|
||||||
public IASTComment getFreestandingComment(IASTStatement statement) {
|
public IASTComment getFreestandingComment(IASTStatement statement) {
|
||||||
return CxxAstUtils.getInstance().getFreestandingComment(statement);
|
return getCommentMap().getLastFreestandingCommentForNode(statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -265,6 +258,9 @@ public class CaseBreakChecker extends AbstractIndexAstChecker implements IChecke
|
||||||
_checkEmptyCase = (Boolean) getPreference(getProblemById(ER_ID, getFile()), PARAM_EMPTY_CASE);
|
_checkEmptyCase = (Boolean) getPreference(getProblemById(ER_ID, getFile()), PARAM_EMPTY_CASE);
|
||||||
_noBreakComment = (String) getPreference(getProblemById(ER_ID, getFile()), PARAM_NO_BREAK_COMMENT);
|
_noBreakComment = (String) getPreference(getProblemById(ER_ID, getFile()), PARAM_NO_BREAK_COMMENT);
|
||||||
SwitchFindingVisitor visitor = new SwitchFindingVisitor();
|
SwitchFindingVisitor visitor = new SwitchFindingVisitor();
|
||||||
|
|
||||||
ast.accept(visitor);
|
ast.accept(visitor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,14 +14,10 @@ package org.eclipse.cdt.codan.core.cxx;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
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.ASTVisitor;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
|
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.IASTCompositeTypeSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
|
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
||||||
|
@ -395,51 +391,5 @@ public final class CxxAstUtils {
|
||||||
return functionNameExpression.getRawSignature().equals("exit"); //$NON-NLS-1$
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||||
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
|
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of ICodanCommentMap.
|
* Implementation of ICodanCommentMap.
|
||||||
*/
|
*/
|
||||||
public class CodanCommentMap implements ICodanCommentMap {
|
public class CodanCommentMap implements ICodanCommentMap {
|
||||||
private NodeCommentMap commentedNodeMap;
|
private NodeCommentMap commentedNodeMap;
|
||||||
|
@ -50,10 +50,53 @@ public class CodanCommentMap implements ICodanCommentMap {
|
||||||
return commentedNodeMap.getLeadingCommentsForNode(node);
|
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) {
|
public List<IASTComment> getFreestandingForNode(IASTNode node) {
|
||||||
return commentedNodeMap.getFreestandingCommentsForNode(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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,7 @@ import org.eclipse.core.runtime.Path;
|
||||||
public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblemPreferences implements ICAstChecker,
|
public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblemPreferences implements ICAstChecker,
|
||||||
IRunnableInEditorChecker {
|
IRunnableInEditorChecker {
|
||||||
private IFile file;
|
private IFile file;
|
||||||
|
private ICodanCommentMap commentmap;
|
||||||
|
|
||||||
protected IFile getFile() {
|
protected IFile getFile() {
|
||||||
return file;
|
return file;
|
||||||
|
@ -46,6 +47,7 @@ public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblem
|
||||||
}
|
}
|
||||||
|
|
||||||
void processFile(IFile file) throws CoreException, InterruptedException {
|
void processFile(IFile file) throws CoreException, InterruptedException {
|
||||||
|
commentmap = null;
|
||||||
IASTTranslationUnit ast = CxxModelsCache.getInstance().getAst(file);
|
IASTTranslationUnit ast = CxxModelsCache.getInstance().getAst(file);
|
||||||
if (ast == null)
|
if (ast == null)
|
||||||
return;
|
return;
|
||||||
|
@ -121,7 +123,29 @@ public abstract class AbstractIndexAstChecker extends AbstractCheckerWithProblem
|
||||||
IPath location = new Path(ast.getFilePath());
|
IPath location = new Path(ast.getFilePath());
|
||||||
IFile astFile = ResourceLookup.selectFileForLocation(location, getProject());
|
IFile astFile = ResourceLookup.selectFileForLocation(location, getProject());
|
||||||
file = astFile;
|
file = astFile;
|
||||||
|
commentmap = null;
|
||||||
processAst(ast);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,7 @@
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* QNX Software Systems (Alena Laskavaia) - initial API and implementation
|
* QNX Software Systems (Alena Laskavaia) - initial API and implementation
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
package org.eclipse.cdt.codan.core.cxx.model;
|
package org.eclipse.cdt.codan.core.cxx.model;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -21,24 +20,54 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||||
*/
|
*/
|
||||||
public interface ICodanCommentMap {
|
public interface ICodanCommentMap {
|
||||||
/**
|
/**
|
||||||
* Returns an Collection of comments for the given node (after the node). This list contains all the comments
|
* Returns an Collection of comments for the given node (after the node).
|
||||||
* which are assigned to this specific node. If no comments are available an empty
|
* This list contains all the comments
|
||||||
|
* which are assigned to this specific node. If no comments are available an
|
||||||
|
* empty
|
||||||
* collection is returned.
|
* collection is returned.
|
||||||
|
*
|
||||||
* @param node The key to fetch the associated comments.
|
* @param node The key to fetch the associated comments.
|
||||||
* @return list of comments
|
* @return list of comments
|
||||||
*/
|
*/
|
||||||
public List<IASTComment> getTrailingCommentsForNode(IASTNode node);
|
public List<IASTComment> getTrailingCommentsForNode(IASTNode node);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an Collection of comments for the given node (before the node). This list contains all the comments
|
* Returns an Collection of comments for the given node (before the node).
|
||||||
* which are assigned to this specific node. If no comments are available an empty
|
* This list contains all the comments
|
||||||
|
* which are assigned to this specific node. If no comments are available an
|
||||||
|
* empty
|
||||||
* collection is returned.
|
* collection is returned.
|
||||||
|
*
|
||||||
* @param node The key to fetch the associated comments.
|
* @param node The key to fetch the associated comments.
|
||||||
* @return list of comments
|
* @return list of comments
|
||||||
*/
|
*/
|
||||||
public List<IASTComment> getLeadingCommentsForNode(IASTNode node);
|
public List<IASTComment> getLeadingCommentsForNode(IASTNode node);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param statement
|
* Returns an ArrayList for the given node. This ArrayList contains all the comments
|
||||||
* @return
|
* 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);
|
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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue