Cpp.Field Class
Namespace: Cpp
Superclasses: ObjectWithPosition
Description
Field class represent an object that is member of a structure, class, or
union. This class inherits from the class ObjectWithPosition. You can use the predicates associated with this class and the
base class to query its properties such as name, type, mutability and others.
Predicates
You can report a defect on Raisable types. If a type is
Printable, it can be reported in the message. For
Field objects, print the string obtained by the predicate
Cpp.Field.name.
| Type | Raisable | Printable |
|---|---|---|
Field
| Yes | No |
Lang.Int
| No | Yes |
Lang.String
| No | Yes |
This class defines these predicates that act on the Field objects. In
addition, objects of Field class can access the predicates defined by the
base class ObjectWithPosition. An object of Field class is an object of
ObjectWithPosition class.
| Predicate | Description | Example |
|---|---|---|
is(Field &field)
| Retrieves all Field object in your code, including structure
and class member variables, bitfields, anonymous fields, and other fields. |
This rule flags the structure and class member variables in your code: rule is = {
defect Testis =
when Cpp.Field.is(&Field)
raise "Field detected"
on Field
} |
isUnused(Field self)
| Retrieves unused Field objects in your C/C++ code. |
This rule flags all unused fields in your code: rule isUnused = {
defect TestisUnused =
when Cpp.Field.is(&Field)
and Field.isUnused()
raise "Unused field detected"
on Field
} |
isAnonymous(Field self)
| Retrieves anonymous Field objects in your C/C++
code. |
This rule flags the anonymous fields in your code: rule isAnonymous = {
defect TestisAnonymous =
when Cpp.Field.is(&Field)
and Field.isAnonymous()
raise "Anonymous field detected"
on Field
} |
name(Field self, Lang.String &name)
| Retrieves the name of the object self as a string and stores
the string in name. |
This rule flags the unused fields in your code that are not anonymous and reports their names in the message: rule name = {
defect Testname =
when Cpp.Field.is(&Field)
and Field.isUnused()
and not Field.isAnonymous()
and Field.name(&name)
raise "Unused Field detected \"{name}\" "
on Field
} |
type(Field self, Cpp.Type.Type &type)
| Retrieves the type of the object self and stores the type in
type. |
This rule flags the fields in your code that has the type
rule type = {
defect Testtype =
when Cpp.Field.is(&Field)
and Field.type(&type)
and type.toString(&typestr)
and typestr== "int"
raise "Integer type Field detected"
on Field
} |
enclosingStructClassOrUnion(Field self, Cpp.Type.Type
&type)
| Retrieves the type of the enclosing structure, class, or union of which
self is a member. |
This rule flags the fields in your class that are members of a union: rule enclosingStructClassOrUnion = {
defect TestenclosingStructClassOrUnion =
when Cpp.Field.is(&Field)
and Field.enclosingStructClassOrUnion(&enclosingType)
and enclosingType.toString(&tstr)
and tstr.startsWith("union")
raise "Union member field detected \"{tstr}\""
on Field
} |
isPrivate(Field self)
| Retrieves the Field objects in your C/C++ code that are
private. |
This rule flags the private fields in your code: rule isPrivate = {
defect TestisPrivate =
when Cpp.Field.is(&Field)
and Field.isPrivate()
raise "Private Field detected"
on Field
} |
isProtected(Field self)
| Retrieves the Field objects in your C/C++ code that are
protected. |
This rule flags the protected fields in your code: rule isProtected = {
defect TestisProtected =
when Cpp.Field.is(&Field)
and Field.isProtected()
raise "Protected Field detected"
on Field
} |
isPublic(Field self)
| Retrieves the Field objects in your C/C++ code that are
public. |
This rule flags the public fields in your code: rule isPublic = {
defect TestisPublic =
when Cpp.Field.is(&Field)
and Field.isPublic()
raise "Public Field detected"
on Field
} |
isMutable(Field self)
| Retrieves the Field objects in your C/C++ code that are
specified as mutable. |
This rule flags the mutable fields in your code: rule isMutable = {
defect TestisMutable =
when Cpp.Field.is(&Field)
and Field.isMutable()
raise "Mutable Field detected"
on Field
} |
offsetInBits(Field self, Lang.Int
&offset)
| Retrieves the bit offset of the field self as an integer and
stores the value in offset. |
This rule reports the offset in bits for the fields in your code: rule offsetInBits = {
defect TestoffsetInBits =
when Cpp.Field.is(&Field)
and Field.offsetInBits(&offset)
raise "Field offset: \"{offset}\""
on Field
} |
isBitField(Field self)
| Retrieves the fields that are bitfields. |
This rule flags the bit fields in your code. rule isBitField = {
defect TestisBitField =
when Cpp.Field.is(&Field)
and Field.isBitField()
raise "Field detected"
on Field
} |
order(Field self, Lang.Int &order)
| Retrieves the declaration order of the field self. For a
union, all fields have the order 0. For
classes and structures, the first declared field has the order 0. |
This rule reports a violation if a bitfield has the
rule order = {
defect Testorder =
when Cpp.Field.is(&Field)
and Field.isBitField()
and Field.order(&order)
and order==1
raise "BitField declared as second variable \"{order}\""
on Field
} |
sizeInBits(Field self, Lang.Int &size)
| Retrieves the size of the field self and stores the value in
size |
This rule reports a violation if a bitfield is declared as the second field in a class or structure and its size is smaller than 16 bits. rule sizeInBits = {
defect TestsizeInBits =
when Cpp.Field.is(&Field)
and Field.isBitField()
and Field.order(&order)
and order==1
and Field.sizeInBits(&size)
and size < 16
raise "Too small BitField declared as second variable"
on Field
} |
isPadding(Field self)
| Retrieves the padding fields in your code. |
This rule flags the padding fields in your code. rule isPadding = {
defect TestisPadding =
when Cpp.Field.is(&Field)
and Field.isPadding()
raise "Padding field detected"
on Field
} |
Examples
In a new folder
Field, 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 FieldExample = { #[Description("Example Section")] section ExampleSection = { #[Description("Unused public field in header"),Id(myRule)] rule ExampleRule = { defect Exampledefect = when Cpp.Field.is(&field) and field.isUnused() and field.isPublic() and field.extension(&ext) and ext == ".h" and field.filename(&fnstr) raise "Unused public field in header: \"{fnstr}\"" on field } } }Create the coding standard
Field.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. Foe example, at the command line, enter:
The analysis reports defects on the unused member variablepolyspace-bug-finder -sources src.cpp -lang cpp -checkers-activation-file Field.pschk
unusedFieldin the header filesrc.h.
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)