Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 70a7e9248a7ea94575259b6e3842825c458735d8 (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
/*******************************************************************************
 * Copyright (c) 2007, 2010 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.internal.ui.search;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;

import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorElseStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorEndifStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIfStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIfdefStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIfndefStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroDefinition;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorStatement;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorUndefStatement;
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.ast.cpp.ICPPASTTemplateId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;

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

/**
 * Finds locations of linked names. Used by Rename in File.
 */
public class LinkedNamesFinder {
	private static final IRegion[] EMPTY_LOCATIONS_ARRAY = new IRegion[0];

	private LinkedNamesFinder() {
		super();
	}

	public static IRegion[] findByName(IASTTranslationUnit root, IASTName name) {
		IBinding target = name.resolveBinding();
		if (target == null) {
			return EMPTY_LOCATIONS_ARRAY;
		}
		BindingFinder bindingFinder = new BindingFinder(root);
		bindingFinder.find(target);
		return bindingFinder.getLocations();
	}

	private static class BindingFinder {
		private final IASTTranslationUnit root;
		private final List<IRegion> locations;

		public BindingFinder(IASTTranslationUnit root) {
			this.root = root;
			locations = new ArrayList<IRegion>();
		}

		public void find(IBinding target) {
			if (target instanceof IMacroBinding) {
				findMacro((IMacroBinding) target);
				return;
			}

			if (target instanceof ICPPConstructor ||
					target instanceof ICPPMethod && ((ICPPMethod) target).isDestructor()) {
				target = ((ICPPMethod) target).getClassOwner();
			}

			findBinding(target);
			if (target instanceof ICPPClassType) {
				ICPPConstructor[] constructors = ((ICPPClassType) target).getConstructors();
				for (ICPPConstructor ctor : constructors) {
					if (!ctor.isImplicit()) {
						findBinding(ctor);
					}
				}
				ICPPMethod[] methods = ((ICPPClassType) target).getDeclaredMethods();
				for (ICPPMethod method : methods) {
					if (method.isDestructor()) {
						findBinding(method);
					}
				}
			}
		}

		public IRegion[] getLocations() {
			if (locations.isEmpty()) {
				return EMPTY_LOCATIONS_ARRAY;
			}
			return locations.toArray(new IRegion[locations.size()]);
		}

		private void findBinding(IBinding target) {
			IASTName[] names= root.getDeclarationsInAST(target);
			for (IASTName candidate : names) {
				if (candidate.isPartOfTranslationUnitFile()) {
					addLocation(candidate);
				}
			}
			names= root.getReferences(target);
			for (IASTName candidate : names) {
				if (candidate.isPartOfTranslationUnitFile()) {
					addLocation(candidate);
				}
			}
		}

		private void addLocation(IASTName name) {
			IBinding binding = name.resolveBinding();
			if (binding != null) {
				if (name instanceof ICPPASTTemplateId) {
					name= ((ICPPASTTemplateId) name).getTemplateName();
				}
				IASTFileLocation fileLocation= name.getImageLocation();
				if (fileLocation == null || !root.getFilePath().equals(fileLocation.getFileName())) {
					fileLocation= name.getFileLocation();
				}
				if (fileLocation != null) {
					int offset= fileLocation.getNodeOffset();
					int length= fileLocation.getNodeLength();
					if (binding instanceof ICPPMethod && ((ICPPMethod) binding).isDestructor()) {
						// Skip tilde.
						offset++;
						length--;
					}
					if (offset >= 0 && length > 0) {
						locations.add(new Region(offset, length));
					}
				}
			}
		}

		/**
		 * Adds all occurrences of a macro name to the list of locations. Macro occurrences
		 * may belong to multiple macro bindings with the same name. Macro names are also
		 * looked for in the comments of #else and #endif statements.
		 * Comments of #else and #endif statements related to #ifdef or #ifndef are searched
		 * for the macro name referenced by the #if[n]def. 
		 * @param target a binding representing a macro. 
		 */
		private void findMacro(IMacroBinding target) {
			findBinding(target);
			char[] nameChars = target.getNameCharArray();
			List<IASTName> ifdefNameStack = new ArrayList<IASTName>();
			IASTPreprocessorStatement[] statements = root.getAllPreprocessorStatements();
			for (IASTPreprocessorStatement statement : statements) {
				if (!statement.isPartOfTranslationUnitFile()) {
					continue;
				}
				IASTName macroName = null;
				boolean ifStatement = false;
				if (statement instanceof IASTPreprocessorIfdefStatement) {
					macroName = ((IASTPreprocessorIfdefStatement) statement).getMacroReference();
					ifStatement = true;
				} else if (statement instanceof IASTPreprocessorIfndefStatement) {
					macroName = ((IASTPreprocessorIfndefStatement) statement).getMacroReference();
					ifStatement = true;
				} else if (statement instanceof IASTPreprocessorMacroDefinition) {
					macroName = ((IASTPreprocessorMacroDefinition) statement).getName();
				} else if (statement instanceof IASTPreprocessorUndefStatement) {
					macroName = ((IASTPreprocessorUndefStatement) statement).getMacroName();
				} else if (statement instanceof IASTPreprocessorIfStatement) {
					ifStatement = true;
				} else if (statement instanceof IASTPreprocessorEndifStatement) {
					if (!ifdefNameStack.isEmpty())
						if (ifdefNameStack.remove(ifdefNameStack.size() - 1) != null) {
							findInStatementComment(nameChars, statement);
						}
				} else if (statement instanceof IASTPreprocessorElseStatement) {
					if (!ifdefNameStack.isEmpty())
						if (ifdefNameStack.get(ifdefNameStack.size() - 1) != null) {
							findInStatementComment(nameChars, statement);
						}
				}
				if (macroName != null) {
					if (Arrays.equals(nameChars, macroName.getSimpleID())) {
						IBinding binding = macroName.resolveBinding();
						if (!target.equals(binding)) {
							findBinding(binding);
						}
					} else {
						macroName = null;
					}
				}
				if (ifStatement) {
					ifdefNameStack.add(macroName);
				}
			}
		}

		/**
		 * Finds locations of a given name in the comment of a preprocessor statement.
		 */
		private void findInStatementComment(char[] nameChars, IASTPreprocessorStatement statement) {
			IASTFileLocation location = statement.getFileLocation();
			IASTComment comment = findComment(location.getNodeOffset() + location.getNodeLength());
			if (comment != null &&
					comment.getFileLocation().getStartingLineNumber() == location.getStartingLineNumber()) {
				findInComment(nameChars, comment);
			}
		}

		/**
		 * Returns the first comment after the given offset.
		 * @param startOffset a file offset.
		 * @return a comment or <code>null</code>, if there are no comments after the offset.
		 */
		private IASTComment findComment(int startOffset) {
			IASTComment[] comments = ((ASTTranslationUnit) root).getComments();
			int low = 0;
			int high = comments.length;
			while (low < high) {
				int mid = (low + high) / 2;
				int offset = comments[mid].getFileLocation().getNodeOffset();
				if (offset < startOffset) {
					low = mid + 1;
				} else {
					high = mid;
					if (offset == startOffset) {
						break;
					}
				}
			}
			return high < comments.length ? comments[high] : null;
		}

		/**
		 * Adds all occurrences of a name in a comment to the list of locations.
		 */
		private void findInComment(char[] name, IASTComment comment) {
			char[] text = comment.getComment();
	    	int j = 0;
	    	// First two characters are either /* or //
	    	for (int i = 2; i <= text.length - name.length + j; i++) {
	    		char c = text[i];
				if (!Character.isJavaIdentifierPart(c)) {
					j = 0;
				} else if (j >= 0 && j < name.length && name[j] == c) {
					j++;
					if (j == name.length &&
							(i + 1 == text.length || !Character.isJavaIdentifierPart(text[i + 1]))) {
						int offset = comment.getFileLocation().getNodeOffset() + i + 1 - name.length;
						locations.add(new Region(offset, name.length));
						j = 0;
					}
				} else {
					j = -1;
				}
	    	}
		}
	}
}

Back to the top