diff options
Diffstat (limited to 'core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/AttributeUtil.java')
-rw-r--r-- | core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/AttributeUtil.java | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/AttributeUtil.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/AttributeUtil.java new file mode 100644 index 00000000000..53c22374665 --- /dev/null +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/parser/util/AttributeUtil.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2012 Google, Inc and others. + * 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: + * Sergey Prigogin (Google) - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.core.parser.util; + +import org.eclipse.cdt.core.dom.ast.IASTAttribute; +import org.eclipse.cdt.core.dom.ast.IASTAttributeOwner; +import org.eclipse.cdt.core.dom.ast.IASTToken; + +/** + * Collection of static methods for dealing with attributes. + * @see org.eclipse.cdt.core.dom.ast.IASTAttribute + * @see org.eclipse.cdt.core.dom.ast.IASTAttributeOwner + * @since 5.4 + */ +public class AttributeUtil { + private static final String[] ATTRIBUTE_NORETURN = new String[] { "__noreturn__", "noreturn" }; //$NON-NLS-1$//$NON-NLS-2$ + + // Not instantiatable. + private AttributeUtil() {} + + /** + * Returns {@code true} if a declarator has an attribute with one of the given names. + * The {@code names} array is assumed to be small. + */ + public static boolean hasAttribute(IASTAttributeOwner node, String[] names) { + IASTAttribute[] attributes = node.getAttributes(); + for (IASTAttribute attribute : attributes) { + char[] name = attribute.getName(); + for (int i = 0; i < names.length; i++) { + if (CharArrayUtils.equals(name, names[i])) + return true; + } + } + return false; + } + + /** + * Returns {@code true} if the node has a "noreturn" or "__noreturn__" attribute. + */ + public static boolean hasNoreturnAttribute(IASTAttributeOwner node) { + return hasAttribute(node, ATTRIBUTE_NORETURN); + } + + /** + * Returns character representation of the attribute argument, or {@code null} if the attribute + * has zero or more than one argument. + */ + public static char[] getSimpleArgument(IASTAttribute attribute) { + IASTToken argumentClause = attribute.getArgumentClause(); + return argumentClause == null ? null : argumentClause.getTokenCharImage(); + } +} |