Cpp.NewExpression Class
Namespace: Cpp
Superclasses: AstNodeProperties
Represents the new_expression nodes in the syntax tree of your code
Since R2026a
Description
The PQL class Cpp.NewExpression represents the node new_expression in the syntax tree of your code.
struct MyClass {
MyClass(int, int) {}
};
void foo() {
int *a = new int;
int *b = new int[10];
MyClass *c = new MyClass(1, 2);
MyClass *d = new(ptr) MyClass;
}The example contains four new_expression occurrences. Each new expression in the code corresponds to a Cpp.NewExpression node.
Predicates
| Type | Raisable | Printable |
|---|---|---|
NewExpression
| 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 NewExpression &ne)
| Matches a new_expression node and binds it to
ne. Use this to select new expressions
directly. | This PQL defect checks for defect find_new =
when
Cpp.NewExpression.is(&ne)
raise "Found new expression" on neIn this C++ code the
defect finds each
void f() {
int *p = new int;
} |
cast(Cpp.Node.Node node, required NewExpression &cast)
| Checks whether the given node is a
new_expression and if so binds that
new_expression to cast. Use this to narrow a
generic node to a new_expression. | This PQL defect checks whether a node within a declaration statement is
a defect cast_to_new =
when
Cpp.Declaration.is(&decl)
and decl.getADescendant(&node)
and Cpp.NewExpression.cast(node, &ne)
raise "Identifier's node is a new expression"
on neIn this C++ code the defect finds new expressions that are part of declarations.
void g() {
int *p = new int; // new in declaration
} |
isa(Cpp.Node.Node node)
| True when the given node is a
new_expression. Use this in boolean tests or negations to
filter nodes. | This PQL defect checks whether a node that is a descendant of a
declaration is also a
defect is_new_node =
when
Cpp.Declaration.is(&decl)
and decl.getADescendant(&node)
and Cpp.NewExpression.isa(node)
raise "Node is a new expression" on nodeIn this C++ code
the defects flags
void h() {
int *p = new int;
} |
arguments(NewExpression self, Cpp.Node.Node &child)
| Matches the argument list of a new_expression and binds the
parentheses content node to child. | This PQL defect checks for defect new_with_args =
when
Cpp.NewExpression.is(&ne)
and ne.arguments(&args)
raise "new expression has arguments"
on argsIn this C++ code the defect finds the
struct MyClass { MyClass(int, int) {} };
void i() {
MyClass *m = new MyClass(1, 2); // arguments = (1, 2)
} |
declarator(NewExpression self, Cpp.Node.Node &child)
| Matches the array declarator portion of a new_expression and
binds the size/index expression node to child. | This PQL defect checks for defect new_array =
when
Cpp.NewExpression.is(&ne)
and ne.declarator(&decl)
raise "new expression has declarator" on declIn this C++
code the defect finds the
void j() {
int *arr = new int[10]; // declarator = [10]
} |
placement(NewExpression self, Cpp.Node.Node &child)
| Matches the placement arguments of a placement new and binds
the placement list node to child. | This PQL defect checks for defect placement_new =
when
Cpp.NewExpression.is(&ne)
and ne.placement(&plc)
raise "placement new used" on plcIn this C++ code the
defect detects the placement
#include <new>
class MyClass{};
void k(void *ptr) {
MyClass *m = new(ptr) MyClass; // placement = (ptr)
} |
type(NewExpression self, Cpp.Node.Node &child)
| Matches the allocated type portion of a new_expression and
binds the type node to child. | This PQL defect checks which type is being allocated by a
defect new_type =
when
Cpp.NewExpression.is(&ne)
and ne.type(&typ)
raise "new allocates type" on typIn this C++ code the
defect identifies the
struct MyClass {};
void l() {
int *p = new int; // type = int
MyClass *m = new MyClass; // type = MyClass
} |
getEnclosingNewExpression(Cpp.Node.Node child, required NewExpression
&parent)
| Finds the nearest ancestor new_expression that encloses the
given child node and binds it to
parent. | This PQL defect checks for the closest defect enclosing_new =
when
Cpp.TypeIdentifier.is(&id)
and id.toNode(&node)
and Cpp.NewExpression.getEnclosingNewExpression(node, &ne)
raise "Type Identifier is inside a new expression" on neIn
this C++ code the defect finds the
struct MyClass { MyClass(int x) {} };
void m() {
MyClass *mobj = new MyClass(42); // MyClass is enclosed by the new expression
} |
isEnclosedInNewExpression(Cpp.Node.Node child)
| True when the given child node has any ancestor that is a
new_expression. Use this to test whether a node is located
inside a new. | This PQL defect checks whether a type identifier node lies anywhere
within a
defect inside_new =
when
Cpp.TypeIdentifier.is(&id)
and id.toNode(&node)
and Cpp.NewExpression.isEnclosedInNewExpression(node)
raise "Node is inside a new expression" on nodeIn this
C++ code the defect detects that a type identifier such as
struct MyClass { MyClass(int) {} };
void n() {
MyClass *m = new MyClass(5); // MyClass is enclosed in the new expression
} |
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)