-classification
Control precisely which files to include in Polyspace analysis and how to analyze them
Since R2023a
Syntax
-classification <file>
Description
-classification <file>
specifies which files to include in a
Polyspace® analysis and how to analyze them. Here, <file>
is an
XML file that enumerates file sets and the behaviors that Polyspace uses with those
files.
For instance, you can skip the definitions of function bodies in third-party libraries. When running a Bug Finder analysis, you can force analysis of all functions in files that you own.
If you run a verification:
At the command line, specify the absolute path to the XML files or path relative to the folder from which you run the command.
In the user interface (desktop products only), in the Other field, specify the option along with an absolute path to the XML or the path relative to the location of your Polyspace project. See
Other
.
A report generated from the analysis results only show the use of this option and not the details of which behaviors were associated with code elements.
A sample template file classification-template.xml
shows the XML syntax. The file is in
where polyspaceroot
\polyspace\verifier\cxx\
is the Polyspace installation folder. polyspaceroot
You can associate a file classification with one or more products. That is, you can reuse the same classification XML file for a Bug Finder, Code Prover, and Polyspace as You Code analysis. Polyspace Code Prover™ supports a subset of classification XML syntax. For details about the syntax, see Classification XML File Syntax.
Examples
Exclude Generated Files in Bug Finder Analysis
Suppose your source code consists of the files utils.c
,
utils_generated.c
, and header.h
. Both
.c
files contain a comparison that incorrectly uses an
=
operator instead of the ==
operator.
src\utils.c
:#include "header.h" int getEvenElement(int *arr, int size, int idx) { int isIndexEven = isEven(idx); if(isIndexEven = 0 && idx < size) // Invalid use of = operator return arr[idx]; else return -1; }
src\utils_generated.c
:#include "header.h" int isEven(int arg) { int rem = arg%2; if(rem = 0) { // Invalid use of = operator return 1; } else { return 0; } }
inc\header.h
:int isEven(int);
Run the default Bug Finder analysis by using one of these commands:
polyspace-bug-finder -sources src\utils.c,src\utils_generated.c -I inc\
polyspace-bug-finder-server -sources src\utils.c,src\utils_generated.c -I inc\
You see an Invalid use of = operator
defect in both .c
files.
Suppose your generated files follow a naming convention and end with _generated
. To exclude these files from analysis:
Write a classification file that defines a set of files to exclude from analysis. The XML file excludes files with filename that end in _generated.
Specifying the pattern<?xml version="1.0" encoding="UTF-8"?> <specification> <classification product="bug-finder"> <fileset name="generated code"> <files-in-set> <file-pattern>src/**/*_generated.c</file-pattern> </files-in-set> <behaviors> <do-not-analyze-functions> <function-pattern>*</function-pattern> </do-not-analyze-functions> <show-results value="false"/> </behaviors> </fileset> </classification> </specification>
src/**/*_generated.c
in the nodefile-pattern
excludes all.c
files in thesrc
folder and all subfolders that have filenames that end in_generated.c
. For more information on the syntax of classification XML files, see Classification XML File Syntax.Save the classification file as
exclude_generated_files.xml
. Specify this classification file with the option-classification
by using one of these commands:polyspace-bug-finder -sources src\utils.c,src\utils_generated.c -I inc\ -classification exclude_generated_files.xml
polyspace-bug-finder-server -sources src\utils.c,src\utils_generated.c -I inc\ -classification exclude_generated_files.xml
Polyspace does not report the Invalid use of = operator defect
utils_generated.c
.
Exclude Functions from a Specific Namespace in Code Prover Analysis
Consider this code:
namespace NamespaceA {
class Widget {
public:
int foo(int x) {
return 42 / x; // Division by zero
}
};
}
namespace NamespaceB {
class Gadget {
public:
int foo(int x) {
return 42 / x; // Division by zero
}
};
}
int main() {
volatile int rd = 0;
if(rd) {
NamespaceA::Widget w;
w.foo(0);
}
if(rd) {
NamespaceB::Gadget g;
g.foo(0);
}
}
Division by zero
(Polyspace Code Prover) check in
NamespaceA::Widget::foo()
and another one in
NamesapceB::Gadget::foo()
.To exclude functions from a specific namespace while running a Code Prover analysis:
Write a classification XML file and define a set of file that encompasses all relevant files in the current analysis.
By specifying the function pattern<?xml version="1.0" encoding="UTF-8"?> <specification> <classification product="code-prover"> <fileset name="all_files"> <files-in-set> <file-pattern>$(source-files)</file-pattern> </files-in-set> <behaviors> <analyze-functions> <function-pattern>$(all-relevant-functions)</function-pattern> </analyze-functions> <do-not-analyze-functions> <function-pattern>::NamespaceA::</function-pattern> </do-not-analyze-functions> <show-results value="true"/> </behaviors> </fileset> </classification> </specification>
$(all-relevant-functions)
in the nodeanalyze-functions
, you specify that the analysis must analyze the called functions. You exclude the functions in namespaceNamespaceA
by using the pattern::NamespaceA::
in the nodedo-not-analyze-function
. For more information on the syntax of classification XML files, see Classification XML File Syntax.Save the classification file as
classification_namespace_example.xml
. Specify this classification file with the option-classification
when you run a Code Prover analysis:Polyspace no longer reports a redpolyspace-code-prover -sources src.cpp -classification classification_namespace_example.xml
Division by zero
check inNamespaceA::Widget::foo()
.
More About
Tips
If you use the option
Generate results for sources and (-generate-results-for)
orDo not generate results for (-do-not-generate-results-for)
with the option-classification
, Polyspace reports an error.If you use the option
-classification
with the optionFunctions to stub (-functions-to-stub)
, the functions specified as input to-function-to-stub
are stubbed regardless of how they are classified in the classification XML file.If you specify an entry point function, Polyspace analyzes the entry point function regardless of how the functions are classified in the classification XML file. If the classification XML is configured to hide the result of these functions, Polyspace hides the result after the analysis. The options to specify entry points are:
The classification XML syntax currently does not support symbolic links. Say you have a file
src.c
that is a symbolic link to the target fileactual_src.c
. If the pattern you specify in the classification XML matchessrc.c
, Polyspace does not resolve the symbolic link toactual_src.c
. Instead, Polyspace attempts to match the specified pattern toactual_src.c
. Avoid using symbolic links when creating classification XML files.