mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-22 06:02:11 +02:00
Bug 534420 - Add support for nodiscard attribute for class types
Change-Id: I7466301118bacc04029c315d97441ff8e56142b5
This commit is contained in:
parent
7458b960c9
commit
703310dfa6
19 changed files with 212 additions and 4 deletions
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,3 +35,6 @@ class D {
|
|||
|
||||
class E final : public A {
|
||||
};
|
||||
|
||||
class [[nodiscard]] F : public A {
|
||||
};
|
||||
|
|
|
@ -137,4 +137,11 @@ public interface ICPPClassType extends ICompositeType, ICPPBinding {
|
|||
* @since 5.5
|
||||
*/
|
||||
public int getVisibility(IBinding member);
|
||||
|
||||
/**
|
||||
* Returns whether this type is declared 'nodiscard'.
|
||||
*
|
||||
* @since 6.12
|
||||
*/
|
||||
public boolean isNoDiscard();
|
||||
}
|
||||
|
|
|
@ -46,6 +46,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition;
|
|||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameterMap;
|
||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDeclaration;
|
||||
import org.eclipse.cdt.core.parser.util.AttributeUtil;
|
||||
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
|
||||
import org.eclipse.cdt.core.parser.util.ObjectMap;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.IRecursionResolvingBinding;
|
||||
|
@ -510,6 +511,19 @@ public class CPPClassSpecialization extends CPPSpecialization
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
ICPPASTCompositeTypeSpecifier typeSpecifier = getCompositeTypeSpecifier();
|
||||
if (typeSpecifier != null) {
|
||||
return AttributeUtil.hasNodiscardAttribute(typeSpecifier);
|
||||
}
|
||||
ICPPClassType clazz = getSpecializedBinding();
|
||||
if (clazz != null) {
|
||||
return clazz.isNoDiscard();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVisibility(IBinding member) {
|
||||
return ClassTypeHelper.getVisibility(this, member);
|
||||
|
|
|
@ -44,6 +44,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDeclaration;
|
|||
import org.eclipse.cdt.core.index.IIndexBinding;
|
||||
import org.eclipse.cdt.core.index.IIndexFileSet;
|
||||
import org.eclipse.cdt.core.parser.util.ArrayUtil;
|
||||
import org.eclipse.cdt.core.parser.util.AttributeUtil;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPTemplates;
|
||||
|
||||
/**
|
||||
|
@ -266,6 +267,15 @@ public class CPPClassTemplate extends CPPTemplateDefinition
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
ICPPASTCompositeTypeSpecifier typeSpecifier = getCompositeTypeSpecifier();
|
||||
if (typeSpecifier != null) {
|
||||
return AttributeUtil.hasNodiscardAttribute(typeSpecifier);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVisibility(IBinding member) {
|
||||
return ClassTypeHelper.getVisibility(this, member);
|
||||
|
|
|
@ -44,6 +44,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDeclaration;
|
|||
import org.eclipse.cdt.core.index.IIndex;
|
||||
import org.eclipse.cdt.core.index.IIndexBinding;
|
||||
import org.eclipse.cdt.core.parser.util.ArrayUtil;
|
||||
import org.eclipse.cdt.core.parser.util.AttributeUtil;
|
||||
import org.eclipse.cdt.internal.core.dom.Linkage;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
|
||||
import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
|
||||
|
@ -142,6 +143,11 @@ public class CPPClassType extends PlatformObject implements ICPPInternalClassTyp
|
|||
public int getVisibility(IBinding member) {
|
||||
throw new IllegalArgumentException(member.getName() + " is not a member of " + getName()); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private IASTName definition;
|
||||
|
@ -455,6 +461,15 @@ public class CPPClassType extends PlatformObject implements ICPPInternalClassTyp
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
ICPPASTCompositeTypeSpecifier typeSpecifier = getCompositeTypeSpecifier();
|
||||
if (typeSpecifier != null) {
|
||||
return AttributeUtil.hasNodiscardAttribute(typeSpecifier);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private IASTName stripQualifier(IASTName name) {
|
||||
if (name instanceof ICPPASTQualifiedName) {
|
||||
name = ((ICPPASTQualifiedName) name).getLastName();
|
||||
|
|
|
@ -592,4 +592,9 @@ public class CPPClosureType extends PlatformObject implements ICPPClassType, ICP
|
|||
return CPPClosureType.this.getConstructors();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -178,6 +178,11 @@ public class CPPDeferredClassInstance extends CPPUnknownBinding
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ICPPTemplateArgument[] getTemplateArguments() {
|
||||
return fArguments;
|
||||
|
|
|
@ -259,6 +259,11 @@ public class CPPTemplateTemplateParameter extends CPPTemplateParameter
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVisibility(IBinding member) {
|
||||
throw new IllegalArgumentException(member.getName() + " is not a member of " + getName()); //$NON-NLS-1$
|
||||
|
|
|
@ -127,6 +127,11 @@ public class CPPTemplateTemplateParameterSpecialization extends CPPTemplateParam
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVisibility(IBinding member) {
|
||||
return getSpecializedBinding().getVisibility(member);
|
||||
|
|
|
@ -139,6 +139,11 @@ public class CPPUnknownMemberClass extends CPPUnknownMember implements ICPPUnkno
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVisibility(IBinding member) {
|
||||
throw new IllegalArgumentException(member.getName() + " is not a member of " + getName()); //$NON-NLS-1$
|
||||
|
|
|
@ -231,4 +231,9 @@ class CompositeCPPClassType extends CompositeCPPBinding implements ICPPClassType
|
|||
public int getVisibility(IBinding member) {
|
||||
return ((ICPPClassType) rbinding).getVisibility(member);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
return ((ICPPClassType) rbinding).isNoDiscard();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -190,6 +190,11 @@ public class CompositeCPPTemplateTemplateParameter extends CompositeCPPBinding
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getVisibility(IBinding member) {
|
||||
throw new IllegalArgumentException(member.getName() + " is not a member of " + getName()); //$NON-NLS-1$
|
||||
|
|
|
@ -305,10 +305,11 @@ public class PDOM extends PlatformObject implements IPDOM {
|
|||
*
|
||||
* CDT 9.12 development (version not supported on the 9.11.x branch)
|
||||
* 216.0 - Added nodiscard function information, bug 534420
|
||||
* 217.0 - Added nodiscard class/struct information, bug 534420
|
||||
*/
|
||||
private static final int MIN_SUPPORTED_VERSION = version(216, 0);
|
||||
private static final int MAX_SUPPORTED_VERSION = version(216, Short.MAX_VALUE);
|
||||
private static final int DEFAULT_VERSION = version(216, 0);
|
||||
private static final int MIN_SUPPORTED_VERSION = version(217, 0);
|
||||
private static final int MAX_SUPPORTED_VERSION = version(217, Short.MAX_VALUE);
|
||||
private static final int DEFAULT_VERSION = version(217, 0);
|
||||
|
||||
private static int version(int major, int minor) {
|
||||
return (major << 16) + minor;
|
||||
|
|
|
@ -669,6 +669,11 @@ public class PDOMASTAdapter {
|
|||
public int getVisibility(IBinding member) {
|
||||
return ((ICPPClassType) fDelegate).getVisibility(member);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
return ((ICPPClassType) fDelegate).isNoDiscard();
|
||||
}
|
||||
}
|
||||
|
||||
private static class AnonymousClassSpecialization extends AnonymousClassType implements ICPPClassSpecialization {
|
||||
|
|
|
@ -72,6 +72,7 @@ class PDOMCPPClassSpecialization extends PDOMCPPSpecialization
|
|||
|
||||
private static final byte FLAGS_FINAL = 0x01;
|
||||
private static final byte FLAGS_HAS_OWN_SCOPE = 0x02;
|
||||
private static final byte FLAGS_NODISCARD = 0x03;
|
||||
|
||||
private volatile ICPPClassScope fScope;
|
||||
private ObjectMap specializationMap; // Obtained from the synchronized PDOM cache.
|
||||
|
@ -508,6 +509,16 @@ class PDOMCPPClassSpecialization extends PDOMCPPSpecialization
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
try {
|
||||
return (getFlags() & FLAGS_NODISCARD) != 0;
|
||||
} catch (CoreException e) {
|
||||
CCorePlugin.log(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasOwnScope() throws CoreException {
|
||||
return (getFlags() & FLAGS_HAS_OWN_SCOPE) != 0;
|
||||
}
|
||||
|
|
|
@ -60,8 +60,9 @@ class PDOMCPPClassType extends PDOMCPPBinding implements IPDOMCPPClassType, IPDO
|
|||
private static final int ANONYMOUS = KEY + 1; // byte
|
||||
private static final int FINAL = ANONYMOUS + 1; // byte
|
||||
private static final int VISIBLE_TO_ADL_ONLY = FINAL + 1; // byte
|
||||
private static final int NO_DISCARD = VISIBLE_TO_ADL_ONLY + 1; // byte
|
||||
@SuppressWarnings("hiding")
|
||||
protected static final int RECORD_SIZE = VISIBLE_TO_ADL_ONLY + 1;
|
||||
protected static final int RECORD_SIZE = NO_DISCARD + 1;
|
||||
|
||||
private PDOMCPPClassScope fScope; // No need for volatile, all fields of PDOMCPPClassScope are final.
|
||||
|
||||
|
@ -73,6 +74,7 @@ class PDOMCPPClassType extends PDOMCPPBinding implements IPDOMCPPClassType, IPDO
|
|||
setAnonymous(classType);
|
||||
setFinal(classType);
|
||||
setVisibleToAdlOnly(visibleToAdlOnly);
|
||||
setNoDiscard(classType);
|
||||
// Linked list is initialized by storage being zero'd by malloc.
|
||||
}
|
||||
|
||||
|
@ -97,6 +99,7 @@ class PDOMCPPClassType extends PDOMCPPBinding implements IPDOMCPPClassType, IPDO
|
|||
setKind(ct);
|
||||
setAnonymous(ct);
|
||||
setFinal(ct);
|
||||
setNoDiscard(ct);
|
||||
super.update(linkage, newBinding);
|
||||
}
|
||||
}
|
||||
|
@ -113,6 +116,10 @@ class PDOMCPPClassType extends PDOMCPPBinding implements IPDOMCPPClassType, IPDO
|
|||
getDB().putByte(record + FINAL, (byte) (ct.isFinal() ? 1 : 0));
|
||||
}
|
||||
|
||||
private void setNoDiscard(ICPPClassType ct) throws CoreException {
|
||||
getDB().putByte(record + NO_DISCARD, (byte) (ct.isNoDiscard() ? 1 : 0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVisibleToAdlOnly(boolean visibleToAdlOnly) throws CoreException {
|
||||
getDB().putByte(record + VISIBLE_TO_ADL_ONLY, (byte) (visibleToAdlOnly ? 1 : 0));
|
||||
|
@ -279,6 +286,16 @@ class PDOMCPPClassType extends PDOMCPPBinding implements IPDOMCPPClassType, IPDO
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
try {
|
||||
return getDB().getByte(record + NO_DISCARD) != 0;
|
||||
} catch (CoreException e) {
|
||||
CCorePlugin.log(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isVisibleToAdlOnly() {
|
||||
try {
|
||||
|
|
|
@ -349,6 +349,11 @@ public class PDOMCPPTemplateTemplateParameter extends PDOMCPPBinding implements
|
|||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNoDiscard() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ICPPTemplateParameter adaptTemplateParameter(ICPPTemplateParameter param) {
|
||||
int pos = param.getParameterPosition();
|
||||
|
|
Loading…
Add table
Reference in a new issue