Skip to content

Commit 5310acc

Browse files
authored
C# extractor: extract catch(Ex) type as TypeAccess instead of TypeMention
1 parent f057604 commit 5310acc

19 files changed

Lines changed: 217 additions & 127 deletions

File tree

csharp/extractor/Semmle.Extraction.CSharp/Entities/Statements/Catch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected override void PopulateStatement(TextWriter trapFile)
2525
{
2626
var type = Type.Create(Context, Context.GetType(Stmt.Declaration!.Type));
2727
trapFile.catch_type(this, type.TypeRef, true);
28-
TypeMention.Create(Context, Stmt.Declaration!.Type, this, type);
28+
Expression.Create(Context, Stmt.Declaration!.Type, this, 0);
2929
}
3030
else // A catch clause of the form 'catch { ... }'
3131
{

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,23 @@ class SpecificCatchClause extends CatchClause {
995995
/** Gets the local variable declaration of this catch clause, if any. */
996996
LocalVariableDeclExpr getVariableDeclExpr() { result.getParent() = this }
997997

998+
/**
999+
* Gets the type access of this catch clause, if it has no variable declaration.
1000+
*
1001+
* For example, the type access in
1002+
*
1003+
* ```csharp
1004+
* try { ... }
1005+
* catch (IOException) { ... }
1006+
* ```
1007+
*
1008+
* is `IOException`.
1009+
*/
1010+
TypeAccess getTypeAccess() {
1011+
not exists(this.getVariableDeclExpr()) and
1012+
result = this.getChild(0)
1013+
}
1014+
9981015
override string toString() { result = "catch (...) {...}" }
9991016

10001017
override string getAPrimaryQlClass() { result = "SpecificCatchClause" }

csharp/ql/lib/semmle/code/csharp/controlflow/internal/ControlFlowGraph.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ module Ast implements AstSig<Location> {
9090
private AstNode getStmtChild0(Stmt s, int i) {
9191
not s instanceof FixedStmt and
9292
not s instanceof UsingBlockStmt and
93+
not skipControlFlow(result) and
9394
result = s.getChild(i)
9495
or
9596
s =
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The C# extractor now extracts the exception type in a `catch(ExceptionType)` clause (without a variable) as a `TypeAccess` expression instead of a `TypeMention`. The new `SpecificCatchClause.getTypeAccess()` predicate provides access to this expression.

csharp/ql/test/library-tests/csharp6/PrintAst.expected

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ csharp6.cs:
101101
# 32| 0: [IntLiteral] 2
102102
# 32| 0: [IntLiteral] 1
103103
# 34| 1: [SpecificCatchClause] catch (...) {...}
104-
# 34| 0: [TypeMention] IndexOutOfRangeException
104+
# 34| 0: [TypeAccess] access to type IndexOutOfRangeException
105+
# 34| 0: [TypeMention] IndexOutOfRangeException
105106
# 35| 1: [BlockStmt] {...}
106107
# 34| 2: [EQExpr] ... == ...
107108
# 34| 0: [PropertyCall] access to property Value
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| statements.cs:234:13:237:13 | checked {...} | statements.cs:235:13:237:13 | {...} |
1+
| statements.cs:250:13:253:13 | checked {...} | statements.cs:251:13:253:13 | {...} |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
| fixed.cs:3:7:3:11 | {...} | fixed.cs:3:7:3:11 | Fixed |
22
| statements.cs:8:11:8:15 | {...} | statements.cs:8:11:8:15 | Class |
3-
| statements.cs:244:15:244:25 | {...} | statements.cs:244:15:244:25 | AccountLock |
3+
| statements.cs:260:15:260:25 | {...} | statements.cs:260:15:260:25 | AccountLock |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| statements.cs:273:21:273:31 | MainLabeled | statements.cs:276:9:276:13 | Label: |
1+
| statements.cs:289:21:289:31 | MainLabeled | statements.cs:292:9:292:13 | Label: |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| statements.cs:249:17:256:17 | lock (...) {...} | statements.cs:249:23:249:26 | this access | statements.cs:250:17:256:17 | {...} |
1+
| statements.cs:265:17:272:17 | lock (...) {...} | statements.cs:265:23:265:26 | this access | statements.cs:266:17:272:17 | {...} |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| statements.cs:285:13:288:13 | lock (...) {...} | statements.cs:285:19:285:28 | access to field lockObject | Lock | statements.cs:286:13:288:13 | {...} |
1+
| statements.cs:301:13:304:13 | lock (...) {...} | statements.cs:301:19:301:28 | access to field lockObject | Lock | statements.cs:302:13:304:13 | {...} |

0 commit comments

Comments
 (0)