Skip to content

Commit c63fd4a

Browse files
authored
Merge pull request github#6260 from tamasvajk/feature/method-name
C#: Change generic method names to include <> and type args/params
2 parents 5cecea4 + b7f13a7 commit c63fd4a

File tree

133 files changed

+2822
-2569
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

133 files changed

+2822
-2569
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
lgtm,codescanning
2+
* Generic methods return their names (`getName()`, `getQualifiedName()` and `toStringWithTypes()`) with angle brackets,
3+
for example `System.Linq.Enumerable.Empty<TResult>()` returns `Empty<>`, `System.Linq.Enumerable.Empty<>` and
4+
`Empty<TResult>()` respectively for the unbound generic method; and `Empty<Int32>`,
5+
`System.Linq.Enumerable.Empty<System.Int32>` and `Empty<int>()` respectively for the constructed generic case.
6+
* When accessing `getName()`, `getQualifiedName()` and `toStringWithTypes()` on constructed types, the type argument
7+
names are rendered by `getName()`, `getQualifiedName()` and `toStringWithTypes()` respectively.
8+
* `getUndecoratedName()` can be used to access the name without angle brackets.

csharp/ql/lib/semmle/code/cil/Method.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ class Method extends DotNet::Callable, Element, Member, TypeContainer, DataFlowN
8181

8282
override string getName() { cil_method(this, result, _, _) }
8383

84+
override string getUndecoratedName() { result = getName() }
85+
8486
override string toString() { result = this.getName() }
8587

8688
override Type getDeclaringType() { cil_method(this, _, result, _) }

csharp/ql/lib/semmle/code/cil/Type.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class Namespace extends DotNet::Namespace, TypeContainer, @namespace {
2525

2626
override Namespace getParentNamespace() { parent_namespace(this, result) }
2727

28-
override string getName() { namespaces(this, result) }
29-
3028
override Location getLocation() { none() }
3129
}
3230

