Cpp.ObjectWithPosition Class
R2026bNamespace: Cpp
Description
ObjectWithPosition class represents an object that has a position, such
as call sites, variables, types, casts, fields, and functions. These objects are
represented by classes that inherit from ObjectWithPosition.
The class ObjectWithPosition is a superclass. It cannot interact with
your C/C++ code directly. The child classes of this class can invoke the predicates of this
class to analyze your C/C++ code.
Predicates
The predicates in this class uses objects of these types:
| Type | Raisable | Printable |
|---|---|---|
ObjectWithPosition
| Yes | No |
Lang.Int
| No | Yes |
Lang.String
| No | Yes |
This class defines these predicates. All classes that inherit from this class has
access to these predicates. For example, the child class CallSite has
access to these predicates and objects of class CallSite can invoke
these predicates. Predicates that accept a self object can be invoked
on an object using the dot notation. For example, for an object foo
of class ObjectWithPosition, the predicate locPath
can be invoked as foo.locpath(&path).
| Predicate | Description |
|---|---|
locPath(ObjectWithPosition self, Cpp.SourceLoc.Path
&path)
| Retrieves the location of the C/C++ source file where the
self is defined or declared, and assigns the path to
the variable path. |
extension(ObjectWithPosition self, Lang.String
&extension)
| Retrieves the path to the C/C++ source file in which
self is declared or defined and checks if the
extension of the file is extension.
|
stem(ObjectWithPosition self, Lang.String &stem)
| Retrieves the path to the C/C++ source file in which
self is declared or defined and checks if the name of
the file after excluding the file extension is stem.
|
path(ObjectWithPosition self, Lang.String
&pathString)
| Retrieves the path to the C/C++ source file in which
self is declared or defined and checks if the string
containing the path is pathString.
|
filename(ObjectWithPosition self, Lang.String
&fileNameString)
| Retrieves the path to the C/C++ source file in which
self is declared or defined and checks if the file
name is fileNameString.
|
line(ObjectWithPosition self, Lang.Int &line)
| From the C/C++ source files at which the object self
is defined, retrieves the line number and stores the line number in
line. |
column(ObjectWithPosition self, Lang.Int &column)
| From the C/C++ source files at which the object self
is defined, retrieves the column number at which the object
self is defined and stores the column number in
column. |
isInSameFile(ObjectWithPosition self, ObjectWithPosition
obj)
| Retrieves the objects self that are defined in the
same C/C++ source file as obj. |
isBefore(ObjectWithPosition self, ObjectWithPosition obj)
| Retrieves the objects self that are defined before
the object obj in the C/C++ source file. |
isSameLine(ObjectWithPosition self, ObjectWithPosition
obj)
| Retrieves the objects self that are defined
at the same line as the object obj in
the C/C++ source file. |
linesBetween(ObjectWithPosition self, ObjectWithPosition obj,
Lang.Int &nlines)
| Retrieves the number of lines between the objects
self and obj in the C/C++ source
file and stores the number in nlines |
sourceLocation(ObjectWithPosition self, Cpp.SourceLoc.Path
&file, Lang.Int &line, Lang.Int &column)
|
For an object, retrieves:
|
Child Classes
The class ObjectWithpsotion has these child classes.
| Child Class | Description |
|---|---|
Cpp.CallSite
| Represents function call sites. |
Cpp.Cast
| Represents casts. |
Cpp.Field
| Represents member variables of structures, classes, and unions. |
Cpp.Function
| Represents a callable function. |
Cpp.Type
| Represent a built-in or user-defined type. |
Cpp.Variable
| Represents a variable. |
Examples
Consider this code:
#include <iostream>
// File-static variable
static int counter = 100;
void printCounters() {
// Local variable with the same name
int counter = 5; // variable shadowing issue
std::cout << "Local counter: " << counter << std::endl;
// To access the file-static 'counter', you need to rename one of them;
// otherwise, 'counter' always refers to the local variable in this scope.
}
void incrementFileStaticCounter() {
counter++;
std::cout << "File-static counter incremented to: " << counter << std::endl;
}
int main() {
printCounters(); // Prints the local variable
incrementFileStaticCounter(); // Increments and prints the file-static variable
incrementFileStaticCounter(); // Again increments and prints the file-static variable
return 0;
}counter is shadowed by a local
variable that is declared using the same name and in the same file. Use the predicates
of this class and its children classes to detect this issue. Copy the code in the file
src.cpp for later use.
In a new folder
ObjectWithExample, initialize a new coding standard. At the command line, enter:polyspace-query-language init
In the file
main.pql, enter this content:package main // Main PQL file defines the catalog of your PQL project. // The catalog is a collection of sections. catalog ObjectWithpositionExample = { #[Description("Example Section")] section ExampleSection = { #[Description("Local variable shadows global variable"),Id(myRule)] rule ExampleRule = { defect Exampledefect = when Cpp.Variable.is(&gVar) and gVar.isFileStatic() and gVar.name(&GlobalName) and Cpp.Function.is(&Function) and Function.localVariable(&localVar) and localVar.name(&LocalName) and LocalName == GlobalName and localVar.isInSameFile(gVar) raise "Local and global share name\"{LocalName}\"" on localVar } } }Create the coding standard
ObjectWithExample.pschk using this command at the command line:polyspace-query-language package
Using the generated coding standard, run a Bug Finder analysis on your source file. For example, at the command line, enter:
The analysis reports the variable shadowing on the local variable namedpolyspace-bug-finder -sources src.cpp -lang cpp -checkers-activation-file ObjectWithExample.pschk
counter.
Version History
Introduced in R2026a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)