Skip to main content

A simple code complexity analyzer without caring about the C/C++ header files or Java imports. It can deal with Java/C/C++/Objective C code. It counts the cyclomatic complexity number etc.

Project description

Was hfcca, A Header Free Cyclomatic Complexity Analyzer.

[![Build Status](](

lizard is a simple code complexity analyzer without caring about the C/C++
header files or Java imports. It can deal with

``*`` Java
``*`` C/C++
``*`` Objective C.

It counts

``*`` the nloc (lines of code without comments),
``*`` CCN (cyclomatic complexity number),
``*`` token count of functions.
``*`` parameter count of functions.

You can set limitation for CCN (-C), the number of parameters (-a). Functions
that exceed these limitations will generate warnings. The exit code of lizard
will be none-Zero if there are warnings.

This tool actually calculates how complex the code 'looks' rather than how
complex the code real 'is'. People will need this tool because it's often very
hard to get all the included folders and files right when they are complicated.
But we don't really need that kind of accuracy when come to cyclomatic

It requires python2.6 or above (early versions are not verified).

##Installation is only a single python file, and everything is in that single script. You can always use it without any installation.


If you want a proper install:

[sudo] pip install lizard

Or if you've got the source:

[sudo] python install


lizard [options] [PATH or FILE] [PATH] ...
Run for the code under current folder (recursively):
Exclude anything in the tests folder:

lizard mySource/ -x"./tests/``*``"

### Options

--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose Output in verbose mode (long function name)
-C CCN, --CCN=CCN Threshold for cyclomatic complexity number warning.
The default value is 15. Functions with CCN bigger
than this number will generate warning
Limit for number of parameters
-w, --warnings_only Show warnings only, using clang/gcc's warning format
for printing warnings.
-i NUMBER, --ignore_warnings=NUMBER
If the number of warnings is equal or less than the
number, the tool will exit normally, otherwize it will
generate error. Useful in makefile when improving
legacy code.
-x EXCLUDE, --exclude=EXCLUDE
Exclude files that match this pattern. ``*`` matches
everything, ? matches any single characoter,
"./folder/``*``" exclude everything in the folder,
recursively. Multiple patterns can be specified. Don't
forget to add "" around the pattern.
-X, --xml Generate XML in cppncss style instead of the normal
tabular output. Useful to generate report in Jenkins
-P, --no_preprocessor_count
By default, a #if will also increase the complexity.
Adding this option to ignore them
number of working threads. The default value is 1.
-d, --find_duplicates
Find and skip analysis for file duplicates.

##Example use
### Analyze a folder recursively: lizard mahjong_game/src
NLOC CCN token param function@line@file
10 2 29 2 start_new_player@26@./html_game.c
6 1 3 0 set_shutdown_flag@449@./httpd.c
24 3 61 1 server_main@454@./httpd.c
2 file analyzed.
LOC Avg.NLOC AvgCCN Avg.ttoken function_cnt file
191 15 3 51 12 ./html_game.c
363 24 4 86 15 ./httpd.c

!!!! Warnings (CCN > 15) !!!!
66 19 247 1 accept_request@64@./httpd.c
Total NLOC Avg.NLOC Avg CCN Avg token Fun Cnt Warning cnt Fun Rt NLOC Rt
554 20 4.07 71.15 27 1 0.04 0.12
### Warnings only (in clang/gcc formation):lizard -w mahjong_game
./src/html_ui/httpd.c:64: warning: accept_request has 19 CCN and 1 params (66 NLOC, 247 tokens)
./src/mahjong_game/mj_table.c:109: warning: mj_table_update_state has 20 CCN and 1 params (72 NLOC, 255 tokens)

## Using lizard as Python module
You can also use lizard as a Python module in your code:
>>> import lizard
>>> i = lizard.analyze_file("../cpputest/tests/AllTests.cpp")
>>> print i.__dict__
{'nloc': 9, 'function_list': [<lizard.FunctionInfo object at 0x10bf7af10>], 'filename': '../cpputest/tests/AllTests.cpp'}
>>> print i.function_list[0].__dict__
{'cyclomatic_complexity': 1, 'token_count': 22, 'name': 'main', 'parameter_count': 2, 'nloc': 3, 'long_name': 'main( int ac , const char ``*`*`` av )', 'start_line': 30}

You can also use source code string instead of file. But you need to provide a file name (to identify the language).
>>> i = lizard.analyze_file.analyze_source_code("AllTests.cpp", "int foo(){}")

## Whitelist
If for some reason you would like to ignore the warnings, you can use the whitelist. Add 'whitelizard.txt' to the current folder,
then the functions defined in the file will be ignored. This is an example

#The file name can only be whitelizard.txt and put it in the current folder.
#You may have commented lines begin with #.
function_name1, function_name2 # list function names in mulitple lines or split with comma.
filename:function1, function2 # you can also specify the filename

### Options in Comments
You can use options in the comments of the source code to change the behavior of lizard.
By putting "#lizard forgives" inside a function or before a function it will suppress the warning for that function.
int foo() {
// #lizard forgives the complexity
## Wishlist (The features that will come in the near future)

Project details

Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for lizard, version 1.7.7
Filename, size File type Python version Upload date Hashes
Filename, size lizard-1.7.7.tar.gz (21.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page