diff options
author | Marco Stornelli | 2019-03-02 15:32:21 +0000 |
---|---|---|
committer | Jonah Graham | 2019-03-04 14:36:50 +0000 |
commit | 97e18b4b3108d02fadac3b7892b76999cc600d24 (patch) | |
tree | d3f6960c6e42809bdaba8dff75497047a23495cf /codan | |
parent | 5ac0c37e6b0b1f571a24ad510cddea4b3f7fa993 (diff) | |
download | org.eclipse.cdt-97e18b4b3108d02fadac3b7892b76999cc600d24.tar.gz org.eclipse.cdt-97e18b4b3108d02fadac3b7892b76999cc600d24.tar.xz org.eclipse.cdt-97e18b4b3108d02fadac3b7892b76999cc600d24.zip |
Bug 544980: Added a checker for using directive in header file
Change-Id: Ic77fd2f7504e7a6cc4e15a2fdeb507724db9e74f
Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
Diffstat (limited to 'codan')
6 files changed, 169 insertions, 2 deletions
diff --git a/codan/org.eclipse.cdt.codan.checkers/META-INF/MANIFEST.MF b/codan/org.eclipse.cdt.codan.checkers/META-INF/MANIFEST.MF index 153a218bf87..4b9b6ba5777 100644 --- a/codan/org.eclipse.cdt.codan.checkers/META-INF/MANIFEST.MF +++ b/codan/org.eclipse.cdt.codan.checkers/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %Bundle-Name 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 Require-Bundle: org.eclipse.core.runtime, org.eclipse.core.resources, diff --git a/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties b/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties index 7d239bbf2b3..068e6f05cf7 100644 --- a/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties +++ b/codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties @@ -134,3 +134,8 @@ checker.name.DecltypeAutoChecker = Invalid 'decltype(auto)' specifier checker problem.name.DecltypeAutoProblem = Invalid 'decltype(auto)' specifier 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 + +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 diff --git a/codan/org.eclipse.cdt.codan.checkers/plugin.xml b/codan/org.eclipse.cdt.codan.checkers/plugin.xml index 7d44acf36d0..93c074f67b4 100644 --- a/codan/org.eclipse.cdt.codan.checkers/plugin.xml +++ b/codan/org.eclipse.cdt.codan.checkers/plugin.xml @@ -439,5 +439,20 @@ name="%problem.name.DecltypeAutoProblem"> </problem> </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> </plugin> diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/UsingInHeaderChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/UsingInHeaderChecker.java new file mode 100644 index 00000000000..e2448eaab76 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/UsingInHeaderChecker.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * 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 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; + } + }); + } +} diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/UsingInHeaderCheckerTest.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/UsingInHeaderCheckerTest.java new file mode 100644 index 00000000000..0d39a4f10b5 --- /dev/null +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/UsingInHeaderCheckerTest.java @@ -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); + } +} diff --git a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/CodanTestCase.java b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/CodanTestCase.java index 1e926828042..b7b5b3a9d00 100644 --- a/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/CodanTestCase.java +++ b/codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/CodanTestCase.java @@ -76,6 +76,15 @@ public class CodanTestCase extends BaseTestCase { return false; } + /** + * Override for header files + * + * @return is header tests + */ + public boolean isHeader() { + return false; + } + @Override public void setUp() throws Exception { super.setUp(); @@ -215,7 +224,18 @@ public class CodanTestCase extends BaseTestCase { 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; try { testFile = File.createTempFile("test", ext, tmpDir); //$NON-NLS-1$ |