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

Improved comment assignment for the NodeCommenter, by Guido Zgraggen, bug 222529.

This commit is contained in:
Markus Schorn 2008-03-13 09:49:37 +00:00
parent da521c277c
commit 2ee490ecf3
9 changed files with 318 additions and 148 deletions

View file

@ -142,7 +142,7 @@ public class RewriteTester extends TestSuite{
}
return test;
} catch (ClassNotFoundException e) {
throw new Exception("Unknown TestClass. Make sure the test's sourcefile specifies a valid test class.");
throw new Exception("Unknown TestClass: " + e.getMessage() + ". Make sure the test's sourcefile specifies a valid test class.");
} catch (SecurityException e) {
throw new Exception("Security Exception during Test creation", e);
} catch (NoSuchMethodException e) {

View file

@ -39,17 +39,29 @@ public class NodeCommentMapTest extends TestCase {
public void testNoComment(){
ASTNode node = new CPPASTName();
assertEquals(0, map.getCommentsForNode(node).size());
assertEquals(0, map.getLeadingCommentsForNode(node).size());
assertEquals(0, map.getTrailingCommentsForNode(node).size());
assertEquals(0, map.getFreestandingCommentsForNode(node).size());
}
public void testOneComment(){
ASTNode node = new CPPASTName();
IASTComment comm = new Comment();
IASTComment comm1 = new Comment();
IASTComment comm2 = new Comment();
IASTComment comm3 = new Comment();
map.addCommentToNode(node, comm);
assertEquals(1, map.getCommentsForNode(node).size());
assertEquals(comm, map.getCommentsForNode(node).get(0));
map.addLeadingCommentToNode(node, comm1);
map.addTrailingCommentToNode(node, comm2);
map.addFreestandingCommentToNode(node, comm3);
assertEquals(1, map.getLeadingCommentsForNode(node).size());
assertEquals(1, map.getTrailingCommentsForNode(node).size());
assertEquals(1, map.getFreestandingCommentsForNode(node).size());
assertEquals(comm1, map.getLeadingCommentsForNode(node).get(0));
assertEquals(comm2, map.getTrailingCommentsForNode(node).get(0));
assertEquals(comm3, map.getFreestandingCommentsForNode(node).get(0));
}
public void testTwoComment(){
@ -57,12 +69,23 @@ public class NodeCommentMapTest extends TestCase {
IASTComment com1 = new Comment();
IASTComment com2 = new Comment();
map.addCommentToNode(node, com1);
map.addCommentToNode(node, com2);
map.addLeadingCommentToNode(node, com1);
map.addLeadingCommentToNode(node, com2);
map.addTrailingCommentToNode(node, com1);
map.addTrailingCommentToNode(node, com2);
map.addFreestandingCommentToNode(node, com1);
map.addFreestandingCommentToNode(node, com2);
assertEquals(2, map.getCommentsForNode(node).size());
assertEquals(com1, map.getCommentsForNode(node).get(0));
assertEquals(com2, map.getCommentsForNode(node).get(1));
assertEquals(2, map.getLeadingCommentsForNode(node).size());
assertEquals(2, map.getTrailingCommentsForNode(node).size());
assertEquals(2, map.getFreestandingCommentsForNode(node).size());
assertEquals(com1, map.getLeadingCommentsForNode(node).get(0));
assertEquals(com2, map.getLeadingCommentsForNode(node).get(1));
assertEquals(com1, map.getTrailingCommentsForNode(node).get(0));
assertEquals(com2, map.getTrailingCommentsForNode(node).get(1));
assertEquals(com1, map.getFreestandingCommentsForNode(node).get(0));
assertEquals(com2, map.getFreestandingCommentsForNode(node).get(1));
}
@ -73,16 +96,36 @@ public class NodeCommentMapTest extends TestCase {
IASTComment com2 = new Comment();
IASTComment com3 = new Comment();
map.addLeadingCommentToNode(node1, com1);
map.addLeadingCommentToNode(node2, com2);
map.addLeadingCommentToNode(node1, com3);
map.addCommentToNode(node1, com1);
map.addCommentToNode(node2, com2);
map.addCommentToNode(node1, com3);
map.addTrailingCommentToNode(node1, com1);
map.addTrailingCommentToNode(node2, com2);
map.addTrailingCommentToNode(node1, com3);
assertEquals(2, map.getCommentsForNode(node1).size());
assertEquals(1, map.getCommentsForNode(node2).size());
assertEquals(com1, map.getCommentsForNode(node1).get(0));
assertEquals(com2, map.getCommentsForNode(node2).get(0));
assertEquals(com3, map.getCommentsForNode(node1).get(1));
map.addFreestandingCommentToNode(node1, com1);
map.addFreestandingCommentToNode(node2, com2);
map.addFreestandingCommentToNode(node1, com3);
assertEquals(2, map.getLeadingCommentsForNode(node1).size());
assertEquals(1, map.getLeadingCommentsForNode(node2).size());
assertEquals(2, map.getTrailingCommentsForNode(node1).size());
assertEquals(1, map.getTrailingCommentsForNode(node2).size());
assertEquals(2, map.getFreestandingCommentsForNode(node1).size());
assertEquals(1, map.getFreestandingCommentsForNode(node2).size());
assertEquals(com1, map.getLeadingCommentsForNode(node1).get(0));
assertEquals(com2, map.getLeadingCommentsForNode(node2).get(0));
assertEquals(com3, map.getLeadingCommentsForNode(node1).get(1));
assertEquals(com1, map.getTrailingCommentsForNode(node1).get(0));
assertEquals(com2, map.getTrailingCommentsForNode(node2).get(0));
assertEquals(com3, map.getTrailingCommentsForNode(node1).get(1));
assertEquals(com1, map.getFreestandingCommentsForNode(node1).get(0));
assertEquals(com2, map.getFreestandingCommentsForNode(node2).get(0));
assertEquals(com3, map.getFreestandingCommentsForNode(node1).get(1));
}

View file

@ -35,6 +35,44 @@ int i = 2, y = 3; //TEST 2
//TEST 3
int b = 0; //TEST 4
//!Commented SimpleDeclarationTest 2
//%CPP
/*TEST 1*/
int i = 2, y = 3; /*TEST 2*/
/*TEST 3*/
int b = 0; /*TEST 4*/
//!Commented ExplicitTemplateInstantion 1
//%CPP
//TEST 1
template class vector<int>; //TEST 2
//!Commented ExplicitTemplateInstantion 2
//%CPP
/*TEST 1*/
template class vector<int>; /*TEST 2*/
//!Commented GPPExplicitTemplateInstantion 1
//%CPP GNU
//TEST 1
static template class vector<int>; //TEST 2
//TEST 3
inline template class vector<int>; //TEST 4
//TEST 5
inline template class vector<int>; //TEST 6
//!Commented LinkageSpecification 1
//%CPP
//TEST 1
extern "C" typedef void FUNC(); //TEST 2
//!Commented LinkageSpecification 2
//%CPP
/*TEST 1*/
extern "C" typedef void FUNC(); /*TEST 2*/
//!Commented NamespaceAlias 1
//%CPP
//TEST 1
@ -45,6 +83,41 @@ namespace kurz = ziemlichlangernamespace; //TEST 2
/*TEST 1*/
namespace kurz = ziemlichlangernamespace; /*TEST 2*/
//!Commented NamespaceDefinition 1
//%CPP
//TEST 1
namespace ziemlichlangernamespace
{
//TEST 2
} //TEST 3
//!Commented NamespaceDefinition 2
//%CPP
/*TEST 1*/
namespace ziemlichlangernamespace
{
/*TEST 2*/
} /*TEST 3*/
//!Commented NamespaceDefinition 3
//%CPP
namespace ziemlichlangernamespace
{
//TEST
}
//!Commented NamespaceDefinition 4
//%CPP
namespace ziemlichlangernamespace
{
//TEST
}
void doIt()
{
int i = 0;
}
//!Commented TemplateDeclaration 1
//%CPP
//TEST 1

View file

@ -250,26 +250,3 @@ void foo()
}
//!Commented ICPPASTFunctionTryBlockDeclarator 14
//%CPP
void foo()
{
int f(int);
class C
{
int i;
double d;
public:
C(int, double);
};
C::C(int ii, double id)
try
:i(f(ii)), d(id)
{
}
catch(...){
}
}

View file

@ -20,6 +20,16 @@ template<typename T> int tempFunct(T p)
}
//!Commented TemplateFunction3
//%CPP
template<typename T> int tempFunct(T p)
{
++p;
p + 4;
return 0;
} //Kommentar
//!Commented ExpressionList1
//%CPP
void foo()

View file

@ -1,78 +1,3 @@
//!CommentRecognition209 - von ASTWriter GPPExplicitTemplateInstantitation
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
//@Klasse1.h
//TEST 1
static template class vector<int>; //TEST 2
//TEST 3
inline template class vector<int>; //TEST 4
//TEST 5
inline template class vector<int>; //TEST 6
//=
=>leading
static template class vector<int>; = //TEST 1
inline template class vector<int>; = //TEST 3
inline template class vector<int>; = //TEST 5
=>trailing
static template class vector<int>; = //TEST 2
inline template class vector<int>; = //TEST 4
inline template class vector<int>; = //TEST 6
=>freestanding
//!CommentRecognition210 - von ASTWriter LinkageSpecification
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
//@Klasse1.h
//TEST 1
extern "C" typedef void FUNC(); //TEST 2
//=
=>leading
extern "C" typedef void FUNC(); = //TEST 1
=>trailing
extern "C" typedef void FUNC(); = //TEST 2
=>freestanding
//!CommentRecognition212 - von ASTWriter TemplateFunction
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
//@Klasse1.h
template<typename T> int tempFunct(T p)
{
++p;
p + 4;
return 0;
} //Kommentar
//=
=>leading
=>trailing
template<typename T> int tempFunct(T p)
{
++p;
p + 4;
return 0;
} = //Kommentar
=>freestanding
//!CommentRecognition213 - von ASTWriter DeclarationWithTraillingDoubleComments
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
//@Klasse1.h
int foo = bar; //Comment
//Zweiteilig
//=
=>leading
=>trailing
int foo = bar; = //Comment
=>freestanding
int foo = bar; = //Zweiteilig
//!CommentHandlingTest 1 - von CERP
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
//@test.h
@ -2772,10 +2697,161 @@ template class vector<int>; //TEST 2
template class vector<int>; = //TEST 1
=>trailing
template class vector<int>; = //TEST 2
class vector<int>; = //TEST 2
=>freestanding
//!CommentRecognition209 - von ASTWriter GPPExplicitTemplateInstantitation
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
//@Klasse1.h
//TEST 1
static template class vector<int>; //TEST 2
//TEST 3
inline template class vector<int>; //TEST 4
//TEST 5
inline template class vector<int>; //TEST 6
//=
=>leading
static template class vector<int>; = //TEST 1
inline template class vector<int>; = //TEST 3
inline template class vector<int>; = //TEST 5
=>trailing
class vector<int>; = //TEST 2
class vector<int>; = //TEST 4
class vector<int>; = //TEST 6
=>freestanding
//!CommentRecognition210 - von ASTWriter LinkageSpecification
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
//@Klasse1.h
//TEST 1
extern "C" typedef void FUNC(); //TEST 2
//=
=>leading
extern "C" typedef void FUNC(); = //TEST 1
=>trailing
typedef void FUNC(); = //TEST 2
=>freestanding
//!CommentRecognition212 - von ASTWriter TemplateFunction
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
//@Klasse1.h
template<typename T> int tempFunct(T p)
{
++p;
p + 4;
return 0;
} //Kommentar
//=
=>leading
=>trailing
{
++p;
p + 4;
return 0;
} = //Kommentar
=>freestanding
//!CommentRecognition213 - von ASTWriter DeclarationWithTraillingDoubleComments
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
//@Klasse1.h
int foo = bar; //Comment
//Zweiteilig
//=
=>leading
=>trailing
int foo = bar; = //Comment
=>freestanding
int foo = bar; = //Zweiteilig
//!CommentRecognition214 - von ASTWriter NamesapceDefinition
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
//@Klasse1.h
//TEST 1
namespace ziemlichlangernamespace
{
//TEST 2
} //TEST 3
//=
=>leading
namespace ziemlichlangernamespace
{
//TEST 2
} = //TEST 1
=>trailing
namespace ziemlichlangernamespace
{
//TEST 2
} = //TEST 3
=>freestanding
namespace ziemlichlangernamespace
{
//TEST 2
} = //TEST 2
//!CommentRecognition215 - von ASTWriter NamesapceDefinition
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
//@Klasse1.h
namespace ziemlichlangernamespace
{
//TEST
}
void doIt(){
int i = 0;
}
//=
=>leading
=>trailing
=>freestanding
namespace ziemlichlangernamespace
{
//TEST
} = //TEST
//!CommentRecognition216 - von ASTWriter NamesapceDefinition
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
//@Klasse1.h
namespace ziemlichlangernamespace
{
int i = 0;
//TEST
}
void doIt(){
int i = 0;
}
//=
=>leading
=>trailing
=>freestanding
namespace ziemlichlangernamespace
{
int i = 0;
//TEST
} = //TEST
//!ImplementationFileCommentRecognition1 - von CERP
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
//@Klasse1.h

View file

@ -139,6 +139,10 @@ public class ASTCommenterVisitor extends CPPASTVisitor {
return PROCESS_CONTINUE;
}
@Override
public int leave(ICPPASTNamespaceDefinition namespaceDefinition) {
return nodeCommenter.appendFreestandingComments((ASTNode)namespaceDefinition);
}
@Override
public int leave(IASTInitializer initializer) {
nodeCommenter.appendComments((ASTNode)initializer);
return PROCESS_CONTINUE;

View file

@ -22,34 +22,6 @@ import org.eclipse.cdt.core.dom.ast.IASTNode;
*
*/
public class NodeCommentMap {
protected final HashMap<IASTNode, ArrayList<IASTComment>> map = new HashMap<IASTNode, ArrayList<IASTComment>>();
public void addCommentToNode(IASTNode node, IASTComment comment){
ArrayList<IASTComment> comments = map.get(node);
if(comments == null){
comments = new ArrayList<IASTComment>();
}
comments.add(comment);
map.put(node, comments);
}
public ArrayList<IASTComment> getCommentsForNode(IASTNode node){
if(map.get(node) == null) {
return new ArrayList<IASTComment>();
}
return map.get(node);
}
//needed for testing
public HashMap<IASTNode, ArrayList<IASTComment>> getMap() {
return map;
}
//===
//Erst nur einmal zu test zwecken
// Wenn wircklich gebraucht refactorn
protected final HashMap<IASTNode, ArrayList<IASTComment>> leadingMap = new HashMap<IASTNode, ArrayList<IASTComment>>();
protected final HashMap<IASTNode, ArrayList<IASTComment>> trailingMap = new HashMap<IASTNode, ArrayList<IASTComment>>();
protected final HashMap<IASTNode, ArrayList<IASTComment>> freestandingMap = new HashMap<IASTNode, ArrayList<IASTComment>>();

View file

@ -24,12 +24,16 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
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.parser.cpp.CPPASTDeclarationStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTExplicitTemplateInstantiation;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTForStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDefinition;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIfStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLabelStatement;
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.parser.cpp.GPPASTExplicitTemplateInstantiation;
import org.eclipse.cdt.internal.core.dom.rewrite.util.OffsetHelper;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
@ -164,12 +168,12 @@ public class NodeCommenter {
return true;
}else if(parent instanceof IASTTranslationUnit) {
return true;
}else if(parent instanceof ICPPASTBaseSpecifier) {
parent = (ASTNode) parent.getParent();
}else if(parent instanceof ICPPASTTemplateDeclaration) {
return true;
}else if(parent instanceof CPPASTIfStatement) {
return true;
}else if(parent instanceof ICPPASTBaseSpecifier) {
parent = (ASTNode) parent.getParent();
}
return !(OffsetHelper.getNodeOffset(com) >= OffsetHelper.getNodeEndPoint(parent));
}
@ -193,6 +197,14 @@ public class NodeCommenter {
return true;
}else if(node instanceof CPPASTWhileStatement) {
return true;
}else if(node instanceof CPPASTTemplateDeclaration) {
return true;
}else if(node instanceof CPPASTLinkageSpecification) {
return true;
}else if(node instanceof GPPASTExplicitTemplateInstantiation) {
return true;
}else if(node instanceof CPPASTExplicitTemplateInstantiation) {
return true;
}
return false;
}
@ -234,6 +246,9 @@ public class NodeCommenter {
public void appendRemainingComments(IASTDeclaration declaration) {
while(commHandler.hasMore()) {
IASTComment comment = commHandler.getFirst();
if(appendComment((ASTNode)declaration, comment)) {
continue;
}
addFreestandingCommentToMap((ASTNode) declaration, comment);
}
}