Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Stornelli2020-03-28 10:15:08 +0000
committerMarco Stornelli2020-04-01 06:31:07 +0000
commit703310dfa6a2e03993b269ecfaba2e001d72124d (patch)
tree00b76d0c7ed1f59ea25dda580968a1365b1a2388 /core/org.eclipse.cdt.core.tests
parent7458b960c98c40748897773bb255fe15eedcecde (diff)
downloadorg.eclipse.cdt-703310dfa6a2e03993b269ecfaba2e001d72124d.tar.gz
org.eclipse.cdt-703310dfa6a2e03993b269ecfaba2e001d72124d.tar.xz
org.eclipse.cdt-703310dfa6a2e03993b269ecfaba2e001d72124d.zip
Bug 534420 - Add support for nodiscard attribute for class types
Diffstat (limited to 'core/org.eclipse.cdt.core.tests')
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java75
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/ClassTests.java10
-rw-r--r--core/org.eclipse.cdt.core.tests/resources/pdomtests/classTests/class.cpp3
3 files changed, 88 insertions, 0 deletions
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java
index d27cea8328e..c7f0d1a7af5 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java
@@ -145,15 +145,20 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
import org.eclipse.cdt.core.dom.ast.cpp.SemanticQueries;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.ParserLanguage;
+import org.eclipse.cdt.core.parser.util.AttributeUtil;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNameBase;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPBasicType;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClassInstance;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClassTemplate;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClassTemplatePartialSpecialization;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPClassType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPConstructor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunctionType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPMethod;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPPointerType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPQualifierType;
+import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVariable;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ClassTypeHelper;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.OverloadableOperator;
@@ -13426,4 +13431,74 @@ public class AST2CPPTests extends AST2CPPTestBase {
ICPPASTExpression f = helper.assertNode("f[] = \"waldo\"", "\"waldo\"");
assertTrue(e.getEvaluation().isEquivalentTo(f.getEvaluation()));
}
+
+ // struct Base {
+ // };
+ // struct [[nodiscard]] S : public Base {
+ // };
+ public void testNoDiscardClass_Bug534420() throws Exception {
+ String code = getAboveComment();
+ parseAndCheckBindings(code);
+
+ BindingAssertionHelper bh = new AST2AssertionHelper(code, true);
+
+ CPPClassType structBase = bh.assertNonProblem("Base {", 4);
+ assertFalse(structBase.isNoDiscard());
+ IASTNode baseDefinitionName = structBase.getDefinition();
+ IASTNode baseDefinition = baseDefinitionName.getParent();
+ assertInstance(baseDefinition, ICPPASTCompositeTypeSpecifier.class);
+ assertFalse(AttributeUtil.hasNodiscardAttribute(((ICPPASTCompositeTypeSpecifier) baseDefinition)));
+
+ CPPClassType structS = bh.assertNonProblem("S", 1);
+ assertTrue(structS.isNoDiscard());
+ IASTNode sDefinitionName = structS.getDefinition();
+ IASTNode sDefinition = sDefinitionName.getParent();
+ assertInstance(sDefinition, ICPPASTCompositeTypeSpecifier.class);
+ assertTrue(AttributeUtil.hasNodiscardAttribute(((ICPPASTCompositeTypeSpecifier) sDefinition)));
+ }
+
+ //template <typename T>
+ //struct Foo {};
+ //template <typename T>
+ //struct [[nodiscard]] Foo<T*> {};
+ //template <>
+ //struct [[nodiscard]] Foo<int> {};
+ //
+ //Foo<double> var1;
+ //Foo<int*> var2;
+ //Foo<int> var3;
+ public void testNoDiscardTemplateSpecialization_Bug534420() throws Exception {
+ String code = getAboveComment();
+ parseAndCheckBindings(code);
+
+ BindingAssertionHelper bh = new AST2AssertionHelper(code, true);
+
+ CPPClassTemplate structFoo = bh.assertNonProblem("Foo {", 3);
+ assertFalse(structFoo.isNoDiscard());
+ IASTNode fooDefinitionName = structFoo.getDefinition();
+ IASTNode fooDefinition = fooDefinitionName.getParent();
+ assertInstance(fooDefinition, ICPPASTCompositeTypeSpecifier.class);
+ assertFalse(AttributeUtil.hasNodiscardAttribute(((ICPPASTCompositeTypeSpecifier) fooDefinition)));
+
+ CPPClassTemplatePartialSpecialization structSpec = bh.assertNonProblem("Foo<T*> {", 7);
+ assertTrue(structSpec.isNoDiscard());
+ IASTNode specDefinitionName = structSpec.getDefinition();
+ IASTNode specDefinition = specDefinitionName.getParent();
+ assertInstance(specDefinition, ICPPASTCompositeTypeSpecifier.class);
+ assertTrue(AttributeUtil.hasNodiscardAttribute(((ICPPASTCompositeTypeSpecifier) specDefinition)));
+
+ CPPClassInstance structFullSpec = bh.assertNonProblem("Foo<int> {", 8);
+ assertTrue(structSpec.isNoDiscard());
+ IASTNode specFullDefinitionName = structFullSpec.getDefinition();
+ IASTNode specFullDefinition = specFullDefinitionName.getParent();
+ assertInstance(specFullDefinition, ICPPASTCompositeTypeSpecifier.class);
+ assertTrue(AttributeUtil.hasNodiscardAttribute(((ICPPASTCompositeTypeSpecifier) specFullDefinition)));
+
+ CPPVariable var1Base = bh.assertNonProblem("var1", 4);
+ CPPVariable var2Spec = bh.assertNonProblem("var2", 4);
+ CPPVariable var3Spec = bh.assertNonProblem("var3", 4);
+ assertFalse(((CPPClassInstance) var1Base.getType()).isNoDiscard());
+ assertTrue(((CPPClassInstance) var2Spec.getType()).isNoDiscard());
+ assertTrue(((CPPClassInstance) var3Spec.getType()).isNoDiscard());
+ }
}
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/ClassTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/ClassTests.java
index b698182cfc7..28de19d5a6f 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/ClassTests.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/ClassTests.java
@@ -244,4 +244,14 @@ public class ClassTests extends PDOMTestBase {
assertTrue(classBinding.isFinal());
}
+
+ public void testNoDiscardClass() throws Exception {
+ char[][] name = { "F".toCharArray() };
+ IBinding[] bindings = pdom.findBindings(name, IndexFilter.ALL, npm());
+ assertEquals(1, bindings.length);
+ assertInstance(bindings[0], ICPPClassType.class);
+ ICPPClassType classBinding = (ICPPClassType) bindings[0];
+
+ assertTrue(classBinding.isNoDiscard());
+ }
}
diff --git a/core/org.eclipse.cdt.core.tests/resources/pdomtests/classTests/class.cpp b/core/org.eclipse.cdt.core.tests/resources/pdomtests/classTests/class.cpp
index 6a606e8e3df..17c5fd0f009 100644
--- a/core/org.eclipse.cdt.core.tests/resources/pdomtests/classTests/class.cpp
+++ b/core/org.eclipse.cdt.core.tests/resources/pdomtests/classTests/class.cpp
@@ -35,3 +35,6 @@ class D {
class E final : public A {
};
+
+class [[nodiscard]] F : public A {
+};

Back to the top