Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukas Felber2014-02-10 10:28:28 -0500
committerThomas Corbat2014-02-26 10:16:13 -0500
commitc558463abee55727d9c47c4e776899af89887d95 (patch)
treede3c89866ab6694ae19e18fce22c708b58619c1d /core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler
parent8000c7fe6120f2efbd3780f9b330918193fd4c22 (diff)
downloadorg.eclipse.cdt-c558463abee55727d9c47c4e776899af89887d95.tar.gz
org.eclipse.cdt-c558463abee55727d9c47c4e776899af89887d95.tar.xz
org.eclipse.cdt-c558463abee55727d9c47c4e776899af89887d95.zip
Bug 427002. [ast rewrite] Comments originating from other translation
units are now automatically taken into consideration when rewriting. Change-Id: If535ab6cdd1ec293a0d95a6f726d848f6348f0ed Signed-off-by: Lukas Felber <l.felber@gmx.ch> Reviewed-on: https://git.eclipse.org/r/21761 Reviewed-by: Thomas Corbat <tcorbat@hsr.ch> IP-Clean: Thomas Corbat <tcorbat@hsr.ch> Tested-by: Thomas Corbat <tcorbat@hsr.ch>
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.java19
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommentMap.java17
2 files changed, 35 insertions, 1 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 8fc605eee4..9fd70e52f6 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
@@ -179,6 +179,23 @@ public class ASTCommenter {
if (ast == null) {
return commentMap;
}
+ addCommentsToMap(ast, commentMap);
+ return commentMap;
+ }
+
+ /**
+ * Adds all comments given in {@code ast} to the {@code commentMap}. Calling this twice will have no
+ * effect.
+ *
+ * @param ast
+ * the AST which contains the comments to add
+ * @param commentMap
+ * the comment map to which the comments are added to
+ */
+ public static void addCommentsToMap(IASTTranslationUnit ast, NodeCommentMap commentMap) {
+ if (ast == null || commentMap.isASTCovered(ast)) {
+ return;
+ }
IASTComment[] commentsArray = ast.getComments();
List<IASTComment> comments = new ArrayList<IASTComment>(commentsArray.length);
for (IASTComment comment : commentsArray) {
@@ -189,8 +206,8 @@ public class ASTCommenter {
assignPreprocessorComments(commentMap, comments, ast);
CommentHandler commentHandler = new CommentHandler(comments);
ASTCommenterVisitor commenter = new ASTCommenterVisitor(commentHandler, commentMap);
+ commentMap.setASTCovered(ast);
ast.accept(commenter);
- return commentMap;
}
private static boolean isCommentDirectlyBeforePreprocessorStatement(IASTComment comment,
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommentMap.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommentMap.java
index 74303f4def..63a9c47114 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommentMap.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommentMap.java
@@ -19,6 +19,7 @@ import java.util.Map;
import org.eclipse.cdt.core.dom.ast.IASTComment;
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.internal.core.dom.rewrite.util.ASTNodes;
/**
@@ -32,6 +33,7 @@ public class NodeCommentMap {
protected final Map<IASTNode, List<IASTComment>> leadingMap = new HashMap<IASTNode, List<IASTComment>>();
protected final Map<IASTNode, List<IASTComment>> trailingMap = new HashMap<IASTNode, List<IASTComment>>();
protected final Map<IASTNode, List<IASTComment>> freestandingMap = new HashMap<IASTNode, List<IASTComment>>();
+ protected final List<IASTTranslationUnit> coveredUnits = new ArrayList<IASTTranslationUnit>();
/**
* Add a comment to the map with the trailing comments.
@@ -188,4 +190,19 @@ public class NodeCommentMap {
}
return endOffset;
}
+
+ /**
+ * Makes this comment map aware that comments of the given {@code ast} are already contained in the map.
+ * This can be used to make sure no-one accidentally tries to re-add already contained comments.
+ */
+ public void setASTCovered(IASTTranslationUnit ast) {
+ coveredUnits.add(ast);
+ }
+
+ /**
+ * Checks whether comments of the {@code ast} are already present in the map.
+ */
+ public boolean isASTCovered(IASTTranslationUnit ast) {
+ return coveredUnits.contains(ast);
+ }
}

Back to the top