Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Stornelli2019-03-02 15:32:21 +0000
committerJonah Graham2019-03-04 00:21:29 +0000
commiteadd7c083ab23f637f0886d23a426dcc5287ac72 (patch)
treed4d2ddbab6c2d7b8c7c70c769e90e9bd3b6281e2
parent9e01bea8aaa39dbe25fa54005c41b3301017cb57 (diff)
downloadorg.eclipse.cdt-eadd7c083ab23f637f0886d23a426dcc5287ac72.tar.gz
org.eclipse.cdt-eadd7c083ab23f637f0886d23a426dcc5287ac72.tar.xz
org.eclipse.cdt-eadd7c083ab23f637f0886d23a426dcc5287ac72.zip
Bug 544980: Added a checker for using directive in header file
Change-Id: Ic77fd2f7504e7a6cc4e15a2fdeb507724db9e74f Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/META-INF/MANIFEST.MF2
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/OSGI-INF/l10n/bundle.properties5
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/plugin.xml15
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/UsingInHeaderChecker.java49
-rw-r--r--codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/internal/checkers/UsingInHeaderCheckerTest.java83
-rw-r--r--codan/org.eclipse.cdt.codan.core.tests/src/org/eclipse/cdt/codan/core/tests/CodanTestCase.java22
6 files changed, 174 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 153a218bf8..4b9b6ba577 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 7d239bbf2b..068e6f05cf 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 7d44acf36d..93c074f67b 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 0000000000..b3b1f2c30b
--- /dev/null
+++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/UsingInHeaderChecker.java
@@ -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;
+ }
+ });
+ }
+}
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 0000000000..0d39a4f10b
--- /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 1e92682804..b7b5b3a9d0 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$

Back to the top