Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 20f2801458aca6910008985fd51030b7cf87d81c (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*******************************************************************************
 * Copyright (c) 2013, 2015 QNX Software Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Andrew Eidsness - Initial implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.ast.tag;

import java.util.Arrays;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.tag.IBindingTagger;
import org.eclipse.cdt.core.dom.ast.tag.ITag;
import org.eclipse.cdt.core.dom.ast.tag.ITagWriter;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.core.expressions.EvaluationContext;
import org.eclipse.core.expressions.EvaluationResult;
import org.eclipse.core.expressions.Expression;
import org.eclipse.core.expressions.ExpressionConverter;
import org.eclipse.core.expressions.ExpressionTagNames;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;

/**
 * Internal container for extensions of org.eclipse.cdt.core.tagger. The implementation of
 * the tagger is instantiated only after checking the enablement expression (if present) for
 * the specified binding. This avoids activating the contributing plugin until it is actually
 * needed.
 */
public class TaggerDescriptor {
	private static final String Attr_LocalId = "local-id"; //$NON-NLS-1$
	private static final String Attr_Class = "class"; //$NON-NLS-1$

	private final IConfigurationElement element;
	private final Expression enablementExpression;
	private Boolean fStatus = null;

	private String id;
	private IBindingTagger tagger;

	private static final String VAR_PROJECTNATURES = "projectNatures"; //$NON-NLS-1$
	private static final String VAR_LANGUAGEID = "languageId"; //$NON-NLS-1$

	/**
	 * An empty implementation of the tagger used as a placeholder in descriptors that are unable
	 * to load the contributed class.
	 */
	private static final IBindingTagger NULL_TAGGER = new IBindingTagger() {
		@Override
		public ITag process(ITagWriter tagWriter, IBinding binding, IASTName ast) {
			return null;
		}
	};

	public TaggerDescriptor(IConfigurationElement element) {
		this.element = element;

		Expression expr = null;
		IConfigurationElement[] children = element.getChildren(ExpressionTagNames.ENABLEMENT);
		switch (children.length) {
		case 0:
			fStatus = Boolean.TRUE;
			break;
		case 1:
			try {
				ExpressionConverter parser = ExpressionConverter.getDefault();
				expr = parser.perform(children[0]);
			} catch (CoreException e) {
				CCorePlugin.log("Error in enablement expression of " + id, e); //$NON-NLS-1$
			}
			break;
		default:
			CCorePlugin.log("Too many enablement expressions for " + id); //$NON-NLS-1$
			fStatus = Boolean.FALSE;
			break;
		}
		enablementExpression = expr;
	}

	public String getId() {
		if (id != null)
			return id;

		String globalId = element.getContributor().getName();
		String localId = element.getAttribute(Attr_LocalId);

		// there must be a valid local id
		if (localId == null) {
			String extId = element.getDeclaringExtension().getSimpleIdentifier();
			CCorePlugin.log("Invalid extension " + globalId + '.' + extId //$NON-NLS-1$
					+ " must provide tagger's local-id"); //$NON-NLS-1$
			return null;
		}

		// the extension should not include the plugin id, but return immediately if it does
		if (localId.startsWith(globalId) && localId.length() > globalId.length())
			return localId;

		// make sure the local id has real content
		if (localId.isEmpty()) {
			String extId = element.getDeclaringExtension().getSimpleIdentifier();
			CCorePlugin.log("Invalid extension " + globalId + '.' + extId //$NON-NLS-1$
					+ " must provide value for tagger's local-id"); //$NON-NLS-1$
			return null;
		}

		// otherwise prepend with the globalId, and ensure a dot between them
		if (localId.charAt(0) == '.')
			return globalId + localId;
		return globalId + '.' + localId;
	}

	private boolean matches(ITranslationUnit tu) {
		// If the enablement expression is missing or structurally invalid, then return immediately
		if (fStatus != null)
			return fStatus.booleanValue();

		// If there is no tu, then the enablement expression cannot be evaluated, assume that all
		// taggers are needed
		if (tu == null)
			return true;

		if (enablementExpression != null)
			try {
				IProject project = null;
				ICProject cProject = tu.getCProject();
				if (cProject != null)
					project = cProject.getProject();

				EvaluationContext evalContext = new EvaluationContext(null, project);

				// if the project is not accessible, then only taggers that don't care about it will
				// get a chance to run
				if (project != null) {
					String[] natures = project.getDescription().getNatureIds();
					evalContext.addVariable(VAR_PROJECTNATURES, Arrays.asList(natures));
				}

				ILanguage language = tu.getLanguage();
				if (language != null)
					evalContext.addVariable(VAR_LANGUAGEID, language.getId());

				return enablementExpression.evaluate(evalContext) == EvaluationResult.TRUE;
			} catch (CoreException e) {
				CCorePlugin.log("Error while evaluating enablement expression for " + id, e); //$NON-NLS-1$
			}

		fStatus = Boolean.FALSE;
		return false;
	}

	private IBindingTagger getTagger() {
		if (tagger == null)
			synchronized (this) {
				if (tagger == null) {
					try {
						tagger = (IBindingTagger) element.createExecutableExtension(Attr_Class);
					} catch (CoreException e) {
						String id = element.getDeclaringExtension().getNamespaceIdentifier() + '.'
								+ element.getDeclaringExtension().getSimpleIdentifier();
						CCorePlugin.log("Error in class attribute of " + id, e); //$NON-NLS-1$

						// mark the tagger with an empty implementation to prevent future load attempts
						tagger = NULL_TAGGER;
					}
				}
			}

		return tagger;
	}

	// Activates the plugin if needed.
	public IBindingTagger getBindingTaggerFor(IBinding binding, IASTName ast) {
		// If there isn't an ast with an AST-TU accessible, then there is no way to defer processing,
		// just return the tagger and let it try to sort things out. E.g., this happens for built-in
		// things.
		if (ast == null)
			return getTagger();
		IASTTranslationUnit astTU = ast.getTranslationUnit();
		if (astTU == null)
			return getTagger();

		// Otherwise evaluate the enablement expression for this TU
		return matches(astTU.getOriginatingTranslationUnit()) ? getTagger() : null;
	}
}

Back to the top