Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de4ed59

Browse files
committed
C#: Simplify toString() for CIL entities
1 parent 8f6dbe9 commit de4ed59

11 files changed

Lines changed: 65 additions & 55 deletions

File tree

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/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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| System.Runtime.CompilerServices.AsyncTaskMethodBuilder.DebugFinalizableAsyncStateMachineBox.Finalize | Overridden method from System.Object is not in a base type |
1+
| Finalize | Overridden method from Object is not in a base type |
Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
| C:/dev/projects/Sandbox/EmbeddedPdb/Class1.cs:6:3:6:4 | 0: nop |
2-
| C:/dev/projects/Sandbox/EmbeddedPdb/Class1.cs:7:4:7:13 | 1: ldc.i4.1 |
3-
| C:/dev/projects/Sandbox/EmbeddedPdb/Class1.cs:7:4:7:13 | 2: stloc.0 L0 |
4-
| C:/dev/projects/Sandbox/EmbeddedPdb/Class1.cs:7:4:7:13 | 3: br.s 4: |
5-
| C:/dev/projects/Sandbox/EmbeddedPdb/Class1.cs:8:3:8:4 | 4: ldloc.0 |
6-
| C:/dev/projects/Sandbox/EmbeddedPdb/Class1.cs:8:3:8:4 | 5: ret |
7-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:6:3:6:4 | 0: nop |
8-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:7:4:7:20 | 1: ldc.i4.1 |
9-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:7:4:7:20 | 2: stloc.0 L0 |
10-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:9:8:18 | 3: ldc.i4.1 |
11-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:9:8:18 | 4: stloc.1 L1 |
12-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:9:8:18 | 5: br.s 14: |
13-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | 14: ldloc.1 |
14-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | 15: ldarg.1 |
15-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | 16: cgt |
16-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | 17: ldc.i4.0 |
17-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | 18: ceq |
18-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | 19: stloc.2 L2 |
19-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | 20: ldloc.2 |
20-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | 21: brtrue.s 6: |
21-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:28:8:31 | 10: ldloc.1 |
22-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:28:8:31 | 11: ldc.i4.1 |
23-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:28:8:31 | 12: add |
24-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:28:8:31 | 13: stloc.1 L1 |
25-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:9:5:9:18 | 6: ldloc.0 |
26-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:9:5:9:18 | 7: ldloc.1 |
27-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:9:5:9:18 | 8: mul |
28-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:9:5:9:18 | 9: stloc.0 L0 |
29-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:10:4:10:19 | 22: ldloc.0 |
30-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:10:4:10:19 | 23: stloc.3 L3 |
31-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:10:4:10:19 | 24: br.s 25: |
32-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:11:3:11:4 | 25: ldloc.3 |
33-
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:11:3:11:4 | 26: ret |
34-
| EmbeddedPdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | 0: ldarg.0 |
35-
| EmbeddedPdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | 1: call System.Object..ctor |
36-
| EmbeddedPdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | 2: nop |
37-
| EmbeddedPdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | 3: ret |
38-
| PortablePdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | 0: ldarg.0 |
39-
| PortablePdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | 1: call System.Object..ctor |
40-
| PortablePdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | 2: nop |
41-
| PortablePdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | 3: ret |
1+
| C:/dev/projects/Sandbox/EmbeddedPdb/Class1.cs:6:3:6:4 | nop |
2+
| C:/dev/projects/Sandbox/EmbeddedPdb/Class1.cs:7:4:7:13 | br.s |
3+
| C:/dev/projects/Sandbox/EmbeddedPdb/Class1.cs:7:4:7:13 | ldc.i4.1 |
4+
| C:/dev/projects/Sandbox/EmbeddedPdb/Class1.cs:7:4:7:13 | stloc.0 |
5+
| C:/dev/projects/Sandbox/EmbeddedPdb/Class1.cs:8:3:8:4 | ldloc.0 |
6+
| C:/dev/projects/Sandbox/EmbeddedPdb/Class1.cs:8:3:8:4 | ret |
7+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:6:3:6:4 | nop |
8+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:7:4:7:20 | ldc.i4.1 |
9+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:7:4:7:20 | stloc.0 |
10+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:9:8:18 | br.s |
11+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:9:8:18 | ldc.i4.1 |
12+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:9:8:18 | stloc.1 |
13+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | brtrue.s |
14+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | ceq |
15+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | cgt |
16+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | ldarg.1 |
17+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | ldc.i4.0 |
18+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | ldloc.1 |
19+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | ldloc.2 |
20+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:20:8:26 | stloc.2 |
21+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:28:8:31 | add |
22+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:28:8:31 | ldc.i4.1 |
23+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:28:8:31 | ldloc.1 |
24+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:8:28:8:31 | stloc.1 |
25+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:9:5:9:18 | ldloc.0 |
26+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:9:5:9:18 | ldloc.1 |
27+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:9:5:9:18 | mul |
28+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:9:5:9:18 | stloc.0 |
29+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:10:4:10:19 | br.s |
30+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:10:4:10:19 | ldloc.0 |
31+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:10:4:10:19 | stloc.3 |
32+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:11:3:11:4 | ldloc.3 |
33+
| C:/dev/projects/Sandbox/PortablePdb/Class1.cs:11:3:11:4 | ret |
34+
| EmbeddedPdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | call |
35+
| EmbeddedPdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | ldarg.0 |
36+
| EmbeddedPdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | nop |
37+
| EmbeddedPdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | ret |
38+
| PortablePdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | call |
39+
| PortablePdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | ldarg.0 |
40+
| PortablePdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | nop |
41+
| PortablePdb, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null | ret |

0 commit comments

Comments
 (0)