cstyle
cstyle copied to clipboard
C/C++ Style Checker
CStyle
CStyle is a simple tool to check the conformance of C/C++ coding against a predefined style convention for variable / function naming etc.
CStyle uses libclang to parse and identify elements (variable declarations, function names / parameters etc) and then simply checks each against a list of rules.
Installation
git clone https://github.com/alexmurray/cstyle.git
cd cstyle
pip3 install .
cstyle --generate-config > ~/.cstyle
# edit ~/.cstyle as required
Usage
cstyle supports a few command-line arguments:
-
--generate-config- Generate a sample configuration file -
--config- Used to specify the path to the configuration file (See Configuration below) -
--msg-template- Used to specify the template string for outputting messages. This is specified as a Python new-style format string. This supports the following named arguments:file- the path to the fileline- the line of the messagecolumn- the column number of the identifier which the message concernsreason- a human readable description of the message
The default is
'{file}:{line}:{column}: {reason}'
See cstyle --help for more details.
Configuration
CStyle is configured using a configuration file (a sample configuration can be
automatically generated with the --generate-config command-line
argument). This defaults to ~/.cstyle but a particular configuration can be
specified using the --config command-line option.
The configuration file supports some basic options in the Options section:
-
ignore_leading_underscores- If set totrueignore any leading underscores in variable and function names - these will be stripped before checking for any possiblepointer_prefixor name format etc. -
ignore_goto- If set totrue, will ignore all gotos - disablesprefer_gotochecking. -
prefer_goto- If set totrue, will warn when multiplereturnstatements exist in a single function. However, if set tofalsewill warn about any use ofgotoat all. -
pointer_prefix- If a variable is a pointer, this prefix is checked to exist at the start of the variable name. To disable simply remove this configuration option from the configuration file. -
pointer_prefix_repeat- If set totrue(andpointer_prefixis set), then thepointer_prefixis expected to be repeated by the depth of the pointer. i.e. for the argumentchar **ppArgv,pointer_prefixshould be set topandpointer_prefix_repeatshould betrue. -
arrays_are_pointers- If a variable is an array, treat it as a pointer forpointer_prefixand related checks.
Rules for naming variables are specified in the Rules section - these specify
a libclang kind and the associated regular expression to validate the name. For
instance, var_decl is used to specify the regular expression for all variable
declarations.
For more information on the various libclang kinds see the
libclang documentation
for the CursorKind type. NOTE: In the cstyle configuration file, each kind is
specified as lowercase without the CXCursor_ prefix, with the CamelCase suffix
written in snake_case.
Requirements
- python3
- python-clang
Emacs Integration
To integrate with Emacs there are two options - either standalone or via flycheck.
For standalone usage see cstyle.el.
To integrate with flycheck see flycheck-cstyle.
Vim Integration
To integrate with vim via syntastic see syntastic-cstyle.