1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Cosmetics.

This commit is contained in:
Sergey Prigogin 2011-11-01 15:45:38 -07:00
parent d8d335dbf8
commit 0177053e2b
9 changed files with 107 additions and 158 deletions

View file

@ -26,7 +26,6 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
* @author Emanuel Graf * @author Emanuel Graf
*/ */
public class ASTWriter { public class ASTWriter {
private ChangeGeneratorWriterVisitor transformationVisitor;
private ASTModificationStore modificationStore = new ASTModificationStore(); private ASTModificationStore modificationStore = new ASTModificationStore();
private String givenIndentation = ""; //$NON-NLS-1$ private String givenIndentation = ""; //$NON-NLS-1$
@ -76,14 +75,12 @@ public class ASTWriter {
*/ */
public String write(IASTNode rootNode, String fileScope, NodeCommentMap commentMap) public String write(IASTNode rootNode, String fileScope, NodeCommentMap commentMap)
throws ProblemRuntimeException { throws ProblemRuntimeException {
transformationVisitor = new ChangeGeneratorWriterVisitor(modificationStore, givenIndentation, ChangeGeneratorWriterVisitor transformationVisitor = new ChangeGeneratorWriterVisitor(
fileScope, commentMap); modificationStore, givenIndentation, fileScope, commentMap);
if (rootNode != null) { if (rootNode != null) {
rootNode.accept(transformationVisitor); rootNode.accept(transformationVisitor);
} }
String str = transformationVisitor.toString(); return transformationVisitor.toString();
transformationVisitor.cleanCache();
return str;
} }
public void setModificationStore(ASTModificationStore modificationStore) { public void setModificationStore(ASTModificationStore modificationStore) {

View file

@ -224,7 +224,6 @@ public class DeclSpecWriter extends NodeWriter {
} }
} }
scribe.print('}'); scribe.print('}');
} }
private void writeEnumerator(IASTEnumerator enumerator) { private void writeEnumerator(IASTEnumerator enumerator) {

View file

@ -32,19 +32,14 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap; import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
/** /**
*
* Generates source code of declarator nodes. The actual string operations are delegated * Generates source code of declarator nodes. The actual string operations are delegated
* to the <code>Scribe</code> class. * to the {@link Scribe} class.
* *
* @see Scribe
* @see IASTDeclarator * @see IASTDeclarator
* @author Emanuel Graf IFS * @author Emanuel Graf IFS
*
*/ */
public class DeclaratorWriter extends NodeWriter { public class DeclaratorWriter extends NodeWriter {
private static final String AMPERSAND_SPACE = "& "; //$NON-NLS-1$ private static final String AMPERSAND_SPACE = "& "; //$NON-NLS-1$
private static final String AMPERSAND__AMPERSAND_SPACE = "&& "; //$NON-NLS-1$ private static final String AMPERSAND__AMPERSAND_SPACE = "&& "; //$NON-NLS-1$
private static final String STAR_SPACE = "* "; //$NON-NLS-1$ private static final String STAR_SPACE = "* "; //$NON-NLS-1$
@ -186,7 +181,6 @@ public class DeclaratorWriter extends NodeWriter {
scribe.print('*'); scribe.print('*');
} }
if (operator.isConst()) { if (operator.isConst()) {
scribe.printStringSpace(CONST); scribe.printStringSpace(CONST);
} }
@ -267,12 +261,10 @@ public class DeclaratorWriter extends NodeWriter {
scribe.print(')'); scribe.print(')');
scribe.newLine(); scribe.newLine();
writeKnRParameterDeclarations(knrFunct, knrFunct.getParameterDeclarations()); writeKnRParameterDeclarations(knrFunct, knrFunct.getParameterDeclarations());
} }
protected void writeKnRParameterDeclarations( protected void writeKnRParameterDeclarations(ICASTKnRFunctionDeclarator knrFunct,
ICASTKnRFunctionDeclarator knrFunct, IASTDeclaration[] knrDeclarations) { IASTDeclaration[] knrDeclarations) {
for (int i = 0; i < knrDeclarations.length; ++i) { for (int i = 0; i < knrDeclarations.length; ++i) {
scribe.noNewLines(); scribe.noNewLines();
knrDeclarations[i].accept(visitor); knrDeclarations[i].accept(visitor);

View file

@ -21,15 +21,12 @@ import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap; import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
/** /**
*
* Base class for node writers. This class contains methods and string constants * Base class for node writers. This class contains methods and string constants
* used by multiple node writers. * used by multiple node writers.
* *
* @author Emanuel Graf IFS * @author Emanuel Graf IFS
*
*/ */
public class NodeWriter { public class NodeWriter {
protected Scribe scribe; protected Scribe scribe;
protected ASTVisitor visitor; protected ASTVisitor visitor;
protected NodeCommentMap commentMap; protected NodeCommentMap commentMap;
@ -86,29 +83,11 @@ public class NodeWriter {
} }
} }
protected void writeTrailingComments(IASTNode node) { protected void writeTrailingComments(IASTNode node) {
//default write newLine // Default is to write a new line after trailing comments.
writeTrailingComments(node, true); writeTrailingComments(node, true);
} }
protected boolean hasTrailingComments(IASTNode node){
if(getTrailingComments(node).size()>0) {
return true;
}
return false;
}
private ArrayList<IASTComment> getTrailingComments(IASTNode node) {
ArrayList<IASTComment> trailingComments = commentMap.getTrailingCommentsForNode(node);
IASTNodeLocation[] locs = node.getNodeLocations();
if (locs != null && locs.length > 0 && locs[0] instanceof IASTCopyLocation) {
IASTCopyLocation loc = (IASTCopyLocation) locs[0];
trailingComments.addAll(commentMap.getTrailingCommentsForNode(loc.getOriginalNode()));
}
return trailingComments;
}
protected void writeTrailingComments(IASTNode node, boolean newLine) { protected void writeTrailingComments(IASTNode node, boolean newLine) {
for (IASTComment comment : getTrailingComments(node)) { for (IASTComment comment : getTrailingComments(node)) {
scribe.printSpace(); scribe.printSpace();
@ -119,11 +98,22 @@ public class NodeWriter {
} }
} }
protected boolean hasFreestandingComments(IASTNode node){ protected boolean hasTrailingComments(IASTNode node) {
if(getFreestandingComments(node).size()>0) { return !getTrailingComments(node).isEmpty();
return true;
} }
return false;
private ArrayList<IASTComment> getTrailingComments(IASTNode node) {
ArrayList<IASTComment> trailingComments = commentMap.getTrailingCommentsForNode(node);
IASTNodeLocation[] locs = node.getNodeLocations();
if (locs != null && locs.length > 0 && locs[0] instanceof IASTCopyLocation) {
IASTCopyLocation loc = (IASTCopyLocation) locs[0];
trailingComments.addAll(commentMap.getTrailingCommentsForNode(loc.getOriginalNode()));
}
return trailingComments;
}
protected boolean hasFreestandingComments(IASTNode node) {
return !getFreestandingComments(node).isEmpty();
} }
private ArrayList<IASTComment> getFreestandingComments(IASTNode node) { private ArrayList<IASTComment> getFreestandingComments(IASTNode node) {

View file

@ -46,17 +46,14 @@ import org.eclipse.cdt.internal.core.dom.rewrite.util.FileHelper;
import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFile;
/** /**
*
* Generates source code of statement nodes. The actual string operations are delegated * Generates source code of statement nodes. The actual string operations are delegated
* to the <code>Scribe</code> class. * to the <code>Scribe</code> class.
* *
* @see Scribe * @see Scribe
* @see IASTStatement * @see IASTStatement
* @author Emanuel Graf IFS * @author Emanuel Graf IFS
*
*/ */
public class StatementWriter extends NodeWriter { public class StatementWriter extends NodeWriter {
private static final String DEFAULT = "default:"; //$NON-NLS-1$ private static final String DEFAULT = "default:"; //$NON-NLS-1$
private static final String CASE = "case "; //$NON-NLS-1$ private static final String CASE = "case "; //$NON-NLS-1$
private static final String WHILE = "while("; //$NON-NLS-1$ private static final String WHILE = "while("; //$NON-NLS-1$
@ -85,7 +82,7 @@ public class StatementWriter extends NodeWriter{
/** /**
* *
* @param statement * @param statement
* @param newLine if true print a newline if statment usually have one. * @param newLine if true print a newline if statement usually have one.
* @return {@link ASTVisitor#PROCESS_SKIP} * @return {@link ASTVisitor#PROCESS_SKIP}
*/ */
protected int writeStatement(IASTStatement statement, boolean newLine) { protected int writeStatement(IASTStatement statement, boolean newLine) {
@ -158,12 +155,9 @@ public class StatementWriter extends NodeWriter{
if (hasTrailingComments(statement)) { if (hasTrailingComments(statement)) {
writeTrailingComments(statement, newLine); writeTrailingComments(statement, newLine);
} } else if (newLine) {
else{
if(newLine){
scribe.newLine(); scribe.newLine();
} }
}
return ASTVisitor.PROCESS_SKIP; return ASTVisitor.PROCESS_SKIP;
} }
@ -192,7 +186,6 @@ public class StatementWriter extends NodeWriter{
} else { } else {
cppConditionDeclaration.accept(visitor); cppConditionDeclaration.accept(visitor);
} }
} else { } else {
if (forStatment.getConditionExpression() != null) { if (forStatment.getConditionExpression() != null) {
forStatment.getConditionExpression().accept(visitor); forStatment.getConditionExpression().accept(visitor);
@ -238,7 +231,7 @@ public class StatementWriter extends NodeWriter{
scribe.newLines(); scribe.newLines();
nextCompoundNoNewLine(); nextCompoundNoNewLine();
IASTStatement elseClause = ifStatement.getElseClause(); IASTStatement elseClause = ifStatement.getElseClause();
writeBodyStatement(ifStatement.getThenClause(), elseClause != null ? true : false); writeBodyStatement(ifStatement.getThenClause(), elseClause != null);
if (elseClause != null) { if (elseClause != null) {
scribe.print(ELSE); scribe.print(ELSE);
@ -247,8 +240,7 @@ public class StatementWriter extends NodeWriter{
} }
} }
protected void writeDeclarationWithoutSemicolon( protected void writeDeclarationWithoutSemicolon(IASTDeclaration declaration) {
IASTDeclaration declaration) {
declWriter.writeDeclaration(declaration, false); declWriter.writeDeclaration(declaration, false);
} }
@ -432,7 +424,6 @@ public class StatementWriter extends NodeWriter{
/** /**
* Write no new Line after the next Compound-Statement * Write no new Line after the next Compound-Statement
*
*/ */
protected void nextCompoundNoNewLine() { protected void nextCompoundNoNewLine() {
compoundNoNewLine = true; compoundNoNewLine = true;
@ -441,7 +432,6 @@ public class StatementWriter extends NodeWriter{
/** /**
* Indent one time more at the end (before the closing Brackets) * Indent one time more at the end (before the closing Brackets)
* of a Compound-Statement * of a Compound-Statement
*
*/ */
protected void nextCompoundIndentationLevelOneMore() { protected void nextCompoundIndentationLevelOneMore() {
decrementIndentationLevelOneMore = true; decrementIndentationLevelOneMore = true;

View file

@ -28,14 +28,12 @@ import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ContainerNode;
import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap; import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
public class ASTModificationHelper { public class ASTModificationHelper {
private final ModificationScopeStack modificationStore; private final ModificationScopeStack modificationStore;
public ASTModificationHelper(ModificationScopeStack stack) { public ASTModificationHelper(ModificationScopeStack stack) {
this.modificationStore = stack; this.modificationStore = stack;
} }
public <T extends IASTNode> T[] createModifiedChildArray(IASTNode parent, T[] unmodifiedChildren, Class<T> clazz, NodeCommentMap commentMap) { public <T extends IASTNode> T[] createModifiedChildArray(IASTNode parent, T[] unmodifiedChildren, Class<T> clazz, NodeCommentMap commentMap) {
ArrayList<T> modifiedChildren = new ArrayList<T>(Arrays.asList(unmodifiedChildren)); ArrayList<T> modifiedChildren = new ArrayList<T>(Arrays.asList(unmodifiedChildren));
@ -151,21 +149,16 @@ public class ASTModificationHelper {
return null; return null;
} }
public List<ASTModification> modificationsForNode(IASTNode targetNode) {
public List<ASTModification> modificationsForNode(
IASTNode targetNode) {
List<ASTModification> modificationsForNode; List<ASTModification> modificationsForNode;
if (modificationStore.getModifiedNodes().contains(targetNode)) { if (modificationStore.getModifiedNodes().contains(targetNode)) {
modificationsForNode = modificationStore.getModificationsForNode(targetNode); modificationsForNode = modificationStore.getModificationsForNode(targetNode);
} } else {
else{
modificationsForNode = Collections.emptyList(); modificationsForNode = Collections.emptyList();
} }
return modificationsForNode; return modificationsForNode;
} }
public IASTInitializer getInitializer(IASTDeclarator decl) { public IASTInitializer getInitializer(IASTDeclarator decl) {
IASTInitializer initializer = decl.getInitializer(); IASTInitializer initializer = decl.getInitializer();
@ -179,8 +172,6 @@ public class ASTModificationHelper {
return null; return null;
} }
throw new UnhandledASTModificationException(childModification); throw new UnhandledASTModificationException(childModification);
case INSERT_BEFORE: case INSERT_BEFORE:
throw new UnhandledASTModificationException(childModification); throw new UnhandledASTModificationException(childModification);
@ -188,9 +179,7 @@ public class ASTModificationHelper {
throw new UnhandledASTModificationException(childModification); throw new UnhandledASTModificationException(childModification);
} }
} }
} } else {
else
{
for (ASTModification parentModification : modificationsForNode(decl)) { for (ASTModification parentModification : modificationsForNode(decl)) {
if (parentModification.getKind() == ModificationKind.APPEND_CHILD) { if (parentModification.getKind() == ModificationKind.APPEND_CHILD) {
IASTNode newNode = parentModification.getNewNode(); IASTNode newNode = parentModification.getNewNode();
@ -203,7 +192,6 @@ public class ASTModificationHelper {
return initializer; return initializer;
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends IASTNode> T getNodeAfterReplacement(T replacedNode) { public <T extends IASTNode> T getNodeAfterReplacement(T replacedNode) {
List<ASTModification> modifications = modificationsForNode(replacedNode); List<ASTModification> modifications = modificationsForNode(replacedNode);
@ -212,8 +200,7 @@ public class ASTModificationHelper {
if (currentModification.getKind() == ModificationKind.REPLACE) { if (currentModification.getKind() == ModificationKind.REPLACE) {
return (T) currentModification.getNewNode(); return (T) currentModification.getNewNode();
} }
} } catch (ClassCastException e) {
catch(ClassCastException e){
throw new UnhandledASTModificationException(currentModification); throw new UnhandledASTModificationException(currentModification);
} }
} }

View file

@ -84,10 +84,8 @@ public class ChangeGeneratorWriterVisitor extends ASTWriterVisitor {
this.fileScope = fileScope; this.fileScope = fileScope;
this.shouldVisitTranslationUnit = true; this.shouldVisitTranslationUnit = true;
this.stack = new ModificationScopeStack(modificationStore); this.stack = new ModificationScopeStack(modificationStore);
declaratorWriter = new ModifiedASTDeclaratorWriter(scribe, this, declaratorWriter = new ModifiedASTDeclaratorWriter(scribe, this, stack, commentMap);
stack, commentMap); expWriter = new ModifiedASTExpressionWriter(scribe, this, macroHandler, stack, commentMap);
expWriter = new ModifiedASTExpressionWriter(scribe, this, macroHandler,
stack, commentMap);
statementWriter = new ModifiedASTStatementWriter(scribe, this, stack, commentMap); statementWriter = new ModifiedASTStatementWriter(scribe, this, stack, commentMap);
declSpecWriter = new ModifiedASTDeclSpecWriter(scribe, this, stack, commentMap); declSpecWriter = new ModifiedASTDeclSpecWriter(scribe, this, stack, commentMap);
declarationWriter = new ModifiedASTDeclarationWriter(scribe, this, stack, commentMap); declarationWriter = new ModifiedASTDeclarationWriter(scribe, this, stack, commentMap);

View file

@ -41,10 +41,6 @@ public class ExtractFunctionRefactoringTest extends RefactoringTest {
private boolean virtual; private boolean virtual;
private static int nr = 1; private static int nr = 1;
/**
* @param name
* @param files
*/
public ExtractFunctionRefactoringTest(String name, Collection<TestSourceFile> files) { public ExtractFunctionRefactoringTest(String name, Collection<TestSourceFile> files) {
super(name, files); super(name, files);
} }