1+ /**
2+ * Provides classes to help reasoning about `operator new`, `operator new[]`,
3+ * `operator delete`, and `operator delete[]`.
4+ *
5+ * These are described in section [support.dynamic] of the C++ standard.
6+ */
7+
18import cpp
29import codingstandards.cpp.Handlers
310
@@ -21,6 +28,13 @@ abstract class OperatorNewOrDelete extends Operator {
2128 }
2229}
2330
31+ /**
32+ * An `operator new` and `operator new[]` function described in [new.delete.single]
33+ * and [new.delete.array], respectively.
34+ *
35+ * Note that these do not include [new.delete.placement]. These are captured in
36+ * `PlacementOperatorNew`.
37+ */
2438class ReplaceableOperatorNew extends OperatorNewOrDelete {
2539 ReplaceableOperatorNew ( ) {
2640 this .getName ( ) .regexpMatch ( "operator new(\\[\\])?" ) and
@@ -34,6 +48,12 @@ class ReplaceableOperatorNew extends OperatorNewOrDelete {
3448 }
3549}
3650
51+ /**
52+ * `operator new`, `operator new[]`, `operator delete`, or `operator delete[]` functions
53+ * that are very likely provided by the user.
54+ *
55+ * Note that this captures _any_ function that has one of the above four names.
56+ */
3757class CustomOperatorNewOrDelete extends OperatorNewOrDelete {
3858 CustomOperatorNewOrDelete ( ) {
3959 this .hasDefinition ( ) and
@@ -60,8 +80,18 @@ class CustomOperatorNewOrDelete extends OperatorNewOrDelete {
6080 }
6181}
6282
83+ /**
84+ * The replaceable `operator new` or `operator new[]` functions that have custom
85+ * definitions provided by the user.
86+ *
87+ * Also see `CustomReplaceableOperatorDelete`.
88+ */
6389class CustomReplaceableOperatorNew extends CustomOperatorNewOrDelete , ReplaceableOperatorNew { }
6490
91+ /**
92+ * An `operator delete` or `operator delete[]` deallocation function described in
93+ * [new.delete.single] and [new.delete.array], respectively.
94+ */
6595class ReplaceableOperatorDelete extends OperatorNewOrDelete {
6696 ReplaceableOperatorDelete ( ) {
6797 this .getName ( ) .regexpMatch ( "operator delete(\\[\\])?" ) and
@@ -85,6 +115,12 @@ class ReplaceableOperatorDelete extends OperatorNewOrDelete {
85115 }
86116}
87117
118+ /**
119+ * The replaceable `operator new` or `operator new[]` functions that have custom
120+ * definitions provided by the user.
121+ *
122+ * Also see `CustomReplaceableOperatorNew`.
123+ */
88124class CustomReplaceableOperatorDelete extends CustomOperatorNewOrDelete , ReplaceableOperatorDelete {
89125 CustomReplaceableOperatorDelete getPartner ( ) {
90126 if this .getAParameter ( ) .getType ( ) instanceof Size_t
@@ -95,3 +131,18 @@ class CustomReplaceableOperatorDelete extends CustomOperatorNewOrDelete, Replace
95131 else result .getPartner ( ) = this
96132 }
97133}
134+
135+ /**
136+ * An `operator new` or `operator new[]` allocation function called by a placement-new expression,
137+ * as described in [new.delete.placement].
138+ *
139+ * The operator functions have a `std::size_t` as their first parameter and a
140+ * `void*` parameter somewhere in the rest of the parameter list.
141+ */
142+ class PlacementOperatorNew extends AllocationFunction {
143+ PlacementOperatorNew ( ) {
144+ this .getName ( ) in [ "operator new" , "operator new[]" ] and
145+ this .getParameter ( 0 ) .getType ( ) .resolveTypedefs * ( ) instanceof Size_t and
146+ this .getAParameter ( ) .getUnderlyingType ( ) instanceof VoidPointerType
147+ }
148+ }
0 commit comments