Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey Prigogin2011-12-14 03:48:19 -0500
committerSergey Prigogin2011-12-14 03:48:19 -0500
commit8b3ce7c39a92825b1e2ad89dd96f7f9ed41d5f23 (patch)
tree90b707241550b71c577745f2e0ffe09c5c3328b6 /core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler
parentb8ac132da4c357d47db43dfa365f8a3cd2e1c945 (diff)
downloadorg.eclipse.cdt-8b3ce7c39a92825b1e2ad89dd96f7f9ed41d5f23.tar.gz
org.eclipse.cdt-8b3ce7c39a92825b1e2ad89dd96f7f9ed41d5f23.tar.xz
org.eclipse.cdt-8b3ce7c39a92825b1e2ad89dd96f7f9ed41d5f23.zip
Code cleanup.
Diffstat (limited to 'core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler')
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java11
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommenter.java95
2 files changed, 50 insertions, 56 deletions
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java
index d6defc940b..a7d748a333 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java
@@ -23,6 +23,7 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
+import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
@@ -37,7 +38,6 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
-import org.eclipse.cdt.internal.core.dom.rewrite.util.OffsetHelper;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
@@ -215,7 +215,7 @@ public class ASTCommenter {
for (IASTPreprocessorStatement statement : preprocessorStatements) {
if (isInWorkspace(statement)) {
String fileName = statement.getFileLocation().getFileName();
- treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement), fileName);
+ treeOfPreProcessorLines.put(statement.getFileLocation().getStartingLineNumber(), fileName);
ArrayList<Integer> offsetList = ppOffsetForFiles.get(fileName);
if (offsetList == null) {
offsetList = new ArrayList<Integer>();
@@ -227,8 +227,9 @@ public class ASTCommenter {
ArrayList<IASTComment> commentsInCode = new ArrayList<IASTComment>();
for (IASTComment comment : comments) {
- int comStartLineNumber = OffsetHelper.getStartingLineNumber(comment);
- String fileName = comment.getFileLocation().getFileName();
+ IASTFileLocation commentFileLocation = comment.getFileLocation();
+ int comStartLineNumber = commentFileLocation.getStartingLineNumber();
+ String fileName = commentFileLocation.getFileName();
if (treeOfPreProcessorLines.containsKey(comStartLineNumber)
&& treeOfPreProcessorLines.get(comStartLineNumber).equals(fileName)) {
continue;
@@ -248,7 +249,7 @@ public class ASTCommenter {
return false;
}
- if (comment.getTranslationUnit()==null || comment.getTranslationUnit().getDeclarations().length < 1) {
+ if (comment.getTranslationUnit() == null || comment.getTranslationUnit().getDeclarations().length < 1) {
return true;
}
IASTDeclaration decl = comment.getTranslationUnit().getDeclarations()[0];
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommenter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommenter.java
index 7543052896..8938c60ff2 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommenter.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommenter.java
@@ -8,19 +8,20 @@
*
* Contributors:
* Institute for Software - initial API and implementation
+ * Sergey Prigogin (Google)
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;
-import java.io.InputStream;
import java.util.Vector;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
+import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
-import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
+import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarationStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTExplicitTemplateInstantiation;
@@ -32,11 +33,6 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLinkageSpecification;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSwitchStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTemplateDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTWhileStatement;
-import org.eclipse.cdt.internal.core.dom.rewrite.util.OffsetHelper;
-import org.eclipse.cdt.internal.core.resources.ResourceLookup;
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.Path;
/**
* The NodeCommenter contains all the logic that is needed for the ASTCommentVisitor to assign
@@ -96,10 +92,10 @@ public class NodeCommenter {
return false;
}
- int nodeLineNumber = OffsetHelper.getEndingLineNumber(node);
- int commentLineNumber= OffsetHelper.getStartingLineNumber(comment);
+ int nodeLineNumber = getEndingLineNumber(node);
+ int commentLineNumber= getStartingLineNumber(comment);
- if (OffsetHelper.getNodeEndPoint(com) <= OffsetHelper.getNodeOffset(node)) {
+ if (getNodeEndPoint(com) <= getNodeOffset(node)) {
addLeadingCommentToMap(node, comment);
return true;
} else if (isTrailing(node, com, nodeLineNumber, commentLineNumber)) {
@@ -116,7 +112,7 @@ public class NodeCommenter {
// MacroExpansions have no FileLocation
return false;
}
- if (OffsetHelper.getNodeEndPoint(com) <= OffsetHelper.getNodeEndPoint(node)) {
+ if (getNodeEndPoint(com) <= getNodeEndPoint(node)) {
addFreestandingCommentToMap(node, comment);
return true;
}
@@ -139,47 +135,23 @@ public class NodeCommenter {
}
private boolean isTrailing(ASTNode node, ASTNode com, int nodeLineNumber, int commentLineNumber) {
- if (nodeLineNumber == commentLineNumber
- && OffsetHelper.getNodeOffset(com) >= OffsetHelper.getNodeEndPoint(node)
- && canNotBeAddedToParent(node,com)
- && !mustBeAddToSubnodes(node)) {
- if (OffsetHelper.getNodeOffset(com) < OffsetHelper.getNodeEndPoint(node) + 2) {
- return true;
- }
- IPath path = new Path(node.getContainingFilename());
- IFile file = ResourceLookup.selectFileForLocation(path, null); // NPE thrown below, like original behavior
-
- //XXX HSR Guido: Possible Performance Issue (File access)
- try {
- InputStream is = file.getContents();
-
- int length = OffsetHelper.getNodeOffset(com) - OffsetHelper.getNodeEndPoint(node);
- byte[] b = new byte[length];
-
- long count = is.skip(OffsetHelper.getEndOffsetWithoutComments(node));
- if (count < OffsetHelper.getEndOffsetWithoutComments(node)) {
- return false;
- }
- if (is.read(b, 0, length) == -1) {
- return false;
- }
-
- for (byte bb : b) {
- if (!Character.isWhitespace(bb)) {
- is.close();
- return false;
- }
- }
- is.close();
- return true;
- } catch (Exception e) {
+ if (nodeLineNumber != commentLineNumber ||
+ getNodeOffset(com) < getNodeEndPoint(node) ||
+ !canNotBeAddedToParent(node, com) ||
+ mustBeAddedToSubnodes(node)) {
+ return false;
+ }
+ if (getNodeOffset(com) < getNodeEndPoint(node) + 2) {
+ return true;
+ }
+ String code = node.getTranslationUnit().getRawSignature();
+ int commentOffset = getNodeOffset(com) - getNodeEndPoint(node) + getNodeEndOffset(node);
+ for (int offset = getNodeEndOffset(node); offset < commentOffset; offset++) {
+ if (!Character.isWhitespace(code.charAt(offset)))
return false;
- }
}
-
- return false;
+ return true;
}
-
private boolean canNotBeAddedToParent(ASTNode node, ASTNode com) {
ASTNode parent = (ASTNode) node.getParent();
@@ -194,10 +166,10 @@ public class NodeCommenter {
} else if (parent instanceof ICPPASTBaseSpecifier) {
parent = (ASTNode) parent.getParent();
}
- return !(OffsetHelper.getNodeOffset(com) >= OffsetHelper.getNodeEndPoint(parent));
+ return getNodeOffset(com) < getNodeEndPoint(parent);
}
- private boolean mustBeAddToSubnodes(ASTNode node) {
+ private boolean mustBeAddedToSubnodes(ASTNode node) {
return hasNodeSameEndingAsSubnode(node);
}
@@ -276,4 +248,25 @@ public class NodeCommenter {
}
return !node.getFileLocation().getFileName().equals(comment.getFileLocation().getFileName());
}
+
+ private static int getNodeEndOffset(IASTNode node) {
+ IASTFileLocation fileLocation = node.getFileLocation();
+ return fileLocation.getNodeOffset() + fileLocation.getNodeLength();
+ }
+
+ private static int getNodeOffset(ASTNode node) {
+ return node.getOffset();
+ }
+
+ private static int getNodeEndPoint(ASTNode node) {
+ return node.getOffset() + node.getLength();
+ }
+
+ private static int getStartingLineNumber(IASTNode node) {
+ return node.getFileLocation().getStartingLineNumber();
+ }
+
+ private static int getEndingLineNumber(IASTNode node) {
+ return node.getFileLocation().getEndingLineNumber();
+ }
}

Back to the top