Cpp.BinaryExpression Class
Namespace: Cpp
Superclasses: AstNodeProperties
Represents the binary_expression nodes in the syntax tree of your code
Since R2026a
Description
The PQL class BinaryExpression represents the node binary_expression in the syntax tree of your code.
// example.cpp
int main() {
int a = 1 + 2;
int b = a * 3;
bool c = a < b;
int d = (a + b) / 2;
(void)c;
return 0;
}The code contains several binary_expression nodes such as a *
3 and 1 + 2. These nodes are matched by the
BinaryExpression PQL class.
Predicates
| Type | Raisable | Printable |
|---|---|---|
BinaryExpression
| 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 BinaryExpression &binaryExpression)
| Matches a binary_expression node and returns it; use to directly select binary expressions in the syntax tree. |
This PQL defect checks for binary expressions anywhere in the code. defect findBinary =
when
Cpp.BinaryExpression.is(&be)
and be.nodeText(&txt)
raise "Found binary expression: \"{txt}\""
on beIn this C++ code, the defect finds every binary expression such as additions and multiplications.
int main() {
int x = 1 + 2; // matches
int y = x * 3; // matches
return 0;
} |
cast(Cpp.Node.Node node, required BinaryExpression &cast)
| Checks whether a general Cpp.Node.Node is a binary_expression; if so, returns it as a BinaryExpression for further inspection. |
This PQL defect checks whether a generic node is specifically a defect castToBinary =
when
Cpp.Node.is(&n, &,&,&)
and Cpp.BinaryExpression.cast(n, &be)
and be.nodeText(&txt)
raise "Node cast to binary_expression: \"{txt}\""
on beIn this C++ code, the defect locates a node known generically (e.g., from a larger query) and verifies it is a binary expression like
int g(int a, int b) {
int r = a * b; // cast will succeed: node is a binary_expression
return r;
} |
isa(Cpp.Node.Node node)
| Returns true if the given Cpp.Node.Node is a binary_expression; useful for boolean checks or negation. | This PQL defect checks that a node is not a
defect notBinary =
when
Cpp.Node.is(&n, &,&,&)
and not Cpp.BinaryExpression.isa(n)
raise "Node is not a binary_expression"
on nIn this C++ code, the defect reports all nodes that are not a binary expression..
int h() {
int x = 42; // for example, the literal '42' is not a binary_expression
return x;
} |
left(BinaryExpression self, Cpp.Node.Node &child)
| Returns the left operand node of a binary_expression (the subexpression before the operator). | This PQL defect checks for binary expressions whose left operand is a specific identifier. defect leftIsX =
when
Cpp.BinaryExpression.is(&be)
and be.left(&lhs)
and lhs.nodeText(&txt)
and txt == "x"
raise "Binary expression with left operand 'x' found"
on beIn this C++ code, the defect looks for binary
expressions where
int main() {
int x = 1, y = 2;
int z = x + y; // matches
int w = u + y; // does not match
return z;
} |
operator(BinaryExpression self, Cpp.Node.Node &child)
| Returns the operator token node of a binary_expression (e.g., +, -, *, /, ==, &&). | This PQL defect checks for binary expressions using a specific operator token. defect plusOperator =
when
Cpp.BinaryExpression.is(&be)
and be.operator(&op)
and op.nodeText(&txt)
and txt == "+"
raise "Binary expression using '+' found"
on beIn this C++ code, the defect finds expressions that
use the
int sum(int a, int b, int c) {
return a + b - c; // operator + is matched, - is not matched
} |
right(BinaryExpression self, Cpp.Node.Node &child)
| Returns the right operand node of a binary_expression (the subexpression after the operator). | This PQL defect checks for binary expressions whose right operand is a particular literal. defect rightIsZero =
when
Cpp.BinaryExpression.is(&be)
and be.right(&rhs)
and rhs.nodeText(&txt)
and txt == "0"
raise "Binary expression with right operand 0 found"
on beIn this C++ code, the defect locates binary
expressions where the right operand is the constant
bool is_zero(int a) {
return a == 0; // right operand is '0'
} |
getEnclosingBinaryExpression(Cpp.Node.Node child, required BinaryExpression &parent)
| Finds the nearest enclosing binary_expression ancestor for a given node and returns that parent expression. | This PQL defect checks which binary expression directly contains a given subnode. defect enclosingBinary =
when
Cpp.Node.is(&n, &,&,&)
and Cpp.BinaryExpression.getEnclosingBinaryExpression(n, &parent)
and parent.nodeText(&txt)
raise "Enclosing binary expression: \"{txt}\""
on parentIn this C++ code, the defect finds the binary expression that encloses a syntax node.
int foo(int a, int b, int c) {
int v = a + b * c; // Enclosing binary_expression flagged
return v;
} |
isEnclosedInBinaryExpression(Cpp.Node.Node child)
| Returns true when the given node has any binary_expression as an ancestor; useful to detect nodes inside binary expressions. | This PQL defect checks whether a node appears anywhere inside a binary expression. defect insideBinary =
when
Cpp.Node.is(&n, &,&,&)
and Cpp.BinaryExpression.isEnclosedInBinaryExpression(n)
raise "Node is inside a binary_expression"
on nIn this C++ code, the defect flags nodes that are
inside binary expressions, such as identifiers inside
int bar(int a, int b) {
int r = (a + b); // all nodes inside the binary expression flagged
return r;
} |
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)