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

Fixes comment handling with preprocessor nodes by Emanuel Graf, bug 233438.

This commit is contained in:
Markus Schorn 2008-05-23 14:26:46 +00:00
parent 291094d65d
commit f44c51e7d3
7 changed files with 208 additions and 24 deletions

View file

@ -87,19 +87,19 @@ public class CommentHandlingTest extends RewriteBaseTest {
@Override @Override
protected void runTest() throws Throwable { protected void runTest() throws Throwable {
if (fileMap.size() > 1) { if (fileMap.size() == 0) {
fail("To many files for CommentHandlingTest"); //$NON-NLS-1$
} else if (fileMap.size() == 0) {
fail("No file for testing"); //$NON-NLS-1$ fail("No file for testing"); //$NON-NLS-1$
} }
TestSourceFile file = fileMap.values().iterator().next(); for(String fileName : fileMap.keySet()) {
NodeCommentMap nodeMap = ASTCommenter.getCommentedNodeMap(getUnit()); TestSourceFile file = fileMap.get(fileName);
NodeCommentMap nodeMap = ASTCommenter.getCommentedNodeMap(getUnit(fileName));
StringBuilder expectedResultBuilder = buildExpectedResult(file); StringBuilder expectedResultBuilder = buildExpectedResult(file);
StringBuilder actualResultBuilder = buildActualResult(nodeMap); StringBuilder actualResultBuilder = buildActualResult(nodeMap);
assertEquals(expectedResultBuilder.toString(), actualResultBuilder.toString()); assertEquals(expectedResultBuilder.toString(), actualResultBuilder.toString());
}
} }
private StringBuilder buildExpectedResult(TestSourceFile file) { 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; return LEADING_COMMENT_SEPARATOR + ANY_CHAR_REGEXP + TRAILING_COMMENT_SEPARATOR + ANY_CHAR_REGEXP + FREESTANDING_COMMENT_SEPARATOR + ANY_CHAR_REGEXP;
} }
private IASTTranslationUnit getUnit() throws CoreException { private IASTTranslationUnit getUnit(String fileName) throws CoreException {
ITranslationUnit tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(project.getFile(fileMap.keySet().iterator().next())); ITranslationUnit tu = (ITranslationUnit) CCorePlugin.getDefault().getCoreModel().create(project.getFile(fileName));
return tu.getAST(); return tu.getAST();
} }

View file

@ -3054,3 +3054,47 @@ private:
=>trailing =>trailing
=>freestanding =>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

View file

