mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-03 23:25:26 +02:00
Bug 544980: Added a checker for using directive in header file
Change-Id: Ic77fd2f7504e7a6cc4e15a2fdeb507724db9e74f Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
parent
9e01bea8aa
commit
eadd7c083a
6 changed files with 174 additions and 2 deletions
|
@ -2,7 +2,7 @@ Manifest-Version: 1.0
|
||||||
Bundle-ManifestVersion: 2
|
Bundle-ManifestVersion: 2
|
||||||
Bundle-Name: %Bundle-Name
|
Bundle-Name: %Bundle-Name
|
||||||
Bundle-SymbolicName: org.eclipse.cdt.codan.checkers;singleton:=true
|
Bundle-SymbolicName: org.eclipse.cdt.codan.checkers;singleton:=true
|
||||||
Bundle-Version: 3.2.100.qualifier
|
Bundle-Version: 3.3.0.qualifier
|
||||||
Bundle-Activator: org.eclipse.cdt.codan.checkers.CodanCheckersActivator
|
Bundle-Activator: org.eclipse.cdt.codan.checkers.CodanCheckersActivator
|
||||||
Require-Bundle: org.eclipse.core.runtime,
|
Require-Bundle: org.eclipse.core.runtime,
|
||||||
org.eclipse.core.resources,
|
org.eclipse.core.resources,
|
||||||
|
|
|
@ -134,3 +134,8 @@ checker.name.DecltypeAutoChecker = Invalid 'decltype(auto)' specifier checker
|
||||||
problem.name.DecltypeAutoProblem = Invalid 'decltype(auto)' specifier
|
problem.name.DecltypeAutoProblem = Invalid 'decltype(auto)' specifier
|
||||||
problem.messagePattern.DecltypeAutoProblem = Combining 'decltype(auto)' with other type specifiers is not allowed
|
problem.messagePattern.DecltypeAutoProblem = Combining 'decltype(auto)' with other type specifiers is not allowed
|
||||||
problem.description.DecltypeAutoProblem = This rule will flag 'decltype(auto)' if combined with other type specifiers
|
problem.description.DecltypeAutoProblem = This rule will flag 'decltype(auto)' if combined with other type specifiers
|
||||||
|
|
||||||
|
checker.name.UsingInHeaderChecker = Using directive in header files checker
|
||||||
|
problem.name.UsingInHeaderProblem = Using directive in header
|
||||||
|
problem.messagePattern.UsingInHeaderProblem = Using directive in header files can cause name clashes in other files
|
||||||
|
problem.description.UsingInHeaderProblem = This rule will flag 'using' directive in header files
|
||||||
|
|
|
@ -439,5 +439,20 @@
|
||||||
name="%problem.name.DecltypeAutoProblem">
|
name="%problem.name.DecltypeAutoProblem">
|
||||||
</problem>
|
</problem>
|
||||||
</checker>
|
</checker>
|
||||||
|
<checker
|
||||||
|
class="org.eclipse.cdt.codan.internal.checkers.UsingInHeaderChecker"
|
||||||
|
id="org.eclipse.cdt.codan.internal.checkers.UsingInHeaderChecker"
|
||||||
|
name="%checker.name.UsingInHeaderChecker">
|
||||||
|
<problem
|
||||||
|
category="org.eclipse.cdt.codan.core.categories.CodeStyle"
|
||||||
|
defaultEnabled="false"
|
||||||
|
defaultSeverity="Warning"
|
||||||
|
description="%problem.description.UsingInHeaderProblem"
|
||||||
|
id="org.eclipse.cdt.codan.internal.checkers.UsingInHeaderProblem"
|
||||||
|
markerType="org.eclipse.cdt.codan.core.codanProblem"
|
||||||
|
messagePattern="%problem.messagePattern.UsingInHeaderProblem"
|
||||||
|
name="%problem.name.UsingInHeaderProblem">
|
||||||
|
</problem>
|
||||||
|
</checker>
|
||||||
</extension>
|
</extension>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* 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 org.eclipse.cdt.codan.core.cxx.model.AbstractIndexAstChecker;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTranslationUnit;
|
||||||
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDirective;
|
||||||
|
|
||||||
|
public class UsingInHeaderChecker extends AbstractIndexAstChecker {
|
||||||
|
public static final String ERR_ID = "org.eclipse.cdt.codan.internal.checkers.UsingInHeaderProblem"; //$NON-NLS-1$
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean runInEditor() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void processAst(IASTTranslationUnit ast) {
|
||||||
|
ast.accept(new ASTVisitor() {
|
||||||
|
{
|
||||||
|
shouldVisitDeclarations = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int visit(IASTDeclaration specifier) {
|
||||||
|
if (specifier instanceof ICPPASTUsingDirective) {
|
||||||
|
IASTNode parent = specifier.getParent();
|
||||||
|
if ((parent instanceof ICPPASTTranslationUnit || parent instanceof ICPPASTNamespaceDefinition)
|
||||||
|
&& specifier.getTranslationUnit().isHeaderUnit())
|
||||||
|
reportProblem(ERR_ID, specifier);
|
||||||
|
}
|
||||||
|
return PROCESS_CONTINUE;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,83 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* 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.UsingInHeaderChecker;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link UsingInHeaderChecker} class
|
||||||
|
*/
|
||||||
|
public class UsingInHeaderCheckerTest extends CheckerTestCase {
|
||||||
|
|
||||||
|
public static final String ERR_ID = UsingInHeaderChecker.ERR_ID;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
super.setUp();
|
||||||
|
enableProblems(ERR_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCpp() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isHeader() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//using namespace std;
|
||||||
|
//class Foo {
|
||||||
|
//public:
|
||||||
|
//void bar() {
|
||||||
|
//}
|
||||||
|
//};
|
||||||
|
public void testUsingInGlobalNamespace() throws Exception {
|
||||||
|
loadCodeAndRun(getAboveComment());
|
||||||
|
checkErrorLine(1, ERR_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
//namespace GLOBAL {
|
||||||
|
//using namespace std;
|
||||||
|
//}
|
||||||
|
//class Foo {
|
||||||
|
//public:
|
||||||
|
//void bar() {
|
||||||
|
//}
|
||||||
|
//};
|
||||||
|
public void testUsingInNamespace() throws Exception {
|
||||||
|
loadCodeAndRun(getAboveComment());
|
||||||
|
checkErrorLine(2, ERR_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
//class Foo {
|
||||||
|
//public:
|
||||||
|
//void bar() {
|
||||||
|
//}
|
||||||
|
//};
|
||||||
|
public void testWithoutUsing() throws Exception {
|
||||||
|
loadCodeAndRun(getAboveComment());
|
||||||
|
checkNoErrorsOfKind(ERR_ID);
|
||||||
|
}
|
||||||
|
|
||||||
|
//class Foo {
|
||||||
|
//public:
|
||||||
|
//void bar() {
|
||||||
|
//using namespace std;
|
||||||
|
//}
|
||||||
|
//};
|
||||||
|
public void testUsingInFunctionDeclaration() throws Exception {
|
||||||
|
loadCodeAndRun(getAboveComment());
|
||||||
|
checkNoErrorsOfKind(ERR_ID);
|
||||||
|
}
|
||||||
|
}
|
|
@ -76,6 +76,15 @@ public class CodanTestCase extends BaseTestCase {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Override for header files
|
||||||
|
*
|
||||||
|
* @return is header tests
|
||||||
|
*/
|
||||||
|
public boolean isHeader() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
super.setUp();
|
super.setUp();
|
||||||
|
@ -215,7 +224,18 @@ public class CodanTestCase extends BaseTestCase {
|
||||||
return loadcode(code, new File(tmpDir, fileName));
|
return loadcode(code, new File(tmpDir, fileName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String ext = cpp ? ".cpp" : ".c";
|
String ext;
|
||||||
|
if (cpp) {
|
||||||
|
if (isHeader())
|
||||||
|
ext = ".hpp";
|
||||||
|
else
|
||||||
|
ext = ".cpp";
|
||||||
|
} else {
|
||||||
|
if (isHeader())
|
||||||
|
ext = ".h";
|
||||||
|
else
|
||||||
|
ext = ".c";
|
||||||
|
}
|
||||||
File testFile = null;
|
File testFile = null;
|
||||||
try {
|
try {
|
||||||
testFile = File.createTempFile("test", ext, tmpDir); //$NON-NLS-1$
|
testFile = File.createTempFile("test", ext, tmpDir); //$NON-NLS-1$
|
||||||
|
|
Loading…
Add table
Reference in a new issue