Skip to content

Commit abe5d0d

Browse files
committed
C#: Fixes to stub generation.
1 parent 593f0a9 commit abe5d0d

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

csharp/ql/src/Stubs/MinimalStubsFromSource.ql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ class UsedInSource extends GeneratedDeclaration {
2222
exists(Variable v | v.fromSource() | this = v.getType())
2323
or
2424
exists(Virtualizable v | v.fromSource() | this = v.getImplementee() or this = v.getOverridee())
25+
or
26+
this = any(Attribute a).getType()
27+
or
28+
this = any(Attribute a).getType().getAConstructor()
2529
)
2630
and
2731
this.fromLibrary()

csharp/ql/src/Stubs/Stubs.qll

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ private abstract class GeneratedType extends ValueOrRefType, GeneratedElement {
117117

118118
private ValueOrRefType getAnInterestingBaseType() {
119119
result = this.getABaseType() and
120-
not result instanceof ObjectType
120+
not result instanceof ObjectType and
121+
not result.getQualifiedName() = "System.ValueType"
121122
}
122-
123+
123124
private string stubBaseTypesString() {
124125
if this instanceof Enum then
125126
result = ""
@@ -132,7 +133,7 @@ private abstract class GeneratedType extends ValueOrRefType, GeneratedElement {
132133
else
133134
result = ""
134135
}
135-
136+
136137
private string stubMembers() {
137138
result = concat(stubMember(this.getAGeneratedMember()))
138139
}
@@ -171,15 +172,15 @@ private class IndirectType extends GeneratedType {
171172
or
172173
this.(UnboundGenericType).getAConstructedGeneric().getASubType() instanceof GeneratedType
173174
or
174-
exists(GeneratedType t | this = getAContainedType(t.getAGeneratedType()))
175+
exists(GeneratedType t | this = getAContainedType(t.getAGeneratedType()).getSourceDeclaration())
175176
or
176-
exists(GeneratedDeclaration decl | decl.(Member).getDeclaringType() = this)
177+
exists(GeneratedDeclaration decl | decl.(Member).getDeclaringType().getSourceDeclaration() = this)
177178
}
178179
}
179180

180181
private class RootGeneratedType extends GeneratedType {
181182
RootGeneratedType() {
182-
this instanceof GeneratedDeclaration
183+
this = any(GeneratedDeclaration decl).getSourceDeclaration()
183184
}
184185
}
185186

@@ -191,7 +192,7 @@ private Type getAContainedType(Type t) {
191192

192193
private class RootGeneratedMember extends GeneratedMember {
193194
RootGeneratedMember() {
194-
this instanceof GeneratedDeclaration
195+
this = any(GeneratedDeclaration d).getSourceDeclaration()
195196
}
196197
}
197198

@@ -209,7 +210,7 @@ private class InheritedMember extends GeneratedMember, Virtualizable {
209210
or
210211
declarationExists(this.getOverridee+())
211212
or
212-
declarationExists(this.getAnOverrider*())
213+
declarationExists(this.getAnOverrider+())
213214
}
214215
}
215216

@@ -328,6 +329,8 @@ private string stubClassName(Type t) {
328329
result = "float"
329330
else if t instanceof DoubleType then
330331
result = "double"
332+
else if t instanceof NullableType then
333+
result = stubClassName(t.(NullableType).getUnderlyingType()) + "?"
331334
else if t instanceof TypeParameter then
332335
result = t.getName()
333336
else if t instanceof ArrayType then
@@ -345,9 +348,9 @@ private string stubClassName(Type t) {
345348
language[monotonicAggregates]
346349
private string stubGenericArguments(ValueOrRefType t) {
347350
if t instanceof UnboundGenericType then
348-
result = "<" + concat(int n | exists(t.(UnboundGenericType).getTypeParameter(n)) | t.(UnboundGenericType).getTypeParameter(n).getName(),",") + ">"
351+
result = "<" + concat(int n | exists(t.(UnboundGenericType).getTypeParameter(n)) | t.(UnboundGenericType).getTypeParameter(n).getName(),"," order by n) + ">"
349352
else if t instanceof ConstructedType then
350-
result = "<" + concat(int n | exists(t.(ConstructedType).getTypeArgument(n)) | stubClassName(t.(ConstructedType).getTypeArgument(n)),",") + ">"
353+
result = "<" + concat(int n | exists(t.(ConstructedType).getTypeArgument(n)) | stubClassName(t.(ConstructedType).getTypeArgument(n)),"," order by n) + ">"
351354
else
352355
result = ""
353356
}
@@ -398,11 +401,17 @@ private string stubMember(Member m) {
398401
"(" + stubParameters(c) + ")" + stubImplementation(c) + ";\n"
399402
)
400403
or
401-
exists(Operator op | m = op and not m.getDeclaringType() instanceof Enum |
404+
exists(Operator op | m = op and not m.getDeclaringType() instanceof Enum and not op instanceof ConversionOperator |
402405
result = " " + stubModifiers(op) + stubClassName(op.getReturnType()) + " operator " +
403406
op.getName() +"(" + stubParameters(op) + ") => throw null;\n"
404407
)
405408
or
409+
exists(ConversionOperator op | m = op |
410+
result = " " + stubModifiers(op) + stubExplicit(op) + "operator " +
411+
stubClassName(op.getReturnType()) + "(" + stubParameters(op) +
412+
") => throw null;\n"
413+
)
414+
or
406415
result = " " + m.(EnumConstant).getName() + ",\n"
407416
or
408417
exists(Property p | m = p |
@@ -422,6 +431,12 @@ private string stubMember(Member m) {
422431
)
423432
}
424433

434+
private string stubExplicit(ConversionOperator op) {
435+
op instanceof ImplicitConversionOperator and result = "implicit "
436+
or
437+
op instanceof ExplicitConversionOperator and result = "explicit "
438+
}
439+
425440
private string stubGetter(DeclarationWithGetSetAccessors p) {
426441
if exists(p.getGetter()) then (
427442
if p.isAbstract() or p.getDeclaringType() instanceof Interface then
@@ -442,7 +457,17 @@ private string stubSetter(DeclarationWithGetSetAccessors p) {
442457
result = ""
443458
}
444459

460+
private string stubSemmleExtractorOptions() {
461+
result = concat(string s |
462+
exists(CommentLine comment |
463+
s = "// original-extractor-options:" + comment.getText().regexpCapture("\\w*semmle-extractor-options:(.*)", 1) + "\n"
464+
)
465+
)
466+
}
467+
445468
/** Gets the generated C# code. */
446469
string generatedCode() {
447-
result = any(GeneratedNamespace ns | ns.isGlobalNamespace()).getStubs()
470+
result = "// This file contains auto-generated code.\n" +
471+
stubSemmleExtractorOptions() + "\n" +
472+
any(GeneratedNamespace ns | ns.isGlobalNamespace()).getStubs()
448473
}

0 commit comments

Comments
 (0)