@@ -73,7 +71,7 @@ class Type extends DotNet::Type, Declaration, TypeContainer, @cil_type {
7371
predicate isSystemType(string name) {
7472
exists(Namespace system | this.getParent() = system |
7573
system.getName() = "System" and
76-
system.getParentNamespace() instanceof DotNet::GlobalNamespace and
74+
system.getParentNamespace().getName() = "" and
7775
name = this.getName()
7876
)
7977
}

csharp/ql/lib/semmle/code/cil/Types.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class TypeParameter extends DotNet::TypeParameter, Type, @cil_typeparameter {
3636
class ValueOrRefType extends DotNet::ValueOrRefType, Type, @cil_valueorreftype {
3737
override ValueOrRefType getDeclaringType() { result = getParent() }
3838

39-
override string getUndecoratedName() { result = getName() }
39+
override string getUndecoratedName() { cil_type(this, result, _, _, _) }
4040

4141
override Namespace getDeclaringNamespace() { result = getNamespace() }
4242

csharp/ql/lib/semmle/code/csharp/Callable.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ class Method extends Callable, Virtualizable, Attributable, @method {
243243
/** Gets the name of this method. */
244244
override string getName() { methods(this, result, _, _, _) }
245245

246+
override string getUndecoratedName() { methods(this, result, _, _, _) }
247+
246248
override ValueOrRefType getDeclaringType() { methods(this, _, result, _, _) }
247249

248250
override Type getReturnType() { methods(this, _, _, getTypeRef(result), _) }
@@ -448,6 +450,8 @@ class Operator extends Callable, Member, Attributable, @operator {
448450

449451
override string getName() { operators(this, _, result, _, _, _) }
450452

453+
override string getUndecoratedName() { operators(this, _, result, _, _, _) }
454+
451455
/**
452456
* Gets the metadata name of the operator, such as `op_implicit` or `op_RightShift`.
453457
*/
@@ -989,6 +993,8 @@ class ExplicitConversionOperator extends ConversionOperator {
989993
class LocalFunction extends Callable, Modifiable, Attributable, @local_function {
990994
override string getName() { local_functions(this, result, _, _) }
991995

996+
override string getUndecoratedName() { local_functions(this, result, _, _) }
997+
992998
override LocalFunction getUnboundDeclaration() { local_functions(this, _, _, result) }
993999

9941000
override Type getReturnType() { local_functions(this, _, result, _) }

csharp/ql/lib/semmle/code/csharp/Event.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ private import TypeRef
1919
class Event extends DeclarationWithAccessors, @event {
2020
override string getName() { events(this, result, _, _, _) }
2121

22+
override string getUndecoratedName() { events(this, result, _, _, _) }
23+
2224
override ValueOrRefType getDeclaringType() { events(this, _, result, _, _) }
2325

2426
override DelegateType getType() { events(this, _, _, getTypeRef(result), _) }
@@ -111,6 +113,8 @@ class EventAccessor extends Accessor, @event_accessor {
111113
class AddEventAccessor extends EventAccessor, @add_event_accessor {
112114
override string getName() { result = "add" + "_" + getDeclaration().getName() }
113115

116+
override string getUndecoratedName() { result = "add" + "_" + getDeclaration().getName() }
117+
114118
override string getAPrimaryQlClass() { result = "AddEventAccessor" }
115119
}
116120

@@ -130,5 +134,7 @@ class AddEventAccessor extends EventAccessor, @add_event_accessor {
130134
class RemoveEventAccessor extends EventAccessor, @remove_event_accessor {
131135
override string getName() { result = "remove" + "_" + getDeclaration().getName() }
132136

137+
override string getUndecoratedName() { result = "remove" + "_" + getDeclaration().getName() }
138+
133139
override string getAPrimaryQlClass() { result = "RemoveEventAccessor" }
134140
}

csharp/ql/lib/semmle/code/csharp/Generics.qll

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ class UnboundGeneric extends DotNet::UnboundGeneric, Generic {
4747
}
4848
}
4949

50+
/** Gets the type parameters as a comma-separated string. */
51+
language[monotonicAggregates]
52+
private string getTypeParametersToString(UnboundGeneric ug) {
53+
result =
54+
strictconcat(Type t, int i | t = ug.getTypeParameter(i) | t.toStringWithTypes(), ", " order by i)
55+
}
56+
57+
/** Gets a string of `N` commas where `N + 1` is the number of type parameters of this unbound generic. */
58+
private string getTypeParameterCommas(UnboundGeneric ug) {
59+
result = strictconcat(int i | exists(ug.getTypeParameter(i)) | "", ",")
60+
}
61+
5062
/**
5163
* A constructed generic.
5264
*
@@ -72,6 +84,26 @@ class ConstructedGeneric extends DotNet::ConstructedGeneric, Generic {
7284
final AnnotatedType getAnnotatedTypeArgument(int i) { result.appliesToTypeArgument(this, i) }
7385
}
7486

87+
/** Gets the type arguments as a comma-separated string. */
88+
language[monotonicAggregates]
89+
private string getTypeArgumentsToString(ConstructedGeneric cg) {
90+
result =
91+
strictconcat(Type t, int i | t = cg.getTypeArgument(i) | t.toStringWithTypes(), ", " order by i)
92+
}
93+
94+
/** Gets the concatenation of the `getName()` of type arguments. */
95+
language[monotonicAggregates]
96+
private string getTypeArgumentsNames(ConstructedGeneric cg) {
97+
result = strictconcat(Type t, int i | t = cg.getTypeArgument(i) | t.getName(), "," order by i)
98+
}
99+
100+
/** Gets the concatenation of the `getQualifiedName()` of type arguments. */
101+
language[monotonicAggregates]
102+
private string getTypeArgumentsQualifiedNames(ConstructedGeneric cg) {
103+
result =
104+
strictconcat(Type t, int i | t = cg.getTypeArgument(i) | t.getQualifiedName(), "," order by i)
105+
}
106+
75107
/**
76108
* An unbound generic type. This is a generic type with type parameters
77109
* (for example `List<T>`) or elided type parameters (for example `List<>`).
@@ -111,20 +143,16 @@ class UnboundGenericType extends ValueOrRefType, UnboundGeneric {
111143

112144
final override Type getChild(int n) { result = getTypeParameter(n) }
113145

114-
private string getTypeParameterCommas() {
115-
result = strictconcat(int i | exists(this.getTypeParameter(i)) | "", ",")
116-
}
117-
118146
override string toStringWithTypes() {
119-
result = this.getUndecoratedName() + "<" + this.typeParametersToString() + ">"
147+
result = this.getUndecoratedName() + "<" + getTypeParametersToString(this) + ">"
120148
}
121149

122150
final override string getName() {
123-
result = this.getUndecoratedName() + "<" + this.getTypeParameterCommas() + ">"
151+
result = this.getUndecoratedName() + "<" + getTypeParameterCommas(this) + ">"
124152
}
125153

126154
final override predicate hasQualifiedName(string qualifier, string name) {
127-
exists(string name0 | name = name0 + "<" + this.getTypeParameterCommas() + ">" |
155+
exists(string name0 | name = name0 + "<" + getTypeParameterCommas(this) + ">" |
128156
exists(string enclosing |
129157
this.getDeclaringType().hasQualifiedName(qualifier, enclosing) and
130158
name0 = enclosing + "+" + this.getUndecoratedName()
@@ -348,8 +376,8 @@ class UnboundGenericDelegateType extends DelegateType, UnboundGenericType {
348376

349377
override string toStringWithTypes() {
350378
result =
351-
getUndecoratedName() + "<" + this.typeParametersToString() + ">(" + parameterTypesToString() +
352-
")"
379+
getUndecoratedName() + "<" + getTypeParametersToString(this) + ">(" + parameterTypesToString()
380+
+ ")"
353381
}
354382
}
355383

@@ -378,37 +406,16 @@ class ConstructedType extends ValueOrRefType, ConstructedGeneric {
378406

379407
final override Type getChild(int n) { result = getTypeArgument(n) }
380408

381-
language[monotonicAggregates]
382-
private string getTypeArgumentsString() {
383-
result =
384-
strictconcat(Type t, int i | t = this.getTypeArgument(i) | t.toString(), ", " order by i)
385-
}
386-
387-
language[monotonicAggregates]
388-
private string getTypeArgumentsNames() {
389-
result = strictconcat(Type t, int i | t = this.getTypeArgument(i) | t.getName(), "," order by i)
390-
}
391-
392-
language[monotonicAggregates]
393-
private string getTypeArgumentsQualifiedNames() {
394-
result =
395-
strictconcat(Type t, int i |
396-
t = this.getTypeArgument(i)
397-
|
398-
t.getQualifiedName(), "," order by i
399-
)
400-
}
401-
402409
final override string toStringWithTypes() {
403-
result = this.getUndecoratedName() + "<" + this.getTypeArgumentsString() + ">"
410+
result = this.getUndecoratedName() + "<" + getTypeArgumentsToString(this) + ">"
404411
}
405412

406413
final override string getName() {
407-
result = this.getUndecoratedName() + "<" + this.getTypeArgumentsNames() + ">"
414+
result = this.getUndecoratedName() + "<" + getTypeArgumentsNames(this) + ">"
408415
}
409416

410417
final override predicate hasQualifiedName(string qualifier, string name) {
411-
exists(string name0 | name = name0 + "<" + this.getTypeArgumentsQualifiedNames() + ">" |
418+
exists(string name0 | name = name0 + "<" + getTypeArgumentsQualifiedNames(this) + ">" |
412419
exists(string enclosing |
413420
this.getDeclaringType().hasQualifiedName(qualifier, enclosing) and
414421
name0 = enclosing + "+" + this.getUndecoratedName()
@@ -535,8 +542,15 @@ class UnboundGenericMethod extends Method, UnboundGeneric {
535542

536543
override string toStringWithTypes() {
537544
result =
538-
getName() + "<" + this.typeParametersToString() + ">" + "(" + parameterTypesToString() + ")"
545+
getUndecoratedName() + "<" + getTypeParametersToString(this) + ">" + "(" +
546+
parameterTypesToString() + ")"
547+
}
548+
549+
final override string getName() {
550+
result = getUndecoratedName() + "<" + getTypeParameterCommas(this) + ">"
539551
}
552+
553+
final override string getUndecoratedName() { methods(this, result, _, _, _) }
540554
}
541555

542556
/**
@@ -566,12 +580,24 @@ class ConstructedMethod extends Method, ConstructedGeneric {
566580

567581
override string toStringWithTypes() {
568582
result =
569-
getName() + "<" + this.typeArgumentsToString() + ">" + "(" + parameterTypesToString() + ")"
583+
getUndecoratedName() + "<" + getTypeArgumentsToString(this) + ">" + "(" +
584+
parameterTypesToString() + ")"
570585
}
571586

572587
override UnboundGenericMethod getUnboundDeclaration() {
573588
result = Method.super.getUnboundDeclaration()
574589
}
590+
591+
final override string getName() {
592+
result = getUndecoratedName() + "<" + getTypeArgumentsNames(this) + ">"
593+
}
594+
595+
override predicate hasQualifiedName(string qualifier, string name) {
596+
qualifier = getDeclaringType().getQualifiedName() and
597+
name = getUndecoratedName() + "<" + getTypeArgumentsQualifiedNames(this) + ">"
598+
}
599+
600+
final override string getUndecoratedName() { methods(this, result, _, _, _) }
575601
}
576602

577603
/**

csharp/ql/lib/semmle/code/csharp/Implements.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private DeclarationWithAccessors getACompatibleInterfaceAccessor(DeclarationWith
125125
}
126126

127127
private DeclarationWithAccessors getACompatibleInterfaceAccessorCandidate(DeclarationWithAccessors d) {
128-
getACompatibleInterfaceAccessorAux(result, d.getDeclaringType(), d.getName()) and
128+
getACompatibleInterfaceAccessorAux(result, d.getDeclaringType(), d.getUndecoratedName()) and
129129
not d instanceof Indexer and
130130
d.isPublic()
131131
}
@@ -135,7 +135,7 @@ private predicate getACompatibleInterfaceAccessorAux(
135135
DeclarationWithAccessors d, ValueOrRefType t, string name
136136
) {
137137
t = getAPossibleImplementor(d.getDeclaringType()) and
138-
name = d.getName()
138+
name = d.getUndecoratedName()
139139
}
140140

141141
/**
@@ -215,7 +215,7 @@ private Method getACompatibleInterfaceMethod(Method m) {
215215
* in a type that is a possible implementor type for the interface type.
216216
*/
217217
private Method getAnInterfaceMethodCandidate(Method m) {
218-
getAPotentialInterfaceMethodAux(result, m.getDeclaringType(), m.getName(),
218+
getAPotentialInterfaceMethodAux(result, m.getDeclaringType(), m.getUndecoratedName(),
219219
m.getNumberOfParameters()) and
220220
m.isPublic()
221221
}
@@ -225,7 +225,7 @@ private predicate getAPotentialInterfaceMethodAux(
225225
Method m, ValueOrRefType t, string name, int params
226226
) {
227227
t = getAPossibleImplementor(m.getDeclaringType()) and
228-
name = m.getName() and
228+
name = m.getUndecoratedName() and
229229
params = m.getNumberOfParameters()
230230
}
231231

csharp/ql/lib/semmle/code/csharp/Namespace.qll

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ class TypeContainer extends DotNet::NamedElement, Element, @type_container { }
1919
* ```
2020
*/
2121
class Namespace extends DotNet::Namespace, TypeContainer, Declaration, @namespace {
22-
/** Gets the simple name of this namespace, for example `IO` in `System.IO`. */
23-
override string getName() { namespaces(this, result) }
24-
2522
override Namespace getParent() { result = this.getParentNamespace() }
2623

2724
override Namespace getParentNamespace() { parent_namespace(this, result) }

csharp/ql/lib/semmle/code/csharp/Property.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ class DeclarationWithGetSetAccessors extends DeclarationWithAccessors, TopLevelE
116116
class Property extends DotNet::Property, DeclarationWithGetSetAccessors, @property {
117117
override string getName() { properties(this, result, _, _, _) }
118118

119+
override string getUndecoratedName() { properties(this, result, _, _, _) }
120+
119121
override ValueOrRefType getDeclaringType() { properties(this, _, result, _, _) }
120122

121123
override Type getType() { properties(this, _, _, getTypeRef(result), _) }
@@ -251,6 +253,8 @@ class Property extends DotNet::Property, DeclarationWithGetSetAccessors, @proper
251253
class Indexer extends DeclarationWithGetSetAccessors, Parameterizable, @indexer {
252254
override string getName() { indexers(this, result, _, _, _) }
253255

256+
override string getUndecoratedName() { indexers(this, result, _, _, _) }
257+
254258
/** Gets the dimension of this indexer, that is, its number of parameters. */
255259
int getDimension() { result = getNumberOfParameters() }
256260

@@ -393,6 +397,8 @@ class Accessor extends Callable, Modifiable, Attributable, @callable_accessor {
393397
class Getter extends Accessor, @getter {
394398
override string getName() { result = "get" + "_" + getDeclaration().getName() }
395399

400+
override string getUndecoratedName() { result = "get" + "_" + getDeclaration().getName() }
401+
396402
override Type getReturnType() { result = getDeclaration().getType() }
397403

398404
/**
@@ -440,6 +446,8 @@ class Getter extends Accessor, @getter {
440446
class Setter extends Accessor, @setter {
441447
override string getName() { result = "set" + "_" + getDeclaration().getName() }
442448

449+
override string getUndecoratedName() { result = "set" + "_" + getDeclaration().getName() }
450+
443451
override Type getReturnType() {
444452
exists(this) and // needed to avoid compiler warning
445453
result instanceof VoidType

0 commit comments

Comments
 (0)