Skip to content

Commit 4463293

Browse files
committed
C++: Move common code from NewExpr and NewArrayExpr into the NewOrNewArrayExpr class.
1 parent 167dc86 commit 4463293

File tree

1 file changed

+18
-20
lines changed
  • cpp/ql/src/semmle/code/cpp/exprs

1 file changed

+18
-20
lines changed

cpp/ql/src/semmle/code/cpp/exprs/Expr.qll

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,24 @@ class NewOrNewArrayExpr extends Expr, @any_new_expr {
850850
this.getAllocatorCall()
851851
.getArgument(this.getAllocator().(OperatorNewAllocationFunction).getPlacementArgument())
852852
}
853+
854+
/**
855+
* For `operator new`, this gets the call or expression that initializes the allocated object, if any.
856+
*
857+
* As examples, for `new int(4)`, this will be `4`, and for `new std::vector(4)`, this will
858+
* be a call to the constructor `std::vector::vector(size_t)` with `4` as an argument.
859+
*
860+
* For `operator new[]`, this gets the call or expression that initializes the first element of the
861+
* array, if any.
862+
*
863+
* This will either be a call to the default constructor for the array's element type (as
864+
* in `new std::string[10]`), or a literal zero for arrays of scalars which are zero-initialized
865+
* due to extra parentheses (as in `new int[10]()`).
866+
*
867+
* At runtime, the constructor will be called once for each element in the array, but the
868+
* constructor call only exists once in the AST.
869+
*/
870+
final Expr getInitializer() { result = this.getChild(1) }
853871
}
854872

855873
/**
@@ -871,14 +889,6 @@ class NewExpr extends NewOrNewArrayExpr, @new_expr {
871889
override Type getAllocatedType() {
872890
new_allocated_type(underlyingElement(this), unresolveElement(result))
873891
}
874-
875-
/**
876-
* Gets the call or expression that initializes the allocated object, if any.
877-
*
878-
* As examples, for `new int(4)`, this will be `4`, and for `new std::vector(4)`, this will
879-
* be a call to the constructor `std::vector::vector(size_t)` with `4` as an argument.
880-
*/
881-
Expr getInitializer() { result = this.getChild(1) }
882892
}
883893

884894
/**
@@ -909,18 +919,6 @@ class NewArrayExpr extends NewOrNewArrayExpr, @new_array_expr {
909919
result = getType().getUnderlyingType().(PointerType).getBaseType()
910920
}
911921

912-
/**
913-
* Gets the call or expression that initializes the first element of the array, if any.
914-
*
915-
* This will either be a call to the default constructor for the array's element type (as
916-
* in `new std::string[10]`), or a literal zero for arrays of scalars which are zero-initialized
917-
* due to extra parentheses (as in `new int[10]()`).
918-
*
919-
* At runtime, the constructor will be called once for each element in the array, but the
920-
* constructor call only exists once in the AST.
921-
*/
922-
Expr getInitializer() { result = this.getChild(1) }
923-
924922
/**
925923
* Gets the extent of the non-constant array dimension, if any.
926924
*

0 commit comments

Comments
 (0)