주요 콘텐츠

Cpp.ObjectWithPosition Class

R2026b

Namespace: Cpp

Represents an object that has a known position

Since R2026a

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

expand all

Child Classes

The class ObjectWithpsotion has these child classes.

Child ClassDescription
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

collapse all

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;
}
The global variable 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.

  1. In a new folder ObjectWithExample, initialize a new coding standard. At the command line, enter:

    polyspace-query-language init

  2. 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
    
    		}
    	}
    }
    
    

  3. Create the coding standard ObjectWithExample.pschk using this command at the command line:

    polyspace-query-language package

  4. Using the generated coding standard, run a Bug Finder analysis on your source file. For example, at the command line, enter:

    polyspace-bug-finder -sources src.cpp -lang cpp -checkers-activation-file ObjectWithExample.pschk
    The analysis reports the variable shadowing on the local variable named counter.

Version History

Introduced in R2026a