terryyin / lizard

A simple code complexity analyser without caring about the C/C++ header files or Java imports, supports most of the popular languages.
Other
1.84k stars 248 forks source link

lizard gets confused by C++ thousands seperator #352

Closed dhylands closed 2 years ago

dhylands commented 2 years ago

If I have a C++ function which uses the thousands seperator (introduced in C++14), then lizard will go looking for a matching single quote and will consider all lines from the ' in 100'000 until the next ' to be part of the same function.

This is a simple file that reproduces the problem:

#include <stdio.h>

int func() {
    return 100'000;
}

// Line 1
// Line 2
// Line 3
// Line 4
// Line 5

void func2() {
    printf("x = '%d'\n", 1234);
}

If I run lizard --length 10 then it incorrectly reports that func is longer than 10 lines:

================================================
  NLOC    CCN   token  PARAM  length  location  
------------------------------------------------
      13      1     18      0      13 func@3-15@./foo.cpp
1 file analyzed.
==============================================================
NLOC    Avg.NLOC  AvgCCN  Avg.token  function_cnt    file
--------------------------------------------------------------
     14      13.0     1.0       18.0         1     ./foo.cpp

=========================================================================================================
!!!! Warnings (cyclomatic_complexity > 15 or length > 10 or nloc > 1000000 or parameter_count > 100) !!!!
================================================
  NLOC    CCN   token  PARAM  length  location  
------------------------------------------------
      13      1     18      0      13 func@3-15@./foo.cpp
==========================================================================================
Total nloc   Avg.NLOC  AvgCCN  Avg.token   Fun Cnt  Warning cnt   Fun Rt   nloc Rt
------------------------------------------------------------------------------------------
        14      13.0     1.0       18.0        1            1      1.00    1.00

(it considers func to be 13 lines long).

If I add a comment with a single quote in it at the end of line 2, then everything works as expected:

#include <stdio.h>

int func() {
    return 100'000; // '
}

// Line 1
// Line 2
// Line 3
// Line 4
// Line 5

void func2() {
    printf("x = '%d'\n", 1234);
}
  NLOC    CCN   token  PARAM  length  location  
------------------------------------------------
       3      1      8      0       3 func@3-5@./foo.cpp
       3      1     12      0       3 func2@13-15@./foo.cpp
1 file analyzed.
==============================================================
NLOC    Avg.NLOC  AvgCCN  Avg.token  function_cnt    file
--------------------------------------------------------------
      7       3.0     1.0       10.0         2     ./foo.cpp

=============================================================================================================
No thresholds exceeded (cyclomatic_complexity > 15 or length > 10 or nloc > 1000000 or parameter_count > 100)
==========================================================================================
Total nloc   Avg.NLOC  AvgCCN  Avg.token   Fun Cnt  Warning cnt   Fun Rt   nloc Rt
------------------------------------------------------------------------------------------
         7       3.0     1.0       10.0        2            0      0.00    0.00

EDIT: Tweaked C code to compile clean.

terryyin commented 2 years ago

@dhylands thanks for raising the issue.

This https://github.com/terryyin/lizard/commit/bb8aa4d200a2935b07e6d2bf1e6072b449bc5ae3 should fix the issue. I've made a new release to include change.