Reputation: 7575
I'm working for a company that has strict coding style guidelines but no automatic tool to validate them. I've looked around and the only tools I could find were lint like tools that seem to be aimed at verifying what the code does, and preventing bugs and not at making sure the coding style is correct.
What tool should we use, if at all?
NOTE: I'm looking for something for C code, although something that works for C++ would be good as well.
Upvotes: 25
Views: 41265
Reputation: 9130
From a similar question: Vera++:
Vera++ is a programmable tool for verification, analysis and transformation of C++ source code.
The main usage scenarios that are foreseen for Vera++ are:
Ensure that the source code complies with the given coding standards and conventions.
Provide source code metrics and statistics.
Perform automated transformations of the source code, which can range from pretty-printing to diagnostics to fault injection and advanced testing.
Upvotes: 3
Reputation: 832
You can use clang-format
. More information can be found at official site:
http://clang.llvm.org/docs/ClangFormat.html
Upvotes: 4
Reputation: 14622
Try UniversalIndentGUI, which is a GUI wrapper around popular indenters/beautifiers such as Uncrustify, Artistic Style, and GNU Indent. Its GUI and live preview function make it very easy to try different indenters, and once you've found an indenter and config you like, you can export the config or even export a shell script. Great if you're still trying things out.
Upvotes: 2
Reputation: 9994
I would argue against using a formal tool here. The best enforcement of coding standards is peer pressure. Peer pressure and mutual respect between team members are key elements of that team building environment....no different that the good natured harassing that is necessary when somebody inadvertently "breaks the build", etc.
Review of newer team members' code is a key part of the learning process and integrating into the team and learning the coding standards, etc. If the coding standard is too complicated or subtle to be picked up in this way, then instead of looking for a tool to validate the standard, you should be lookng for a new coding standard.
Upvotes: 2
Reputation: 4878
Please use VIM and enjoy the easiness. You can do anything related to source programming by simple commands. Also you can make the VIM more reliable and powerful by editing .vimrc file. The help in it will help you a lot.
/renjith g
Upvotes: -8
Reputation: 35459
The traditional beautifier indent, available on every Unix machine. The version found on some is GNU indent, which can be compiled and installed on every machine. GNU indent can read a set of rules from the file ~/.indent.pro
, for instance:
--original --dont-format-first-column-comments --no-blank-lines-after-commas --parameter-indentation 8 --indent-level 8 --line-length 85 --no-space-after-parentheses --no-comment-delimiters-on-blank-lines
So, just running indent before commiting guarantees uniformity of the presentation. If you want to enforce it, define a pre-commit hook in the Version Control System you use, which will run indent and refuse the commit if the committed version differs from what indent produces.
Upvotes: 16
Reputation: 4335
AStyle does what you want:
Artistic Style is a source code indenter, formatter, and beautifier for the C, C++, C# and Java programming languages.
There's an AStyle Plugin available for Eclipse.
Eclipse also has a code formatter, but I'm not sure if it works in CDT.
Upvotes: 9
Reputation: 753725
There are (or were) numerous tools for this. One of the oldest is cb
(C Beautifier) that was around in ancient versions of Unix - meaning, in this case, Version 7 Unix:
CB ( 1 ) UNIX Programmer’s Manual CB ( 1 )
NAME
cb – C program beautifier
SYNOPSIS
cb
DESCRIPTION
Cb places a copy of the C program from the standard input on the standard
output with spacing and indentation that displays the structure of the
program.
BUGS
GNU indent
is a vastly more complex and configurable beastie. There were commercial tools as well. For example, Abraxas Software provides a CodeCheck tool, for example; we used that briefly in the mid-90s, but the changes it wanted us to make were (very necessary, but) too intrusive for management.
Upvotes: 4
Reputation: 340241
You are looking for a 'code beautifier'. Uncrustify's a free one.
You only have to be able to describe your coding style in its configuration file, and it'll make sure every file fits the described style.
Upvotes: 11