Help Center/ CodeArts Check/ API Reference/ Appendixes/ CMetrics Specifications
Updated on 2024-12-02 GMT+08:00

CMetrics Specifications

Overview

CMetrics, a Huawei-developed code measurement tool, measures source code from different indicators, including code volume, cyclomatic complexity, lines of code, duplication rate, dangerous functions, and redundant code. The current version is 2.1.5.

Parameters

Table 1 Parameters

Parameter

Description

code_size

The number of lines of code without comments and blank lines.

raw_lines

The number of lines of code with comments and blank lines.

methods_total

Total number of functions.

Total number of methods in all code files in a project. If the declaration of a Java abstract method does not contain a function body, the method will not be identified as a function.

cyclomatic_complexity_total

Total cyclomatic complexity.

Sum of cyclomatic complexity of all functions.

The default cyclomatic complexity of a method is 1. Each time a specific keyword appears, the cyclomatic complexity is incremented by 1. When a ternary operator appears, the cyclomatic complexity is incremented by 1.

The distinction between cyclomatic complexity and CCA cyclomatic complexity lies in how the switch statement is handled. The cyclomatic complexity is incremented by the number of cases in the switch statement, wile the CCA cyclomatic complexity is incremented by 1 regardless of the number of cases in the switch statement.

cyclomatic_complexity_per_method

Average cyclomatic complexity.

Average cyclomatic complexity = Total cyclomatic complexity/Total number of functions

maximum_cyclomatic_complexity

Max. cyclomatic complexity of all counted functions.

huge_cyclomatic_complexity_total

Total number of functions with huge cyclomatic complexity.

The number of functions whose cyclomatic complexity is greater than the threshold. The threshold is determined by the CMetrics version. For details, see Table 2. You can modify the threshold based on the Cmetrics rules for some languages.

huge_cyclomatic_complexity_ratio

Proportion of functions with huge cyclomatic complexity.

Proportion of huge cyclomatic complexity = Total huge cyclomatic complexity/Total number of functions × 100%

cca_cyclomatic_complexity_total

Total CCA cyclomatic complexity.

Sum of the CCA cyclomatic complexity of all functions.

The default cyclomatic complexity of a method is 1. Each time a specific keyword appears, the cyclomatic complexity is incremented by 1. When a ternary operator appears, the cyclomatic complexity is incremented by 1.

The distinction between cyclomatic complexity and CCA cyclomatic complexity lies in how the switch statement is handled. The cyclomatic complexity is incremented by the number of cases in the switch statement, wile the CCA cyclomatic complexity is incremented by 1 regardless of the number of cases in the switch statement.

cca_cyclomatic_complexity_per_method

Average CCA cyclomatic complexity.

Average CCA cyclomatic complexity = Total CCA cyclomatic complexity/Total number of functions

maximum_cca_cyclomatic_complexity

Max. CCA cyclomatic complexity of all counted functions.

huge_cca_cyclomatic_complexity_total

Total number of functions with huge CCA cyclomatic complexity.

The number of functions whose cyclomatic complexity is greater than the threshold. The threshold is determined by the CMetrics version. You can modify the threshold based on the Cmetrics rules for some languages.

cyclomatic_complexity_adequacy

Cyclomatic complexity adequacy (CCA).

Cyclomatic complexity adequacy = (Total number of functions – Total number of functions with huge CCA cyclomatic complexity)/Total number of functions × 100%

maximum_depth

Max. depth of all counted functions.

huge_depth_total

The number of huge depths.

The number of functions whose maximum depth is greater than the threshold. The threshold is determined by the CMetrics version. For details, see Table 2. You can modify the threshold based on the Cmetrics rules for some languages.

huge_depth_ratio

Proportion of huge depths.

Proportion of huge depths = The number of huge depths/Total number of functions × 100%

method_lines

The number of lines containing functions.

The number of lines in the internal code blocks of functions, excluding the definition lines at the beginning of functions, blank lines, and comment lines.

lines_per_method

Average lines of code containing functions.

Average lines of code containing functions = Total number of lines of code with functions/Total number of functions × 100%

huge_method_total

The number of huge functions.

The number of functions whose lines of code exceed the threshold. The threshold is determined by the CMetrics version. For details, see Table 2. You can modify the threshold based on the Cmetrics rules for some languages.

huge_method_ratio

Proportion of huge functions.

Proportion of huge functions = The number of huge functions/Total number of functions × 100%

