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

Bug 351399

Summary: Formatting C code is broken for certain coding styles
Product: [Tools] CDT Reporter: Branko Drevensek <branko.drevensek>
Component: cdt-editorAssignee: Anton Leherbauer <aleherb+eclipse>
Status: RESOLVED FIXED QA Contact: Anton Leherbauer <aleherb+eclipse>
Severity: normal    
Priority: P3 CC: cdtdoug, laurent.mazuel, odracirnumira, yevshif
Version: 8.0   
Target Milestone: 8.0.1   
Hardware: PC   
OS: All   
Whiteboard:

Description Branko Drevensek CLA 2011-07-07 04:26:12 EDT
Build Identifier: I20110613-1736

After for statement with braces on level of identation is lost and code is ill-formatted from there on.

Reproducible: Always

Steps to Reproduce:
1. Crate C project, add new C source file.
You can use this code:
void foo() {
	int i;
	for (i = 0; i < 10; i++) {
	}
	foo();
}
2.
Go to preferences, C/C++, Code style, and select GNU or Whitesmiths. Select source, do Source/Format. 
3.
For GNU you get:

void
foo()
{
  int i;
  for (i = 0; i < 10; i++)
    {
  }
foo();
}

(Notice that function call is not idented.)
Comment 1 Anton Leherbauer CLA 2011-07-08 07:12:48 EDT
The problem seems to be with the for loop.  The closing brace should be aligned with the opening one.  This does work with 7.0.2, so it's a regression.
Comment 2 Ricardo Juan Palma urĂ¡n CLA 2011-07-26 14:05:10 EDT
Hi there.

I too have been experiencing problems with the code formatter in this new version of Eclipse CDT (8.0, build ID 20110615-0604). These problems do not occur in the previous version of Eclipse CDT (7.0.2). 

In order to reproduce the problem, just create a new C++ project and create a single CPP file as follows:

#include <iostream>

using namespace std;

long function()
{
	cout << "From function" << endl;
}

class Foo
{
public:
	Foo();
	~Foo();
	void
	SayHello() const;

};

int main()
{
	Foo * foo = new Foo();

	int i = static_cast<int>(5);

	foo->SayHello();

	return 0;
}

If you select the BSD/Allman built-in formatting profile and format the code above, everything is fine, and it is displayed just as I just showed you. However, if we change the expression inside the static_cast to a call to 'function()', the line right after it gets messed up, and we get:

int main()
{
	Foo * foo = new Foo();

	int i = static_cast<int>(function());

foo	->SayHello();

	return 0;
}


This problem also occurs if the expression inside the static_cast is an arithmetic operation, such as '5 * 6' or '4 + 5'. I tried to copy and paste the static_cast all over the main() function, and it seems that only the line right after it gets corrupted. Everything afterwards is properly formatted. Looks like the problem arises when whatever is inside the static_cast is different from a simple identifier or constant.
Comment 3 Anton Leherbauer CLA 2011-08-05 03:17:19 EDT
(In reply to comment #2)
> I too have been experiencing problems with the code formatter in this new
> version of Eclipse CDT (8.0, build ID 20110615-0604). These problems do not
> occur in the previous version of Eclipse CDT (7.0.2). 

Please create a separate bug for this problem.  Thanks!
Comment 4 Anton Leherbauer CLA 2011-08-05 05:28:43 EDT
Fixed in master and 8.0.1.
Comment 5 CDT Genie CLA 2011-08-05 06:23:02 EDT
*** cdt git genie on behalf of Anton Leherbauer ***

    Bug 351399 - Formatting C code is broken for certain coding styles

[*] http://git.eclipse.org/c/cdt/org.eclipse.cdt.git/commit/?id=44e3ec0cde0a4961628c4d35f23f704c398be6b2
Comment 6 CDT Genie CLA 2011-08-05 06:23:04 EDT
*** cdt git genie on behalf of Anton Leherbauer ***

    Bug 351399 - Formatting C code is broken for certain coding styles

[*] http://git.eclipse.org/c/cdt/org.eclipse.cdt.git/commit/?id=2d8f61dc4fa595d458623b4cd607ad6373bb068b
Comment 7 Anton Leherbauer CLA 2011-09-08 03:21:41 EDT
*** Bug 357040 has been marked as a duplicate of this bug. ***