Skip to content

Commit 3542cda

Browse files
authored
Merge pull request #20677 from aschackmull/csharp/disable-exc-split
C#: Delete exception splitting.
2 parents 7acd214 + ebb50cd commit 3542cda

File tree

15 files changed

+398
-582
lines changed

15 files changed

+398
-582
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,6 @@ module ControlFlow {
294294
}
295295

296296
class Split = Splitting::Split;
297-
298-
class ExceptionHandlerSplit = Splitting::ExceptionHandlerSplitting::ExceptionHandlerSplit;
299297
}
300298

301299
class BasicBlock = BBs::BasicBlock;

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

Lines changed: 2 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ private module Cached {
2525
newtype TSplitKind =
2626
TInitializerSplitKind() or
2727
TConditionalCompletionSplitKind() or
28-
TAssertionSplitKind() or
29-
TExceptionHandlerSplitKind()
28+
TAssertionSplitKind()
3029

3130
cached
3231
newtype TSplit =
@@ -35,8 +34,7 @@ private module Cached {
3534
TAssertionSplit(AssertionSplitting::Assertion a, int i, boolean success) {
3635
exists(a.getExpr(i)) and
3736
success in [false, true]
38-
} or
39-
TExceptionHandlerSplit(ExceptionClass ec)
37+
}
4038
}
4139

