end0tknr's kipple - 新web写経開発

http://d.hatena.ne.jp/end0tknr/ から移転しました

lizard (python製) で、C# (csharp) の 循環的複雑度 (cyclomatic_complexity)計測

https://github.com/terryyin/lizard

c# の code metricsは、visual studio で計測できますが、 全srcの循環的複雑度をコマンドラインから一覧で生成したかった為、lizard を利用。

install & usage

$ sudo /usr/local/bin/pip install lizard

$ /usr/local/bin/lizard --version
1.16.3
$ /usr/local/bin/lizard --help
usage: lizard [options] [PATH or FILE] [PATH] ...

lizard is an extensible Cyclomatic Complexity Analyzer for many programming
languages including C/C++ (doesn't require all the header files). For more
information visit http://www.lizard.ws

positional arguments:
  paths                 list of the filename/paths.

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -l LANGUAGES, --languages LANGUAGES
                        List the programming languages you want to analyze. if
                        left empty, it'll search for all languages it knows.
                        `lizard -l cpp -l java`searches for C++ and Java code.
                        The available languages are: cpp, java, csharp,
                        javascript, python, objectivec, ttcn, ruby, php,
                        swift, scala, GDScript, go, lua
  -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 it will generate warning
  -f INPUT_FILE, --input_file INPUT_FILE
                        get a list of filenames from the given file
  -L LENGTH, --length LENGTH
                        Threshold for maximum function length warning. The
                        default value is 1000. Functions length bigger than it
                        will generate warning
  -a ARGUMENTS, --arguments ARGUMENTS
                        Limit for number of parameters
  -w, --warnings_only   Show warnings only, using clang/gcc's warning format
                        for printing warnings.
                        http://clang.llvm.org/docs/UsersManual.html#cmdoption-
                        fdiagnostics-format
  --warning-msvs        Show warnings only, using Visual Studio's warning
                        format for printing warnings.
                        https://msdn.microsoft.com/en-us/library/yxkt8b26.aspx
  -i NUMBER, --ignore_warnings NUMBER
                        If the number of warnings is equal or less than the
                        number, the tool will exit normally; otherwise, it
                        will generate error. If the number is negative, the
                        tool exits normally regardless of the number of
                        warnings. Useful in makefile for legacy code.
  -x EXCLUDE, --exclude EXCLUDE
                        Exclude files that match the pattern. * matches
                        everything, ? matches any single character,
                        "./folder/*" exclude everything in the folder
                        recursively. Multiple patterns can be specified. Don't
                        forget to add "" around the pattern.
  -t WORKING_THREADS, --working_threads WORKING_THREADS
                        number of working threads. The default value is 1.
                        Using a bigger number can fully utilize the CPU and
                        often faster.
  -X, --xml             Generate XML in cppncss style instead of the tabular
                        output. Useful to generate report in Jenkins server
  --csv                 Generate CSV output as a transform of the default
                        output
  -H, --html            Output HTML report
  -m, --modified        Calculate modified cyclomatic complexity number ,
                        which count a switch/case with multiple cases as one
                        CCN.
  -E EXTENSIONS, --extension EXTENSIONS
                        User the extensions. The available extensions are:
                        -Ecpre: it will ignore code in the #else branch.
                        -Ewordcount: count word frequencies and generate tag
                        cloud. -Eoutside: include the global code as one
                        function. -EIgnoreAssert: to ignore all code in
                        assert. -ENS: count nested control structures.
  -s SORTING, --sort SORTING
                        Sort the warning with field. The field can be nloc,
                        cyclomatic_complexity, token_count, p#arameter_count,
                        etc. Or an customized field.
  -T THRESHOLDS, --Threshold THRESHOLDS
                        Set the limit for a field. The field can be nloc,
                        cyclomatic_complexity, token_count, parameter_count,
                        etc. Or an customized file. Lizard will report warning
                        if a function exceed the limit
  -W WHITELIST, --whitelist WHITELIST
                        The path and file name to the whitelist file. It's
                        './whitelizard.txt' by default. Find more information
                        in README.
$

sample

$ /usr/local/bin/lizard --exclude "./GRA_batch_report/Backup/*" \
                                  --sort cyclomatic_complexity \
                  --languages csharp . > foo.txt
$ cat foo.txt
================================================
  NLOC    CCN   token  PARAM  length  location
------------------------------------------------
       4      1      9      0       4 CM.rpt::CM_A?_0?_R?2::CM_A?_0?_R02@34-37@./GRA_batch_report/ba_??_a??_0??_r02/rpt/CM_A??_0??_R0??.cs
      11      1     91      2      13 CM.rpt::CM_A?_0?_R?2::CM_A?_0?_R02_Re???Start@63-75@./GRA_batch_report/ba_cm_??_03_r02/rpt/CM_??_03_R02.cs
    <略>
      37      3    227      2      44 CM::BL_CM_M?_1?_???4::Get??ti??Grp@430-473@./GRA_web/bl_??_ms_11/??_CM_??_11_S04.cs
1074 file analyzed.

==============================================================
NLOC    Avg.NLOC  AvgCCN  Avg.token  function_cnt    file
--------------------------------------------------------------
   1475     228.2     1.7     2709.0         6     ./GRA_batch_report/ba_??_??_03_r02/rpt/??_AJ_??_R02.cs
     14       0.0     0.0        0.0         0     ./GRA_batch_report/ba_??_??_03_r02/Asse??yInfo.cs
    <略>
      0       0.0     0.0        0.0         0     ./GRA_web/bl_??_??_11/BL_??_??_11_S05.cs

=========================================================================================
!!!! Warnings (cyclomatic_complexity > 15 or length > 1000 or parameter_count > 100) !!!!

================================================
  NLOC    CCN   token  PARAM  length  location  
------------------------------------------------
    1369    313   8701      2    1807 FI::FI_??_03_??2::Da????id1_Item??ta??und@2503-4309@./GRA_web/allegroweb/FI_GL_03_S02.aspx.cs
     981    222   6540      2    1252 FI::FI_??_03_??9::Da????id1_Item??ta??und@950-2201@./GRA_web/allegroweb/FI_GL_03_S09.aspx.cs
    <略>
    1313      1  16162      0    1521 FI.rpt::??_??_05_R12::Initi??zeCo??nent@307-1827@./GRA_web/allegroweb/rpt/XF_GL_05_R12.cs
==========================================================================================
Total nloc   Avg.NLOC  AvgCCN  Avg.token   Fun Cnt  Warning cnt   Fun Rt   nloc Rt
------------------------------------------------------------------------------------------
    871324      49.8     4.5      446.4    15996          885      0.06    0.56