Skip to content

Commit b254aa7

Browse files
Copilotaschackmull
authored andcommitted
C#: Extract catch(Ex) type as TypeAccess instead of TypeMention
1 parent f057604 commit b254aa7

4 files changed

Lines changed: 21 additions & 2 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 =

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

0 commit comments

Comments
 (0)