Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Schorn2008-05-23 10:26:46 -0400
committerMarkus Schorn2008-05-23 10:26:46 -0400
commitf44c51e7d3da255ac9628890d10a749600e14413 (patch)
treef82d8b13857271b2f3046da80330c02eaf7e693b
parent291094d65d60d1e90b0cff6c5155db1386f21cd3 (diff)
downloadorg.eclipse.cdt-f44c51e7d3da255ac9628890d10a749600e14413.tar.gz
org.eclipse.cdt-f44c51e7d3da255ac9628890d10a749600e14413.tar.xz
org.eclipse.cdt-f44c51e7d3da255ac9628890d10a749600e14413.zip
Fixes comment handling with preprocessor nodes by Emanuel Graf, bug 233438.
-rw-r--r--core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/CommentHandlingTest.java24
-rw-r--r--core/org.eclipse.cdt.core.tests/resources/rewrite/CommentHandlingTestSource.rts44
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java55
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenter.java10
-rw-r--r--core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/NodeCommentMap.java15
-rw-r--r--core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractConstant.rts81
-rw-r--r--core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts3
7 files changed, 208 insertions, 24 deletions
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/CommentHandlingTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/CommentHandlingTest.java
index b84f8c6e58..77c4987218 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/CommentHandlingTest.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/comenthandler/CommentHandlingTest.java
@@ -87,19 +87,19 @@ public class CommentHandlingTest extends RewriteBaseTest {
@Override
protected void runTest() throws Throwable {
- if (fileMap.size() > 1) {
- fail("To many files for CommentHandlingTest"); //$NON-NLS-1$
- } else if (fileMap.size() == 0) {
+ if (fileMap.size() == 0) {
fail("No file for testing"); //$NON-NLS-1$
}
-
- TestSourceFile file = fileMap.values().iterator().next();
- NodeCommentMap nodeMap = ASTCommenter.getCommentedNodeMap(getUnit());
-
- StringBuilder expectedResultBuilder = buildExpectedResult(file);
- StringBuilder actualResultBuilder = buildActualResult(nodeMap);
- assertEquals(expectedResultBuilder.toString(), actualResultBuilder.toString());
+ for(String fileName : fileMap.keySet()) {
+ TestSourceFile file = fileMap.get(fileName);
+ NodeCommentMap nodeMap = ASTCommenter.getCommentedNodeMap(getUnit(fileName));
+
+ StringBuilder expectedResultBuilder = buildExpectedResult(file);
+ StringBuilder actualResultBuilder = buildActualResult(nodeMap);
+
+ assertEquals(expectedResultBuilder.toString(), actualResultBuilder.toString());
+ }
}
private StringBuilder buildExpectedResult(TestSourceFile file) {
@@ -160,8 +160,8 @@ public class CommentHandlingTest extends RewriteBaseTest {
return LEADING_COMMENT_SEPARATOR + ANY_CHAR_REGEXP + TRAILING_COMMENT_SEPARATOR + ANY_CHAR_REGEXP + FREESTANDING_COMMENT_SEPARATOR + ANY_CHAR_REGEXP;
}
- private IASTTranslationUnit getUnit() throws CoreException {
- ITranslationUnit tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(project.getFile(fileMap.keySet().iterator().next()));
+ private IASTTranslationUnit getUnit(String fileName) throws CoreException {
+ ITranslationUnit tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(project.getFile(fileName));
return tu.getAST();
}
diff --git a/core/org.eclipse.cdt.core.tests/resources/rewrite/CommentHandlingTestSource.rts b/core/org.eclipse.cdt.core.tests/resources/rewrite/CommentHandlingTestSource.rts
index 443348952a..98fc157d93 100644
--- a/core/org.eclipse.cdt.core.tests/resources/rewrite/CommentHandlingTestSource.rts
+++ b/core/org.eclipse.cdt.core.tests/resources/rewrite/CommentHandlingTestSource.rts
@@ -3054,3 +3054,47 @@ private:
=>trailing
=>freestanding
+//!CommentRecognition Bug 233438
+//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
+//@test.h
+#ifndef HIDEMETHOD_H_
+#define HIDEMETHOD_H_
+
+class HideMethod {
+public:
+ HideMethod();
+ virtual ~HideMethod();
+ int methode2();
+private:
+ int i;
+};
+
+#endif /* HIDEMETHOD_H_ */
+
+//=
+=>leading
+=>trailing
+=>freestanding
+
+//@test.cpp
+#include "test.h"
+
+HideMethod::HideMethod()
+{
+}
+
+HideMethod::~HideMethod()
+{
+}
+
+int HideMethod::methode2(){
+ i++;
+ //comment
+ return i;
+}
+
+//=
+=>leading
+return i; = //comment
+=>trailing
+=>freestanding
diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java
index 67a53836e3..8901a86885 100644
--- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java
+++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java
@@ -11,10 +11,12 @@
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
+import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
+import org.eclipse.cdt.core.dom.ast.IASTComment;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
@@ -241,7 +243,7 @@ public class ChangeGenerator extends CPPASTVisitor {
targetLocation.getNodeLength(), newNodeCode));
break;
case INSERT_BEFORE:
- edit.addChild(new InsertEdit(targetLocation.getNodeOffset(),
+ edit.addChild(new InsertEdit(getOffsetIncludingComments(modification.getTargetNode()),
newNodeCode));
break;
case APPEND_CHILD:
@@ -303,17 +305,56 @@ public class ChangeGenerator extends CPPASTVisitor {
return formattedCode.toString();
}
- public static String originalCodeOfNode(IASTNode node) {
+ public String originalCodeOfNode(IASTNode node) {
if (node.getFileLocation() != null) {
IFile sourceFile = FileHelper.getIFilefromIASTNode(node);
- int nodeOffset = node.getFileLocation().getNodeOffset();
- int nodeLength = node.getFileLocation().getNodeLength();
- return FileContentHelper.getContent(sourceFile, nodeOffset,
- nodeLength);
+ int nodeOffset = getOffsetIncludingComments(node);
+ int nodeLength = getNodeLengthIncludingComments(node);
+
+ return FileContentHelper.getContent(sourceFile, nodeOffset, nodeLength);
}
return null;
}
+ private int getNodeLengthIncludingComments(IASTNode node) {
+ int nodeOffset = node.getFileLocation().getNodeOffset();
+ int nodeLength = node.getFileLocation().getNodeLength();
+
+ ArrayList<IASTComment> comments = commentMap.getAllCommentsForNode(node);
+ if(!comments.isEmpty()) {
+ int startOffset = nodeOffset;
+ int endOffset = nodeOffset + nodeLength;
+ for(IASTComment comment : comments) {
+ IASTFileLocation commentLocation = comment.getFileLocation();
+ if(commentLocation.getNodeOffset() < startOffset) {
+ startOffset = commentLocation.getNodeOffset();
+ }
+ if(commentLocation.getNodeOffset() + commentLocation.getNodeLength() >= endOffset) {
+ endOffset = commentLocation.getNodeOffset() + commentLocation.getNodeLength();
+ }
+ }
+ nodeLength = endOffset - startOffset;
+ }
+ return nodeLength;
+ }
+
+ private int getOffsetIncludingComments(IASTNode node) {
+ int nodeOffset = node.getFileLocation().getNodeOffset();
+
+ ArrayList<IASTComment> comments = commentMap.getAllCommentsForNode(node);
+ if(!comments.isEmpty()) {
+ int startOffset = nodeOffset;
+ for(IASTComment comment : comments) {
+ IASTFileLocation commentLocation = comment.getFileLocation();
+ if(commentLocation.getNodeOffset() < startOffset) {
+ startOffset = commentLocation.getNodeOffset();
+ }
+ }
+ nodeOffset = startOffset;
+ }
+ return nodeOffset;
+ }
+
private String getIndent(IASTNode nextNode) {
IASTFileLocation fileLocation = nextNode.getFileLocation();
int length = fileLocation.getNodeOffset()
@@ -564,7 +605,7 @@ public class ChangeGenerator extends CPPASTVisitor {
}
protected void createChange(MultiTextEdit edit, IASTNode changedNode) {
- int changeOffset = changedNode.getFileLocation().getNodeOffset();
+ int changeOffset = getOffsetIncludingComments(changedNode);
TextEditGroup editGroup = new TextEditGroup(Messages.ChangeGenerator_group);
for (ASTModification currentModification : modificationParent
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 53aa825070..12505df379 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
@@ -83,12 +83,12 @@ public class ASTCommenter {
private static ArrayList<IASTComment> removeAllPreprocessorComments(IASTTranslationUnit tu, ArrayList<IASTComment> comments) {
IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements();
- TreeMap<Integer,Object> treeOfPreProcessorLines = new TreeMap<Integer,Object>();
+ TreeMap<Integer,String> treeOfPreProcessorLines = new TreeMap<Integer,String>();
ArrayList<Integer> listOfPreProcessorOffset = new ArrayList<Integer>();
for (IASTPreprocessorStatement statement : preprocessorStatements) {
if (isInWorkspace(statement)) {
- treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement),null);
+ treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement), statement.getFileLocation().getFileName());
listOfPreProcessorOffset.add(statement.getFileLocation().getNodeOffset());
}
}
@@ -96,8 +96,10 @@ public class ASTCommenter {
ArrayList<IASTComment> commentsInCode = new ArrayList<IASTComment>();
for (IASTComment comment : comments) {
int comStartLineNumber = OffsetHelper.getStartingLineNumber(comment);
- if (treeOfPreProcessorLines.containsKey(comStartLineNumber)) {
- continue;
+ if (treeOfPreProcessorLines.containsKey(comStartLineNumber)
+ && treeOfPreProcessorLines.get(comStartLineNumber).equals(comment.getFileLocation().getFileName()
+ )) {
+ continue;
}
if(commentIsAtTheBeginningBeforePreprocessorStatements(comment, listOfPreProcessorOffset)) {
continue;
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 a711bbb1ef..4f65d33671 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
@@ -135,4 +135,19 @@ public class NodeCommentMap {
public HashMap<IASTNode, ArrayList<IASTComment>> getFreestandingMap() {
return freestandingMap;
}
+
+ /**
+ * Returns an ArrayList for the given node. This ArrayList contains all the comments
+ * which are assigned to this specific node. If no comments are available an empty
+ * ArrayList is returned.
+ * @param node The key to fetch the associated comments.
+ * @return ArrayList
+ */
+ public ArrayList<IASTComment> getAllCommentsForNode(IASTNode node) {
+ ArrayList<IASTComment> comment = new ArrayList<IASTComment>();
+ comment.addAll(getFreestandingCommentsForNode(node));
+ comment.addAll(getLeadingCommentsForNode(node));
+ comment.addAll(getTrailingCommentsForNode(node));
+ return comment;
+ }
}
diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractConstant.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractConstant.rts
index 54829d9960..6aac51914f 100644
--- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractConstant.rts
+++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractConstant.rts
@@ -77,6 +77,87 @@ void A::bar()
int b = theAnswer;
}
+//!ExtractConstantInt 2
+//#org.eclipse.cdt.ui.tests.refactoring.extractconstant.ExtractConstantRefactoringTest
+//@A.h
+#ifndef A_H_
+#define A_H_
+
+class A
+{
+public:
+ A();
+ virtual ~A();
+ int foo();
+ void bar();
+};
+
+#endif /*A_H_*/
+
+
+//=
+#ifndef A_H_
+#define A_H_
+
+class A
+{
+public:
+ A();
+ virtual ~A();
+ int foo();
+ void bar();
+ static const int theAnswer = 42;
+};
+
+#endif /*A_H_*/
+
+
+//@A.cpp
+#include "A.h"
+
+A::A()
+{
+}
+
+A::~A()
+{
+}
+
+int A::foo()
+{
+ //Hallo
+ return //$42$//;
+}
+
+void A::bar()
+{
+ int a = 42;
+ int b = 42;
+}
+
+//=
+#include "A.h"
+
+A::A()
+{
+}
+
+A::~A()
+{
+}
+
+int A::foo()
+{
+ //Hallo
+ return theAnswer;
+}
+
+void A::bar()
+{
+ int a = theAnswer;
+ int b = theAnswer;
+}
+
//!ExtractConstantFloat
//#org.eclipse.cdt.ui.tests.refactoring.extractconstant.ExtractConstantRefactoringTest
//@A.h
diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts
index 25ebbb0943..e48a802330 100644
--- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts
+++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts
@@ -855,7 +855,8 @@ int A::foo()
{
int* i = new int(2);
exp(i);
- return *i;
+ //A end-comment
+ return *i;
}
int A::help()

Back to the top