Coding style checker for C

2019-01-21 11:39发布

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.

9条回答
2楼-- · 2019-01-21 11:56

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.

查看更多
欢心
3楼-- · 2019-01-21 12:01

You can use clang-format. More information can be found at official site: http://clang.llvm.org/docs/ClangFormat.html

查看更多
在下西门庆
4楼-- · 2019-01-21 12:01

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.

查看更多
成全新的幸福
5楼-- · 2019-01-21 12:06

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.

查看更多
女痞
6楼-- · 2019-01-21 12:10

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.

查看更多
贪生不怕死
7楼-- · 2019-01-21 12:11

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.

查看更多
登录 后发表回答