Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 333938 - 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
Summary: If a statement is replaced in a block that is not a function body in a C sour...
Status: RESOLVED FIXED
Alias: None
Product: CDT
Classification: Tools
Component: cdt-refactoring (show other bugs)
Version: 8.0   Edit
Hardware: Macintosh Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: 8.0   Edit
Assignee: Emanuel Graf CLA
QA Contact: Emanuel Graf CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 296192
  Show dependency tree
 
Reported: 2011-01-10 22:54 EST by Fredrik Berg Kjolstad CLA
Modified: 2011-01-11 02:26 EST (History)
1 user (show)

See Also:


Attachments
Bugfix that fixes a case where comments at the end of a block are lost for C code (1.71 KB, patch)
2011-01-10 23:20 EST, Fredrik Berg Kjolstad CLA
emanuel: iplog+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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