4240
import Cached
@@ -449,179 +447,3 @@ module AssertionSplitting {
449447
}
450448
}
451449
}
452-
453-
module ExceptionHandlerSplitting {
454-
private newtype TMatch =
455-
TAlways() or
456-
TMaybe() or
457-
TNever()
458-
459-
/**
460-
* A split for elements belonging to a `catch` clause, which determines the type of
461-
* exception to handle. For example, in
462-
*
463-
* ```csharp
464-
* try
465-
* {
466-
* if (M() > 0)
467-
* throw new ArgumentException();
468-
* else if (M() < 0)
469-
* throw new ArithmeticException("negative");
470-
* else
471-
* return;
472-
* }
473-
* catch (ArgumentException e)
474-
* {
475-
* Log.Write("M() positive");
476-
* }
477-
* catch (ArithmeticException e) when (e.Message != null)
478-
* {
479-
* Log.Write($"M() {e.Message}");
480-
* }
481-
* ```
482-
*
483-
* all control flow nodes in
484-
* ```csharp
485-
* catch (ArgumentException e)
486-
* ```
487-
* and
488-
* ```csharp
489-
* catch (ArithmeticException e) when (e.Message != null)
490-
* ```
491-
* have two splits: one representing the `try` block throwing an `ArgumentException`,
492-
* and one representing the `try` block throwing an `ArithmeticException`.
493-
*/
494-
class ExceptionHandlerSplit extends Split, TExceptionHandlerSplit {
495-
private ExceptionClass ec;
496-
497-
ExceptionHandlerSplit() { this = TExceptionHandlerSplit(ec) }
498-
499-
/** Gets the exception type that this split represents. */
500-
ExceptionClass getExceptionClass() { result = ec }
501-
502-
override string toString() { result = "exception: " + ec.toString() }
503-
}
504-
505-
private class ExceptionHandlerSplitKind extends SplitKind, TExceptionHandlerSplitKind {
506-
override int getListOrder() { result = AssertionSplitting::getNextListOrder() }
507-
508-
override string toString() { result = "ExceptionHandler" }
509-
}
510-
511-
int getNextListOrder() { result = AssertionSplitting::getNextListOrder() + 1 }
512-
513-
private class ExceptionHandlerSplitImpl extends SplitImpl instanceof ExceptionHandlerSplit {
514-
override ExceptionHandlerSplitKind getKind() { any() }
515-
516-
override predicate hasEntry(AstNode pred, AstNode succ, Completion c) {
517-
// Entry into first catch clause
518-
exists(Statements::TryStmtTree ts |
519-
super.getExceptionClass() = ts.getAThrownException(pred, c)
520-
|
521-
succ(pred, succ, c) and
522-
succ = ts.(TryStmt).getCatchClause(0).(SpecificCatchClause)
523-
)
524-
}
525-
526-
override predicate hasEntryScope(CfgScope scope, AstNode first) { none() }
527-
528-
/**
529-
* Holds if this split applies to catch clause `scc`. The parameter `match`
530-
* indicates whether the catch clause `scc` may match the exception type of
531-
* this split.
532-
*/
533-
private predicate appliesToCatchClause(SpecificCatchClause scc, TMatch match) {
534-
exists(Statements::TryStmtTree ts, ExceptionClass ec |
535-
ec = super.getExceptionClass() and
536-
ec = ts.getAThrownException(_, _) and
537-
scc = ts.(TryStmt).getACatchClause()
538-
|
539-
if scc.getCaughtExceptionType() = ec.getABaseType*()
540-
then match = TAlways()
541-
else
542-
if scc.getCaughtExceptionType() = ec.getASubType+()
543-
then match = TMaybe()
544-
else match = TNever()
545-
)
546-
}
547-
548-
/**
549-
* Holds if this split applies to control flow element `pred`, where `pred`
550-
* is a valid predecessor with completion `c`.
551-
*/
552-
private predicate appliesToPredecessor(AstNode pred, Completion c) {
553-
this.appliesTo(pred) and
554-
(succ(pred, _, c) or scopeLast(_, pred, c)) and
555-
(
556-
pred instanceof SpecificCatchClause
557-
implies
558-
pred =
559-
any(SpecificCatchClause scc |
560-
if c instanceof MatchingCompletion
561-
then
562-
exists(TMatch match | this.appliesToCatchClause(scc, match) |
563-
c =
564-
any(MatchingCompletion mc |
565-
if mc.isMatch() then match != TNever() else match != TAlways()
566-
)
567-
)
568-
else (
569-
(scc.isLast() and c instanceof ThrowCompletion)
570-
implies
571-
exists(TMatch match | this.appliesToCatchClause(scc, match) | match != TAlways())
572-
)
573-
)
574-
)
575-
}
576-
577-
/**
578-
* Holds if this split applies to `pred`, and `pred` may exit this split
579-
* with throw completion `c`, because it belongs to the last `catch` clause
580-
* in a `try` statement.
581-
*/
582-
private predicate hasLastExit(AstNode pred, ThrowCompletion c) {
583-
this.appliesToPredecessor(pred, c) and
584-
exists(TryStmt ts, SpecificCatchClause scc, int last |
585-
last(ts.getCatchClause(last), pred, c)
586-
|
587-
ts.getCatchClause(last) = scc and
588-
scc.isLast() and
589-
c.getExceptionClass() = super.getExceptionClass()
590-
)
591-
}
592-
593-
override predicate hasExit(AstNode pred, AstNode succ, Completion c) {
594-
this.appliesToPredecessor(pred, c) and
595-
succ(pred, succ, c) and
596-
(
597-
// Exit out to `catch` clause block
598-
first(any(SpecificCatchClause scc).getBlock(), succ)
599-
or
600-
// Exit out to a general `catch` clause
601-
succ instanceof GeneralCatchClause
602-
or
603-
// Exit out from last `catch` clause (no catch clauses match)
604-
this.hasLastExit(pred, c)
605-
)
606-
}
607-
608-
override predicate hasExitScope(CfgScope scope, AstNode last, Completion c) {
609-
// Exit out from last `catch` clause (no catch clauses match)
610-
this.hasLastExit(last, c) and
611-
scopeLast(scope, last, c)
612-
}
613-
614-
override predicate hasSuccessor(AstNode pred, AstNode succ, Completion c) {
615-
this.appliesToPredecessor(pred, c) and
616-
this.appliesSucc(pred, succ, c) and
617-
not first(any(SpecificCatchClause scc).getBlock(), succ) and
618-
not succ instanceof GeneralCatchClause and
619-
not exists(TryStmt ts, SpecificCatchClause scc, int last |
620-
last(ts.getCatchClause(last), pred, c)
621-
|
622-
ts.getCatchClause(last) = scc and
623-
scc.isLast()
624-
)
625-
}
626-
}
627-
}

