Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1c54216072321094c8210eb936ba8bcad1645d66 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*******************************************************************************
 * Copyright (c) 2007, 2012 Wind River Systems, Inc. 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:
 *     Anton Leherbauer (Wind River Systems) - initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.internal.ui.text.c.hover;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTNodeSelector;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroExpansion;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IMacroBinding;
import org.eclipse.cdt.core.dom.rewrite.MacroExpansionExplorer;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.model.IWorkingCopy;
import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
import org.eclipse.cdt.internal.ui.editor.ASTProvider;
import org.eclipse.cdt.internal.ui.text.CHeuristicScanner;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.IWorkingCopyManager;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.Region;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * An input object to the {@link CMacroExpansionExplorationControl}.
 *
 * @since 5.0
 */
public class CMacroExpansionInput {

	private static class SingletonRule implements ISchedulingRule {
		public static final ISchedulingRule INSTANCE = new SingletonRule();

		@Override
		public boolean contains(ISchedulingRule rule) {
			return rule == this;
		}

		@Override
		public boolean isConflicting(ISchedulingRule rule) {
			return rule == this;
		}
	}

	/**
	 * Computes the expansion region for a selection.
	 */
	private static class ExpansionRegionComputer implements ASTRunnable {
		private final Position fTextRegion;
		private final boolean fAllowSelection;
		private IASTNode fEnclosingNode;
		private List<IASTNode> fExpansionNodes = new ArrayList<IASTNode>();
		private MacroExpansionExplorer fExplorer;
		private IRegion fExpansionRegion;

		private ExpansionRegionComputer(ITranslationUnit tUnit, IRegion textRegion, boolean allowSelection) {
			fTextRegion = new Position(textRegion.getOffset(), textRegion.getLength());
			fAllowSelection = allowSelection;
		}

		/*
		 * @see org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable#runOnAST(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit)
		 */
		@Override
		public IStatus runOnAST(ILanguage lang, IASTTranslationUnit ast) {
			if (ast != null) {
				final IASTNodeSelector nodeSelector = ast.getNodeSelector(ast.getFilePath());
				// try exact macro name match first
				IASTNode node = nodeSelector.findName(fTextRegion.getOffset(), fTextRegion.getLength());
				if (node instanceof IASTName) {
					IASTName macroName = (IASTName) node;
					IBinding binding = macroName.getBinding();
					// skip macro references that belong to a macro definition or an undef directive
					if (binding instanceof IMacroBinding && !macroName.isDefinition()
							&& macroName.getParent() instanceof IASTPreprocessorMacroExpansion) {
						addExpansionNode(node);
						createMacroExpansionExplorer(ast);
						return Status.OK_STATUS;
					}
				}
				if (fAllowSelection) {
					// selection
					boolean macroOccurrence = false;
					fEnclosingNode = nodeSelector.findEnclosingNode(fTextRegion.getOffset(), fTextRegion.getLength());
					if (fEnclosingNode == null) {
						// selection beyond last declaration
						fEnclosingNode = ast;
					} else if (fEnclosingNode.getParent() instanceof IASTPreprocessorMacroExpansion) {
						// selection enclosed by the name of a macro expansion
						fEnclosingNode = fEnclosingNode.getParent();
					}

					if (fEnclosingNode instanceof IASTPreprocessorMacroExpansion) {
						// selection enclosed by a macro expansion
						addExpansionNode(fEnclosingNode);
						macroOccurrence = true;
					} else {
						IASTNodeLocation[] locations = fEnclosingNode.getNodeLocations();
						for (int i = 0; i < locations.length; i++) {
							IASTNodeLocation location = locations[i];
							if (location instanceof IASTMacroExpansionLocation) {
								IASTFileLocation fileLocation = location.asFileLocation();
								if (fileLocation != null && ast.getFilePath().equals(fileLocation.getFileName())) {
									if (fTextRegion.overlapsWith(fileLocation.getNodeOffset(),
											fileLocation.getNodeLength())) {
										addExpansionNode(nodeSelector.findEnclosingNode(fileLocation.getNodeOffset(),
												fileLocation.getNodeLength()));
										macroOccurrence = true;
									}
								}
							}
						}
					}
					if (macroOccurrence) {
						createMacroExpansionExplorer(ast);
						return Status.OK_STATUS;
					}
				}
			}
			return Status.CANCEL_STATUS;
		}

		private void createMacroExpansionExplorer(IASTTranslationUnit ast) {
			IRegion region = getExpansionRegion();
			if (region != null) {
				fExplorer = MacroExpansionExplorer.create(ast, region);
				fExpansionRegion = region;
			}
		}

		private void addExpansionNode(IASTNode node) {
			if (node != null) {
				fEnclosingNode = computeCommonAncestor(node, fEnclosingNode);
				fExpansionNodes.add(node);
			}
		}

