Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 333938

Summary: 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
Product: [Tools] CDT Reporter: Fredrik Berg Kjolstad <fredrikbk>
Component: cdt-refactoringAssignee: Emanuel Graf <emanuel>
Status: RESOLVED FIXED QA Contact: Emanuel Graf <emanuel>
Severity: normal    
Priority: P3 CC: cdtdoug
Version: 8.0   
Target Milestone: 8.0   
Hardware: Macintosh   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:
Bug Depends on:    
Bug Blocks: 296192    
Attachments:
Description Flags
Bugfix that fixes a case where comments at the end of a block are lost for C code emanuel: iplog+

Description Fredrik Berg Kjolstad CLA 2011-01-10 22:54:23 EST
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
Comment 1 Fredrik Berg Kjolstad CLA 2011-01-10 23:20:16 EST
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).
Comment 2 Fredrik Berg Kjolstad CLA 2011-01-10 23:21:13 EST
*** 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
Comment 3 Emanuel Graf CLA 2011-01-11 02:26:18 EST
Fixed in HEAD > 20110104