csharp/ql/test/library-tests/assignables/AssignableDefinitionNode.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
| Assignables.cs:69:13:69:17 | ... = ... | Assignables.cs:69:13:69:17 | ... = ... |
3232
| Assignables.cs:78:22:78:22 | String s | Assignables.cs:78:22:78:22 | String s |
3333
| Assignables.cs:82:21:82:33 | String temp = ... | Assignables.cs:82:21:82:33 | String temp = ... |
34-
| Assignables.cs:84:30:84:30 | Exception e | Assignables.cs:84:30:84:30 | [exception: OutOfMemoryException] Exception e |
34+
| Assignables.cs:84:30:84:30 | Exception e | Assignables.cs:84:30:84:30 | Exception e |
3535
| Assignables.cs:92:9:92:54 | ... = ... | Assignables.cs:92:9:92:54 | ... = ... |
3636
| Assignables.cs:92:9:92:54 | ... = ... | Assignables.cs:92:9:92:54 | ... = ... |
3737
| Assignables.cs:92:9:92:54 | ... = ... | Assignables.cs:92:9:92:54 | ... = ... |

csharp/ql/test/library-tests/controlflow/graph/BasicBlock.expected

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,10 @@
367367
| ExitMethods.cs:20:10:20:11 | enter M3 | ExitMethods.cs:20:10:20:11 | exit M3 | 7 |
368368
| ExitMethods.cs:26:10:26:11 | enter M4 | ExitMethods.cs:26:10:26:11 | exit M4 | 7 |
369369
| ExitMethods.cs:32:10:32:11 | enter M5 | ExitMethods.cs:32:10:32:11 | exit M5 | 7 |
370-
| ExitMethods.cs:38:10:38:11 | enter M6 | ExitMethods.cs:42:13:42:30 | call to method ErrorAlways | 7 |
370+
| ExitMethods.cs:38:10:38:11 | enter M6 | ExitMethods.cs:44:9:47:9 | catch (...) {...} | 8 |
371371
| ExitMethods.cs:38:10:38:11 | exit M6 (normal) | ExitMethods.cs:38:10:38:11 | exit M6 | 2 |
372-
| ExitMethods.cs:44:9:47:9 | [exception: ArgumentException] catch (...) {...} | ExitMethods.cs:44:9:47:9 | [exception: ArgumentException] catch (...) {...} | 1 |
373-
| ExitMethods.cs:44:9:47:9 | [exception: Exception] catch (...) {...} | ExitMethods.cs:44:9:47:9 | [exception: Exception] catch (...) {...} | 1 |
374372
| ExitMethods.cs:45:9:47:9 | {...} | ExitMethods.cs:46:13:46:19 | return ...; | 2 |
375-
| ExitMethods.cs:48:9:51:9 | [exception: Exception] catch (...) {...} | ExitMethods.cs:48:9:51:9 | [exception: Exception] catch (...) {...} | 1 |
373+
| ExitMethods.cs:48:9:51:9 | catch (...) {...} | ExitMethods.cs:48:9:51:9 | catch (...) {...} | 1 |
376374
| ExitMethods.cs:49:9:51:9 | {...} | ExitMethods.cs:50:13:50:19 | return ...; | 2 |
377375
| ExitMethods.cs:54:10:54:11 | enter M7 | ExitMethods.cs:54:10:54:11 | exit M7 | 6 |
378376
| ExitMethods.cs:60:10:60:11 | enter M8 | ExitMethods.cs:60:10:60:11 | exit M8 | 6 |
@@ -424,25 +422,26 @@
424422
| Finally.cs:19:10:19:11 | exit M2 (abnormal) | Finally.cs:19:10:19:11 | exit M2 (abnormal) | 1 |
425423
| Finally.cs:19:10:19:11 | exit M2 (normal) | Finally.cs:19:10:19:11 | exit M2 (normal) | 1 |
426424
| Finally.cs:24:13:24:19 | return ...; | Finally.cs:24:13:24:19 | return ...; | 1 |
427-
| Finally.cs:26:9:29:9 | [exception: Exception] catch (...) {...} | Finally.cs:26:9:29:9 | [exception: Exception] catch (...) {...} | 1 |
428-
| Finally.cs:26:38:26:39 | [exception: Exception] IOException ex | Finally.cs:26:48:26:51 | [exception: Exception] true | 2 |
425+
| Finally.cs:26:9:29:9 | catch (...) {...} | Finally.cs:26:9:29:9 | catch (...) {...} | 1 |
426+
| Finally.cs:26:38:26:39 | IOException ex | Finally.cs:26:48:26:51 | true | 2 |
429427
| Finally.cs:27:9:29:9 | {...} | Finally.cs:28:13:28:18 | throw ...; | 2 |
430-
| Finally.cs:30:9:40:9 | [exception: Exception] catch (...) {...} | Finally.cs:30:9:40:9 | [exception: Exception] catch (...) {...} | 1 |
431-
| Finally.cs:30:41:30:42 | [exception: Exception] ArgumentException ex | Finally.cs:34:21:34:24 | true | 6 |
428+
| Finally.cs:30:9:40:9 | catch (...) {...} | Finally.cs:30:9:40:9 | catch (...) {...} | 1 |
429+
| Finally.cs:30:41:30:42 | ArgumentException ex | Finally.cs:34:21:34:24 | true | 6 |
432430
| Finally.cs:34:27:34:32 | throw ...; | Finally.cs:38:17:38:44 | throw ...; | 5 |
433-
| Finally.cs:41:9:43:9 | [exception: Exception] catch (...) {...} | Finally.cs:41:9:43:9 | [exception: Exception] catch (...) {...} | 1 |
431+
| Finally.cs:41:9:43:9 | catch (...) {...} | Finally.cs:41:9:43:9 | catch (...) {...} | 1 |
434432
| Finally.cs:42:9:43:9 | {...} | Finally.cs:42:9:43:9 | {...} | 1 |
433+
| Finally.cs:44:9:47:9 | catch {...} | Finally.cs:46:13:46:19 | return ...; | 3 |
435434
| Finally.cs:49:9:51:9 | {...} | Finally.cs:50:13:50:40 | call to method WriteLine | 4 |
436435
| Finally.cs:54:10:54:11 | enter M3 | Finally.cs:58:13:58:37 | call to method WriteLine | 7 |
437436
| Finally.cs:54:10:54:11 | exit M3 | Finally.cs:54:10:54:11 | exit M3 | 1 |
438437
| Finally.cs:54:10:54:11 | exit M3 (abnormal) | Finally.cs:54:10:54:11 | exit M3 (abnormal) | 1 |
439438
| Finally.cs:54:10:54:11 | exit M3 (normal) | Finally.cs:54:10:54:11 | exit M3 (normal) | 1 |
440439
| Finally.cs:59:13:59:19 | return ...; | Finally.cs:59:13:59:19 | return ...; | 1 |
441-
| Finally.cs:61:9:64:9 | [exception: Exception] catch (...) {...} | Finally.cs:61:9:64:9 | [exception: Exception] catch (...) {...} | 1 |
442-
| Finally.cs:61:38:61:39 | [exception: Exception] IOException ex | Finally.cs:61:48:61:51 | [exception: Exception] true | 2 |
440+
| Finally.cs:61:9:64:9 | catch (...) {...} | Finally.cs:61:9:64:9 | catch (...) {...} | 1 |
441+
| Finally.cs:61:38:61:39 | IOException ex | Finally.cs:61:48:61:51 | true | 2 |
443442
| Finally.cs:62:9:64:9 | {...} | Finally.cs:63:13:63:18 | throw ...; | 2 |
444-
| Finally.cs:65:9:67:9 | [exception: Exception] catch (...) {...} | Finally.cs:65:9:67:9 | [exception: Exception] catch (...) {...} | 1 |
445-
| Finally.cs:65:26:65:26 | [exception: Exception] Exception e | Finally.cs:65:35:65:51 | [exception: Exception] ... != ... | 5 |
443+
| Finally.cs:65:9:67:9 | catch (...) {...} | Finally.cs:65:9:67:9 | catch (...) {...} | 1 |
444+
| Finally.cs:65:26:65:26 | Exception e | Finally.cs:65:35:65:51 | ... != ... | 5 |
446445
| Finally.cs:66:9:67:9 | {...} | Finally.cs:66:9:67:9 | {...} | 1 |
447446
| Finally.cs:69:9:71:9 | {...} | Finally.cs:70:13:70:40 | call to method WriteLine | 4 |
448447
| Finally.cs:74:10:74:11 | enter M4 | Finally.cs:77:9:100:9 | while (...) ... | 6 |
@@ -490,10 +489,8 @@
490489
| Finally.cs:158:36:158:36 | 1 | Finally.cs:158:21:158:36 | ... == ... | 2 |
491490
| Finally.cs:159:21:159:45 | throw ...; | Finally.cs:159:21:159:45 | throw ...; | 1 |
492491
| Finally.cs:159:41:159:43 | "1" | Finally.cs:159:27:159:44 | object creation of type Exception | 2 |
493-
| Finally.cs:161:13:164:13 | [exception: Exception] catch (...) {...} | Finally.cs:161:13:164:13 | [exception: Exception] catch (...) {...} | 1 |
494-
| Finally.cs:161:13:164:13 | [exception: NullReferenceException] catch (...) {...} | Finally.cs:161:13:164:13 | [exception: NullReferenceException] catch (...) {...} | 1 |
495-
| Finally.cs:161:30:161:30 | [exception: Exception] Exception e | Finally.cs:161:39:161:54 | [exception: Exception] ... == ... | 5 |
496-
| Finally.cs:161:30:161:30 | [exception: NullReferenceException] Exception e | Finally.cs:161:39:161:54 | [exception: NullReferenceException] ... == ... | 5 |
492+
| Finally.cs:161:13:164:13 | catch (...) {...} | Finally.cs:161:13:164:13 | catch (...) {...} | 1 |
493+
| Finally.cs:161:30:161:30 | Exception e | Finally.cs:161:39:161:54 | ... == ... | 5 |
497494
| Finally.cs:162:13:164:13 | {...} | Finally.cs:163:17:163:42 | call to method WriteLine | 6 |
498495
| Finally.cs:165:13:168:13 | catch {...} | Finally.cs:167:17:167:37 | call to method WriteLine | 5 |
499496
| Finally.cs:172:11:172:20 | enter ExceptionA | Finally.cs:172:11:172:20 | exit ExceptionA | 5 |
@@ -506,11 +503,10 @@
506503
| Finally.cs:180:21:180:43 | throw ...; | Finally.cs:180:21:180:43 | throw ...; | 1 |
507504
| Finally.cs:180:27:180:42 | object creation of type ExceptionA | Finally.cs:180:27:180:42 | object creation of type ExceptionA | 1 |
508505
| Finally.cs:183:9:192:9 | {...} | Finally.cs:186:21:186:22 | access to parameter b2 | 5 |
509-
| Finally.cs:186:25:186:47 | throw ...; | Finally.cs:188:13:191:13 | [exception: ExceptionB] catch (...) {...} | 2 |
506+
| Finally.cs:186:25:186:47 | throw ...; | Finally.cs:186:25:186:47 | throw ...; | 1 |
510507
| Finally.cs:186:31:186:46 | object creation of type ExceptionB | Finally.cs:186:31:186:46 | object creation of type ExceptionB | 1 |
511-
| Finally.cs:188:13:191:13 | [exception: Exception] catch (...) {...} | Finally.cs:188:13:191:13 | [exception: Exception] catch (...) {...} | 1 |
512-
| Finally.cs:188:38:188:39 | [exception: ExceptionB] access to parameter b2 | Finally.cs:188:38:188:39 | [exception: ExceptionB] access to parameter b2 | 1 |
513-
| Finally.cs:188:38:188:39 | [exception: Exception] access to parameter b2 | Finally.cs:188:38:188:39 | [exception: Exception] access to parameter b2 | 1 |
508+
| Finally.cs:188:13:191:13 | catch (...) {...} | Finally.cs:188:13:191:13 | catch (...) {...} | 1 |
509+
| Finally.cs:188:38:188:39 | access to parameter b2 | Finally.cs:188:38:188:39 | access to parameter b2 | 1 |
514510
| Finally.cs:189:13:191:13 | {...} | Finally.cs:190:21:190:22 | access to parameter b1 | 3 |
515511
| Finally.cs:190:31:190:46 | object creation of type ExceptionC | Finally.cs:190:25:190:47 | throw ...; | 2 |
516512
| Finally.cs:195:10:195:12 | enter M10 | Finally.cs:199:17:199:18 | access to parameter b1 | 6 |

0 commit comments

Comments
 (0)