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.85k stars 250 forks source link

NLOC is 0 for FileInformation after calling `analyze` #274

Closed k0nserv closed 4 years ago

k0nserv commented 5 years ago

Hey 👋,

I've been trying to write a script that uses lizard to analyze some Objective-C code and while CCN seems to be working NLOC is always zero for me, but invoking lizard via the CLI or using analyze_file reports accurate numbers.

Here's the code I have

import csv
from sys import stdout

from lizard import analyze, analyze_file

EXCLUDE_PATTERN = ['Pods/*']

def collect_results(generator):
    result = []

    for (i, x) in enumerate(generator, 1):
        result.append(x)

        # if i % 1000 == 0:
        #     print(f"Processed {i} files")

    return result

if __name__ == '__main__':
    generator = analyze(
        ["Source/"],
        lans=['objectivec']
    )

    result = collect_results(generator)

    for info in result:
        print(info.__dict__)
        for fun in info.function_list:
            print("{fun.nloc}")

    writer = csv.DictWriter(
        stdout, fieldnames=['filename', 'CCN', 'NLOC', 'avg_tokens']
    )
    writer.writeheader()

    for file_info in result:
        writer.writerow({
            'filename': file_info.filename,
            'CCN': file_info.CCN,
            'NLOC': file_info.nloc,
            'avg_tokens': file_info.average_token_count,
        })

Is this a bug or am I making a really simple mistake here?

Environment **Python:** 3.7.4 **pip freeze** ``` entrypoints==0.3 flake8==3.7.8 lizard==1.16.6 mccabe==0.6.1 pycodestyle==2.5.0 pyflakes==2.1.1 ```

FrancescElies commented 5 years ago

I am not sure but I am facing I believe a similar problem, maybe caused by the same issue.

If I download following c file lz4_decompress.c and run the following command.

python -m lizard --C=1 --ignore_warnings 0 --sort cyclomatic_complexity --html lz4_decompress.c

Exit code is always 0, but in the html output I see parts of the code with a higher CCN.

I printed the warning_count in here and it's zero

terryyin commented 4 years ago

@k0nserv sorry for the super late reply.

The api you used analyze didn't have the default extension list. Everything in lizard is counted using an extension. get_extensions([]) will get the basic list of extensions.

Now I've added it as default if no exts are provided. So your code should work fine now.

@FrancescElies I believe you were having a different problem, which is also fixed now.

k0nserv commented 4 years ago

Thanks @terryyin :)