Skip to content

Commit 2fea6f0

Browse files
authored
Merge pull request #4489 from hvitved/csharp/cil-to-string
C#: Simplify `toString()` for CIL entities
2 parents 8e57f57 + 92461d4 commit 2fea6f0

File tree

13 files changed

+31
-16
lines changed

13 files changed

+31
-16
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
lgtm,codescanning
2+
* The `toString()` predicate is now less verbose for CIL entities. The old `toString()` behavior
3+
can be recovered by using `toStringExtra()` and `getQualifiedName()`, respectively.

csharp/ql/src/semmle/code/cil/Attribute.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Attribute extends Element, @cil_attribute {
1414
/** Gets the type of this attribute. */
1515
Type getType() { result = getConstructor().getDeclaringType() }
1616

17-
override string toString() { result = "[" + getType().toString() + "(...)]" }
17+
override string toString() { result = "[" + getType().getName() + "(...)]" }
1818

1919
/** Gets the value of the `i`th argument of this attribute. */
2020
string getArgument(int i) { cil_attribute_positional_argument(this, i, result) }

csharp/ql/src/semmle/code/cil/ConsistencyChecks.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,9 @@ class InvalidOverride extends MethodViolation {
477477
}
478478

479479
override string getMessage() {
480-
result = "Overridden method from " + base.getDeclaringType() + " is not in a base type"
480+
result =
481+
"Overridden method from " + base.getDeclaringType().getQualifiedName() +
482+
" is not in a base type"
481483
}
482484
}
483485

csharp/ql/src/semmle/code/cil/Instruction.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ private import CIL
44

55
/** An instruction. */
66
class Instruction extends Element, ControlFlowNode, DataFlowNode, @cil_instruction {
7-
override string toString() { result = getIndex() + ": " + getOpcodeName() + getExtraStr() }
7+
override string toString() { result = getOpcodeName() }
8+
9+
/** Gets a more verbose textual representation of this instruction. */
10+
string toStringExtra() { result = getIndex() + ": " + getOpcodeName() + getExtraStr() }
811

912
/** Gets the method containing this instruction. */
1013
override MethodImplementation getImplementation() { cil_instruction(this, _, _, result) }

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ class MethodImplementation extends EntryPoint, @cil_method_implementation {
5454
/** Gets a string representing the disassembly of this implementation. */
5555
string getDisassembly() {
5656
result =
57-
concat(Instruction i | i = this.getAnInstruction() | i.toString(), ", " order by i.getIndex())
57+
concat(Instruction i |
58+
i = this.getAnInstruction()
59+
|
60+
i.toStringExtra(), ", " order by i.getIndex()
61+
)
5862
}
5963
}
6064

@@ -76,7 +80,7 @@ class Method extends DotNet::Callable, Element, Member, TypeContainer, DataFlowN
7680

7781
override string getName() { cil_method(this, result, _, _) }
7882

79-
override string toString() { result = this.getQualifiedName() }
83+
override string toString() { result = this.getName() }
8084

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Type extends DotNet::Type, Declaration, TypeContainer, @cil_type {
3838

3939
override string getName() { cil_type(this, result, _, _, _) }
4040

41-
override string toString() { result = getQualifiedName() }
41+
override string toString() { result = this.getName() }
4242

4343
/** Gets the containing type of this type, if any. */
4444
override Type getDeclaringType() { result = getParent() }

csharp/ql/src/semmle/code/cil/Variable.qll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class StackVariable extends Variable, @cil_stack_variable {
3939
*/
4040
class LocalVariable extends StackVariable, @cil_local_variable {
4141
override string toString() {
42-
result = "Local variable " + getIndex() + " of method " + getImplementation().getMethod()
42+
result =
43+
"Local variable " + getIndex() + " of method " + getImplementation().getMethod().getName()
4344
}
4445

4546
/** Gets the method implementation defining this local variable. */
@@ -65,7 +66,7 @@ class Parameter extends DotNet::Parameter, StackVariable, @cil_parameter {
6566
/** Gets the index of this parameter. */
6667
int getIndex() { cil_parameter(this, _, result, _) }
6768

68-
override string toString() { result = "Parameter " + getIndex() + " of " + getMethod() }
69+
override string toString() { result = "Parameter " + getIndex() + " of " + getMethod().getName() }
6970

7071
override Type getType() { cil_parameter(this, _, _, result) }
7172

csharp/ql/src/semmle/code/csharp/Attribute.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Attribute extends TopLevelExprParent, @attribute {
8989
override Location getALocation() { attribute_location(this, result) }
9090

9191
override string toString() {
92-
exists(string type, string name | type = getType().toString() |
92+
exists(string type, string name | type = getType().getName() |
9393
(if type.matches("%Attribute") then name = type.prefix(type.length() - 9) else name = type) and
9494
result = "[" + name + "(...)]"
9595
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ class TypeParameterConstraints extends Element, @type_parameter_constraints {
227227
predicate hasNullableRefTypeConstraint() { general_type_parameter_constraints(this, 5) }
228228

229229
/** Gets a textual representation of these constraints. */
230-
override string toString() { result = "where " + this.getTypeParameter().toString() + ": ..." }
230+
override string toString() { result = "where " + this.getTypeParameter().getName() + ": ..." }
231231
}
232232

233233
/**

csharp/ql/src/semmle/code/csharp/dataflow/internal/DataFlowPrivate.qll

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ private class ExprNodeImpl extends ExprNode, NodeImpl {
6262
override string toStringImpl() {
6363
result = this.getControlFlowNode().toString()
6464
or
65-
this = TCilExprNode(_) and
66-
result = "CIL expression"
65+
exists(CIL::Expr e |
66+
this = TCilExprNode(e) and
67+
result = e.toString()
68+
)
6769
}
6870
}
6971

0 commit comments

Comments
 (0)