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 totrue
ignore any leading underscores in variable and function names - these will be stripped before checking for any possiblepointer_prefix
or name format etc. -
ignore_goto
- If set totrue
, will ignore all gotos - disablesprefer_goto
checking. -
prefer_goto
- If set totrue
, will warn when multiplereturn
statements exist in a single function. However, if set tofalse
will warn about any use ofgoto
at 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_prefix
is set), then thepointer_prefix
is expected to be repeated by the depth of the pointer. i.e. for the argumentchar **ppArgv
,pointer_prefix
should be set top
andpointer_prefix_repeat
should betrue
. -
arrays_are_pointers
- If a variable is an array, treat it as a pointer forpointer_prefix
and 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.