|
|
|
@ -26,35 +26,32 @@ public class NumericalCopyChecker extends SquidCheck<Grammar> {//当私有数组 |
|
|
|
|
public void init() { |
|
|
|
|
// 订阅要检查AST节点类型,用于在visitNode方法中检查该类型节点
|
|
|
|
|
this.subscribeTo( |
|
|
|
|
CxxGrammarImpl.functionDefinition |
|
|
|
|
CxxGrammarImpl.translationUnit |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void visitNode(AstNode astNode) { |
|
|
|
|
List<AstNode> astNodeList = astNode.getDescendants(CxxGrammarImpl.declarator); |
|
|
|
|
List<String> lists = new ArrayList(); |
|
|
|
|
for (AstNode desc : astNodeList) { |
|
|
|
|
if (desc.getTokenValue().contains("private")) { |
|
|
|
|
lists.add(desc.getTokenValue()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
for (AstNode as : astNodeList) { |
|
|
|
|
if (as.getTokenValue().contains("&") || as.getTokenValue().contains("*")) { |
|
|
|
|
List<String> lists = new ArrayList(); |
|
|
|
|
List<AstNode> descendant = astNode.getDescendants(CxxGrammarImpl.expression); |
|
|
|
|
for (AstNode desc : descendant) { |
|
|
|
|
lists.add(desc.getTokenValue()); |
|
|
|
|
} |
|
|
|
|
List<AstNode> descendan = astNode.getDescendants(CxxGrammarImpl.statement); |
|
|
|
|
for (AstNode ast : descendan) { |
|
|
|
|
List<AstNode> descendants = ast.getDescendants(CxxGrammarImpl.expression); |
|
|
|
|
for (AstNode des : descendants) { |
|
|
|
|
if ("return".equals(des.getParent().getTokenValue()) || des.getTokenValue().contains("0")){ |
|
|
|
|
if (lists.contains(des.getTokenValue())) { |
|
|
|
|
getContext().createLineViolation(this, "应返回该私有数值的副本", des); |
|
|
|
|
} |
|
|
|
|
if ("return".equals(des.getParent().getTokenValue())) { |
|
|
|
|
if (lists.contains(des.getTokenValue())) { |
|
|
|
|
getContext().createLineViolation(this, "应返回该私有数值的副本", des); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|