coding style checker for c (variable names, not in

2020-08-09 07:32发布

问题:

This question asks about a coding style checker, but the focus seems to be on indentation and brace placement. GNU indent deals with indentation (which isn't a problem in this code base, amazingly enough).

I'm working with a pile of code that is full of various naming schemes: camelCase, everythingruntogetherinlowercase, underscores_as_separators, SomeStructsEndWithT, etc.

I'd like to be able to pick a convention and at least have an automatic check that new changes are in line with the convention.

Is there a good tool for checking naming in C? Something like Python's pep8 checker tool, I don't want a beautifier.

Thanks.

回答1:

It looks like Google's cpplint (a C++ style checker) can be hacked into submission for checking C like I want.

(I'm still interested in knowing if there any better checkers out there.)



回答2:

It is an unorthodox choice, but I'd go with cxref, if you are willing to put in half a days work. It is a cross referencer, comes with the source code, it has a clean parser and doesn't build a parse tree. Yet, with a few lines of code you can dump all the variables to examine them, or rewrite them to your favoured style (or if you are as lazy as I am instead of rewriting you could generate replace commands for emacs/sed). I only managed to build it for Mac.



回答3:

This one has a number of answers already in this thread Coding style checker for C

from which Vera++ might be the most promising, since most of the other suggestions are formatters not validators. There's a webpage about running vera++ at https://bitbucket.org/verateam/vera/wiki/Running.

There's a download from https://bitbucket.org/verateam/vera/downloads/vera++-1.1.1.tar.gz Compiling presents a few issues:

  • sudo apt-get install libboost-dev tcl-dev
  • An include of tcl.h that should have been tcl/tcl.h
  • Need a full boost src tree, like that from http://sourceforge.net/projects/boost/files/boost/1.53.0/boost_1_53_0.tar.gz/download
  • The build command becomes something like: make BOOST_DIR=/home/fluffy/tmp/boost_1_53_0
  • vera++ needs a ~/.vera++/profiles/ but doesn't autocreate a default (it can be copied from the one in the distribution, however)

Finally, running it on a C++ file produced output like (with duplicate errors omitted for brevity):

../dllist.c:1: no copyright notice found
../dllist.c:4: horizontal tab used
../dllist.c:10: horizontal tab used
../dllist.c:10: closing curly bracket not in the same line or column
../dllist.c:29: horizontal tab used
../dllist.c:38: keyword 'if' not followed by a single space
../dllist.c:38: negation operator used in its short form
../dllist.c:40: horizontal tab used
../dllist.c:40: full block {} expected in the control structure
../dllist.c:42: horizontal tab used
../dllist.c:71: keyword 'if' not followed by a single space
../dllist.c:71: negation operator used in its short form
../dllist.c:72: horizontal tab used
../dllist.c:72: full block {} expected in the control structure
../dllist.c:73: horizontal tab used