Skip to content

Commit 22f3b05

Browse files
committed
Handle all structs (simple types, intptr, system.void)
1 parent 914da6b commit 22f3b05

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

csharp/ql/src/Stubs/Stubs.qll

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,34 @@
1212
*/
1313

1414
import csharp
15+
private import semmle.code.csharp.frameworks.System
16+
private import semmle.code.dotnet.DotNet as DotNet
1517

1618
/** An element that should be in the generated code. */
1719
abstract class GeneratedElement extends Element { }
1820

1921
/** A member that should be in the generated code. */
2022
abstract class GeneratedMember extends Member, GeneratedElement { }
2123

24+
/** Class representing all `struct`s, such as user defined ones and built-in ones, like `int`. */
25+
private class StructEx extends Type {
26+
StructEx() {
27+
this instanceof Struct or
28+
this instanceof SimpleType or
29+
this instanceof VoidType or
30+
this instanceof SystemIntPtrType
31+
}
32+
}
33+
2234
/** A type that should be in the generated code. */
23-
abstract private class GeneratedType extends ValueOrRefType, GeneratedElement {
35+
abstract private class GeneratedType extends Type, GeneratedElement {
2436
GeneratedType() {
2537
(
2638
this instanceof Interface
2739
or
2840
this instanceof Class
2941
or
30-
this instanceof Struct
42+
this instanceof StructEx
3143
or
3244
this instanceof Enum
3345
or
@@ -53,7 +65,7 @@ abstract private class GeneratedType extends ValueOrRefType, GeneratedElement {
5365
private string stubKeyword() {
5466
this instanceof Interface and result = "interface"
5567
or
56-
this instanceof Struct and result = "struct"
68+
this instanceof StructEx and result = "struct"
5769
or
5870
this instanceof Class and result = "class"
5971
or
@@ -79,7 +91,7 @@ abstract private class GeneratedType extends ValueOrRefType, GeneratedElement {
7991
}
8092

8193
private string stubAttributes() {
82-
if this.getAnAttribute().getType().getQualifiedName() = "System.FlagsAttribute"
94+
if this.(ValueOrRefType).getAnAttribute().getType().getQualifiedName() = "System.FlagsAttribute"
8395
then result = "[System.Flags]\n"
8496
else result = ""
8597
}
@@ -112,7 +124,7 @@ abstract private class GeneratedType extends ValueOrRefType, GeneratedElement {
112124
}
113125

114126
private ValueOrRefType getAnInterestingBaseType() {
115-
result = this.getABaseType() and
127+
result = this.(ValueOrRefType).getABaseType() and
116128
not result instanceof ObjectType and
117129
not result.getQualifiedName() = "System.ValueType" and
118130
(not result instanceof Interface or result.(Interface).isEffectivelyPublic())
@@ -171,9 +183,9 @@ abstract class GeneratedDeclaration extends Modifiable {
171183

172184
private class IndirectType extends GeneratedType {
173185
IndirectType() {
174-
this.getASubType() instanceof GeneratedType
186+
this.(ValueOrRefType).getASubType() instanceof GeneratedType
175187
or
176-
this.getAChildType() instanceof GeneratedType
188+
this.(ValueOrRefType).getAChildType() instanceof GeneratedType
177189
or
178190
this.(UnboundGenericType).getAConstructedGeneric().getASubType() instanceof GeneratedType
179191
or
@@ -273,7 +285,8 @@ private class GeneratedNamespace extends Namespace, GeneratedElement {
273285
}
274286

275287
private predicate isInAssembly(Assembly assembly) {
276-
any(GeneratedType gt | gt.getDeclaringNamespace() = this).isInAssembly(assembly)
288+
any(GeneratedType gt | gt.(DotNet::ValueOrRefType).getDeclaringNamespace() = this)
289+
.isInAssembly(assembly)
277290
or
278291
this.getChildNamespace(_).isInAssembly(assembly)
279292
}
@@ -293,7 +306,7 @@ private class GeneratedNamespace extends Namespace, GeneratedElement {
293306
this.isInAssembly(assembly) and
294307
result =
295308
concat(GeneratedType gt |
296-
gt.getDeclaringNamespace() = this and gt.isInAssembly(assembly)
309+
gt.(DotNet::ValueOrRefType).getDeclaringNamespace() = this and gt.isInAssembly(assembly)
297310
|
298311
gt.getStub(assembly) order by gt.getName()
299312
)
@@ -454,7 +467,7 @@ private string stubClassName(Type t) {
454467
}
455468

456469
language[monotonicAggregates]
457-
private string stubGenericArguments(ValueOrRefType t) {
470+
private string stubGenericArguments(Type t) {
458471
if t instanceof UnboundGenericType
459472
then
460473
result =
@@ -730,7 +743,7 @@ private string stubMember(Member m, Assembly assembly) {
730743
if
731744
not m.getDeclaringType() instanceof Enum and
732745
(
733-
not m.getDeclaringType() instanceof Struct or
746+
not m.getDeclaringType() instanceof StructEx or
734747
m.(Constructor).getNumberOfParameters() > 0
735748
)
736749
then

0 commit comments

Comments
 (0)