General Polyspace Query Language Syntax
This topic describes general Polyspace® Query Language syntax.
PQL types
PQL supports these types:
Number — Integer, float and boolean numbers
String — strings are modelled by
Lang.Stringclass. SeeString.PQL Classes — Semantic and syntax code elements are modelled by various PQL classes. See:
Comments
PQL supports C++ style single comments and C-stye multiline comments. For example:
package main
// A test catalog containing one test section and one defect under test
catalog TestDoesNotHaveCompoundStatement = {
/* Comment describing the importance of this section
in multiple lines.
*/
#[Description("Test Section")]
section TestSection = {
// Wrap the defect in a rule to make it runnable
#[Description("If statement rules"), Id(TestRule)]
rule TestRuleforDefect = {
If_Statement.DoesNotHaveCompoundStatement
}
}
}
Identifiers
In PQL, declared items such as variables or predicates are identified by an identifier. A valid PQL identifier consists of a combinations of these:
Letters — a to z and A to Z
Numbers — 0 to 9
The
_(underscore) character.
The first character must be either _ or a letter. For
example, ab_78Z is a valid identifier. Because 0abc
starts with a number, it is not a valid identifier.
String Constants
String constants are declared using this syntax:
string myConstantString = "Random String"
String. The
content enclosed in double quotes is encoded in UTF-8 and assigned to the String object
myConstantString.String Constants are useful when constructing context sensitive messages for a user-defined defect or to compare the text of a node to a specific string value.
Inferred Variable Declaration
In PQL, variables can be introduced implicitly by calling predicates with an output
parameter. For example, this predicate declares a variable
var:
Cpp.Function.is(&var)
var contains all function in the code on which
this predicate operates. This variable is then available in later code. For example, this
code declares var and then checks the names of the functions contained in
var:Cpp.Function.is(&var) and var.name(&func_name)
func_name which can similarly be used in
subsequent code.Arithmetic
PQL supports binary and unary arithmetic operations:
Binary — PQL supports all binary arithmetic operations between
intandunsigned intobjects.Unary — PQL supports the unary negation of
intandunsigned int.
Comparison operations
In PQL, You can compare:
Numbers — if there are two integer variables you can compare them using operators such as
<,>,<=and>=.String — You can perform various kinds of string comparison operations between two string objects. See
String.PQL classes — You can check for equality of two objects of the same PQL class. For example, consider this code:
defect VariableNameViolation = when Cpp.Variable.is(&variable1) and variable.namespace(&namespace1) and Cpp.Variable.is(&variable2) and variable2.namespace(&namespace2) and not namespace2 == namespace1 and variable1.name(&name1) and variable2.name(&name2) and name1 == name2 raise "2 variables in different namespace have the same name" on variablenamespace2andnamespace1are objects of classNamespace. PQL support equality check between objects of same class. Similarly, the equality between string objectsname1andname2can also be checked.
Branching in PQL
PQL does not support if-else statements. You can implement branching
using logical statement instead. For example, consider a variable var. If
you want to perform different action depending on different mutually exclusive values of
var, you can use logical statements like this:
(
(var==val1) and (
//.. actions for val1
)
) or (
(var==val2) and (
// action for val2
)
) or (
(var==val3) and (
//... actions for val3
)
)