Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9f6382f6c8a6d653d8a090433d191018b6d670af (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*******************************************************************************
 * Copyright (c) 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.ui.tests.reducer;

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.NavigableSet;
import java.util.Set;
import java.util.TreeSet;

import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationListOwner;
import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTPreprocessorMacroExpansion;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.INodeFactory;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTAliasDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConversionName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTExplicitTemplateInstantiation;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLambdaExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceAlias;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTOperatorName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDirective;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisibilityLabel;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPPartialSpecialization;
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterOptions;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.model.ICElement;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
import org.eclipse.cdt.internal.core.dom.rewrite.util.ASTNodes;
import org.eclipse.cdt.internal.ui.refactoring.CRefactoring;
import org.eclipse.cdt.internal.ui.refactoring.ModificationCollector;
import org.eclipse.cdt.internal.ui.refactoring.changes.CCompositeChange;
import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper;
import org.eclipse.cdt.ui.refactoring.CTextFileChange;
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.jface.text.IRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
import org.eclipse.text.edits.DeleteEdit;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.TextEdit;

public class RemoveUnusedDeclarationsRefactoring extends CRefactoring {
	private static final String PROTECTION_TOKEN = "PRESERVE";
	private static final IASTName UNUSED_NAME = new CPPASTName(null);
	private static final ProblemFinder problemFinder = new ProblemFinder();

	private INodeFactory nodeFactory;
	private final DefaultCodeFormatterOptions formattingOptions;

	private IIndex index;
	private IASTTranslationUnit ast;
	private IRegion region;

	public RemoveUnusedDeclarationsRefactoring(ICElement element, ISelection selection, ICProject project) {
		super(element, selection, project);
		name = Messages.RemoveUnusedDeclarationsRefactoring_RemoveUnusedDeclarations;
		formattingOptions = new DefaultCodeFormatterOptions(project.getOptions(true));
	}

	@Override
	public RefactoringStatus checkInitialConditions(IProgressMonitor pm)
			throws CoreException, OperationCanceledException {
		SubMonitor progress = SubMonitor.convert(pm, 10);

		RefactoringStatus status = super.checkInitialConditions(progress.newChild(8));
		if (status.hasError()) {
			return status;
		}

		ast = getAST(tu, progress.newChild(1));
		index = getIndex();
		nodeFactory = ast.getASTNodeFactory();
		region = selectedRegion.getLength() == 0 ? new Region(0, ast.getFileLocation().getNodeLength())
				: selectedRegion;

		if (isProgressMonitorCanceled(progress, initStatus))
			return initStatus;

		return initStatus;
	}

	private ICPPASTFunctionDeclarator getDeclaration(IASTNode node) {
		while (node != null && !(node instanceof IASTFunctionDefinition)) {
			node = node.getParent();
		}
		if (node != null) {
			IASTFunctionDeclarator declarator = ((IASTFunctionDefinition) node).getDeclarator();
			if (declarator instanceof ICPPASTFunctionDeclarator) {
				return (ICPPASTFunctionDeclarator) declarator;
			}
		}
		return null;
	}

	@Override
	public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext checkContext) {
		return new RefactoringStatus();
	}

	@Override
	public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
		// This method bypasses the standard refactoring framework involving ModificationCollector and ASTRewrite since
		// it is too slow for the gigantic changes this refactoring has to deal with.
		NavigableSet<IASTName> names = NameCollector.getContainedNames(ast);

		String code = ast.getRawSignature();

		SortedNodeSet<IASTNode> nodesToDelete = new SortedNodeSet<>();
		IASTPreprocessorMacroExpansion[] macroExpansions = ast.getMacroExpansions();
		for (IASTPreprocessorMacroExpansion macroExpansion : macroExpansions) {
			IASTName name = macroExpansion.getMacroReference();
			if (SelectionHelper.isNodeInsideRegion(name, region) && !containsProtectionToken(name, code)
					&& macroExpansion.getMacroDefinition().getExpansion().isEmpty()) {
				nodesToDelete.add(macroExpansion);
			}
		}

		CandidateDeclarationFinder finder = new CandidateDeclarationFinder();
		ast.accept(finder);
		List<IASTDeclaration> declarations = finder.declarations;

		for (int i = declarations.size(); --i >= 0;) {
			IASTDeclaration declaration = declarations.get(i);
			if (SelectionHelper.isNodeInsideRegion(declaration, region) && !containsProtectionToken(declaration, code)
					&& !problemFinder.containsProblemBinding(declaration)
					&& !isPossiblyUsed(declaration, names, nodesToDelete)) {
				nodesToDelete.add(declaration);
				removeContainedNames(declaration, names);
			}
		}

		CTextFileChange fileChange = new CTextFileChange(tu.getElementName(), tu);
		fileChange.setEdit(new MultiTextEdit());

		int maxOffset = 0;
		TextEdit lastEdit = null;
		IASTNode lastNode = null;
		for (IASTNode node : nodesToDelete) {
			int offset = ASTNodes.offset(node);
			int endOffset = ASTNodes.endOffset(node);
			offset = skipWhitespaceBefore(offset, code);
			if (offset < region.getOffset())
				offset = region.getOffset();
			// Do not attempt to delete nodes inside a deleted region.
			if (endOffset > maxOffset) {
				DeleteEdit edit = new DeleteEdit(offset, endOffset - offset);
				fileChange.addEdit(edit);
				if (maxOffset < endOffset)
					maxOffset = endOffset;
				lastEdit = edit;
				lastNode = node;
			}
		}

		CCompositeChange change = new CCompositeChange(""); //$NON-NLS-1$
		change.markAsSynthetic();
		change.add(fileChange);
		change.setDescription(new RefactoringChangeDescriptor(getRefactoringDescriptor()));
		return change;
	}

	/**
	 * Checks if the node or the rest of its last line contain text matching {@link #PROTECTION_TOKEN}.
	 */
	private boolean containsProtectionToken(IASTNode node, String code) {
		int offset = ASTNodes.offset(node);
		int endOffset = ASTNodes.skipToNextLineAfterNode(code, node);
		for (int i = offset; i < endOffset - PROTECTION_TOKEN.length(); i++) {
			if (code.regionMatches(i, PROTECTION_TOKEN, 0, PROTECTION_TOKEN.length()))
				return true;
		}
		return false;
	}

	private boolean containsAncestor(Collection<IASTNode> nodes, IASTNode node) {
		while ((node = node.getParent()) != null) {
			if (nodes.contains(node))
				return true;
		}
		return false;
	}

	private IASTNode getTopMostContainer(IASTNode node) {
		while (node != null) {
			IASTNode prevNode = node;
			node = node.getParent();
			if (node instanceof IASTTranslationUnit)
				return prevNode;
		}
		return null;
	}

	private boolean isPossiblyUsed(IASTDeclaration declaration, NavigableSet<IASTName> names,
			SortedNodeSet<IASTNode> nodesToDelete) {
		if (declaration instanceof ICPPASTNamespaceDefinition) {
			// An empty namespace definition can be removed.
			IASTDeclaration[] children = ((ICPPASTNamespaceDefinition) declaration).getDeclarations(false);
			for (IASTDeclaration child : children) {
				if (!nodesToDelete.contains(child))
					return true;
			}
			return false;
		} else if (declaration instanceof ICPPASTLinkageSpecification) {
			// An empty linkage specification can be removed.
			IASTDeclaration[] children = ((ICPPASTLinkageSpecification) declaration).getDeclarations(false);
			for (IASTDeclaration child : children) {
				if (!nodesToDelete.contains(child))
					return true;
			}
			return false;
		} else if (declaration instanceof ICPPASTVisibilityLabel) {
			// A visibility label not followed by a member declaration can be removed.
			IASTNode parent = declaration.getParent();
			IASTDeclaration[] siblings = ((ICPPASTCompositeTypeSpecifier) parent).getDeclarations(false);
			boolean after = false;
			for (IASTDeclaration sibling : siblings) {
				if (after) {
					if (sibling instanceof ICPPASTVisibilityLabel)
						break;
					if (!nodesToDelete.contains(sibling))
						return true;
				} else if (sibling == declaration) {
					after = true;
				}
			}
			return false;
		}

		if (ASTQueries.findAncestorWithType(declaration, ICPPASTLambdaExpression.class) != null)
			return true; // Removing inside lambda expressions is unsafe.

		Collection<IASTName> declaredNames = getDeclaredNames(declaration);
		if (declaredNames == null)
			return true;

		for (IASTName declName : declaredNames) {
			char[] declNameChars = declName.getSimpleID();
			IASTNode startPoint = declName;
			int startOffset = ASTNodes.endOffset(declaration);
			if (declaration.getPropertyInParent() == IASTCompositeTypeSpecifier.MEMBER_DECLARATION
					&& !(declName.resolveBinding() instanceof ICPPConstructor)
					&& (declNameChars.length == 0 || declNameChars[0] != '~')) {
				// Member declarations can be referenced by other members declared before them.
				startPoint = declaration.getParent();
				startOffset = ASTNodes.offset(startPoint);
			} else {
				ASTNodeProperty property = declName.getPropertyInParent();
				if (property == IASTCompositeTypeSpecifier.TYPE_NAME
						|| property == ICPPASTEnumerationSpecifier.ENUMERATION_NAME
								&& ((ICPPASTEnumerationSpecifier) declName.getParent()).isScoped()) {
					// Start from the first forward declaration of class or scoped enumeration.
					while (declName instanceof ICPPASTTemplateId) {
						declName = ((ICPPASTTemplateId) declName).getTemplateName();
					}
					IBinding binding = declName.resolveBinding();
					if (binding instanceof IProblemBinding)
						return true;
					if (binding instanceof ICPPInternalBinding) {
						IASTNode[] declarations = ((ICPPInternalBinding) binding).getDeclarations();
						if (declarations != null && declarations.length != 0) {
							IASTNode firstDeclaration = declarations[0];
							int firstDeclarationOffset = ASTNodes.endOffset(firstDeclaration);
							if (startOffset > firstDeclarationOffset) {
								startPoint = firstDeclaration;
								startOffset = firstDeclarationOffset;
							}
						}
					}
				}
			}

			if (declNameChars.length != 0 && declNameChars[0] == '~')
				declNameChars = Arrays.copyOfRange(declNameChars, 1, declNameChars.length);

			int declOffset = ASTNodes.offset(declaration);
			int declEndOffset = ASTNodes.endOffset(declaration);

			for (IASTName name : names) {
				if (name != declName) {
					if (name.getPropertyInParent() == IASTDeclarator.DECLARATOR_NAME
							&& name.getParent().getPropertyInParent() == IASTParameterDeclaration.DECLARATOR) {
						continue; // Ignore parameter names.
					}

					char[] nameChars = name.getSimpleID();

					int offset = nameChars.length != 0 && nameChars[0] == '~' ? 1 : 0;
					if (CharArrayUtils.equals(nameChars, offset, nameChars.length - offset, declNameChars)) {
						int nameOffset = ASTNodes.offset(name);
						if (nameOffset >= declEndOffset || nameOffset >= startOffset && nameOffset < declOffset) {
							IASTDeclaration decl = findTopmostNonTemplateDeclaration(name);
							if (decl == null)
								return true;

							if (!isDeclaredBy(name, decl))
								return true;
						}
					}
				}
			}
		}
		return false;
	}

	private static Collection<IASTName> getDeclaredNames(IASTDeclaration declaration) {
		while (declaration instanceof ICPPASTTemplateDeclaration) {
			declaration = ((ICPPASTTemplateDeclaration) declaration).getDeclaration();
		}
		while (declaration instanceof ICPPASTTemplateSpecialization) {
			declaration = ((ICPPASTTemplateSpecialization) declaration).getDeclaration();
		}
		while (declaration instanceof ICPPASTExplicitTemplateInstantiation) {
			declaration = ((ICPPASTExplicitTemplateInstantiation) declaration).getDeclaration();
		}

		if (declaration instanceof IASTSimpleDeclaration) {
			List<IASTName> names = new ArrayList<>();
			IASTDeclarator[] declarators = ((IASTSimpleDeclaration) declaration).getDeclarators();
			for (IASTDeclarator declarator : declarators) {
				declarator = findInnermostDeclarator(declarator);
				IASTName name = declarator.getName();
				if (name instanceof ICPPASTConversionName || name instanceof ICPPASTOperatorName)
					return null; // Do not remove operators.
				addNameIfNotEmpty(name, names);
			}
			IASTDeclSpecifier declSpecifier = ((IASTSimpleDeclaration) declaration).getDeclSpecifier();
			if (declSpecifier instanceof IASTCompositeTypeSpecifier) {
				addNameIfNotEmpty(((IASTCompositeTypeSpecifier) declSpecifier).getName(), names);
			} else if (declSpecifier instanceof IASTElaboratedTypeSpecifier) {
				addNameIfNotEmpty(((IASTElaboratedTypeSpecifier) declSpecifier).getName(), names);
			} else if (declSpecifier instanceof IASTEnumerationSpecifier) {
				IASTEnumerationSpecifier enumSpecifier = (IASTEnumerationSpecifier) declSpecifier;
				IASTName name = enumSpecifier.getName();
				addNameIfNotEmpty(name, names);
				for (IASTEnumerator enumerator : enumSpecifier.getEnumerators()) {
					addNameIfNotEmpty(enumerator.getName(), names);
				}
			}
			return names;
		} else if (declaration instanceof IASTFunctionDefinition) {
			IASTDeclarator declarator = ((IASTFunctionDefinition) declaration).getDeclarator();
			declarator = findInnermostDeclarator(declarator);
			IASTName name = declarator.getName();
			if (name instanceof ICPPASTConversionName)
				return null; // Do not remove conversion operators.
			return Collections.singletonList(name);
		} else if (declaration instanceof ICPPASTUsingDirective) {
			return Collections.singletonList(((ICPPASTUsingDirective) declaration).getQualifiedName());
		} else if (declaration instanceof ICPPASTUsingDeclaration) {
			return Collections.singletonList(((ICPPASTUsingDeclaration) declaration).getName());
		} else if (declaration instanceof ICPPASTNamespaceAlias) {
			return Collections.singletonList(((ICPPASTNamespaceAlias) declaration).getAlias());
		} else if (declaration instanceof ICPPASTAliasDeclaration) {
			return Collections.singletonList(((ICPPASTAliasDeclaration) declaration).getAlias());
		}
		return null;
	}

	private static void addNameIfNotEmpty(IASTName name, List<IASTName> names) {
		if (name.getSimpleID().length != 0)
			names.add(name);
	}

	/**
	 * Returns the topmost non-template declaration declaring the given name, or {@code null}
	 * if the name appears in a non-declaration context.
	 */
	private static IASTDeclaration findTopmostNonTemplateDeclaration(IASTName name) {
		IASTNode node = name;
		ASTNodeProperty property = name.getPropertyInParent();
		while (property == ICPPASTTemplateId.TEMPLATE_NAME || property == ICPPASTQualifiedName.SEGMENT_NAME
				&& node == ((ICPPASTQualifiedName) node.getParent()).getLastName()) {
			node = node.getParent();
			property = node.getPropertyInParent();
		}
		if (property == IASTDeclarator.DECLARATOR_NAME || property == IASTCompositeTypeSpecifier.TYPE_NAME
				|| property == IASTElaboratedTypeSpecifier.TYPE_NAME) {
			node = node.getParent().getParent();
		}
		for (IASTNode parent; (parent = node.getParent()) != null; node = parent) {
			if (!(parent instanceof IASTDeclaration) || parent instanceof ICPPASTTemplateDeclaration
					|| parent instanceof ICPPASTTemplateSpecialization
					|| parent instanceof ICPPASTNamespaceDefinition) {
				if (node instanceof IASTDeclaration) {
					return (IASTDeclaration) node;
				}
				break;
			}
		}

		return null;
	}

	/**
	 * Checks if {@code name} is declared by the {@code declaration}.
	 */
	private static boolean isDeclaredBy(IASTName name, IASTDeclaration declaration) {
		if (declaration.getPropertyInParent() == ICPPASTTemplateSpecialization.OWNED_DECLARATION)
			return false;
		if (name.getPropertyInParent() == ICPPASTTemplateId.TEMPLATE_NAME
				&& ((ICPPASTTemplateId) name.getParent()).resolveBinding() instanceof ICPPPartialSpecialization)
			return false;
		if (declaration instanceof IASTSimpleDeclaration) {
			IASTDeclSpecifier declSpec = ((IASTSimpleDeclaration) declaration).getDeclSpecifier();
			if (declSpec instanceof ICPPASTDeclSpecifier && ((ICPPASTDeclSpecifier) declSpec).isFriend())
				return false;
		}

		while (name instanceof ICPPASTTemplateId) {
			name = ((ICPPASTTemplateId) name).getTemplateName();
		}
		char[] nameChars = name.getSimpleID();
		Collection<IASTName> declaredNames = getDeclaredNames(declaration);
		if (declaredNames != null) {
			for (IASTName declaredName : declaredNames) {
				while (declaredName instanceof ICPPASTTemplateId) {
					declaredName = ((ICPPASTTemplateId) declaredName).getTemplateName();
				}
				char[] declaredNameChars = declaredName.getSimpleID();
				if (Arrays.equals(nameChars, declaredNameChars))
					return true;
			}
		}

		return false;
	}

	private static void removeContainedNames(IASTNode node, Set<IASTName> names) {
		NavigableSet<IASTName> containedNames = NameCollector.getContainedNames(node);
		names.removeAll(containedNames);
	}

	private static int skipWhitespaceBefore(int offset, String text) {
		while (--offset >= 0) {
			char c = text.charAt(offset);
			if (!Character.isWhitespace(c))
				break;
		}
		return offset + 1;
	}

	@Override
	protected void collectModifications(IProgressMonitor pm, ModificationCollector collector)
			throws CoreException, OperationCanceledException {
		// This method is no-op for this refactoring. The change is created in the createChange method.
	}

	private class CandidateDeclarationFinder extends ASTVisitor {
		final List<IASTDeclaration> declarations = new ArrayList<>();

		CandidateDeclarationFinder() {
			shouldVisitDeclarations = true;
			shouldVisitNamespaces = true;
		}

		@Override
		public int visit(IASTDeclaration declaration) {
			if (!declaration.isPartOfTranslationUnitFile())
				return PROCESS_SKIP;
			if (declaration.getParent() instanceof IASTDeclarationListOwner)
				declarations.add(declaration);
			return PROCESS_CONTINUE;
		}

		@Override
		public int visit(ICPPASTNamespaceDefinition namespaceDefinition) {
			if (!namespaceDefinition.isPartOfTranslationUnitFile())
				return PROCESS_SKIP;
			declarations.add(namespaceDefinition);
			return PROCESS_CONTINUE;
		}
	}

	/**
	 * Collects all simple names.
	 */
	private static class NameCollector extends ASTVisitor {
		NavigableSet<IASTName> names = new SortedNodeSet<>();

		static NavigableSet<IASTName> getContainedNames(IASTNode node) {
			NameCollector collector = new NameCollector();
			node.accept(collector);
			return collector.names;
		}

		NameCollector() {
			this.shouldVisitNames = true;
			this.shouldVisitImplicitNames = true;
		}

		@Override
		public int visit(IASTName name) {
			if (name instanceof ICPPASTQualifiedName || name instanceof ICPPASTTemplateId
					|| name instanceof ICPPASTConversionName) {
				return PROCESS_CONTINUE;
			}
			names.add(name);
			return PROCESS_CONTINUE;
		}
	}

	/**
	 * A set of AST nodes sorted by their offsets, or, if the offsets are equal, by the end offsets
	 * in the reverse order.
	 */
	private static class SortedNodeSet<T extends IASTNode> extends TreeSet<T> {
		private static final Comparator<IASTNode> COMPARATOR = new Comparator<IASTNode>() {
			@Override
			public int compare(IASTNode node1, IASTNode node2) {
				int c = Integer.compare(ASTNodes.offset(node1), ASTNodes.offset(node2));
				if (c != 0)
					return c;
				return -Integer.compare(ASTNodes.endOffset(node1), ASTNodes.endOffset(node2));
			}
		};

		public SortedNodeSet() {
			super(COMPARATOR);
		}
	}

	private static IASTDeclarationStatement getDeclarationStatement(IASTDeclaration declaration) {
		while (true) {
			IASTNode parent = declaration.getParent();
			if (parent instanceof IASTDeclarationStatement)
				return (IASTDeclarationStatement) parent;
			if (!(parent instanceof ICPPASTTemplateDeclaration))
				return null;
			declaration = (IASTDeclaration) parent;
		}
	}

	private static IASTName getAstName(IASTDeclarator decl) {
		IASTName astName = null;
		do {
			astName = decl.getName();
			if (astName != null && astName.getSimpleID().length != 0)
				return astName;

			// Resolve parenthesis if need to.
			decl = decl.getNestedDeclarator();
		} while (decl != null);

		return astName;
	}

	@Override
	protected RefactoringDescriptor getRefactoringDescriptor() {
		return null;
	}
}

Back to the top