Community
Participate
Working Groups
Build Identifier: M20100909-0800 If a statement is replaced in a block that is not a function body in a C source file, then any comments at the end of the block are lost. Reproducible: Always Steps to Reproduce: 1. Create a C source file 2. Create a C block within a function body. Add a statement to it and a comment at the end 3. Invoke ASTRewrite.replace to replace the statement with another statement 4. Observe that the comment was removed
Created attachment 186457 [details] Bugfix that fixes a case where comments at the end of a block are lost for C code The issue is with the ASTCommentVisitor whose job it is to map comments to the correct statements. In the method leave(IASTStatement) it checks whether the statement is a CPPASTCompoundStatement and if so then it attempts to add comment as freestanding comment to that block. However, since this is only performed for C++, C never gets any freestanding comments added to any blocks. The result is that freestanding comments in inner blocks are not added anywhere, and when control returns to ASTCommenter.addCommentsToCommentMap it revert to the default behavior of just adding remanding comments to the function (Is this really correct behavior? I may be missing the use case where it is really necessary, but it seems to me to be code to cover up bugs, such as this one). When the rewriter later tries to add the comment to the synth source it uses (see ChangeGenerator.synthTreatment) it can't find it in the block (since it was added to the function). The comment is therefore lost, which then results in the rewriter getting confused when it tries to compare the synth code and the original code with the goal of restoring formatting. The formatting of the rest of the block therefore gets messed up (prettyprinted).
*** cdt cvs genie on behalf of egraf *** Bug 296192: Extract method removes essential macros and breaks formatting <a href=https://bugs.eclipse.org/bugs/show_bug.cgi?id=296192>https://bugs.eclipse.org/bugs/show_bug.cgi?id=296192</a> Bugfix patch that fixes a case where comments at the end of a block are lost for C code (Comment #2) [*] ASTCommenterVisitor.java 1.4 http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.cdt/all/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/commenthandler/ASTCommenterVisitor.java?root=Tools_Project&r1=1.3&r2=1.4
Fixed in HEAD > 20110104