Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 53c22374665f5b58fa64d6e0c4bcb3c978cab907 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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();
	}
}

Back to the top