		private IASTNode computeCommonAncestor(IASTNode node, IASTNode other) {
			if (node == null) {
				return null;
			}
			if (other == null) {
				return node;
			}
			if (node == other) {
				return other;
			}
			List<IASTNode> ancestors = new ArrayList<IASTNode>();
			while (node != null) {
				node = node.getParent();
				ancestors.add(node);
			}
			while (other != null) {
				if (ancestors.contains(other)) {
					return other;
				}
				other = other.getParent();
			}
			return null;
		}

		IRegion getExpansionRegion() {
			if (fExpansionRegion != null)
				return fExpansionRegion;
			if (fEnclosingNode != null) {
				int startOffset = Integer.MAX_VALUE;
				int endOffset = fTextRegion.getOffset() + fTextRegion.getLength();
				for (Iterator<IASTNode> it = fExpansionNodes.iterator(); it.hasNext();) {
					IASTNode node = it.next();
					if (node != fEnclosingNode) {
						while (node != null && node.getParent() != fEnclosingNode) {
							node = node.getParent();
						}
					}
					if (node != null) {
						IASTFileLocation location = node.getFileLocation();
						if (location != null) {
							startOffset = Math.min(startOffset, location.getNodeOffset());
							endOffset = Math.max(endOffset, location.getNodeOffset() + location.getNodeLength());
						}
					}
				}
				if (endOffset > startOffset) {
					startOffset = Math.min(startOffset, fTextRegion.getOffset());
					return new Region(startOffset, endOffset - startOffset);
				}
			}
			return null;
		}

		MacroExpansionExplorer getMacroExpansionExplorer() {
			return fExplorer;
		}
	}

	final MacroExpansionExplorer fExplorer;
	boolean fStartWithFullExpansion = true;

	private CMacroExpansionInput(MacroExpansionExplorer explorer) {
		Assert.isNotNull(explorer);
		fExplorer = explorer;
	}

	@Override
	public String toString() {
		return fExplorer.getFullExpansion().getCodeAfterStep();
	}

	/**
	 * Creates an input object for the macro expansion exploration control {@link CMacroExpansionExplorationControl}.
	 *
	 * @param editor  the active editor
	 * @param textRegion  the text region where to consider macro expansions
	 * @param force  force computation of the input, if <code>true</code> this may trigger a parse
	 * @return an instance of {@link CMacroExpansionInput} or <code>null</code> if no macro was found in the region
	 */
	public static CMacroExpansionInput create(IEditorPart editor, IRegion textRegion, boolean force) {
		if (editor == null || !(editor instanceof ITextEditor)) {
			return null;
		}
		IEditorInput editorInput = editor.getEditorInput();
		IWorkingCopyManager manager = CUIPlugin.getDefault().getWorkingCopyManager();
		IWorkingCopy tu = manager.getWorkingCopy(editorInput);
		try {
			if (tu == null || !tu.isConsistent()) {
				return null;
			}
		} catch (CModelException exc) {
			return null;
		}

		ExpansionRegionComputer computer = new ExpansionRegionComputer(tu, textRegion, force);
		doRunOnAST(computer, tu, force);

		MacroExpansionExplorer explorer = computer.getMacroExpansionExplorer();
		if (explorer == null) {
			return null;
		}

		CMacroExpansionInput input = new CMacroExpansionInput(explorer);

		return input;
	}

	private static void doRunOnAST(final ASTRunnable runnable, final ITranslationUnit tu, boolean force) {
		Job job = new Job(CHoverMessages.CMacroExpansionInput_jobTitle) {
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				return ASTProvider.getASTProvider().runOnAST(tu, ASTProvider.WAIT_ACTIVE_ONLY, monitor, runnable);
			}
		};

		// If the hover thread is interrupted this might have negative
		// effects on the index - see http://bugs.eclipse.org/219834
		// Therefore we schedule a job to decouple the parsing from this thread.
		job.setPriority(force ? Job.INTERACTIVE : Job.DECORATE);
		job.setSystem(true);
		job.setRule(SingletonRule.INSTANCE);
		job.schedule();
		try {
			job.join();
		} catch (InterruptedException exc) {
			job.cancel();
		}
	}

	/**
	 * Expand the given text region to span complete lines of the document and
	 * add a number of lines before and after the region.
	 *
	 * @param region  the text region
	 * @param document  the underlying text document
	 * @param contextLines  the number of lines to add
	 * @return an extended region
	 */
	public static final IRegion expandRegion(IRegion region, IDocument document, int contextLines) {
		try {
			int start = document.getLineOfOffset(region.getOffset());
			start = Math.max(start - contextLines, 0);
			int offset = document.getLineOffset(start);
			CHeuristicScanner scanner = new CHeuristicScanner(document);
			offset = scanner.findNonWhitespaceForward(offset, region.getOffset() + 1);

			int end = document.getLineOfOffset(region.getOffset() + region.getLength());
			end = Math.min(end + contextLines, document.getNumberOfLines() - 1);

			final int endOffset;
			if (document.getNumberOfLines() > end + 1) {
				endOffset = document.getLineOffset(end + 1);
			} else {
				endOffset = document.getLineOffset(end) + document.getLineLength(end);
			}
			return new Region(offset, endOffset - offset);

		} catch (BadLocationException x) {
			return region;
		}
	}

}

Back to the top