Define custom metrics with xxxCastMetrics.xml
Overview
An xxxCastMetrics.xml file defines custom metrics that are calculated by searching the source code with regular expressions. These metrics do not use the grammar of the language. You can define them for a Universal Analyzer language, or for a technology analyzed by a standard CAST analyzer such as Cobol.
File name and location
The file name must end with CastMetrics.xml. CAST recommends prefixing it with the name of the technology, for example PHPCastMetrics.xml. Create one file per technology.
Save the file in the language folder of the extension:
<extension folder>\Configuration\Languages\<technology folder>\xxxCastMetrics.xml
- For a Universal Analyzer language, the technology folder is the language package folder, which already contains the metamodel and the language pattern. If the language package already has an
xxxCastMetrics.xmlfile, add your metrics in a second file, for examplePHPCustomCastMetrics.xml, rather than editing the existing one. - For a technology analyzed by a standard CAST analyzer, create the technology folder yourself, with the name that CAST uses for that technology. For example, custom metrics for Cobol go in
Cobol\CobolCastMetrics.xml.
Save the file in UTF-8, with this XML declaration:
<?xml version="1.0" encoding="utf-8"?>
File structure
The root element METRIC_LIST contains one METRIC element per metric. Each metric contains one LANGUAGE element per technology it applies to:
<?xml version="1.0" encoding="utf-8"?>
<METRIC_LIST>
<METRIC Name="Use of printf in a do/while" TYPE="2009000">
<LANGUAGE NAME="C-LANGUAGE">
<!-- search configuration for C/C++ -->
</LANGUAGE>
</METRIC>
<METRIC Name="Another metric" TYPE="2009001">
<LANGUAGE NAME="UNIVERSAL-LANGUAGE">
<!-- search configuration -->
</LANGUAGE>
</METRIC>
</METRIC_LIST>
| Attribute | Description |
|---|---|
METRIC/@Name |
The name of the metric. It must be unique among all metrics in the analysis schema. |
METRIC/@TYPE |
The ID of the metric, also called InfSubTyp in the analysis schema. It must be unique and higher than 2,000,000. See Reserve ID ranges. |
LANGUAGE/@NAME |
The technology the metric applies to. See Language names. |
Language names
Use one of the following values in LANGUAGE NAME:
ASP-LANGUAGE, ASPX-LANGUAGE, ABAP-LANGUAGE, ANSISQL-LANGUAGE, ASETSQL-LANGUAGE, BO-LANGUAGE, C-LANGUAGE, COBOL-LANGUAGE, C-SHARP-LANGUAGE, DB2-LANGUAGE, DB2ZOS-LANGUAGE, PROPFORMS-LANGUAGE, FORMS-LANGUAGE, HTML-LANGUAGE, IMS-LANGUAGE, JSP-LANGUAGE, JAVA-LANGUAGE, JCL-LANGUAGE, JAVA-SCRIPT-LANGUAGE, MSTSQL-LANGUAGE, PB-LANGUAGE, VB-LANGUAGE, VB-SCRIPT-LANGUAGE, VB-DOTNET-LANGUAGE, UNIVERSAL-LANGUAGE
Search configuration
Each LANGUAGE element configures the search with the following elements:
| Element | Description |
|---|---|
ACTIVE |
yes to calculate the metric. |
INIT |
A value added to the metric for every object that matches. Objects that do not match get no value. |
SEARCH_IN_CODE |
Yes to search the source code. |
SEARCH_IN_COMMENT |
Yes to search comments. |
SEARCH_IN_STRING |
Yes to search strings. |
SEARCH_IN_EMBEDDEDSQL |
Yes to search embedded SQL. |
SEARCH_CASE_SENSITIVE |
Yes for a case-sensitive search. |
MATCH_WHOLE_WORD_ONLY |
Yes to match whole words only. |
REGEXP |
A regular expression for a simple search. See Simple search. |
EMBEDDED |
A nested search. See Nested search. |
Simple search
A simple search uses one or more REGEXP elements. The search succeeds when at least one of them matches. For example, the following metric counts exit statements in shell scripts:
<METRIC Name="Shell_EXIT_CODE" TYPE="2005000">
<LANGUAGE NAME="SHELL-LANGUAGE">
<ACTIVE>yes</ACTIVE>
<INIT>0</INIT>
<SEARCH_IN_CODE>Yes</SEARCH_IN_CODE>
<SEARCH_IN_COMMENT>No</SEARCH_IN_COMMENT>
<SEARCH_IN_STRING>No</SEARCH_IN_STRING>
<SEARCH_CASE_SENSITIVE>No</SEARCH_CASE_SENSITIVE>
<REGEXP><![CDATA[exit ]]></REGEXP>
</LANGUAGE>
</METRIC>
Nested search
A nested search matches a pattern that has a beginning, a content and an end, such as a loop:
<EMBEDDED>
<BEGIN>
<REGEXP>do</REGEXP>
</BEGIN>
<VALUE>
<REGEXP>printf</REGEXP>
</VALUE>
<END>
<REGEXP>while</REGEXP>
</END>
</EMBEDDED>
Two optional elements change how the value is calculated:
THRESHOLD: if the number of matches in an object is lower than the threshold, the metric is ignored for that object. Otherwise, the metric is set to1.ADD: a value added to the metric for every level of nesting of theBEGINandENDpatterns.
For example, with THRESHOLD set to 5, the following code matches four times, so no value is saved. With THRESHOLD set to 4, the value 1 is saved:
void testThreshold()
{
printf("Out of the loop");
do
{
printf("Hello World!");
printf("Hello World!");
printf("Hello World!");
printf("Hello World!");
}
while(true);
}
To see how INIT and ADD combine, take metric A with INIT 5, ADD 2, BEGIN AAA, VALUE ZZZ and END BBB, and metric B with the same settings but ADD 0. On this code:
AAA
ZZZ
BBB
- metric A is 5 + 2 + 1 = 8 (
INIT+ADD+ number of matches) - metric B is 5 + 1 = 6 (
INIT+ number of matches)
On this code, with two levels of nesting:
AAA
AAA
ZZZ
BBB
BBB
- metric A is 5 + 2 + 2 + 1 = 10 (
INIT+ 2 xADD+ number of matches) - metric B is 5 + 1 = 6
Declare each metric in the metamodel
Object properties are saved in the analysis results only if they are declared in a metamodel. For each metric you define on objects, declare an equivalent property in the xxxMetaModel.xml file of the extension, in a category dedicated to custom metric properties.
The property must have an INF_TYPE attribute and an INF_SUB_TYPE attribute, where INF_SUB_TYPE is equal to the TYPE of the metric. For example, for the Shell_EXIT_CODE metric above:
<category name="SHELL_Metric_Properties" rid="42">
<description>Shell Metric Properties</description>
<property name="Shell_EXIT_CODE" type="string" rid="56">
<description>Shell_EXIT_CODE</description>
<attribute name="ACCESS_APPVIEW" intValue="1"/>
<attribute name="ACCESS_CVS" intValue="1"/>
<attribute name="ACCESS_HTML" intValue="1"/>
<attribute name="INF_TYPE" intValue="9"/>
<attribute name="INF_SUB_TYPE" intValue="2005000"/>
</property>
</category>
If a metric is not declared in a metamodel, its values are not saved.