files_total

Total number of files.

Total number of files with the source code file name extension in the scanned project directory. Empty files are not counted. For details about the file name extensions corresponding to different languages, see Table 3.

folders_total

Total number of directories.

Total number of directories in a project. Empty directories are not counted.

lines_per_file

Average lines of code per file.

Average lines of code per file = Total lines of code/Total number of files

huge_headerfile_total

Huge header files.

In the C source code file, files with the suffix .h, .hh, .hpp, .hxx, .h++, .inc, or .inl are considered as header files. If the number of valid lines of code in a header file is greater than the threshold, the header file is considered as a huge header file. The threshold is determined by the CMetrics version. For details, see Table 2.

huge_headerfile_ratio

Proportion of huge header files.

Proportion of huge header files = The number of huge header files/Total number of files × 100%

huge_non_headerfile_total

Huge source files.

If the file name extension of a source code file is not a header file and the number of valid lines of code is greater than the threshold, the source code file is huge. The threshold is determined by the CMetrics version. For details, see Table 2. You can modify the threshold based on the Cmetrics rules for some languages.

huge_non_headerfile_ratio

Proportion of huge source files.

Proportion of huge source files = The number of huge source files/Total number of files × 100%

huge_folder_total

The number of huge directories.

The total number of files and subdirectories in a directory is counted (excluding the number of subdirectories and files in the subdirectories of the directory node). If the total number of files and subdirectories is greater than the threshold, the directory is considered as a huge directory. Empty sub-directories (sub-directories without source code files) are not counted. The threshold is determined by the CMetrics version. For details, see Table 2. You can modify the threshold based on the Cmetrics rules for some languages.

huge_folder_ratio

Proportion of huge directories.

Proportion of huge directories = The number of huge directories/Total number of directories × 100%

file_duplication_total

The number of duplicate files.

The number of files with the same content. Only the files whose file name extensions meet the source code file name extension requirements of each language are counted. For details about the file name extensions of different languages, see Table 3.

file_duplication_ratio

File duplication rate.

File duplication rate = The number of duplicate files/Total number of files × 100%

non_hfile_duplication_total

The number of duplicate source files.

The number of duplicate C files with the file name extension of source files. Other languages do not have header files, and all files are source files.

non_hfile_duplication_ratio

Source file duplication rate.

Source file duplication rate = The number of duplicate source files/Total number of files × 100%

code_duplication_total

Duplicate code.

Duplicate code refers to non-blank, non-comment lines that are identical and occur consecutively, including import lines.

NOTE:

The count of duplicate code is based on the total number of duplicate lines in each file, rather than individual code snippets. For example, if three files have the same 10 lines of duplicate code, the number of duplicate lines of code is 30. If lines 1 to 10 in file A appear in file B and lines 2 to 11 in file A appear in file C, the union of lines 1 to 10 and lines 2 to 11 are counted as duplicate code of file A, that is, 11 lines of duplicate code.

code_duplication_ratio

Duplication rate.

Code duplicate rate = The number of duplicate code/Total number of lines of code × 100%

non_hfile_code_duplication_total

Duplicate source files.

Total number of duplicate lines in a source file. Only the duplicate lines in the files whose file name extensions meet the requirements of source code files (excluding header files in C/C++) are counted.

non_hfile_code_duplication_ratio

Source file code duplication rate.

Source file code duplication rate = The number of duplicate source file code/Total number of lines of code × 100%

unsafe_functions_total

Unsafe functions.

The number of times that unsafe functions, such as memcpy and memmove, are called in the code.

NOTE:

Currently, only Clike detects unsafe functions.

unsafe_functions_kloc

Density of unsafe functions (the number of unsafe functions per thousand lines of code)

Density of dangerous functions = Total number of unsafe functions/Total number of lines of code × 1000

redundant_code_total

Redundant code.

The number of redundant code blocks in comments. Code in comments and consecutive words that contain programming language keywords or comply with syntax rules in comments are regarded as redundant code.

If a block comment contains multiple lines of redundant code, it is counted as only one redundant code.

redundant_code_kloc

Density of redundant code blocks (the number of lines of redundant code per thousand lines of code).

Density of redundant code blocks = The number of lines of redundant code/Total number of lines of code × 1000

NOTE:

If the total number of lines of code is less than 1,000, the density is not calculated and the value is 0.

warning_suppression_total

