Cpp.OffsetofExpression Class
Namespace: Cpp
Superclasses: AstNodeProperties
Represents the offsetof_expression nodes in the syntax tree of your code
Since R2026a
Description
The PQL class Cpp.OffsetofExpression represents the node offsetof_expression in the syntax tree of your code. It models an offsetof(type, member) expression in C/C++.
#include <cstddef>
struct MyStruct {
int field;
double anotherField;
};
void foo() {
std::size_t offset1 = offsetof(MyStruct, field);
std::size_t offset2 = offsetof(MyStruct, anotherField);
}The two offsetof(MyStruct, ...) expressions in foo
correspond to offsetof_expression nodes and are the nodes matched by
Cpp.OffsetofExpression class.
Predicates
| Type | Raisable | Printable |
|---|---|---|
OffsetofExpression
| Yes | No |
This class defines these predicates that act on the objects of this class. In addition, objects of this class can access the predicates defined by the base class AstNodeProperties. An object of this class is an object of AstNodeProperties class.
| Predicates | Description | Example |
|---|---|---|
is(required OffsetofExpression &oe)
| Checks whether a node in the query is an offsetof_expression
and binds it to oe. Use it to start working directly with an
offsetof node. | This PQL defect checks for any defect find_offsetof = when
Cpp.OffsetofExpression.is(&oe)
and oe.nodeText(&txt)
raise "Found offsetof: \"{txt}\""
on oeIn this C++ code, the defect finds each
#include <cstddef>
struct MyStruct { int field; };
int main() {
std::size_t o = offsetof(MyStruct, field);
(void)o;
return 0;
} |
cast(Cpp.Node.Node node, required OffsetofExpression &cast)
| Binds cast to the offsetof_expression if
the given node is such an expression. Use when you already have a
generic Node and need the specific
OffsetofExpression. | This PQL defect checks for an defect cast_node_to_offsetof =
when
Cpp.CompoundStatement.is(&cs)
and cs.getADescendant(&node)
and Cpp.OffsetofExpression.cast(node, &oe)
and oe.nodeText(&txt)
raise "Cast to offsetof matched: \"{txt}\""
on oeIn this C++ code, the defect finds the
#include <cstddef>
struct S { int a; };
int main() {
std::size_t off = offsetof(S, a);
(void)off;
return 0;
} |
isa(Cpp.Node.Node node)
| Returns true if the given generic node is an
offsetof_expression. Use in boolean checks or negations where
you have a Node. | This PQL defect checks for defect cast_node_to_offsetof =
when
Cpp.CompoundStatement.is(&cs)
and cs.getADescendant(&node)
and Cpp.OffsetofExpression.isa(node)
and node.nodeText(&txt)
raise " offsetof matched: \"{txt}\""
on nodeIn this C++ code, the defect finds the
#include <cstddef>
struct T { double x; };
int main() {
std::size_t o = offsetof(T, x);
(void)o;
return 0;
} |
type(OffsetofExpression self, Cpp.Node.Node &child)
| Binds child to the type node used as the first argument of
the offsetof. Use it to inspect the type whose member offset is
taken. | This PQL defect checks for the type component inside an
defect offsetof_type =
when
Cpp.OffsetofExpression.is(&oe)
and oe.type(&tp)
and tp.nodeText(&txt)
raise "offsetof type: \"{txt}\""
on oeIn this C++ code, the defect extracts
#include <cstddef>
struct MyStruct { int field; };
int main() {
std::size_t x = offsetof(MyStruct, field);
(void)x;
return 0;
} |
member(OffsetofExpression self, Cpp.Node.Node &child)
| Binds child to the member node used as the second argument
of the offsetof. Use it to inspect which member's offset is
requested. | This PQL defect checks for the member identifier inside an
defect offsetof_member =
when
Cpp.OffsetofExpression.is(&oe)
and oe.member(&mb)
and mb.nodeText(&txt)
raise "offsetof member: \"{txt}\""
on oeIn this C++ code, the defect extracts
#include <cstddef>
struct MyStruct { int field; };
int main() {
std::size_t y = offsetof(MyStruct, field);
(void)y;
return 0;
} |
getEnclosingOffsetofExpression(Cpp.Node.Node child, required
OffsetofExpression &parent)
| Finds the nearest offsetof_expression ancestor of
child and binds it to parent. Use it when
you have a subnode and need the surrounding offsetof. | This PQL defect checks for an defect find_enclosing_offsetof =
when
Cpp.TypeIdentifier.is(&id)
and id.toNode(&node)
and Cpp.OffsetofExpression.getEnclosingOffsetofExpression(node, &oe)
and oe.nodeText(&txt)
raise "Enclosing offsetof: \"{txt}\""
on oeIn this C++ code, the defect starts from the type
identifier node
#include <cstddef>
struct MyStruct { int field; };
int main() {
std::size_t z = offsetof(MyStruct, field);
(void)z;
return 0;
} |
isEnclosedInOffsetofExpression(Cpp.Node.Node child)
| Returns true when the given child node has an
offsetof_expression as an ancestor. Use it to test whether a
node appears inside an offsetof. | This PQL defect checks whether a node is inside any
defect inside_offsetof =
when
Cpp.FieldIdentifier.is(&id)
and id.toNode(&node)
and Cpp.OffsetofExpression.isEnclosedInOffsetofExpression(node)
raise "Field Identifier is inside an offsetof"
on nodeIn this C++ code, the defect detects that the
identifier
#include <cstddef>
struct MyStruct { int field; };
int main() {
std::size_t k = offsetof(MyStruct, field);
(void)k;
return 0;
} |
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)