@ -11,10 +11,12 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator; package org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;
import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import org.eclipse.cdt.core.dom.ast.ASTVisitor; 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.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration; import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
@ -241,7 +243,7 @@ public class ChangeGenerator extends CPPASTVisitor {
targetLocation.getNodeLength(), newNodeCode)); targetLocation.getNodeLength(), newNodeCode));
break; break;
case INSERT_BEFORE: case INSERT_BEFORE:
edit.addChild(new InsertEdit(targetLocation.getNodeOffset(), edit.addChild(new InsertEdit(getOffsetIncludingComments(modification.getTargetNode()),
newNodeCode)); newNodeCode));
break; break;
case APPEND_CHILD: case APPEND_CHILD:
@ -303,17 +305,56 @@ public class ChangeGenerator extends CPPASTVisitor {
return formattedCode.toString(); return formattedCode.toString();
} }
public static String originalCodeOfNode(IASTNode node) { public String originalCodeOfNode(IASTNode node) {
if (node.getFileLocation() != null) { if (node.getFileLocation() != null) {
IFile sourceFile = FileHelper.getIFilefromIASTNode(node); IFile sourceFile = FileHelper.getIFilefromIASTNode(node);
int nodeOffset = node.getFileLocation().getNodeOffset(); int nodeOffset = getOffsetIncludingComments(node);
int nodeLength = node.getFileLocation().getNodeLength(); int nodeLength = getNodeLengthIncludingComments(node);
return FileContentHelper.getContent(sourceFile, nodeOffset,
nodeLength); return FileContentHelper.getContent(sourceFile, nodeOffset, nodeLength);
} }
return null; 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) { private String getIndent(IASTNode nextNode) {
IASTFileLocation fileLocation = nextNode.getFileLocation(); IASTFileLocation fileLocation = nextNode.getFileLocation();
int length = fileLocation.getNodeOffset() int length = fileLocation.getNodeOffset()
@ -564,7 +605,7 @@ public class ChangeGenerator extends CPPASTVisitor {
} }
protected void createChange(MultiTextEdit edit, IASTNode changedNode) { protected void createChange(MultiTextEdit edit, IASTNode changedNode) {
int changeOffset = changedNode.getFileLocation().getNodeOffset(); int changeOffset = getOffsetIncludingComments(changedNode);
TextEditGroup editGroup = new TextEditGroup(Messages.ChangeGenerator_group); TextEditGroup editGroup = new TextEditGroup(Messages.ChangeGenerator_group);
for (ASTModification currentModification : modificationParent for (ASTModification currentModification : modificationParent

View file

@ -83,12 +83,12 @@ public class ASTCommenter {
private static ArrayList<IASTComment> removeAllPreprocessorComments(IASTTranslationUnit tu, ArrayList<IASTComment> comments) { private static ArrayList<IASTComment> removeAllPreprocessorComments(IASTTranslationUnit tu, ArrayList<IASTComment> comments) {
IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements(); 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>(); ArrayList<Integer> listOfPreProcessorOffset = new ArrayList<Integer>();
for (IASTPreprocessorStatement statement : preprocessorStatements) { for (IASTPreprocessorStatement statement : preprocessorStatements) {
if (isInWorkspace(statement)) { if (isInWorkspace(statement)) {
treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement),null); treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement), statement.getFileLocation().getFileName());
listOfPreProcessorOffset.add(statement.getFileLocation().getNodeOffset()); listOfPreProcessorOffset.add(statement.getFileLocation().getNodeOffset());
} }
} }
@ -96,8 +96,10 @@ public class ASTCommenter {
ArrayList<IASTComment> commentsInCode = new ArrayList<IASTComment>(); ArrayList<IASTComment> commentsInCode = new ArrayList<IASTComment>();
for (IASTComment comment : comments) { for (IASTComment comment : comments) {
int comStartLineNumber = OffsetHelper.getStartingLineNumber(comment); int comStartLineNumber = OffsetHelper.getStartingLineNumber(comment);
if (treeOfPreProcessorLines.containsKey(comStartLineNumber)) { if (treeOfPreProcessorLines.containsKey(comStartLineNumber)
continue; && treeOfPreProcessorLines.get(comStartLineNumber).equals(comment.getFileLocation().getFileName()
)) {
continue;
} }
if(commentIsAtTheBeginningBeforePreprocessorStatements(comment, listOfPreProcessorOffset)) { if(commentIsAtTheBeginningBeforePreprocessorStatements(comment, listOfPreProcessorOffset)) {
continue; continue;

View file

@ -135,4 +135,19 @@ public class NodeCommentMap {
public HashMap<IASTNode, ArrayList<IASTComment>> getFreestandingMap() { public HashMap<IASTNode, ArrayList<IASTComment>> getFreestandingMap() {
return freestandingMap; 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;
}
} }

View file

@ -77,6 +77,87 @@ void A::bar()
int b = theAnswer; 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 //!ExtractConstantFloat
//#org.eclipse.cdt.ui.tests.refactoring.extractconstant.ExtractConstantRefactoringTest //#org.eclipse.cdt.ui.tests.refactoring.extractconstant.ExtractConstantRefactoringTest
//@A.h //@A.h

View file

@ -855,7 +855,8 @@ int A::foo()
{ {
int* i = new int(2); int* i = new int(2);
exp(i); exp(i);
return *i; //A end-comment
return *i;
} }
int A::help() int A::help()