summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan Ridge2012-12-06 23:17:52 (EST)
committer Sergey Prigogin2012-12-07 15:11:44 (EST)
commit9475bffdeaaf09d297ebc945b0e9fb1bb4ad5d73 (patch)
tree6171e5e80bb88536c79999725e9b2fd2ba49f7ee
parent1289cc3a6e98b216ce687901dbc9fcca1c441e30 (diff)
downloadorg.eclipse.cdt-9475bffdeaaf09d297ebc945b0e9fb1bb4ad5d73.zip
org.eclipse.cdt-9475bffdeaaf09d297ebc945b0e9fb1bb4ad5d73.tar.gz
org.eclipse.cdt-9475bffdeaaf09d297ebc945b0e9fb1bb4ad5d73.tar.bz2
Bug 395018 - False 'member was not initalized in this constructor'
warning for defaulted copy/move constructor Change-Id: Ib7800e46174b195fd15daef923abfff482fd3aff Reviewed-on: https://git.eclipse.org/r/9059 Reviewed-by: Sergey Prigogin <eclipse.sprigogin@gmail.com> IP-Clean: Sergey Prigogin <eclipse.sprigogin@gmail.com> Tested-by: Sergey Prigogin <eclipse.sprigogin@gmail.com>
-rw-r--r--codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ClassMembersInitializationChecker.java13
-rw-r--r--codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ClassMembersInitializationCheckerTest.java26
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/SemanticQueries.java68
3 files changed, 98 insertions, 9 deletions
diff --git a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ClassMembersInitializationChecker.java b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ClassMembersInitializationChecker.java
index a22cc9c..2ef8d25 100644
--- a/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ClassMembersInitializationChecker.java
+++ b/codan/org.eclipse.cdt.codan.checkers/src/org/eclipse/cdt/codan/internal/checkers/ClassMembersInitializationChecker.java
@@ -8,6 +8,7 @@
* Contributors:
* Anton Gorenkov - initial implementation
* Marc-Andre Laperle
+ * Nathan Ridge
*******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers;
@@ -47,6 +48,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ClassTypeHelper;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.SemanticQueries;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVariableReadWriteFlags;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMName;
@@ -254,16 +256,19 @@ public class ClassMembersInitializationChecker extends AbstractIndexAstChecker {
IBinding binding = functionDefinition.getDeclarator().getName().resolveBinding();
if (binding instanceof ICPPConstructor) {
ICPPConstructor constructor = (ICPPConstructor) binding;
- if (constructor.getClassOwner().getKey() != ICompositeType.k_union) {
- return constructor;
- }
+ // Skip defaulted copy and move constructors.
+ if (functionDefinition.isDefaulted() && SemanticQueries.isCopyOrMoveConstructor(constructor))
+ return null;
+ if (constructor.getClassOwner().getKey() == ICompositeType.k_union)
+ return null;
+ return constructor;
}
}
return null;
}
}
-
+
@Override
public void initPreferences(IProblemWorkingCopy problem) {
super.initPreferences(problem);
diff --git a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ClassMembersInitializationCheckerTest.java b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ClassMembersInitializationCheckerTest.java
index ed41608..e318918 100644
--- a/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ClassMembersInitializationCheckerTest.java
+++ b/codan/org.eclipse.cdt.codan.core.test/src/org/eclipse/cdt/codan/core/internal/checkers/ClassMembersInitializationCheckerTest.java
@@ -8,6 +8,7 @@
* Contributors:
* Anton Gorenkov - initial implementation
* Marc-Andre Laperle
+ * Nathan Ridge
*******************************************************************************/
package org.eclipse.cdt.codan.core.internal.checkers;
@@ -578,12 +579,27 @@ public class ClassMembersInitializationCheckerTest extends CheckerTestCase {
checkNoErrors();
}
- // struct S {
- // int i;
- // S() = default;
- // };
- public void testBug365498_defaultedConstructor() throws Exception{
+ // struct S {
+ // int i;
+ // S() = default;
+ // };
+ public void testBug365498_defaultedConstructor() throws Exception {
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
+
+ // struct S {
+ // S(S&) = default;
+ // S(const S&) = default;
+ // S(volatile S&) = default;
+ // S(const volatile S&) = default;
+ // S(S&&) = default;
+ // S(const S&&) = default;
+ // S(volatile S&&) = default;
+ // S(const volatile S&&) = default;
+ // };
+ public void testBug395018_defaultedCopyOrMoveConstructor() throws Exception {
+ loadCodeAndRun(getAboveComment());
+ checkNoErrors();
+ }
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/SemanticQueries.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/SemanticQueries.java
new file mode 100644
index 0000000..f790c05
--- /dev/null
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/SemanticQueries.java
@@ -0,0 +1,68 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Nathan Ridge.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Nathan Ridge - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.cdt.internal.core.dom.parser.cpp;
+
+import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.CVTYPE;
+import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.TDEF;
+
+import org.eclipse.cdt.core.dom.ast.IType;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionTemplate;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil;
+
+public class SemanticQueries {
+
+ public static boolean isCopyOrMoveConstructor(ICPPConstructor constructor) {
+ return isCopyOrMoveConstructor(constructor, CopyOrMoveConstructorKind.COPY_OR_MOVE);
+ }
+
+ public static boolean isMoveConstructor(ICPPConstructor constructor) {
+ return isCopyOrMoveConstructor(constructor, CopyOrMoveConstructorKind.MOVE);
+ }
+
+ public static boolean isCopyConstructor(ICPPConstructor constructor) {
+ return isCopyOrMoveConstructor(constructor, CopyOrMoveConstructorKind.COPY);
+ }
+
+ private enum CopyOrMoveConstructorKind { COPY, MOVE, COPY_OR_MOVE }
+
+ private static boolean isCopyOrMoveConstructor(ICPPConstructor constructor, CopyOrMoveConstructorKind kind) {
+ // 12.8/2-3 [class.copy]:
+ // "A non-template constructor for class X is a copy [move] constructor
+ // if its first parameter is of type X&[&], const X&[&], volatile X&[&]
+ // or const volatile X&[&], and either there are no other parametrs or
+ // else all other parametrs have default arguments."
+ if (constructor instanceof ICPPFunctionTemplate)
+ return false;
+ if (!isCallableWithNumberOfArguments(constructor, 1))
+ return false;
+ IType firstArgumentType = constructor.getType().getParameterTypes()[0];
+ firstArgumentType = SemanticUtil.getNestedType(firstArgumentType, TDEF);
+ if (!(firstArgumentType instanceof ICPPReferenceType))
+ return false;
+ ICPPReferenceType firstArgReferenceType = (ICPPReferenceType) firstArgumentType;
+ boolean isRvalue = firstArgReferenceType.isRValueReference();
+ if (isRvalue && kind == CopyOrMoveConstructorKind.COPY)
+ return false;
+ if (!isRvalue && kind == CopyOrMoveConstructorKind.MOVE)
+ return false;
+ firstArgumentType = firstArgReferenceType.getType();
+ firstArgumentType = SemanticUtil.getNestedType(firstArgumentType, CVTYPE);
+ return firstArgumentType.isSameType(constructor.getClassOwner());
+ }
+
+ private static boolean isCallableWithNumberOfArguments(ICPPFunction function, int numArguments) {
+ return function.getParameters().length >= numArguments
+ && function.getRequiredArgumentCount() <= numArguments;
+ }
+}