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.
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: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.
You can use
clang-format
. More information can be found at official site: http://clang.llvm.org/docs/ClangFormat.htmlTry 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.
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.
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.
AStyle does what you want:
There's an AStyle Plugin available for Eclipse.
Eclipse also has a code formatter, but I'm not sure if it works in CDT.