Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

File-level metrics

Name Description Metric code ID Languages
Lines of code number of lines of code in the file; calculated as (last line number) - (first line number) + 1 LOC_FILE 7 C/C++, Java
Number of class declarations number of classes declared in a file at a global level (inner classes and their members are not included in this metric) NOCLASSDECL 8 C/C++, Java
Number of constant declarations number of constant declarations that are declared in this file at a global level NOCONSTDECL 9 C/C++
Number of data items declared number of data items (outside of any classes) that are declared in this file at a global level NODATADECL 10 C/C++
Number of comment sections in a file number of comment sections in the file NOCOMMSECT* 11 C/C++, Java
Bytes of comments number of bytes of comments in the file. BYTESCOMM* 12 C/C++, Java
Lines of comments number of lines of code with comments. If the line contains both code and comments, it is included in this metric. Comments using the // syntax count as one line. Each visibly distinct line of multi-line comments using the /* ... */ syntax is counted as one line. Example: /*multi-line comment*/ /*another comment*/ LINESCOMM = 3 LINESCOMM* 13 C/C++, C#, Java
Number of macros defined number of macros defined in the file NOMACROS* 14 C/C++
Number of local includes number of local includes, for example, #include "....." NOLOCALINC* 15 C/C++
Number of system includes number of system includes, for example, #include <.....> NOSYSINC* 16 C/C++
Number of third-party includes number of third-party include files (the number of includes, where the file name starts with a '/'' or '../'.) NO3DPINC* 17 C/C++
Number of include directives in a file the total number of include directives in a file INCLDIRECTIVES* 18 C/C++
Number of functions and methods in a file number of class methods and functions met within a file NOROUTINES 19 C/C++, Java
Number of calls to other routines for all routines in the file sum of the number of calls to other routines for all routines in a file RNOCALLS 20 C/C++, Java
Number of conditional arcs for all routines in the file sum of the number of conditional arcs for all routines in a file RNOCONDARCS 21 C/C++, Java
Maximal conditional span for all routines in the file maximum conditional span for all routines in a file RMAXCONDSPAN 22 C/C++, Java
Maximal value of control nesting for all routines in the file maximum value of control nesting for all routines in a file RMAXLEVEL 23 C/C++, Java
Sum of logarithms of the numbers of independent paths for all routines in the file sum of the logarithms of the numbers of independent paths for all routines in a file RNOINDPATHS 24 C/C++, Java
Number of operands for all routines in the file total number of operands for all routines in a file RNOOPUSED 25 C/C++, Java
Number of operators for all routines in the file total number of operators for all routines in a file RNOOPRUSED 26 C/C++, Java
Sum of McCabe Cyclomatic complexity metrics for all routines in the file sum of McCabe Cyclomatic Complexity Metric for all routines in a file (a routine is, as above, either a function or class method) RCYCLOMATIC 27 C/C++, Java
Number of control statements in the file sum of the number of control statements for all routines in a file RNOCONTROLSTAT 28 C/C++, Java
Number of data items declared local for all routines in the file total number of data items declared local for all routines in a file RNOLOCDECL 29 C/C++, Java
Number of executable statements for all routines in the file sum of the number of executable statements for all routines in a file RNOEXSTAT 30 C/C++, Java
Number of lines of code for all routines in the file sum of LOC numbers for all routines of the file RLOC 31 C/C++, Java
Number of declarative statements for all routines in the file sum of the number of declarative statements for all routines in a file RNODECLSTAT 32 C/C++, Java
Number of statements for all routines in the file total number of statements for all routines in a file RNOSTAT 33 C/C++, Java
Complexity risk for the file Klocwork's Complexity Index Metric that predicts the risk of fault insertion during modification or being inherent when created. RISK 34 C/C++, Java
Number of occurrences of global variable usage for all routines in the file sum of the number of occurrences of global variable usages for all routines in a file RNOACCTOGLOB 35 C/C++, Java
Number of methods for all classes of the file methods defined within classes for a file plus stand-alone methods in C++ from the same file CNOMETH 36 C/C++, Java
Number of ways a class can be accessed for all classes of the file number of protected, public and private methods where, in C++, the private methods are only counted if the class has friends CNOMSG 37 C/C++, Java
The Halstead program volume metric for the file. The Halstead program volume metric for the file. The measurable and countable properties are:
  • n1 = number of unique or distinct operators appearing in that implementation
  • n2 = number of unique or distinct operands appearing in that implementation
  • N1 = total usage of all of the operators appearing in that implementation
  • N2 = total usage of all of the operands appearing in that implementation From these metrics, Halstead defines:
  • the vocabulary n as n = n1 + n2
  • the implementation length N as N = N1 + N2 Operators can be "+" and "*" but also an index "[...]" or a statement \ separation "..;..". The number of operands consists of the numbers of literal expressions, constants and variables. Program Volume: This metric is for the size of any implementation of any algorithm. V = Nlog2n
HALSTEADVOL 38 C/C++, Java
Number of blank lines in the file number of blank lines in the file BLANKLINES 39 C/C++
Number of compiler directives in the file number of compiler directives in the file. The directives counted are #if, #ifdef, #ifndef, #else, #endif, and #elif. DIRECTIVES* 40 C/C++
Bytes of global variables declared in the file bytes of global variables declared in the file. For more information about calculating this metric, see Specifying sizes for built-in types. BYTESGLDATADECL 41 C/C++
Maximum level of include file nesting in the file the length of the longest include chain in the file Note: For this metric to be calculated correctly, all directories that contain header files must be available during the Klocwork analysis. MAXINCNEST 42 C/C++
Total number of included files called in the file the total number of files that the preprocessor includes in a translation unit Note: For this metric to be calculated correctly, all directories that contain header files must be available during the Klocwork analysis. TOTALINC 43 C/C++
CRC32 checksum for the file See 'Checksum description' below. CHECKSUM 44 C++
Non-comment, non-blank lines of code in the file the number of lines of code in a file, not including comment lines and blank lines. For C/C++ files, counts only the number of lines of code after precompilation. NCNBLOC_FILE 45 C/C++, C#, Java
First file level used-by coupling metric See 'Functional file coupling and cohesion' below for a full definition. COUPLINGUB1 1000 C/C++, Java
First file level uses coupling metric COUPLINGU1 1001 C/C++, Java
Second file level used-by coupling metric COUPLINGUB2 1002 C/C++, Java
Second file level uses coupling metric COUPLINGU2 1003 C/C++, Java
Third file level used-by coupling metric COUPLINGUB3 1004 C/C++, Java
Third file level uses coupling metric COUPLINGU3 1005 C/C++, Java
Fourth file level used-by coupling metric COUPLINGUB4 1006 C/C++, Java
Fourth file level uses coupling metric COUPLINGU4 1007 C/C++, Java
First cohesion number of relationships COHESION1 1008 C/C++, Java
Second cohesion number of relationships COHESION2 1009 C/C++, Java
Third cohesion number of relationships COHESION3 1010 C/C++, Java
Fourth cohesion number of relationships COHESION4 1011 C/C++, Java

* Note: The following metrics are calculated during pre-processing: NOCOMMSECT, LINESCOMM, BYTESCOMM, NOMACROS, NOLOCALINC, NOSYSINC, NO3DPINC, INCLDIRECTIVES, DIRECTIVES. If a Klocwork project is built with pre-processed files or the Klocwork compiler (kwcc) is run with the never-cpp option, the values for these metrics will be zero.

Checksum description

The CHECKSUM metric is the CRC32 checksum for a file.

CRC stands for Cyclic Redundancy Check. The 32 comes from the fact it calculates a 32-bit checksum. CRC32 is an algorithm for calculating a unique identifier for a file. It is used in programs like PKZIP to identify files and make sure that they are original. (There is 1 in 232 chance of two files having the same CRC32 checksum and being mistaken as being the same file).

It is calculated recursively for characters read from the source file. This metric uses the standard calculation algorithm:

At the start of calculation crc32 is set to 0xffffffff.

crc32 = 0^0xffffffff;

For each next character ch read from the file crc32 is updated by the formula:

crc32(ch) = (ch>>8)^ table[(ch& 0xff) xor crc32];

where:

table is a pre-generated 256-element table

crc32 is the value of crc32 calculated at the previous step

After all characters from the file are read, crc32 is inverted:

crc32 = crc32 ^ 0xffffffff;

Note: This metric may be a negative number when the actual value (stored as an integer) does not fit into ..MAX_INTEGER interval and wraps into the -MAX_INTEGER..0 interval.

Functional file coupling and cohesion

There are four levels of coupling metrics for each of two types of relationships—uses and used-by—and a metric that is the sum of both. See 'Functional file coupling (total)' below. Coupling and cohesion metrics are calculated for both C/C++ and Java projects; however, the following examples use C/C++ sample files.

Sample files: a.h, b.h, and example.cpp

COUPLING and COHESION metrics are calculated using an example consisting of three files a.h, b.h, and example.cpp (see below).

/space/test/coupling_test>cat -n a.h

1 #ifndef __A_H__ 2 #define __A_H__ 3 4 class Foo { 5 6 }; 7 8 class Bar { 9 Foo * foo; 10 11 }; 12 13 #endif

space/test/coupling_test>cat -n b.h

1 #ifndef __B_H__ 2 #define __B_H__ 3 4 class ZZZ { 5 6 }; 7 8 #endif

/space/test/coupling_test>cat -n example.cpp

1 #include "a.h" 2 #include "b.h" 3 4 Foo *foo1; 5 Bar *bar; 6 ZZZ *zzz;

The relations that are calculated for these three files compiled together are:*

example.cpp;INCLUDES;b.h;example.cpp;2 example.cpp;INCLUDES;a.h;example.cpp;1 foo;VARIABLE_USES_CLASS;Foo;example.cpp;4 bar;VARIABLE_USES_CLASS;Bar;example.cpp;5 zzz;VARIABLE_USES_CLASS;ZZZ;example.cpp;6 foo;CLASS-DATA-MEMBER_USES_CLASS;Foo;a.h;9

For functional file coupling/cohesion we pick up only the following relations:

foo;VARIABLE_USES_CLASS;Foo;example.cpp;4 bar;VARIABLE_USES_CLASS;Bar;example.cpp;5 zzz;VARIABLE_USES_CLASS;ZZZ;example.cpp;6 foo;CLASS-DATA-MEMBER_USES_CLASS;Foo;a.h;9

*Note: The format of the relation printout is:

<source>;<relation_kind>;<destination>;<file>;<line>

Sample file a.h entities and relations

Entities in File a.h

a.h;FILE;coupling_test;a.h;1 __A_H__;MACRO;a.h;a.h;2 Foo;CLASS;a.h;a.h;4 Bar;CLASS;a.h;a.h;8 foo;CLASS-DATA-MEMBER;Bar;a.h;9

Relations in File a.h

Entities described in file a.h participate in three of the four relations in Sample files: a.h, b.h, and example.cpp:

foo;VARIABLE_USES_CLASS;Foo;example.cpp;4 bar;VARIABLE_USES_CLASS;Bar;example.cpp;5 foo;CLASS-DATA-MEMBER_USES_CLASS;Foo;a.h;9

Uses and used-by coupling metrics

"Uses" and "used by" coupling metrics 1 to 4 are calculated on relations that have an entity declared in one file as a source and an entity declared in another file as a destination. That means that relation foo;CLASS-DATA-MEMBER_USES_CLASS;Foo;a.h;9 does not participate in calculation of the coupling metrics as a relations that has source and destination within one file. As for the two other relations, entities of the file a.h participate in these two left relationships as destinations:

"foo;VARIABLE_USES_CLASS;Foo;example.cpp;4", "bar;VARIABLE_USES_CLASS;Bar;example.cpp;5"

That means that entities of file a.h does not have any relationships to entities outside file a.h. As a result all the COUPLINGU metrics 1 to 4 should be equal to 0.

See Sample file a.h: Coupling (uses) metric.

Sample file a.h: Coupling (uses) metrics

A nontrivial measure of the file uses relationships associated with this file. More specifically, the metric represents an information-dense measure of the relationship of this file to every other file in the system that it uses.

The formulas below are applied to sample file a.h. See Sample file a.h entities and relations.

For Klocwork system-level reports on C and C++ systems, the non-trivial measure for uses coupling is as follows:

COUPLING U1

CpU1 (Fj) =

Image:Revised_uses_coupling_formula.png

where

i = 1, ..., n is the index of relevant relation-kind. For file a.h, the relation-kinds are VARIABLE_USES_CLASS, CLASS-DATA-MEMBER_USES_CLASS.

Fj is the file of interest in system S, for any j in system S. For this example, Fj is file a.h

Fk are other files within the system S(Image:J_not_equal_K.png). Using our sample files, system S consists of three files a.h, b.h and example.cpp. Consequently, Fk is b.h and example.cpp because of the condition Image:J_not_equal_K.png.

l=1, ..., lj is the index related to the functions/methods/procedures (call them units) within file Fj (that is, consider the first unit in the file Fj as unit 1, the 2nd as unit 2, ..., the last as unit lj), and

m=1, ..., mk is the index related to the units within file Fk. Units of file a.h are: CLASS Foo, CLASS Bar, and CLASS-DATA-MEMBER foo. In file b.h they are the CLASS ZZZ, in file example.cpp they are the VARIABLEs foo, bar and zzz.

URIi( Fj(l), Fk(m)) is a function that counts the number of instances of a uses relationship (or relationship instances, RI) of type i between unit l of file Fj and unit m within file Fk.(unit l in Fj uses unit m in Fk). It may be 0, 1, or more. There are no relations that have source in file a.h and destination outside file a.h. So, URIi(Fj(l), Fk(m)) for any units acceptable as arguments Fj(l), and Fk(m) will return 0 when COUPLINGU1 is calculated for file a.h . When we sum log2(0 +1) which is 0, the overall sum of COUPLINGU1 is 0.

Wi- is the weight of the relationships of type i. In the example where we calculate COUPLINGU1 for the file a.h, weights are not important since we get 0. Also, by default weights of all the relationships are equal to 1 (all the relations are equal in their influence to function coupling). Potentially we can make some relations more influential and some less influential by setting their weights to higher or lower values (for example, set weight for CLASS-DATA-MEMBER_USES_CLASS to 1 and the weight of VARIABLE_USES_CLASS to 0.5).

Also, as one may see from the formula, the more relations with source in file a.h and destination outside a.h the higher the metric will be. Also the more different pairs of (source, destination) we have, the higher the metric will be.

Several relations for different pairs of (source, destination) contribute to COUPLINGU1 more than the same number of relations between entities of the same pair (source, destination). The logarithm decreases the value of the second, third, (and so on) relations within the same pair (source, destination).

COUPLINGU2, COUPLINGU3 and COUPLINGU4

For the same reason (absence of outgoing relations) COUPLINGU2, COUPLINGU3 and COUPLINGU4 are equal to 0. These are the formulas we use to calculate them:

Image:COUPLINGU2.png

The difference between COUPLINGU1 and COUPLINGU2 is the point where the logarithm is applied. For COUPLINGU1 we calculate relations for each pair of units and apply the logarithm to each. For COUPLINGU2 we don't differentiate between pairs of units; we just sum relations of a specific kind going from entities of one file to entities of another file and apply the logarithm to this count (+1).

Image:COUPLINGU3.png

COUPLINGU3 is almost the same metric as COUPLINGU2, we just don't apply any weights to the relations. Since by default all the relation weights are equal to 1, COUPLINGU2 is equal to COUPLINGU3.

Image:COUPLINGU4.png

COUPLINGU4 does not differentiate between various kinds of relations — it sums relations with the source units in the file (a.h in our example) — and destination units in other files (files other than a.h), then the logarithm is applied to the calculated sum + 1.

Sample file example.cpp: Coupling (uses) metrics

This topic applies the four coupling (uses) metrics to file example.cpp.

There are three relations with the source in file example.cpp and destination outside example.cpp:

foo;VARIABLE_USES_CLASS;Foo;example.cpp;4 bar;VARIABLE_USES_CLASS;Bar;example.cpp;5 zzz;VARIABLE_USES_CLASS;ZZZ;example.cpp;6

So:

URIVARIABLE_USES_CLASS(VARIABLE foo, CLASS Foo) = 1, VARIABLE foo uses CLASS Foo URIVARIABLE_USES_CLASS(VARIABLE bar, CLASS Bar) = 1, VARIABLE bar uses CLASS Bar URIVARIABLE_USES_CLASS(VARIABLE zzz, CLASS ZZZ) = 1, VARIABLE zzz uses CLASS ZZZ

COUPLING U1

As a result, COUPLINGU1 =

log2(URIVARIABLE_USES_CLASS(VARIABLE foo, CLASS Foo) + 1) + log2(URIVARIABLE_USES_CLASS(VARIABLE bar, CLASS Bar) + 1) + log2(URIVARIABLE_USES_CLASS(VARIABLE zzz, CLASS ZZZ)+ 1) = 3

COUPLINGU2 = COUPLINGU3 = COUPLINGU4 =

log2(URIVARIABLE_USES_CLASS(VARIABLE foo, CLASS Foo) + URIVARIABLE_USES_CLASS(VARIABLE bar, CLASS Bar) + 1) + log2(URIVARIABLE_USES_CLASS(VARIABLE zzz, CLASS ZZZ)+ 1) = 2.58496

For COUPLINGU1 we have three logarithms, because the logarithm is applied to the relation count for each pair (source, destination) separately.

COUPLINGU2, COUPLINGU3, and COUPLINGU4

For COUPLINGU2, COUPLINGU3 and COUPLINGU4 we have only two logarithms because we don't differentiate between pairs of units, we differentiate only between pairs of files, and the available pairs are (example.cpp, a.h) and (example.cpp, b.h).

Sample file a.h: Coupling (used-by) metrics

For Klocwork system-level reports on C and C++ systems, the non-trivial measure for used-by coupling is as follows (the following formulas are applied to sample file a.h):

CpUB1 (Fj) =

Image:Functional_file_coupling_formula.png

where

i = 1 ..., n is the index of relevant relation-kind. For file a.h, this applies to VARIABLE_USES_CLASS, CLASS-DATA-MEMBER_USES_CLASS.

Fj is the file of interest in system S, for any j in system S. In this example, it's file a.h.

Fk are other files within the system S(Image:J_not_equal_K.png). The other files are b.h and example.cpp. See Sample files: a.h, b.h, and example.cpp.

l=1, ..., lj is the index related to the functions/methods/classes/types (call them units) within file Fj (that is, consider the first unit in the file Fj as unit 1, the 2nd as unit 2, ..., the last as unit lj). For file a.h, CLASS Foo, CLASS Bar, and

m=1, ..., mk is the index related to the units within file Fk. For file b.h, it's CLASS ZZZ . For file example.cpp, it's VARIABLEs foo, bar and zzz.

UBRIi( Fj(l), Fk(m) ) is a function that counts the number of instances of a used-by relationship (or relationship instances, RI) of type i between unit l of file Fj and unit m within file Fk. It may be 0, 1, or more. Using our sample files,

UBRI(CLASS Foo, VARIABLE foo) = 1, CLASS Foo is used by VARIABLE foo

UBRI(CLASS Bar, VARIABLE bar) = 1. CLASS Boo is used by VARIABLE bar

Wi - weight of the relationships of type i. By default, in the sample files, all weights are equal to 1.

For file a.h:

COUPLINGUB1 = log2(UBRIVARIABLE_USES_CLASS (CLASS Foo, VARIABLE foo) + 1) + log2(UBRIVARIABLE_USES_CLASS (CLASS Bar, VARIABLE bar) + 1) = 2 COUPLINGUB2 = COUPLINGUB3 = COUPLINGUB4 = log2(UBRIVARIABLE_USES_CLASS (CLASS Foo, VARIABLE foo) + UBRIVARIABLE_USES_CLASS (CLASS Bar, VARIABLE bar) + 1) = 1.58496

COUPLINGUB2, COUPLINGUB3 and COUPLINGUB4

For the same reason (absence of outgoing relations) COUPLINGUB2, COUPLINGUB3 and COUPLINGUB4 are equal to 0. These are the formulas we use to calculate them:

Image:COUPLINGUB2.png

The difference between COUPLINGUB1 and COUPLINGUB2 is the point where the logarithm is applied. For COUPLINGUB1 we calculate relations for each pair of units and apply the logarithm to each. For COUPLINGUB2 we don't differentiate between pairs of units; we just sum relations of a specific kind going from entities of one file to entities of another file and apply the logarithm to this count (+1).

Image:COUPLINGUB3.png

COUPLINGUB3 is almost the same metric as COUPLINGUB2, we just don't apply any weights to the relations. Since by default all the relation weights are equal to 1, COUPLINGUB2 is equal to COUPLINGUB3.

Image:COUPLINGUB4.png

COUPLINGUB4 does not differentiate between various kinds of relations — it sums relations with the source units in the file (a.h in our example) — and destination units in other files (files other than a.h), then the logarithm is applied to the calculated sum + 1.

Functional file coupling (total)

A nontrivial measure of the file uses and used-by relationships associated with this file. It is the sum of the file coupling (uses) and file coupling (used-by).

Functional file cohesion

Functional file cohesion is a non-trivial measure of the within-file used-by relationships associated with a file. More specifically, the metric represents a normalized information-dense measure of functions, routines, or units within a file and their relationships with other functions, routines, or units within the same file.

COHESION metrics in general are similar to the used and used-by COUPLING metrics. The difference between COUPLING and COHESION metrics is in the relations that we aggregate to calculate metrics. For COUPLING metrics, we pick relations with the source and destination units located in different files. This is opposite to COHESION metrics for which we pick relations with source and destination units in the same file. Here are the formulas used to calculate COHESION 1 to 4 metrics:

COHESION1 metric

Image:COHESION_Metric1.png

where

i = 1, ..., n is the index of relevant relation-kind,

Fj is the file of interest in system S,

l=1, ..., lj is the index related to the functions/methods/classes/types (call them units) within file Fj (that is, consider the first unit in the file Fj as unit 1, the 2nd as unit 2, ..., the last as unit lj), and

m=1, ..., mj is also the index related to the units within file Fj, except that m,

URIi( Fj(l), Fj(m) ) is a function that counts the number of instances of a uses relationship (or relationship instances, RI) of type i between unit l of file Fj and unit m within file Fj (unit l uses unit m). It may be 0, 1, or more. Using the nomenclature defined earlier, this is equivalent to computing the "micro" version of the metrics defined in the relationships metrics table,

Wi - weight of the relationships of type i.

COHESION2 metric

Image:COHESION_Metric2.png

The COHESION3 metric does not take weights into account. By default, with all the weights equal to 1, COHESION2 is equal to COHESION3.

COHESION3 metric

Image:COHESION_Metric4.png

Using Sample files: a.h, b.h, and example.cpp, the only file that has relationships with source and destination inside is header a.h. And the relation with source and destination in one file is:

foo;CLASS-DATA-MEMBER_USES_CLASS;Foo;a.h;9 So, URICLASS-DATA-MEMBER_USES_CLASS (CLASS-DATA-MEMBER foo, CLASS Foo) = 1

That's why, for file a.h:

COHESION1 = COHESION2 = COHESION3 = COHESION4 = log2(URICLASS-DATA-MEMBER_USES_CLASS (CLASS-DATA-MEMBER foo, CLASS Foo) + 1) = 1