Bug 471600 - C/C++ code style formatter - Double indentation within structures depending on class indentation settings
Summary: C/C++ code style formatter - Double indentation within structures depending o...
Status: NEW
Alias: None
Product: CDT
Classification: Tools
Component: cdt-editor (show other bugs)
Version: 8.7.0   Edit
Hardware: PC Windows 7
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact: Jonah Graham CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-07-01 11:05 EDT by Andras Vasko CLA
Modified: 2020-09-04 15:17 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andras Vasko CLA 2015-07-01 11:05:27 EDT
In the C/C++ code style formatter settings window, under the 'Indentation' tab and in 'Indent' box - if I check both:
- 'public', 'protected', 'private' within class
- Declarations relative to 'public', 'protected', 'private
options, then the automatic formatting of a simple C stuct is corrupted. It is indented twice, instead of a single indentation. 

E.g.: assuming a 4 space indentation the indentation of the inner part of the struct will be 8 spaces.
struct MyStruct {
        char a;
        char b;
};
Instead of:
struct MyStruct {
    char a;
    char b;
};


If I uncheck any of the above options, then the indentation of the C structures are OK, but in that case the c++ classes indentation does not match the requirements.



-- Configuration Details --
Product: Eclipse 4.5.0.20150621-1200 (org.eclipse.epp.package.cpp.product)
Installed Features:
 org.eclipse.platform 4.5.0.v20150603-2358
Comment 1 Nathan Ridge CLA 2015-07-01 14:18:02 EDT
(In reply to Andras Vasko from comment #0)
> In the C/C++ code style formatter settings window, under the 'Indentation'
> tab and in 'Indent' box - if I check both:
> - 'public', 'protected', 'private' within class
> - Declarations relative to 'public', 'protected', 'private
> options, then the automatic formatting of a simple C stuct is corrupted. It
> is indented twice, instead of a single indentation. 
> 
> E.g.: assuming a 4 space indentation the indentation of the inner part of
> the struct will be 8 spaces.
> struct MyStruct {
>         char a;
>         char b;
> };
> Instead of:
> struct MyStruct {
>     char a;
>     char b;
> };

That is the expected behaviour.

> If I uncheck any of the above options, then the indentation of the C
> structures are OK, but in that case the c++ classes indentation does not
> match the requirements.

What are the requirements?
Comment 2 Andras Vasko CLA 2015-07-02 04:11:59 EDT
I prefer not to have visibility used within a struct, since if I need to use visibility then using a class is the suggested way. I always see a struct as an "old-fashioned" real struct, and it should not have visibility definitions, so the double indentation is useless. I think the class was derived from a struct, and not the way other, so the formatting of a class should not affect the formatting of a struct.

I now checked how Eclipse behaves for different file types. In a .cpp file the formatter uses double indentation for struct, and in a .c file there is only a single indentation, so Eclipse handles a struct in a .cpp file as a class, but I think if someone writes a struct, it should be a struct, and nothing else.

This problem is also problematic with .h files, since the default for .h files is the C++ style formatting, and in a mixed C/C++ environment is can make a mess. 

I'm programming embedded systems and I have to deal with lots of 3rd party chip drivers that are mostly coded and formatted in C, but the business logic that is added by me is in C++. This is the reason why I have to work in a mixed environment, and also forces me to think of structs as only-C structs.
Comment 3 Andras Vasko CLA 2015-07-02 04:14:36 EDT
What I would really like to see, is to have an option to force the formatter to always indent the struct in a simple way, regardless the class indentation options.
Comment 4 Nathan Ridge CLA 2015-07-02 04:18:27 EDT
So, in your coding style, members of classes are indented by two levels, but member of structs only by one level?
Comment 5 Andras Vasko CLA 2015-07-02 04:36:58 EDT
Correct!

e.g.:
class MyClass {
    public:
        char a;
};

struct MyStruct {
    char a;
};
Comment 6 Nathan Ridge CLA 2015-07-02 12:31:56 EDT
C++ allows structs to have access specifiers, too, so there is no way CDT could know without additional configuration that it should indent structs in C++ code differently from classes.

What we could do is introduce separate "'public', 'protected', 'private' within struct" and "Declarations relative to 'public', 'protected', 'private' within struct" settings for structs. For your coding style, you could then set "'public', 'protected', 'private' within struct" to 0 and get the indentation you describe.
Comment 7 Andras Vasko CLA 2015-07-02 13:02:11 EDT
That would be perfect!
Comment 8 Anton Leherbauer CLA 2015-07-03 02:40:49 EDT
(In reply to Nathan Ridge from comment #6)
> C++ allows structs to have access specifiers, too, so there is no way CDT
> could know without additional configuration that it should indent structs in
> C++ code differently from classes.

One idea would be to test whether the struct definition has any access specifiers and avoid the extra indent if there is none.