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

Skip to content

Commit f1193d0

Browse files
committed
C#: Add missing toString() relations
1 parent 648c199 commit f1193d0

10 files changed

Lines changed: 29 additions & 17 deletions

File tree

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55
import semmle.code.csharp.Element
66
import semmle.code.csharp.Location
77

8-
/** Adapter so that XMLLocatables are elements */
9-
library class XMLLocatableElement extends @xmllocatable, Element {
10-
override string toString() { result = this.(XMLLocatable).toString() }
11-
12-
override Location getALocation() { result = this.(XMLLocatable).getALocation() }
13-
}
14-
158
/** An XML element that has a location. */
169
class XMLLocatable extends @xmllocatable {
1710
XMLLocatable() {

csharp/ql/src/semmle/code/csharp/controlflow/Guards.qll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,10 @@ module Internal {
793793
TBooleanValue(boolean b) { b = true or b = false } or
794794
TIntegerValue(int i) { i = any(Expr e).getValue().toInt() } or
795795
TNullValue(boolean b) { b = true or b = false } or
796-
TMatchValue(Case c, boolean b) { b = true or b = false } or
796+
TMatchValue(Case c, boolean b) {
797+
exists(c.getPattern()) and
798+
(b = true or b = false)
799+
} or
797800
TEmptyCollectionValue(boolean b) { b = true or b = false }
798801

799802
/** A callable that always returns a `null` value. */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ private module Cached {
355355
TSsaDefinitionNode(Ssa::Definition def) or
356356
TInstanceParameterNode(Callable c) { c.hasBody() and not c.(Modifiable).isStatic() } or
357357
TCilParameterNode(CIL::Parameter p) { p.getMethod().hasBody() } or
358-
TTaintedParameterNode(Parameter p) { p.getCallable().hasBody() } or
358+
TTaintedParameterNode(Parameter p) { explicitParameterNode(_, p) } or
359359
TTaintedReturnNode(ControlFlow::Nodes::ElementNode cfn) {
360360
any(Callable c).canYieldReturn(cfn.getElement())
361361
} or

csharp/ql/src/semmle/code/csharp/exprs/Call.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ class MethodCall extends Call, QualifiableExpr, LateBindableExpr, @method_invoca
295295

296296
override Method getQualifiedDeclaration() { result = getTarget() }
297297

298-
override string toString() { result = "call to method " + this.getTarget().getName() }
298+
override string toString() { result = "call to method " + concat(this.getTarget().getName()) }
299299

300300
override Expr getRawArgument(int i) {
301301
if exists(getQualifier())

csharp/ql/src/semmle/code/csharp/exprs/Expr.qll

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,17 @@ private predicate isDynamicElementAccess(@dynamic_element_access_expr e) { any()
116116
* A local variable declaration, for example `var i = 0`.
117117
*/
118118
class LocalVariableDeclExpr extends Expr, @local_var_decl_expr {
119-
/** Gets the local variable being declared. */
119+
/**
120+
* Gets the local variable being declared, if any. The only case where
121+
* no variable is declared is when a discard symbol is used, for example
122+
* ```
123+
* if (int.TryParse(s, out var _))
124+
* ...
125+
* ```
126+
*/
120127
LocalVariable getVariable() { localvars(result, _, _, _, _, this) }
121128

122-
/** Gets the name of the variable being declared. */
129+
/** Gets the name of the variable being declared, if any. */
123130
string getName() { result = this.getVariable().getName() }
124131

125132
/** Gets the initializer expression of this local variable declaration, if any. */
@@ -136,6 +143,9 @@ class LocalVariableDeclExpr extends Expr, @local_var_decl_expr {
136143

137144
override string toString() {
138145
result = this.getVariable().getType().getName() + " " + this.getName()
146+
or
147+
not exists(this.getVariable()) and
148+
result = "_"
139149
}
140150

141151
/** Gets the variable access used in this declaration, if any. */

csharp/ql/test/library-tests/dataflow/global/GetAnOutNode.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@
8282
| GlobalDataFlow.cs:100:24:100:33 | call to method Return | return | GlobalDataFlow.cs:100:24:100:33 | call to method Return |
8383
| GlobalDataFlow.cs:102:28:102:63 | call to method GetMethod | return | GlobalDataFlow.cs:102:28:102:63 | call to method GetMethod |
8484
| GlobalDataFlow.cs:102:28:102:103 | call to method Invoke | return | GlobalDataFlow.cs:102:28:102:103 | call to method Invoke |
85-
| GlobalDataFlow.cs:104:9:104:46 | call to method ReturnOut | out | GlobalDataFlow.cs:104:27:104:34 | SSA def(nonSink0) |
86-
| GlobalDataFlow.cs:104:9:104:46 | call to method ReturnOut | ref | GlobalDataFlow.cs:104:27:104:34 | SSA def(nonSink0) |
85+
| GlobalDataFlow.cs:104:9:104:49 | call to method ReturnOut | out | GlobalDataFlow.cs:104:27:104:34 | SSA def(nonSink0) |
86+
| GlobalDataFlow.cs:104:9:104:49 | call to method ReturnOut | ref | GlobalDataFlow.cs:104:27:104:34 | SSA def(nonSink0) |
8787
| GlobalDataFlow.cs:106:9:106:49 | call to method ReturnOut | out | GlobalDataFlow.cs:106:41:106:48 | SSA def(nonSink0) |
8888
| GlobalDataFlow.cs:108:9:108:49 | call to method ReturnRef | out | GlobalDataFlow.cs:108:27:108:34 | SSA def(nonSink0) |
8989
| GlobalDataFlow.cs:108:9:108:49 | call to method ReturnRef | ref | GlobalDataFlow.cs:108:27:108:34 | SSA def(nonSink0) |

csharp/ql/test/library-tests/dataflow/global/GlobalDataFlow.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void M()
101101
Check(nonSink0);
102102
nonSink0 = (string)typeof(DataFlow).GetMethod("Return").Invoke(null, new object[] { nonSink0 });
103103
Check(nonSink0);
104-
ReturnOut("", out nonSink0, out var _);
104+
ReturnOut("", out nonSink0, out string _);
105105
Check(nonSink0);
106106
ReturnOut(sink1, out var _, out nonSink0);
107107
Check(nonSink0);

csharp/ql/test/library-tests/linq/Linq1.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
| queries.cs:27:11:27:42 | call to method Join | 2 | queries.cs:27:21:27:25 | access to local variable list1 |
2727
| queries.cs:27:11:27:42 | call to method Join | 3 | queries.cs:27:30:27:33 | access to local variable next |
2828
| queries.cs:27:11:27:42 | call to method Join | 4 | queries.cs:27:42:27:42 | access to local variable c |
29+
| queries.cs:28:11:28:18 | call to method | 0 | queries.cs:27:11:27:42 | call to method Join |
30+
| queries.cs:28:11:28:18 | call to method | 1 | queries.cs:28:18:28:18 | 1 |
2931
| queries.cs:32:11:32:21 | call to method SelectMany | 0 | queries.cs:31:11:31:25 | IList<Int32> a = ... |
3032
| queries.cs:32:11:32:21 | call to method SelectMany | 1 | queries.cs:32:11:32:21 | IList<Int32> b = ... |
3133
| queries.cs:32:11:32:21 | call to method SelectMany | 2 | queries.cs:32:21:32:21 | access to local variable a |
@@ -49,3 +51,5 @@
4951
| queries.cs:55:11:55:49 | call to method GroupJoin | 3 | queries.cs:55:30:55:30 | access to local variable a |
5052
| queries.cs:55:11:55:49 | call to method GroupJoin | 4 | queries.cs:55:39:55:42 | access to indexer |
5153
| queries.cs:55:11:55:49 | call to method GroupJoin | 5 | queries.cs:55:11:55:49 | IList<IList<Int32>> d = ... |
54+
| queries.cs:56:11:56:22 | call to method | 0 | queries.cs:55:11:55:49 | call to method GroupJoin |
55+
| queries.cs:56:11:56:22 | call to method | 1 | queries.cs:56:18:56:22 | (..., ...) |

csharp/ql/test/library-tests/standalone/controlflow/cfg.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
| ControlFlow.cs:9:17:9:33 | Call (unknown target) | ControlFlow.cs:9:13:9:33 | (unknown type) v |
66
| ControlFlow.cs:10:9:10:13 | Expression | ControlFlow.cs:10:22:10:22 | access to local variable v |
77
| ControlFlow.cs:10:9:10:43 | Call (unknown target) | ControlFlow.cs:12:9:12:87 | ...; |
8+
| ControlFlow.cs:10:9:10:43 | call to method | ControlFlow.cs:12:9:12:87 | ...; |
89
| ControlFlow.cs:10:9:10:44 | ...; | ControlFlow.cs:10:9:10:13 | Expression |
910
| ControlFlow.cs:10:22:10:22 | access to local variable v | ControlFlow.cs:10:22:10:24 | Expression |
1011
| ControlFlow.cs:10:22:10:24 | Expression | ControlFlow.cs:10:22:10:26 | Expression |
1112
| ControlFlow.cs:10:22:10:26 | Expression | ControlFlow.cs:10:29:10:42 | "This is true" |
1213
| ControlFlow.cs:10:29:10:42 | "This is true" | ControlFlow.cs:10:9:10:43 | Call (unknown target) |
14+
| ControlFlow.cs:10:29:10:42 | "This is true" | ControlFlow.cs:10:9:10:43 | call to method |
1315
| ControlFlow.cs:12:9:12:86 | Call (unknown target) | ControlFlow.cs:12:37:12:47 | Expression |
1416
| ControlFlow.cs:12:9:12:87 | ...; | ControlFlow.cs:12:9:12:86 | Call (unknown target) |
1517
| ControlFlow.cs:12:35:12:86 | { ..., ... } | ControlFlow.cs:7:10:7:10 | exit F |
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
| errors.cs:24:31:24:40 | errors.cs:24:31:24:40 | unknown | none |
1+
| errors.cs:24:31:24:40 | errors.cs:24:31:24:40 | call to method | none |
22
| errors.cs:43:21:43:28 | errors.cs:43:21:43:28 | object creation of type C1 | C1 |
33
| errors.cs:44:13:44:19 | errors.cs:44:13:44:19 | call to method m1 | m1 |
44
| errors.cs:45:13:45:19 | errors.cs:45:13:45:19 | call to method m2 | m2 |
5-
| errors.cs:46:13:46:38 | errors.cs:46:13:46:38 | unknown | none |
5+
| errors.cs:46:13:46:38 | errors.cs:46:13:46:38 | call to method | none |
66
| errors.cs:53:17:53:25 | errors.cs:53:17:53:25 | object creation of type C2 | none |

0 commit comments

Comments
 (0)