Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey Prigogin2011-12-13 22:44:22 -0500
committerSergey Prigogin2011-12-13 22:46:10 -0500
commitc521604dd1dde08dfd55e8e08fae1de4ad6a2e21 (patch)
treec222b9837b561e15d15f9ffc8386058a2d515f31 /core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter
parent18ec28be0f857e0e6b6e7ee293b148f8dc64f21c (diff)
downloadorg.eclipse.cdt-c521604dd1dde08dfd55e8e08fae1de4ad6a2e21.tar.gz
org.eclipse.cdt-c521604dd1dde08dfd55e8e08fae1de4ad6a2e21.tar.xz
org.eclipse.cdt-c521604dd1dde08dfd55e8e08fae1de4ad6a2e21.zip
Bug 363244 - Generate Getters and Setters expands macro with typedef and
breaks formatting. The bug was fixed by minimizing areas of code affected by the refactoring changes. This minimization had a huge ripple effect.
Diffstat (limited to 'core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter')
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriter.java49
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java41
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclSpecWriter.java2
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java11
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclaratorWriter.java8
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/InitializerWriter.java4
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NodeWriter.java14
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/Scribe.java46
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/StatementWriter.java10
9 files changed, 98 insertions, 87 deletions
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriter.java
index 5b7229e5de..dc19453bfb 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriter.java
@@ -14,9 +14,13 @@ package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
import org.eclipse.cdt.core.dom.ast.IASTASMDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
+import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
+import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
import org.eclipse.cdt.core.dom.ast.IASTNode;
+import org.eclipse.cdt.core.dom.ast.IASTPreprocessorIncludeStatement;
+import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
@@ -34,7 +38,6 @@ import org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap;
*/
public class ASTWriter {
private ASTModificationStore modificationStore = new ASTModificationStore();
- private String givenIndentation = ""; //$NON-NLS-1$
/**
* Creates a <code>ASTWriter</code>.
@@ -44,16 +47,6 @@ public class ASTWriter {
}
/**
- * Creates a <code>ASTWriter</code> that indents the code.
- *
- * @param givenIndentation The indentation added to each line
- */
- public ASTWriter(String givenIndentation) {
- super();
- this.givenIndentation = givenIndentation;
- }
-
- /**
* Generates the source code representing this node.
*
* @param rootNode Node to write.
@@ -75,10 +68,9 @@ public class ASTWriter {
*
* @see ASTCommenter#getCommentedNodeMap(org.eclipse.cdt.core.dom.ast.IASTTranslationUnit)
*/
- public String write(IASTNode rootNode, NodeCommentMap commentMap)
- throws ProblemRuntimeException {
+ public String write(IASTNode rootNode, NodeCommentMap commentMap) throws ProblemRuntimeException {
ChangeGeneratorWriterVisitor writer = new ChangeGeneratorWriterVisitor(
- modificationStore, givenIndentation, null, commentMap);
+ modificationStore, null, commentMap);
if (rootNode != null) {
rootNode.accept(writer);
}
@@ -152,10 +144,35 @@ public class ASTWriter {
* @return <code>true</code> if the blank line between the nodes is needed.
*/
public static boolean requireBlankLineInBetween(IASTNode node1, IASTNode node2) {
- if (requiresTrailingBlankLine(node1))
+ if (node1 instanceof ICPPASTVisibilityLabel && node2 instanceof ICPPASTVisibilityLabel) {
+ return true;
+ }
+ if (suppressesTrailingBlankLine(node1)) {
+ return false;
+ }
+ if (node1 instanceof IASTPreprocessorIncludeStatement !=
+ node2 instanceof IASTPreprocessorIncludeStatement) {
+ return true;
+ }
+ if (isFunctionDeclaration(node1) != isFunctionDeclaration(node2)) {
+ return true;
+ }
+ if (requiresTrailingBlankLine(node1)) {
return true;
+ }
+
+ return requiresLeadingBlankLine(node2);
+ }
- return !suppressesTrailingBlankLine(node1) && requiresLeadingBlankLine(node2);
+ private static boolean isFunctionDeclaration(IASTNode node) {
+ if (!(node instanceof IASTSimpleDeclaration)) {
+ return false;
+ }
+ for (IASTDeclarator declarator : ((IASTSimpleDeclaration) node).getDeclarators()) {
+ if (declarator instanceof IASTFunctionDeclarator)
+ return true;
+ }
+ return false;
}
/**
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java
index 16f9325785..4ff5321d55 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/ASTWriterVisitor.java
@@ -13,7 +13,7 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
-import java.util.ArrayList;
+import java.util.List;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
@@ -29,6 +29,7 @@ import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTNodeLocation;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
+import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
@@ -62,28 +63,25 @@ public class ASTWriterVisitor extends ASTVisitor {
private boolean spaceNeededBeforeName;
{
- shouldVisitExpressions = true;
- shouldVisitStatements = true;
- shouldVisitNames = true;
+ shouldVisitArrayModifiers = true;
+ shouldVisitBaseSpecifiers = true;
shouldVisitDeclarations = true;
- shouldVisitDeclSpecifiers = true;
shouldVisitDeclarators = true;
- shouldVisitArrayModifiers= true;
+ shouldVisitDeclSpecifiers = true;
+ shouldVisitExpressions = true;
shouldVisitInitializers = true;
- shouldVisitBaseSpecifiers = true;
+ shouldVisitNames = true;
shouldVisitNamespaces = true;
- shouldVisitTemplateParameters = true;
shouldVisitParameterDeclarations = true;
+ shouldVisitPointerOperators = true;
+ shouldVisitStatements = true;
+ shouldVisitTemplateParameters = true;
shouldVisitTranslationUnit = true;
+ shouldVisitTypeIds = true;
}
public ASTWriterVisitor(NodeCommentMap commentMap) {
- this("", commentMap); //$NON-NLS-1$
- }
-
- public ASTWriterVisitor(String givenIndentation, NodeCommentMap commentMap) {
super();
- scribe.setGivenIndentation(givenIndentation);
init(commentMap);
this.commentMap = commentMap;
this.suppressLeadingBlankLine = true;
@@ -123,8 +121,8 @@ public class ASTWriterVisitor extends ASTVisitor {
}
}
- private ArrayList<IASTComment> getLeadingComments(IASTNode node) {
- ArrayList<IASTComment> leadingComments = commentMap.getLeadingCommentsForNode(node);
+ private List<IASTComment> getLeadingComments(IASTNode node) {
+ List<IASTComment> leadingComments = commentMap.getLeadingCommentsForNode(node);
IASTNodeLocation[] locs = node.getNodeLocations();
if (locs != null && locs.length > 0 && locs[0] instanceof IASTCopyLocation) {
IASTCopyLocation copyLoc = (IASTCopyLocation) locs[0];
@@ -240,6 +238,15 @@ public class ASTWriterVisitor extends ASTVisitor {
return ASTVisitor.PROCESS_SKIP;
}
+ @Override
+ public int visit(IASTPointerOperator pointerOperator) {
+ writeLeadingComments(pointerOperator);
+ if (!macroHandler.checkisMacroExpansionNode(pointerOperator)) {
+ declaratorWriter.writePointerOperator(pointerOperator);
+ }
+ return ASTVisitor.PROCESS_SKIP;
+ }
+
protected IASTName getParameterName(IASTDeclarator declarator) {
return declarator.getName();
}
@@ -303,6 +310,10 @@ public class ASTWriterVisitor extends ASTVisitor {
this.spaceNeededBeforeName = value;
}
+ public Scribe getScribe() {
+ return scribe;
+ }
+
public void newLine() {
scribe.newLine();
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclSpecWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclSpecWriter.java
index ba7fb2e862..66d25dd937 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclSpecWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclSpecWriter.java
@@ -267,7 +267,7 @@ public class DeclSpecWriter extends NodeWriter {
}
if (hasFreestandingComments(compDeclSpec)) {
- writeFreeStandingComments(compDeclSpec);
+ writeFreestandingComments(compDeclSpec);
}
scribe.decrementIndentationLevel();
scribe.print('}');
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java
index 41ed66fc64..29e21800a7 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclarationWriter.java
@@ -98,17 +98,12 @@ public class DeclarationWriter extends NodeWriter {
writeVisibilityLabel((ICPPASTVisibilityLabel) declaration);
}
- if (hasTrailingComments(declaration)) {
- writeTrailingComments(declaration, false);
- }
- if (addNewLine) {
- scribe.newLine();
- }
+ writeTrailingComments(declaration, addNewLine);
if (hasFreestandingComments(declaration)) {
if (declaration instanceof IASTFunctionDefinition) {
scribe.newLine();
}
- writeFreeStandingComments(declaration);
+ writeFreestandingComments(declaration);
}
}
@@ -186,7 +181,7 @@ public class DeclarationWriter extends NodeWriter {
scribe.newLine(2);
writeDeclarationsInNamespace(namespaceDefinition, namespaceDefinition.getDeclarations());
if (hasFreestandingComments(namespaceDefinition)) {
- writeFreeStandingComments(namespaceDefinition);
+ writeFreestandingComments(namespaceDefinition);
}
scribe.newLine();
scribe.print('}');
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclaratorWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclaratorWriter.java
index 4a9a8af385..7d3fdf05c1 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclaratorWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/DeclaratorWriter.java
@@ -62,9 +62,7 @@ public class DeclaratorWriter extends NodeWriter {
}
visitor.setSpaceNeededBeforeName(false);
- if (hasTrailingComments(declarator)) {
- writeTrailingComments(declarator, false);
- }
+ writeTrailingComments(declarator, false);
}
protected void writeDefaultDeclarator(IASTDeclarator declarator) {
@@ -81,7 +79,7 @@ public class DeclaratorWriter extends NodeWriter {
protected void writePointerOperators(IASTDeclarator declarator, IASTPointerOperator[] pointOps) {
for (IASTPointerOperator operator : pointOps) {
- writePointerOp(operator);
+ writePointerOperator(operator);
}
}
@@ -194,7 +192,7 @@ public class DeclaratorWriter extends NodeWriter {
}
}
- private void writePointerOp(IASTPointerOperator operator) {
+ public void writePointerOperator(IASTPointerOperator operator) {
if (operator instanceof IASTPointer) {
IASTPointer pointOp = (IASTPointer) operator;
writePointer(pointOp);
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/InitializerWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/InitializerWriter.java
index 7adc8fdce8..8b1da40bc3 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/InitializerWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/InitializerWriter.java
@@ -51,8 +51,8 @@ public class InitializerWriter extends NodeWriter{
} else if (initializer instanceof ICPPASTConstructorChainInitializer) {
writeConstructorChainInitializer((ICPPASTConstructorChainInitializer) initializer);
}
- if (hasTrailingComments(initializer))
- writeTrailingComments(initializer, false);
+
+ writeTrailingComments(initializer, false);
}
private void writeEqualsInitializer(IASTEqualsInitializer initializer) {
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NodeWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NodeWriter.java
index 627e8158a5..aa160a77bd 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NodeWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/NodeWriter.java
@@ -11,7 +11,7 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
-import java.util.ArrayList;
+import java.util.List;
import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTCopyLocation;
@@ -83,7 +83,7 @@ public class NodeWriter {
}
protected void writeTrailingComments(IASTNode node) {
- // Default is to write a new line after trailing comments.
+ // Default is to write a new line after the trailing comments.
writeTrailingComments(node, true);
}
@@ -106,8 +106,8 @@ public class NodeWriter {
return !getTrailingComments(node).isEmpty();
}
- private ArrayList<IASTComment> getTrailingComments(IASTNode node) {
- ArrayList<IASTComment> trailingComments = commentMap.getTrailingCommentsForNode(node);
+ private List<IASTComment> getTrailingComments(IASTNode node) {
+ List<IASTComment> trailingComments = commentMap.getTrailingCommentsForNode(node);
IASTNodeLocation[] locs = node.getNodeLocations();
if (locs != null && locs.length > 0 && locs[0] instanceof IASTCopyLocation) {
IASTCopyLocation loc = (IASTCopyLocation) locs[0];
@@ -120,8 +120,8 @@ public class NodeWriter {
return !getFreestandingComments(node).isEmpty();
}
- private ArrayList<IASTComment> getFreestandingComments(IASTNode node) {
- ArrayList<IASTComment> freestandingComments = commentMap.getFreestandingCommentsForNode(node);
+ private List<IASTComment> getFreestandingComments(IASTNode node) {
+ List<IASTComment> freestandingComments = commentMap.getFreestandingCommentsForNode(node);
IASTNodeLocation[] locs = node.getNodeLocations();
if (locs != null && locs.length > 0 && locs[0] instanceof IASTCopyLocation) {
IASTCopyLocation loc = (IASTCopyLocation) locs[0];
@@ -130,7 +130,7 @@ public class NodeWriter {
return freestandingComments;
}
- protected void writeFreeStandingComments(IASTNode node) {
+ protected void writeFreestandingComments(IASTNode node) {
for (IASTComment comment : getFreestandingComments(node)) {
scribe.print(comment.getComment());
scribe.newLine();
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/Scribe.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/Scribe.java
index 8bc32d8396..7f715f5e28 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/Scribe.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/Scribe.java
@@ -8,6 +8,7 @@
*
* Contributors:
* Institute for Software - initial API and implementation
+ * Sergey Prigogin (Google)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
@@ -19,42 +20,43 @@ package org.eclipse.cdt.internal.core.dom.rewrite.astwriter;
* @author Emanuel Graf IFS
*/
public class Scribe {
- private int indentationLevel = 0;
- // Any positive indentation size is good enough since the code is going to be formatted anyway.
- private int indentationSize = 4;
+ // Indentation is not necessary since the code is going to be formatted anyway.
+ // Preserved because some tests depend on it.
+ private static final int INDENTATION_SIZE = 4;
+ private final String newLine = System.getProperty("line.separator"); //$NON-NLS-1$
private StringBuilder buffer = new StringBuilder();
- private boolean isAtLineBeginning = true;
- private String newLine = System.getProperty("line.separator"); //$NON-NLS-1$
- private String givenIndentation;
+ private int indentationLevel = 0;
+ private boolean isAtBeginningOfLine = true;
private boolean skipLineBreaks;
private boolean skipSemicolons;
+ public String getLineSeparator() {
+ return newLine;
+ }
+
public void newLine() {
if (!skipLineBreaks) {
- isAtLineBeginning = true;
- buffer.append(getNewline());
+ isAtBeginningOfLine = true;
+ buffer.append(newLine);
}
}
+ public boolean isAtBeginningOfLine() {
+ return isAtBeginningOfLine;
+ }
+
private void indent() {
- if (givenIndentation != null) {
- buffer.append(givenIndentation);
- }
- printSpaces(indentationLevel * indentationSize);
+ printSpaces(indentationLevel * INDENTATION_SIZE);
}
private void indentIfNewLine() {
- if (isAtLineBeginning) {
- isAtLineBeginning = false;
+ if (isAtBeginningOfLine) {
+ isAtBeginningOfLine = false;
indent();
}
}
- private String getNewline() {
- return newLine;
- }
-
public void print(String code) {
indentIfNewLine();
buffer.append(code);
@@ -171,14 +173,6 @@ public class Scribe {
buffer.append(' ');
}
- public String getGivenIndentation() {
- return givenIndentation;
- }
-
- public void setGivenIndentation(String givenIndentation) {
- this.givenIndentation = givenIndentation;
- }
-
public void cleanCache() {
buffer = new StringBuilder();
}
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/StatementWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/StatementWriter.java
index ac8c6f21f3..fd0cf9a59a 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/StatementWriter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/StatementWriter.java
@@ -154,12 +154,8 @@ public class StatementWriter extends NodeWriter {
throw new ProblemRuntimeException((IASTProblemStatement)statement);
}
- if (hasTrailingComments(statement)) {
- writeTrailingComments(statement, newLine);
- } else if (newLine) {
- scribe.newLine();
- }
-
+ writeTrailingComments(statement, newLine);
+
return ASTVisitor.PROCESS_SKIP;
}
@@ -389,7 +385,7 @@ public class StatementWriter extends NodeWriter {
}
if (hasFreestandingComments(compoundStatement)) {
- writeFreeStandingComments(compoundStatement);
+ writeFreestandingComments(compoundStatement);
}
if (decrementIndentationLevelOneMore) {

Back to the top