Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5f68ea039f69d6a5f2b7c7b24791d3a95439946e (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
/*******************************************************************************
 * Copyright (c) 2011, 2016 Google, 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:
 * 	   Sergey Prigogin (Google) - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.utils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;

import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
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.cpp.ICPPMember;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.model.CoreModelUtil;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.parser.util.ArrayUtil;

import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;

import org.eclipse.cdt.internal.ui.editor.ITranslationUnitEditorInput;
import org.eclipse.cdt.internal.ui.refactoring.CRefactoringContext;
import org.eclipse.cdt.internal.ui.util.EditorUtility;

/**
 * Helper class for finding definitions and class member declarations
 */
public class DefinitionFinder {
	/**
	 * Finds the definition for the given name. The definition and the original name may belong
	 * to a different ASTs. The search is done in the index and in the ASTs of dirty editors.
	 *
	 * @param name the name to find the definition for
	 * @param context the refactoring context
	 * @param pm the progress monitor
	 * @return the definition name, or {@code null} if there is no definition or if it is
	 *     not unique.
	 * @throws CoreException thrown in case of errors
	 */
	public static IASTName getDefinition(IASTName name, CRefactoringContext context, IProgressMonitor pm)
			throws CoreException, OperationCanceledException {
		IBinding binding = name.resolveBinding();
		if (binding == null) {
			return null;
		}
		return getDefinition(binding, name.getTranslationUnit(), context, pm);
	}

	/**
	 * Finds the definition for the given binding. The search is done in the index and in the ASTs
	 * of dirty editors.
	 *
	 * @param binding the binding to find the definition for
	 * @param contextTu the translation unit that determines the set of files to search for
	 *     the definition. Only the files directly or indirectly included by the translation unit
	 *     are considered.
	 * @param context the refactoring context
	 * @param pm the progress monitor
	 * @return the definition name, or {@code null} if there is no definition or if it is
	 *     not unique.
	 * @throws CoreException thrown in case of errors
	 */
	public static IASTName getDefinition(IBinding binding, IASTTranslationUnit contextTu, CRefactoringContext context,
			IProgressMonitor pm) throws CoreException {
		SubMonitor sm = SubMonitor.convert(pm, 10);
		IIndex index = context.getIndex();
		if (index == null) {
			return null;
		}
		IIndexBinding indexBinding = index.adaptBinding(binding);
		if (binding == null)
			return null;
		Set<String> searchedFiles = new HashSet<String>();
		List<IASTName> definitions = new ArrayList<IASTName>();
		IIndexName[] definitionsFromIndex = index.findNames(indexBinding,
				IIndex.FIND_DEFINITIONS | IIndex.SEARCH_ACROSS_LANGUAGE_BOUNDARIES);
		int remainingCount = definitionsFromIndex.length;
		SubMonitor loopProgress = sm.newChild(6).setWorkRemaining(remainingCount);
		for (IIndexName name : definitionsFromIndex) {
			if (sm.isCanceled()) {
				throw new OperationCanceledException();
			}
			IIndexFile indexFile = name.getFile();
			if (contextTu.getASTFileSet().contains(indexFile) || contextTu.getIndexFileSet().contains(indexFile)) {
				ITranslationUnit tu = CoreModelUtil.findTranslationUnitForLocation(indexFile.getLocation(), null);
				if (searchedFiles.add(tu.getLocation().toOSString())) {
					findDefinitionsInTranslationUnit(indexBinding, tu, context, definitions, pm);
					if (definitions.size() > 1)
						return null;
				}
			}
			loopProgress.setWorkRemaining(--remainingCount);
		}
		if (definitions.isEmpty()) {
			// Check dirty editors in case definition has just been introduced but not saved yet.
			IEditorPart[] dirtyEditors = EditorUtility.getDirtyEditors(true);
			loopProgress = sm.newChild(3).setWorkRemaining(dirtyEditors.length);
			for (IEditorPart editor : dirtyEditors) {
				if (sm.isCanceled()) {
					throw new OperationCanceledException();
				}
				IEditorInput editorInput = editor.getEditorInput();
				if (editorInput instanceof ITranslationUnitEditorInput) {
					ITranslationUnit tu = ((ITranslationUnitEditorInput) editorInput).getTranslationUnit();
					if (searchedFiles.add(tu.getLocation().toOSString())) {
						findDefinitionsInTranslationUnit(indexBinding, tu, context, definitions,
								loopProgress.newChild(1));
						if (definitions.size() > 1)
							return null;
					}
				}
			}
		}

		return definitions.size() == 1 ? definitions.get(0) : null;
	}

	/**
	 * Checks if the given binding has a definition. The search is done in the index and in the ASTs
	 * of dirty editors.
	 *
	 * @param binding the binding to find the definition for
	 * @param context the refactoring context
	 * @param pm the progress monitor
	 * @return <code>true</code> if the binding has a definition.
	 * @throws CoreException thrown in case of errors
	 */
	public static boolean hasDefinition(IBinding binding, CRefactoringContext context, IProgressMonitor pm)
			throws CoreException, OperationCanceledException {
		SubMonitor sm = SubMonitor.convert(pm, 10);
		IIndex index = context.getIndex();
		if (index == null) {
			return false;
		}
		IIndexBinding indexBinding = index.adaptBinding(binding);
		if (binding == null)
			return false;
		Set<String> dirtyFiles = new HashSet<String>();
		IEditorPart[] dirtyEditors = EditorUtility.getDirtyEditors(true);
		for (IEditorPart editor : dirtyEditors) {
			IEditorInput editorInput = editor.getEditorInput();
			if (editorInput instanceof ITranslationUnitEditorInput) {
				ITranslationUnit tu = ((ITranslationUnitEditorInput) editorInput).getTranslationUnit();
				dirtyFiles.add(tu.getLocation().toOSString());
			}
		}

		Set<String> searchedFiles = new HashSet<String>();
		IIndexName[] definitionsFromIndex = index.findNames(indexBinding,
				IIndex.FIND_DEFINITIONS | IIndex.SEARCH_ACROSS_LANGUAGE_BOUNDARIES);
		int remainingCount = definitionsFromIndex.length;
		SubMonitor loopProgress = sm.newChild(6).setWorkRemaining(remainingCount);
		for (IIndexName name : definitionsFromIndex) {
			if (sm.isCanceled()) {
				throw new OperationCanceledException();
			}
			ITranslationUnit tu = CoreModelUtil.findTranslationUnitForLocation(name.getFile().getLocation(), null);
			String filename = tu.getLocation().toOSString();
			if (searchedFiles.add(filename) && (!dirtyFiles.contains(filename)
					|| hasDefinitionsInTranslationUnit(indexBinding, tu, context, loopProgress.newChild(1)))) {
				return true;
			}
			loopProgress.setWorkRemaining(--remainingCount);
		}

		// Check dirty editors in case definition has just been introduced but not saved yet.
		loopProgress = sm.newChild(3).setWorkRemaining(dirtyEditors.length);
		for (IEditorPart editor : dirtyEditors) {
			if (sm.isCanceled()) {
				throw new OperationCanceledException();
			}
			IEditorInput editorInput = editor.getEditorInput();
			if (editorInput instanceof ITranslationUnitEditorInput) {
				ITranslationUnit tu = ((ITranslationUnitEditorInput) editorInput).getTranslationUnit();
				String filename = tu.getLocation().toOSString();
				if (searchedFiles.add(filename)
						&& hasDefinitionsInTranslationUnit(indexBinding, tu, context, loopProgress.newChild(1))) {
					return true;
				}
			}
		}

		return false;
	}

	private static void findDefinitionsInTranslationUnit(IIndexBinding binding, ITranslationUnit tu,
			CRefactoringContext context, List<IASTName> definitions, IProgressMonitor pm)
			throws CoreException, OperationCanceledException {
		IASTTranslationUnit ast = context.getAST(tu, pm);
		ArrayUtil.addAll(definitions, ast.getDefinitionsInAST(binding));
	}

	private static boolean hasDefinitionsInTranslationUnit(IIndexBinding binding, ITranslationUnit tu,
			CRefactoringContext context, IProgressMonitor pm) throws CoreException, OperationCanceledException {
		IASTTranslationUnit ast = context.getAST(tu, pm);
		return ast.getDefinitionsInAST(binding).length != 0;
	}

	/**
	 * Finds the declaration for the given class member. The declaration and the original member
	 * name may belong to a different ASTs. The search is done in the index and in the ASTs of dirty
	 * editors.
	 *
	 * @param memberName the name of the class member to find the declaration for
	 * @param context the refactoring context
	 * @param pm the progress monitor
	 * @return the declaration name, or {@code null} if there is no declaration or if it is
	 *     not unique.
	 * @throws CoreException thrown in case of errors
	 */
	public static IASTName getMemberDeclaration(IASTName memberName, CRefactoringContext context, IProgressMonitor pm)
			throws CoreException, OperationCanceledException {
		IBinding binding = memberName.resolveBinding();
		if (!(binding instanceof ICPPMember))
			return null;
		return getMemberDeclaration((ICPPMember) binding, memberName.getTranslationUnit(), context, pm);
	}

	/**
	 * Finds the declaration for the given class member. The declaration and the original member
	 * name may belong to a different ASTs. The search is done in the index and in the ASTs of dirty
	 * editors.
	 *
	 * @param member the class member binding to find the declaration for
	 * @param contextTu the translation unit that determines the set of files to search for
	 *     the declaration. Only the files directly or indirectly included by the translation unit
	 *     are considered.
	 * @param context the refactoring context
	 * @param pm the progress monitor
	 * @return the declaration name, or {@code null} if there is no declaration or if it is
	 *     not unique.
	 * @throws CoreException thrown in case of errors
	 */
	public static IASTName getMemberDeclaration(ICPPMember member, IASTTranslationUnit contextTu,
			CRefactoringContext context, IProgressMonitor pm) throws CoreException, OperationCanceledException {
		IASTName classDefintionName = getDefinition(member.getClassOwner(), contextTu, context, pm);
		if (classDefintionName == null)
			return null;
		IASTCompositeTypeSpecifier compositeTypeSpecifier = ASTQueries.findAncestorWithType(classDefintionName,
				IASTCompositeTypeSpecifier.class);
		IASTTranslationUnit ast = classDefintionName.getTranslationUnit();
		IIndex index = context.getIndex();
		if (index == null) {
			return null;
		}
		IASTName[] memberDeclarationNames = ast.getDeclarationsInAST(index.adaptBinding(member));
		for (IASTName name : memberDeclarationNames) {
			if (name.getPropertyInParent() == IASTDeclarator.DECLARATOR_NAME) {
				IASTDeclaration declaration = ASTQueries.findAncestorWithType(name, IASTDeclaration.class);
				if (declaration.getParent() == compositeTypeSpecifier) {
					return name;
				}
			}
		}
		return null;
	}
}

Back to the top