mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 22:52:11 +02:00
Bug 545954 - Added checker for functions/methods blacklist
Change-Id: I69d32f166e09ec38e4bf3dd9eaca2643c2e2d01e Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
parent
9a6fd2ab97
commit
a159173a51
6 changed files with 187 additions and 0 deletions
|
@ -176,3 +176,8 @@ problem.description.MissSelfCheckProblem = This rule will flag assignment operat
|
|||
problem.name.MissReferenceProblem = Missing reference return value in assignment operator
|
||||
problem.messagePattern.MissReferenceProblem = Assignment operators should return by reference
|
||||
problem.description.MissReferenceProblem = This rule will flag assignment operators not returning by reference
|
||||
|
||||
checker.name.BlacklistChecker = Function or method in blacklist checker
|
||||
problem.name.BlacklistProblem = Function or method is blacklisted
|
||||
problem.messagePattern.BlacklistProblem = Function or method ''{0}'' is blacklisted
|
||||
problem.description.BlacklistProblem = This rule will flag the use of functions or methods in blacklist
|
||||
|
|
|
@ -564,5 +564,20 @@
|
|||
name="%problem.name.MissReferenceProblem">
|
||||
</problem>
|
||||
</checker>
|
||||
<checker
|
||||
class="org.eclipse.cdt.codan.internal.checkers.BlacklistChecker"
|
||||
id="org.eclipse.cdt.codan.internal.checkers.BlacklistChecker"
|
||||
name="%checker.name.BlacklistChecker">
|
||||
<problem
|
||||
category="org.eclipse.cdt.codan.core.categories.CodeStyle"
|
||||
defaultEnabled="false"
|
||||
defaultSeverity="Warning"
|
||||
description="%problem.description.BlacklistProblem"
|
||||
id="org.eclipse.cdt.codan.internal.checkers.BlacklistProblem"
|
||||
markerType="org.eclipse.cdt.codan.core.codanProblem"
|
||||
messagePattern="%problem.messagePattern.BlacklistProblem"
|
||||
name="%problem.name.BlacklistProblem">
|
||||
</problem>
|
||||
</checker>
|
||||
</extension>
|
||||
</plugin>
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2019 Marco Stornelli
|
||||
*
|
||||
* This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License 2.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* https://www.eclipse.org/legal/epl-2.0/
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.internal.checkers;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.eclipse.cdt.codan.checkers.CodanCheckersActivator;
|
||||
import org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
|
||||
import org.eclipse.cdt.codan.core.model.IProblemWorkingCopy;
|
||||
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
||||
import org.eclipse.cdt.core.dom.ast.DOMException;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTName;
|
||||
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||
import org.eclipse.cdt.core.dom.ast.IBinding;
|
||||
import org.eclipse.cdt.core.dom.ast.IFunction;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
|
||||
|
||||
public class BlacklistChecker extends AbstractIndexAstChecker {
|
||||
public static final String ERR_ID = "org.eclipse.cdt.codan.internal.checkers.BlacklistProblem"; //$NON-NLS-1$
|
||||
public static final String PARAM_BLACKLIST = "blacklist"; //$NON-NLS-1$
|
||||
|
||||
@Override
|
||||
public void processAst(IASTTranslationUnit ast) {
|
||||
Object[] list = (Object[]) getPreference(getProblemById(ERR_ID, getFile()), PARAM_BLACKLIST);
|
||||
if (list == null || list.length == 0)
|
||||
return;
|
||||
Arrays.sort(list);
|
||||
ast.accept(new ASTVisitor() {
|
||||
{
|
||||
shouldVisitNames = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int visit(IASTName name) {
|
||||
IBinding binding = name.resolveBinding();
|
||||
String completeName = getBindingQualifiedName(binding);
|
||||
if (completeName != null && Arrays.binarySearch(list, completeName) >= 0)
|
||||
reportProblem(ERR_ID, name, completeName);
|
||||
return PROCESS_CONTINUE;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private String getBindingQualifiedName(IBinding binding) {
|
||||
if (binding instanceof ICPPFunction) {
|
||||
ICPPFunction method = (ICPPFunction) binding;
|
||||
try {
|
||||
return String.join("::", method.getQualifiedName()); //$NON-NLS-1$
|
||||
} catch (DOMException e) {
|
||||
CodanCheckersActivator.log(e);
|
||||
}
|
||||
} else if (binding instanceof IFunction) {
|
||||
return binding.getName();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initPreferences(IProblemWorkingCopy problem) {
|
||||
super.initPreferences(problem);
|
||||
addListPreference(problem, PARAM_BLACKLIST, CheckersMessages.BlacklistChecker_list,
|
||||
CheckersMessages.BlacklistChecker_list_item);
|
||||
}
|
||||
}
|
|
@ -36,6 +36,8 @@ public class CheckersMessages extends NLS {
|
|||
public static String SuspiciousSemicolonChecker_ParamElse;
|
||||
public static String ProblemBindingChecker_Candidates;
|
||||
public static String SwitchCaseChecker_ParameterDefaultAllEnums;
|
||||
public static String BlacklistChecker_list;
|
||||
public static String BlacklistChecker_list_item;
|
||||
public static String UnusedSymbolInFileScopeChecker_CharacterSequence;
|
||||
public static String UnusedSymbolInFileScopeChecker_Exceptions;
|
||||
|
||||
|
|
|
@ -26,7 +26,12 @@ StatementHasNoEffectChecker_ParameterMacro=Report problem in statements that com
|
|||
SuggestedParenthesisChecker_SuggestParanthesesAroundNot=Suggest parenthesis around 'not' operator
|
||||
SuspiciousSemicolonChecker_ParamAfterElse=Report an error if semicolon is right after 'else' statement
|
||||
SuspiciousSemicolonChecker_ParamElse=Do not report an error after 'if' when 'else' exists
|
||||
|
||||
SwitchCaseChecker_ParameterDefaultAllEnums=Mark even switches with complete enum items
|
||||
|
||||
BlacklistChecker_list=List of methods/functions to be checked
|
||||
BlacklistChecker_list_item=Fully qualified method name or function name
|
||||
|
||||
ProblemBindingChecker_Candidates=Candidates are:
|
||||
UnusedSymbolInFileScopeChecker_CharacterSequence=Identifier character sequence:
|
||||
UnusedSymbolInFileScopeChecker_Exceptions=Exceptions (identifier character sequence)
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2019 Marco Stornelli
|
||||
*
|
||||
* This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License 2.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* https://www.eclipse.org/legal/epl-2.0/
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.codan.core.internal.checkers;
|
||||
|
||||
import org.eclipse.cdt.codan.core.tests.CheckerTestCase;
|
||||
import org.eclipse.cdt.codan.internal.checkers.BlacklistChecker;
|
||||
|
||||
/**
|
||||
* Test for {@link BlacklistChecker} class
|
||||
*/
|
||||
public class BlacklistCheckerTest extends CheckerTestCase {
|
||||
|
||||
public static final String ERR_ID = BlacklistChecker.ERR_ID;
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
enableProblems(ERR_ID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCpp() {
|
||||
return true;
|
||||
}
|
||||
|
||||
//void* dontcall() {
|
||||
// return 0;
|
||||
//}
|
||||
//void foo() {
|
||||
// int * p = (int*)dontcall());
|
||||
//}
|
||||
public void testWithFunctionCall() throws Exception {
|
||||
setPreferenceValue(BlacklistChecker.ERR_ID, BlacklistChecker.PARAM_BLACKLIST, new String[] { "dontcall" });
|
||||
loadCodeAndRun(getAboveComment());
|
||||
checkErrorLine(1, ERR_ID);
|
||||
checkErrorLine(5, ERR_ID);
|
||||
}
|
||||
|
||||
//void* dontcall() {
|
||||
// return 0;
|
||||
//}
|
||||
//void foo() {
|
||||
// void* (*ptr)() = dontcall;
|
||||
//}
|
||||
public void testWithFunctionPtr() throws Exception {
|
||||
setPreferenceValue(BlacklistChecker.ERR_ID, BlacklistChecker.PARAM_BLACKLIST, new String[] { "dontcall" });
|
||||
loadCodeAndRun(getAboveComment());
|
||||
checkErrorLine(1, ERR_ID);
|
||||
checkErrorLine(5, ERR_ID);
|
||||
}
|
||||
|
||||
//class Foo {
|
||||
//public:
|
||||
// void* dontcall() {
|
||||
// return 0;
|
||||
// }
|
||||
//}
|
||||
//void foo() {
|
||||
// Foo f;
|
||||
// f.dontcall();
|
||||
//}
|
||||
public void testWithMethod() throws Exception {
|
||||
setPreferenceValue(BlacklistChecker.ERR_ID, BlacklistChecker.PARAM_BLACKLIST, new String[] { "Foo::dontcall" });
|
||||
loadCodeAndRun(getAboveComment());
|
||||
checkErrorLine(3, ERR_ID);
|
||||
checkErrorLine(9, ERR_ID);
|
||||
}
|
||||
|
||||
//void* dontcall() {
|
||||
// return 0;
|
||||
//}
|
||||
//void foo() {
|
||||
// int * p = (int*)dontcall());
|
||||
//}
|
||||
public void testWithEmptyList() throws Exception {
|
||||
loadCodeAndRun(getAboveComment());
|
||||
checkNoErrorsOfKind(ERR_ID);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue