Is there any tool to standardize format of C++ code? [closed]
Asked Answered
I

9

13

I'm looking for a tool that works on Windows to reformat some C++ code in my codebase. Essentially, I've got some code I wrote a while ago that I'd like to use, but it doesn't match the style I'm using in a more recent project.

What's the best way to reformat C++ code in a standard manner?

Billy3

Ignatzia answered 17/3, 2010 at 16:19 Comment(0)
C
7

In Visual Studio: Edit / Advanced / Format Document

The format applied to the document will match the settings in: Tools / Options / Text Editor / C/C++

Visual Studio might not support all the formatting options you want applied to your document, in which case you'll need a separate tool (such as Paul Betts is suggesting) to format the way you want.

Chose answered 17/3, 2010 at 16:25 Comment(1)
+1 for the reference to SO on formatters.Shelving
B
10

GNU Indent should get you most of the way there

Benford answered 17/3, 2010 at 16:22 Comment(5)
Yes, I found that one on Google -- but does it work on Windows?Ignatzia
Yes. See gnuwin32.sourceforge.net/packages/indent.htmPritchard
@Billy: it works as well on Windows as it does anywhere else. Regardless of platform, unless you want one of the "canned" formats, getting usable output can be challenging (to the point that unless you're dealing with a lot of code, formatting by hand is often faster and easier).Subhuman
@Herry Coffin: I was asking because the site Paul Betts linked to contained only UNIX source code, and no description as to how to get it running on Windows.Ignatzia
GNU indent does NOT support C++. Section 1.12 of the manual says "While an attempt was made to get indent working for C++, it will not do a good job on any C++ source except the very simplest."Ottoottoman
C
7

In Visual Studio: Edit / Advanced / Format Document

The format applied to the document will match the settings in: Tools / Options / Text Editor / C/C++

Visual Studio might not support all the formatting options you want applied to your document, in which case you'll need a separate tool (such as Paul Betts is suggesting) to format the way you want.

Chose answered 17/3, 2010 at 16:25 Comment(1)
+1 for the reference to SO on formatters.Shelving
S
7

Astyle is one of the more popular tools.

Edit: I don't know why I didn't think about it when I first posted this, but you can also use Vim. The = command will reformat your code according to your indentexpr and cinoptions.

Scarabaeoid answered 17/3, 2010 at 17:2 Comment(1)
We used this a year or two ago to format our entire code base (GNU->ANSI indentation), and it worked flawlessly.Perilous
P
4

I vote for Uncrustify.

Otherwise: Best C++ Code Formatter/Beautifier

Pikestaff answered 7/8, 2010 at 12:46 Comment(0)
S
3

GC Great Code has been an old standby for me. It's quite configurable.

http://sourceforge.net/projects/gcgreatcode/

Schlep answered 7/8, 2010 at 12:56 Comment(0)
P
2

Visual Studio can. And most IDEs can.

Possibility answered 17/3, 2010 at 16:20 Comment(6)
Care to explain how? I did spend some time with Visual Studio before asking the question...Ignatzia
-1: Without 3rd party add-ins, Visual Studio cannot.Doy
@280Z28 let me prove it. In VS: select the whole document, then Edit | Advanced | Format Selection. If you don't know something it doesn't mean that it doesn't exist.Possibility
Actually, it can, to some degree, assist you with formatting of your code. You can run "Format Selection" on a part of or your whole document, and it will re-indent your code.Reimburse
@Andrey: It will reindent if I do that but it won't change the way brackets are handled or other types of formatting. I'll throw you a +1 -- but this really doesn't do what I'm looking for.Ignatzia
@Andrey, you can't possibly believe that indenting a document is what the OP is asking about.Doy
M
2

I have also used AStyle in the past. I used this GUI for it to make it a bit easier (I think--it's been a while): Artistic Style for Windows: http://jimp03.zxq.net/

Morrow answered 17/3, 2010 at 17:52 Comment(0)
S
2

Eclipse/CDT can indent your code very well...

  • Its configurable.
  • You can also share the settings within your team/group/organization.

P.S. I am writing something about that in my Book Post comment if you have some direct questions. :-)

Scrutiny answered 7/8, 2010 at 12:41 Comment(0)
D
0

use AStyle ( inbuilt in codeblocks )

Step 1 : Select your code
Step 2 : Click on Plugins->Source Code Format (AStyle )

Hope it helps.

Diastyle answered 6/6, 2014 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.