|
| 1 | +import cpp |
| 2 | + |
| 3 | +predicate mustBeImplicitlyDefined(Function mf) { |
| 4 | + // If it exists and its defaulted, it must be implicitly defined |
| 5 | + mf.isCompilerGenerated() or mf.isDefaulted() |
| 6 | +} |
| 7 | + |
| 8 | +predicate mustNotBeImplicitlyDefined(Function mf) { |
| 9 | + // Sanity check: exclude member functions that are definitely user defined! |
| 10 | + not mustBeImplicitlyDefined(mf) and |
| 11 | + ( |
| 12 | + // If the user wrote a body, it's not implicitly defined |
| 13 | + not mf.isCompilerGenerated() |
| 14 | + or |
| 15 | + // Deleted functions have no definition at all |
| 16 | + mf.isDeleted() |
| 17 | + ) |
| 18 | +} |
| 19 | + |
| 20 | +predicate mustBeUserDeclared(Function mf) { |
| 21 | + exists(FunctionDeclarationEntry fde | fde.getFunction() = mf and not fde.isImplicit()) |
| 22 | +} |
| 23 | + |
| 24 | +predicate mustNotBeUserDeclared(Function mf) { |
| 25 | + // use forex: at least one declaration must exist, and all declarations must be implicit |
| 26 | + forex(FunctionDeclarationEntry fde | fde.getFunction() = mf | fde.isImplicit()) |
| 27 | +} |
| 28 | + |
| 29 | +private signature class SpecialMember extends Function; |
| 30 | + |
| 31 | +private signature predicate constraint(Function mf); |
| 32 | + |
| 33 | +/** |
| 34 | + * Implicitly defined copy constructors are deprecated if the class has a user-declared copy |
| 35 | + * assignment operator or a user-declared destructor. The same is true for the reverse case |
| 36 | + * (implicit copy assignment operators check for copy constructors). |
| 37 | + * |
| 38 | + * Unfortunately, trivial copy constructors, assignment operators, and destructors are often missing |
| 39 | + * from the database, so we can only approximate. |
| 40 | + * |
| 41 | + * To manage all of the negatives, double negatives, musts, mays, and may nots, the problem is |
| 42 | + * broken down into sets as defined below. |
| 43 | + * |
| 44 | + * Firstly: |
| 45 | + * - CC=copy constructor, CA=copy assignment operator, D=destructor |
| 46 | + * - IDEF = implicitly defined, UDEC = user declared |
| 47 | + * |
| 48 | + * and: |
| 49 | + * - C_has{X} = set of all classes T for which "class T has a {X}" |
| 50 | + * - C_mustHave{X} = set of all classes T for which "class T must have a {X}" |
| 51 | + * - C_cannotHave{X} = set of all classes T for which "class T cannot have a {X}" |
| 52 | + * - C_mayHave{X} = not C_cannotHave{X} |
| 53 | + * |
| 54 | + * then we can find all cases we know to be deprecated via: |
| 55 | + * - Step 1: find C_mustHave{IDEF CC}, C_mustHave{IDEF CA} |
| 56 | + * - Step 2: find C_mustHave{UDEC CC}, C_mustHave{UDEC CA}, C_mustHave{UDEC D} |
| 57 | + * - Step 3: All C' are deprecated where C' in C_mustHave{IDEF CC} and (C' in C_mustHave{UDEC CA} or C' in C_mustHave{UDEC D}) |
| 58 | + * - Step 4: All C' are deprecated where C' in C_mustHave{IDEF CA} and (C' in C_mustHave{UDEC CC} or C' in C_mustHave{UDEC D}) |
| 59 | + * |
| 60 | + * And all cases we may consider deprecated via: |
| 61 | + * - Step 5: find C_cannotHave{IDEF CC}, C_cannotHave{IDEF CA} |
| 62 | + * - Step 6: find C_mayHave{IDEF CC}, C_mayHave{IDEF CA} (by negating the cannot sets) |
| 63 | + * - Step 7: find C_cannotHave{UDEC CC}, C_cannotHave{UDEC CA}, C_cannotHave{UDEC D} |
| 64 | + * - Step 8: find C_mayHave{UDEC CC}, C_mayHave{UDEC CA}, C_mayHave{UDEC D} (by negating the cannot sets) |
| 65 | + * - Step 9: All C' may be deprecated where C' in C_mayHave{IDEF CC} and (C' in C_mayHave{UDEC CA} or C' in C_mayHave{UDEC D}) |
| 66 | + * - Step 10: All C' may be deprecated where C' in C_mayHave{IDEF CA} and (C' in C_mayHave{UDEC CC} or C' in C_mayHave{UDEC D}) |
| 67 | + * |
| 68 | + * This is performed through the various instantiations of this module. |
| 69 | + */ |
| 70 | +private module ClassesWhere<constraint/1 pred, SpecialMember Member> { |
| 71 | + final class FinalClass = Class; |
| 72 | + |
| 73 | + class Matching extends FinalClass { |
| 74 | + Matching() { exists(Member member | member.getDeclaringType() = this and pred(member)) } |
| 75 | + } |
| 76 | + |
| 77 | + class NotMatching extends FinalClass { |
| 78 | + NotMatching() { not this instanceof Matching } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/* Step 1: find C_mustHave{IDEF CC}, C_mustHave{IDEF CA} */ |
| 83 | +private class CMustHaveIdefCC = ClassesWhere<mustBeImplicitlyDefined/1, CopyConstructor>::Matching; |
| 84 | + |
| 85 | +private class CMustHaveIdefCA = |
| 86 | + ClassesWhere<mustBeImplicitlyDefined/1, CopyAssignmentOperator>::Matching; |
| 87 | + |
| 88 | +/* Step 2: find C_mustHave{UDEC CC}, C_mustHave{UDEC CA}, C_mustHave{UDEC D} */ |
| 89 | +private class CMustHaveUdecCC = ClassesWhere<mustBeUserDeclared/1, CopyConstructor>::Matching; |
| 90 | + |
| 91 | +private class CMustHaveUdecCA = |
| 92 | + ClassesWhere<mustBeUserDeclared/1, CopyAssignmentOperator>::Matching; |
| 93 | + |
| 94 | +private class CMustHaveUdecD = ClassesWhere<mustBeUserDeclared/1, Destructor>::Matching; |
| 95 | + |
| 96 | +/* - Step 3: All C' are deprecated where C' in C_mustHave{IDEF CC} and (C' in C_mustHave{UDEC CA} or C' in C_mustHave{UDEC D}) */ |
| 97 | +class MustHaveDeprecatedCopyConstructor extends CMustHaveIdefCC { |
| 98 | + MustHaveDeprecatedCopyConstructor() { |
| 99 | + this instanceof CMustHaveUdecCA or this instanceof CMustHaveUdecD |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/* - Step 4: All C' are deprecated where C' in C_mustHave{IDEF CA} and (C' in C_mustHave{UDEC CC} or C' in C_mustHave{UDEC D}) */ |
| 104 | +class MustHaveDeprecatedCopyAssignmentOperator extends CMustHaveIdefCA { |
| 105 | + MustHaveDeprecatedCopyAssignmentOperator() { |
| 106 | + this instanceof CMustHaveUdecCC or this instanceof CMustHaveUdecD |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +/** |
| 111 | + * Step 5: find C_cannotHave{IDEF CC}, C_cannotHave{IDEF CA} |
| 112 | + * Step 6: find C_mayHave{IDEF CC}, C_mayHave{IDEF CA} (by negating the cannot sets) |
| 113 | + * |
| 114 | + * In our case, `ClassesWhere<...>` performs steps 5 and 6 together via `NotMatching`. |
| 115 | + */ |
| 116 | +private class CMayHaveIdefCC = |
| 117 | + ClassesWhere<mustNotBeImplicitlyDefined/1, CopyConstructor>::NotMatching; |
| 118 | + |
| 119 | +private class CMayHaveIdefCA = |
| 120 | + ClassesWhere<mustNotBeImplicitlyDefined/1, CopyAssignmentOperator>::NotMatching; |
| 121 | + |
| 122 | +/** |
| 123 | + * Step 7: find C_cannotHave{UDEC CC}, C_cannotHave{UDEC CA}, C_cannotHave{UDEC D} |
| 124 | + * Step 8: find C_mayHave{UDEC CC}, C_mayHave{UDEC CA}, C_mayHave{UDEC D} (by negating the cannot sets) |
| 125 | + * |
| 126 | + * In our case, `ClassesWhere<...>` performs steps 7 and 8 together via `NotMatching`. |
| 127 | + */ |
| 128 | +private class CMayHaveUdecCC = ClassesWhere<mustNotBeUserDeclared/1, CopyConstructor>::NotMatching; |
| 129 | + |
| 130 | +private class CMayHaveUdecCA = |
| 131 | + ClassesWhere<mustNotBeUserDeclared/1, CopyAssignmentOperator>::NotMatching; |
| 132 | + |
| 133 | +private class CMayHaveUdecD = ClassesWhere<mustNotBeUserDeclared/1, Destructor>::NotMatching; |
| 134 | + |
| 135 | +/* - Step 9: All C' may be deprecated where C' in C_mayHave{IDEF CC} and (C' in C_mayHave{UDEC CA} or C' in C_mayHave{UDEC D}) */ |
| 136 | +class MayHaveDeprecatedCopyConstructor extends CMayHaveIdefCC { |
| 137 | + MayHaveDeprecatedCopyConstructor() { |
| 138 | + this instanceof CMayHaveUdecCA or this instanceof CMayHaveUdecD |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +/* - Step 10: All C' may be deprecated where C' in C_mayHave{IDEF CA} and (C' in C_mayHave{UDEC CC} or C' in C_mayHave{UDEC D}) */ |
| 143 | +class MayHaveDeprecatedCopyAssignmentOperator extends CMayHaveIdefCA { |
| 144 | + MayHaveDeprecatedCopyAssignmentOperator() { |
| 145 | + this instanceof CMayHaveUdecCC or this instanceof CMayHaveUdecD |
| 146 | + } |
| 147 | +} |
0 commit comments