|
| 1 | +import csharp |
| 2 | + |
| 3 | +query predicate test1(UnboundGenericDelegateType d) { |
| 4 | + d.hasName("GenericDelegate<>") and |
| 5 | + d.getTypeParameter(0).hasName("T") and |
| 6 | + d.getParameter(0).getType().hasName("T") |
| 7 | +} |
| 8 | + |
| 9 | +query predicate test2(Class c) { |
| 10 | + c.hasName("A") and |
| 11 | + not c instanceof UnboundGenericClass |
| 12 | +} |
| 13 | + |
| 14 | +query predicate test3(ConstructedClass c, UnboundGenericClass d) { |
| 15 | + d.hasName("A<>") and |
| 16 | + c.getTypeArgument(0).hasName("X") and |
| 17 | + c.getTypeArgument(0) instanceof TypeParameter and |
| 18 | + c.getUnboundGeneric() = d |
| 19 | +} |
| 20 | + |
| 21 | +query predicate test4(UnboundGenericClass c, string s) { |
| 22 | + c.fromSource() and |
| 23 | + not c.getName().matches("%<%>") and |
| 24 | + s = "Unbound generic class with inconsistent name" |
| 25 | +} |
| 26 | + |
| 27 | +query predicate test5(ConstructedClass c, string s) { |
| 28 | + c.fromSource() and |
| 29 | + not c.getName().matches("%<%>") and |
| 30 | + s = "Constructed class with inconsistent name" |
| 31 | +} |
| 32 | + |
| 33 | +query predicate test6(ConstructedClass at, UnboundGenericClass b, ConstructedClass bt, Field f) { |
| 34 | + at.hasName("A<T>") and |
| 35 | + b.hasName("B<>") and |
| 36 | + bt.hasName("B<X>") and |
| 37 | + at.getTypeArgument(0).hasName("T") and |
| 38 | + at.getTypeArgument(0) instanceof TypeParameter and |
| 39 | + at.getTypeArgument(0) = b.getTypeParameter(0) and |
| 40 | + bt.getUnboundGeneric() = b and |
| 41 | + f.getDeclaringType() = b and |
| 42 | + f.getType() = at |
| 43 | +} |
| 44 | + |
| 45 | +query predicate test7(ConstructedClass aString, ConstructedClass bString) { |
| 46 | + aString.hasName("A<String>") and |
| 47 | + bString.hasName("B<String>") and |
| 48 | + aString.getSourceDeclaration().hasName("A<>") and |
| 49 | + bString.getSourceDeclaration().hasName("B<>") |
| 50 | +} |
| 51 | + |
| 52 | +query predicate test8(ConstructedClass bString, Method m) { |
| 53 | + bString.hasName("B<String>") and |
| 54 | + m.getDeclaringType() = bString and |
| 55 | + m.hasName("fooParams") and |
| 56 | + m.getParameter(0).getType().(ArrayType).getElementType() instanceof StringType and |
| 57 | + m.getSourceDeclaration().getDeclaringType() = m.getDeclaringType().getSourceDeclaration() |
| 58 | +} |
| 59 | + |
| 60 | +query predicate test9(ConstructedClass bString, Setter sourceSetter, Setter setter) { |
| 61 | + exists(Property p | |
| 62 | + bString.hasName("B<String>") and |
| 63 | + p.getDeclaringType() = bString and |
| 64 | + p.hasName("Name") and |
| 65 | + p.getSourceDeclaration().getDeclaringType() = p.getDeclaringType().getSourceDeclaration() and |
| 66 | + p.getSetter().getParameter(0).getType() instanceof StringType and |
| 67 | + p.getSetter().getSourceDeclaration() = p.getSourceDeclaration().getSetter() and |
| 68 | + p.getGetter().getSourceDeclaration() = p.getSourceDeclaration().getGetter() and |
| 69 | + sourceSetter = p.getSourceDeclaration().getSetter() and |
| 70 | + setter = p.getSetter() |
| 71 | + ) |
| 72 | +} |
| 73 | + |
| 74 | +query predicate test10(ConstructedClass bString, Event e) { |
| 75 | + bString.hasName("B<String>") and |
| 76 | + e.getDeclaringType() = bString and |
| 77 | + e.hasName("myEvent") and |
| 78 | + e.getSourceDeclaration().getDeclaringType() = e.getDeclaringType().getSourceDeclaration() and |
| 79 | + e.getType().(ConstructedDelegateType).getTypeArgument(0) instanceof StringType and |
| 80 | + e.getAddEventAccessor().getSourceDeclaration() = e.getSourceDeclaration().getAddEventAccessor() and |
| 81 | + e.getRemoveEventAccessor().getSourceDeclaration() = |
| 82 | + e.getSourceDeclaration().getRemoveEventAccessor() |
| 83 | +} |
| 84 | + |
| 85 | +query predicate test11(ConstructedClass bString, Operator o) { |
| 86 | + bString.hasName("B<String>") and |
| 87 | + o.getDeclaringType() = bString and |
| 88 | + o instanceof IncrementOperator and |
| 89 | + o.getSourceDeclaration().getDeclaringType() = o.getDeclaringType().getSourceDeclaration() |
| 90 | +} |
| 91 | + |
| 92 | +query predicate test12(ConstructedClass gridInt, Indexer i) { |
| 93 | + gridInt.hasName("Grid<Int32>") and |
| 94 | + i.getDeclaringType() = gridInt and |
| 95 | + i.getSourceDeclaration().getDeclaringType() = i.getDeclaringType().getSourceDeclaration() and |
| 96 | + i.getGetter().getSourceDeclaration() = i.getSourceDeclaration().getGetter() and |
| 97 | + i.getSetter().getSourceDeclaration() = i.getSourceDeclaration().getSetter() |
| 98 | +} |
| 99 | + |
| 100 | +query predicate test13(ConstructedClass gridInt, Indexer i) { |
| 101 | + gridInt.hasName("Grid<Int32>") and |
| 102 | + i.getDeclaringType() = gridInt and |
| 103 | + i.getType() instanceof IntType |
| 104 | +} |
| 105 | + |
| 106 | +query predicate test14(UnboundGenericClass gridInt, Indexer i) { |
| 107 | + gridInt.hasName("Grid<>") and |
| 108 | + i.getDeclaringType() = gridInt and |
| 109 | + i.getType() instanceof IntType |
| 110 | +} |
| 111 | + |
| 112 | +query predicate test15(ConstructedDelegateType d) { |
| 113 | + d.hasName("GenericDelegate<String>") and |
| 114 | + d.getTypeArgument(0) instanceof StringType and |
| 115 | + d.getParameter(0).getType() instanceof StringType |
| 116 | +} |
| 117 | + |
| 118 | +query predicate test16(Class c, Method m) { |
| 119 | + c.hasName("Subtle") and |
| 120 | + count(c.getAMethod()) = 3 and |
| 121 | + m = c.getAMethod() |
| 122 | +} |
| 123 | + |
| 124 | +query predicate test17( |
| 125 | + Class c, TypeParameter p, UnboundGenericMethod m, TypeParameter q, UnboundGenericMethod n, |
| 126 | + int numParams |
| 127 | +) { |
| 128 | + c.hasName("Subtle") and |
| 129 | + m = c.getAMethod() and |
| 130 | + m.getATypeParameter() = p and |
| 131 | + n = c.getAMethod() and |
| 132 | + n.getATypeParameter() = q and |
| 133 | + m != n and |
| 134 | + p != q and |
| 135 | + numParams = m.getNumberOfParameters() |
| 136 | +} |
| 137 | + |
| 138 | +query predicate test18( |
| 139 | + Class c, Method m, Parameter p, int numArgs, string typeName, int numParams, int numTypes |
| 140 | +) { |
| 141 | + c.getName().matches("A<%") and |
| 142 | + m = c.getAMethod() and |
| 143 | + p = m.getAParameter() and |
| 144 | + numArgs = count(m.(ConstructedMethod).getATypeArgument()) and |
| 145 | + typeName = p.getType().getName() and |
| 146 | + numParams = count(m.getAParameter()) and |
| 147 | + numTypes = count(m.getAParameter().getType()) |
| 148 | +} |
| 149 | + |
| 150 | +/** Test that locations are populated for the type parameters of generic methods. */ |
| 151 | +query predicate test19(UnboundGenericMethod m, TypeParameter tp, int hasLoc) { |
| 152 | + m.hasName("fs") and |
| 153 | + tp = m.getATypeParameter() and |
| 154 | + if exists(tp.getLocation()) then hasLoc = 1 else hasLoc = 0 |
| 155 | +} |
| 156 | + |
| 157 | +/** Test that locations are populated for unbound generic types. */ |
| 158 | +query predicate test20(UnboundGenericType t, string s) { |
| 159 | + not type_location(t, _) and s = "Missing location" |
| 160 | +} |
| 161 | + |
| 162 | +/** |
| 163 | + * This tests a regression in the extractor where the following failed to extract: |
| 164 | + * |
| 165 | + * class Foo<T> |
| 166 | + * { |
| 167 | + * enum E { a }; |
| 168 | + * } |
| 169 | + */ |
| 170 | +query predicate test21(Enum e, Class c) { |
| 171 | + c.hasName("Param<>") and |
| 172 | + e.hasName("E") and |
| 173 | + e.getDeclaringType() = c |
| 174 | +} |
| 175 | + |
| 176 | +query predicate test22(ConstructedMethod m, string tpName) { |
| 177 | + m.getName().matches("CM%") and |
| 178 | + tpName = m.getATypeArgument().getName() |
| 179 | +} |
| 180 | + |
| 181 | +query predicate test23(Class c, Interface i) { |
| 182 | + c.getName().matches("Inheritance%") and |
| 183 | + i = c.getABaseInterface() |
| 184 | +} |
| 185 | + |
| 186 | +query predicate test24(UnboundGenericInterface ugi, TypeParameter tp, string s) { |
| 187 | + ugi.fromSource() and |
| 188 | + ugi.getATypeParameter() = tp and |
| 189 | + ( |
| 190 | + tp.isOut() and s = "out" |
| 191 | + or |
| 192 | + tp.isIn() and s = "in" |
| 193 | + ) |
| 194 | +} |
| 195 | + |
| 196 | +query predicate test25(ConstructedMethod cm) { |
| 197 | + cm.hasName("CM3") and |
| 198 | + cm.getParameter(0).getType() instanceof DoubleType and |
| 199 | + cm.getParameter(1).getType() instanceof IntType and |
| 200 | + cm.getReturnType() instanceof DoubleType and |
| 201 | + exists(Method sourceDeclaration | |
| 202 | + sourceDeclaration = cm.getSourceDeclaration() and |
| 203 | + sourceDeclaration.getParameter(0).getType().(TypeParameter).hasName("T2") and |
| 204 | + sourceDeclaration.getParameter(1).getType().(TypeParameter).hasName("T1") and |
| 205 | + sourceDeclaration.getReturnType().(TypeParameter).hasName("T2") |
| 206 | + ) and |
| 207 | + exists(Method unbound | |
| 208 | + unbound = cm.getUnboundGeneric() and |
| 209 | + unbound.getParameter(0).getType().(TypeParameter).hasName("T2") and |
| 210 | + unbound.getParameter(1).getType() instanceof IntType and |
| 211 | + unbound.getReturnType().(TypeParameter).hasName("T2") |
| 212 | + ) |
| 213 | +} |
| 214 | + |
| 215 | +query predicate test26(ConstructedGeneric cg, string s) { |
| 216 | + // Source declaration and unbound generic must be unique |
| 217 | + ( |
| 218 | + strictcount(cg.getSourceDeclaration+()) > 1 or |
| 219 | + strictcount(cg.getUnboundGeneric()) > 1 |
| 220 | + ) and |
| 221 | + s = "Non-unique source decl or unbound generic" |
| 222 | +} |
| 223 | + |
| 224 | +query predicate test27(ConstructedType ct, UnboundGenericType ugt, UnboundGenericType sourceDecl) { |
| 225 | + ct instanceof NestedType and |
| 226 | + ugt = ct.getUnboundGeneric() and |
| 227 | + sourceDecl = ct.getSourceDeclaration() and |
| 228 | + ugt != sourceDecl |
| 229 | +} |
0 commit comments