Coding style checker for C

一世执手 提交于 2019-12-17 21:52:54

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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




回答6:


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.




回答7:


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.




回答8:


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.




回答9:


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



来源:https://stackoverflow.com/questions/411249/coding-style-checker-for-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!