mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-30 04:15:35 +02:00
Added "PRESERVE" comment to protect some nodes from removal.
Change-Id: I63038293cf66fb81fa30ab0d14f847d9c4bfea4d
This commit is contained in:
parent
b63adf43db
commit
d203d965a2
2 changed files with 33 additions and 2 deletions
|
@ -61,6 +61,7 @@ import org.eclipse.cdt.internal.ui.refactoring.changes.CCompositeChange;
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper;
|
import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper;
|
||||||
|
|
||||||
public class RemoveFunctionBodiesRefactoring extends CRefactoring {
|
public class RemoveFunctionBodiesRefactoring extends CRefactoring {
|
||||||
|
private static final String PROTECTION_TOKEN = "PRESERVE";
|
||||||
private INodeFactory nodeFactory;
|
private INodeFactory nodeFactory;
|
||||||
private final DefaultCodeFormatterOptions formattingOptions;
|
private final DefaultCodeFormatterOptions formattingOptions;
|
||||||
|
|
||||||
|
@ -124,7 +125,7 @@ public class RemoveFunctionBodiesRefactoring extends CRefactoring {
|
||||||
CTextFileChange fileChange = new CTextFileChange(tu.getElementName(), tu);
|
CTextFileChange fileChange = new CTextFileChange(tu.getElementName(), tu);
|
||||||
fileChange.setEdit(new MultiTextEdit());
|
fileChange.setEdit(new MultiTextEdit());
|
||||||
for (IASTFunctionDefinition definition : finder.functionDefinitions) {
|
for (IASTFunctionDefinition definition : finder.functionDefinitions) {
|
||||||
if (!SelectionHelper.isNodeInsideRegion(definition, region))
|
if (!SelectionHelper.isNodeInsideRegion(definition, region) || containsProtectionToken(definition, code))
|
||||||
continue;
|
continue;
|
||||||
IASTStatement body = definition.getBody();
|
IASTStatement body = definition.getBody();
|
||||||
IASTName name = definition.getDeclarator().getName();
|
IASTName name = definition.getDeclarator().getName();
|
||||||
|
@ -170,6 +171,19 @@ public class RemoveFunctionBodiesRefactoring extends CRefactoring {
|
||||||
return change;
|
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 static int skipWhitespaceBefore(int offset, String text) {
|
private static int skipWhitespaceBefore(int offset, String text) {
|
||||||
while (--offset >= 0) {
|
while (--offset >= 0) {
|
||||||
char c = text.charAt(offset);
|
char c = text.charAt(offset);
|
||||||
|
|
|
@ -98,6 +98,7 @@ import org.eclipse.cdt.internal.ui.refactoring.changes.CCompositeChange;
|
||||||
import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper;
|
import org.eclipse.cdt.internal.ui.refactoring.utils.SelectionHelper;
|
||||||
|
|
||||||
public class RemoveUnusedDeclarationsRefactoring extends CRefactoring {
|
public class RemoveUnusedDeclarationsRefactoring extends CRefactoring {
|
||||||
|
private static final String PROTECTION_TOKEN = "PRESERVE";
|
||||||
private static final IASTName UNUSED_NAME = new CPPASTName(null);
|
private static final IASTName UNUSED_NAME = new CPPASTName(null);
|
||||||
private static final ProblemFinder problemFinder = new ProblemFinder();
|
private static final ProblemFinder problemFinder = new ProblemFinder();
|
||||||
|
|
||||||
|
@ -160,11 +161,14 @@ public class RemoveUnusedDeclarationsRefactoring extends CRefactoring {
|
||||||
// it is too slow for the gigantic changes this refactoring has to deal with.
|
// it is too slow for the gigantic changes this refactoring has to deal with.
|
||||||
NavigableSet<IASTName> names = NameCollector.getContainedNames(ast);
|
NavigableSet<IASTName> names = NameCollector.getContainedNames(ast);
|
||||||
|
|
||||||
|
String code = ast.getRawSignature();
|
||||||
|
|
||||||
SortedNodeSet<IASTNode> nodesToDelete = new SortedNodeSet<>();
|
SortedNodeSet<IASTNode> nodesToDelete = new SortedNodeSet<>();
|
||||||
IASTPreprocessorMacroExpansion[] macroExpansions = ast.getMacroExpansions();
|
IASTPreprocessorMacroExpansion[] macroExpansions = ast.getMacroExpansions();
|
||||||
for (IASTPreprocessorMacroExpansion macroExpansion : macroExpansions) {
|
for (IASTPreprocessorMacroExpansion macroExpansion : macroExpansions) {
|
||||||
IASTName name = macroExpansion.getMacroReference();
|
IASTName name = macroExpansion.getMacroReference();
|
||||||
if (SelectionHelper.isNodeInsideRegion(name, region)
|
if (SelectionHelper.isNodeInsideRegion(name, region)
|
||||||
|
&& !containsProtectionToken(name, code)
|
||||||
&& macroExpansion.getMacroDefinition().getExpansion().isEmpty()) {
|
&& macroExpansion.getMacroDefinition().getExpansion().isEmpty()) {
|
||||||
nodesToDelete.add(macroExpansion);
|
nodesToDelete.add(macroExpansion);
|
||||||
}
|
}
|
||||||
|
@ -177,6 +181,7 @@ public class RemoveUnusedDeclarationsRefactoring extends CRefactoring {
|
||||||
for (int i = declarations.size(); --i >= 0;) {
|
for (int i = declarations.size(); --i >= 0;) {
|
||||||
IASTDeclaration declaration = declarations.get(i);
|
IASTDeclaration declaration = declarations.get(i);
|
||||||
if (SelectionHelper.isNodeInsideRegion(declaration, region)
|
if (SelectionHelper.isNodeInsideRegion(declaration, region)
|
||||||
|
&& !containsProtectionToken(declaration, code)
|
||||||
&& !problemFinder.containsProblemBinding(declaration)
|
&& !problemFinder.containsProblemBinding(declaration)
|
||||||
&& !isPossiblyUsed(declaration, names, nodesToDelete)) {
|
&& !isPossiblyUsed(declaration, names, nodesToDelete)) {
|
||||||
nodesToDelete.add(declaration);
|
nodesToDelete.add(declaration);
|
||||||
|
@ -184,7 +189,6 @@ public class RemoveUnusedDeclarationsRefactoring extends CRefactoring {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String code = ast.getRawSignature();
|
|
||||||
CTextFileChange fileChange = new CTextFileChange(tu.getElementName(), tu);
|
CTextFileChange fileChange = new CTextFileChange(tu.getElementName(), tu);
|
||||||
fileChange.setEdit(new MultiTextEdit());
|
fileChange.setEdit(new MultiTextEdit());
|
||||||
|
|
||||||
|
@ -215,6 +219,19 @@ public class RemoveUnusedDeclarationsRefactoring extends CRefactoring {
|
||||||
return change;
|
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) {
|
private boolean containsAncestor(Collection<IASTNode> nodes, IASTNode node) {
|
||||||
while ((node = node.getParent()) != null) {
|
while ((node = node.getParent()) != null) {
|
||||||
if (nodes.contains(node))
|
if (nodes.contains(node))
|
||||||
|
|
Loading…
Add table
Reference in a new issue