Contenuto principale

Overview of Semantic Classes in Polyspace Query Language

Semantic information in code captures the meaning and relationships between program elements such as which function is called, what types variables have, or how classes are related. In other words, semantic information captures what your code does and how its components interact, rather than just how it is written syntactically. For example, consider these two functions:

int square(int x) {
    return x * x;
}
auto square(int x) -> int {
    return x * x;
}

Even though the syntax of these two functions is different, these functions are semantically the same because they have the same return type, same number of arguments, and same operations in their bodies.

Polyspace® Query Language (PQL) has classes that model common semantic objects in C/C++ code such as call sites, fields, variables, functions, macros, and namespaces. These classes contain predicates that query the code to find specific semantic objects. This topic summarizes the semantic classes in PQL. Use these classes to create user-defined semantic defects. For more information about how these classes are used, see Detect Semantic Issues Using Polyspace Query Language Semantic Classes.

To review the PQL semantic classes, see Create Your Own Coding Rules and Coding Standard. Once you initialize a coding standard using polyspace-query-language init, these classes are located in the .polyspace/api/cpp/semantic folder.

Semantic Classes for Objects with Specific Positions

Some semantic objects have a specific location in the code. Because these objects have a specific position, Polyspace Bug Finder™ can report PQL defects at the location of the object, which can help contextualize the result. Semantic objects with positions are represented by these classes that inherit from the PQL class ObjectWithPosition:

  • CallSite — Call site for a function

  • Cast — C++ cast from one type to another. This class covers both implicit and explicit casts.

  • Field — Data member of a C++ struct, class, or union.

  • Function — Functions in your code, including free functions, member function of a class, or static functions.

  • Type — Type of a C++ object.

  • Variable — Variable.

Semantic Classes for Objects without Specific Position

Some semantic objects do not have a specific location in the code. Because these objects lack a specific position, Polyspace Bug Finder cannot report PQL defects at the specific location of the object. For example, a macro can be defined in multiple places and invoked in multiple places. It is not possible to identify a single location for a macro. PQL classes for semantic objects without specific position are:

You can detect and query the properties of these objects. But to report a defect, find a related object with a specific position and report the defect on it.

See Also