The number of suppressed alarms.

The number of alarm suppression statements in scanned files. If a string in a comment complies with a specific regular expression, the comment is considered as an alarm suppression statement. For details about the regular expressions corresponding to different languages, see Table 5.

This parameter takes effect only after the CMetrics rule is enabled.

warning_suppression_kloc

Density of suppressed alarms.

Density of suppressed alarms = The number of suppressed alarms/Total number of lines of code × 1000 (The number of redundant lines of code per thousand lines of code)

This parameter takes effect only after the CMetrics rule is enabled.

Table 2 Default thresholds of the CMetrics tool

Threshold Name

Default Value

huge_cyclomatic_complexity

20

huge_depth

4

huge_method

50

huge_headerfile

500

huge_non_headerfile

2,000

huge_folder

50

Table 3 CMetrics languages and file name extensions

Language

Extension

Java

.java

C/C++

.c/.cc/.cpp/.cxx/.cp/.c++/.inc/.inl/.mm/.h/.hh/.hpp/.hxx/.h++

TypeScript

.ts/.tsx

Rust

.rs

ArkTS

.ets

C#

.cs

Go

.go

JavaScript

.js/.jsx

Lua

.lua

Python

.py

Table 4 Unsafe C/C++ functions checked by CMetrics

_fstrncat

_vsntprintf

strcatbuff

vos_nsprintf

_fstrncpy

_vsnwprintf

strcatbuffa

vos_nvsprintf

_ftcscat

_vstprintf

strcatbuffw

vos_snprintf

_ftcscpy

bcopy

strcatchainw

vos_sprintf

_getts

copymemory

strcatn

vos_sscanf

_gettws

fscanf

strcatna

vos_strcat

_getws

fwscanf

strcatnw

vos_strcpy

_mbccat

gets

strcatw

vos_strncat

_mbccpy

lstrcat

strcpy

vos_strncpy

_mbscat

lstrcata

strcpya

vos_vsprintf

_mbscpy

lstrcatn

strcpyn

vos_vsscanf

_mbsnbcat

lstrcatnA

strcpyna

vscanf

_mbsnbcpy

lstrcatnW

strcpynw

vsnprintf

_mbsncat

lstrcatw

strcpyw

vsprintf

_mbsncpy

lstrcpy

strncat

vsscanf

_snprintf

lstrcpya

strncata

vswprintf

_sntprintf

lstrcpyn

strncatw

vswscanf

_sntscanf

lstrcpyna

strncpy

vwscanf

_snwprintf

lstrcpynw

strncpya

wcscat

_stprintf

lstrcpyw

strncpyw

wcscpy

_stscanf

lstrncat

swprintf

wcsncat

_tccat

memcpy

swscanf

wcsncpy

_tccpy

memmove

vfscanf

wmemcpy

_tcscat

memset

vfwscanf

wmemmove

_tcscpy

nsprintf

vos_chkmemcpy

wnsprintf

_tcsncat

rtlcopymemory

vos_chkmemmove

wnsprintfA

_tcsncpy

scanf

vos_chkmemset

wnsprintfW

_tscanf

snprintf

vos_chkstrcat

wscanf

_vos_bzero

snscanf

vos_chkstrcpy

wsprintf

_vos_memcpy

sntprintf

vos_chkstrncat

wsprintfa

_vos_memmove

snwscanf

vos_chkstrncpy

wsprintfw

_vos_memset

sprintf

vos_mem_copy

wvnsprintf

_vos_strcat

sprintfa

vos_mem_set

wvnsprintfa

_vos_strcpy

sprintfw

vos_mem_zero

wvnsprintfw

_vos_strncat

sscanf

vos_memcpy

wvsprintf

_vos_strncpy

strcat

vos_memmove

wvsprintfa

_vsnprintf

strcata

vos_memset

wvsprintfw

Table 5 Suppression rules in CMetrics

Language

Regular Expression for Comment Matching

C/C++, Java, JavaScript, TypeScript

  • r'(/(\*|/)\s*(lint\b\s+\S+|coverity\s*\[.+?\]|eslint-disable)'
  • r'|^\s*#\s*pragma\s+\w+\s+diagnostic\s+(ignored|warning)'
  • r'|^\s*#\s*pragma\s+warning\s*\(\s*disable'
  • r'|^\s*@(\w+\.)?SuppressWarnings)'

Python

r'#\s*pylint\s*:'