C++: Add assertion inside catch block. This does not yet work because the 'LocalScopeVariable' does not have a result for 'getFunction'.

This commit is contained in:
Mathias Vorreiter Pedersen
2026-01-20 17:21:54 +00:00
parent d183dcd6fc
commit 048afc2c88
4 changed files with 108 additions and 30 deletions

View File

@@ -25562,7 +25562,33 @@ ir.cpp:
# 2867| Type = [IntType] int
# 2867| Value = [ParenthesisExpr] 0
# 2867| ValueCategory = prvalue
# 2868| getStmt(2): [ReturnStmt] return ...
# 2869| getStmt(2): [TryStmt] try { ... }
# 2869| getStmt(): [BlockStmt] { ... }
# 2870| getStmt(0): [ExprStmt] ExprStmt
# 2870| getExpr(): [ThrowExpr] throw ...
# 2870| Type = [IntType] int
# 2870| ValueCategory = prvalue
# 2870| getExpr(): [Literal] 41
# 2870| Type = [IntType] int
# 2870| Value = [Literal] 41
# 2870| ValueCategory = prvalue
# 2871| getChild(1): [Handler] <handler>
# 2871| getParameter(): [Parameter] c
# 2871| Type = [IntType] int
# 2871| getBlock(): [CatchBlock] { ... }
# 2872| getStmt(0): [ExprStmt] ExprStmt
# 2872| getExpr(): [Literal] 0
# 2872| Type = [IntType] int
# 2872| Value = [Literal] 0
# 2872| ValueCategory = prvalue
# 2872| getExpr().getFullyConverted(): [ParenthesisExpr] (...)
# 2872| Type = [VoidType] void
# 2872| ValueCategory = prvalue
# 2872| getExpr(): [CStyleCast] (void)...
# 2872| Conversion = [VoidConversion] conversion to void
# 2872| Type = [VoidType] void
# 2872| ValueCategory = prvalue
# 2874| getStmt(3): [ReturnStmt] return ...
ir23.cpp:
# 1| [TopLevelFunction] bool consteval_1()
# 1| <params>:

View File

@@ -20990,29 +20990,52 @@ ir.cpp:
# 2867| r2867_2(glval<bool>) = VariableAddress[b] :
# 2867| r2867_3(bool) = Load[b] : &:r2867_2, m2865_8
# 2867| v2867_4(void) = ConditionalBranch : r2867_3
#-----| False -> Block 3
#-----| True -> Block 2
#-----| False -> Block 5
#-----| True -> Block 4
# 2867| Block 1
# 2867| m2867_5(int) = Phi : from 2:m2867_11, from 3:m2867_14
# 2867| r2867_6(glval<int>) = VariableAddress[#temp2867:13] :
# 2867| r2867_7(int) = Load[#temp2867:13] : &:r2867_6, m2867_5
# 2867| m2867_8(int) = Store[z] : &:r2867_1, r2867_7
# 2868| v2868_1(void) = NoOp :
# 2865| v2865_11(void) = ReturnVoid :
# 2865| v2865_12(void) = AliasedUse : m2865_3
# 2865| v2865_13(void) = ExitFunction :
# 2865| Block 1
# 2865| v2865_11(void) = AliasedUse : m2865_3
# 2865| v2865_12(void) = ExitFunction :
# 2867| Block 2
# 2867| r2867_9(int) = Constant[0] :
# 2867| r2867_10(glval<int>) = VariableAddress[#temp2867:13] :
# 2867| m2867_11(int) = Store[#temp2867:13] : &:r2867_10, r2867_9
# 2865| Block 2
# 2865| v2865_13(void) = Unwind :
#-----| Goto -> Block 1
# 2867| Block 3
# 2867| m2867_5(int) = Phi : from 4:m2867_11, from 5:m2867_14
# 2867| r2867_6(glval<int>) = VariableAddress[#temp2867:13] :
# 2867| r2867_7(int) = Load[#temp2867:13] : &:r2867_6, m2867_5
# 2867| m2867_8(int) = Store[z] : &:r2867_1, r2867_7
# 2870| r2870_1(glval<int>) = VariableAddress[#throw2870:9] :
# 2870| r2870_2(int) = Constant[41] :
# 2870| m2870_3(int) = Store[#throw2870:9] : &:r2870_1, r2870_2
# 2870| v2870_4(void) = ThrowValue : &:r2870_1, m2870_3
#-----| C++ Exception -> Block 6
# 2867| Block 4
# 2867| r2867_9(int) = Constant[0] :
# 2867| r2867_10(glval<int>) = VariableAddress[#temp2867:13] :
# 2867| m2867_11(int) = Store[#temp2867:13] : &:r2867_10, r2867_9
#-----| Goto -> Block 3
# 2867| Block 5
# 2867| r2867_12(int) = Constant[1] :
# 2867| r2867_13(glval<int>) = VariableAddress[#temp2867:13] :
# 2867| m2867_14(int) = Store[#temp2867:13] : &:r2867_13, r2867_12
#-----| Goto -> Block 3
# 2871| Block 6
# 2871| v2871_1(void) = CatchByType[int] :
#-----| C++ Exception -> Block 2
#-----| Goto -> Block 7
# 2871| Block 7
# 2871| r2871_2(glval<int>) = VariableAddress[c] :
# 2871| m2871_3(int) = InitializeParameter[c] : &:r2871_2
# 2872| r2872_1(int) = Constant[0] :
# 2872| v2872_2(void) = Convert : r2872_1
# 2874| v2874_1(void) = NoOp :
# 2865| v2865_14(void) = ReturnVoid :
#-----| Goto -> Block 1
ir23.cpp:

View File

@@ -2865,6 +2865,12 @@ template void test_assert_in_template<short>(short, int, unsigned);
void complex_assertions(int x, bool b, int max) {
int y = (assert(x > 0), x);
int z = b ? (assert(x != 0), 0) : 1;
try {
throw 41;
} catch (int c) {
assert(c < 42);
}
}
// semmle-extractor-options: -std=c++20 --clang

View File

@@ -19125,28 +19125,51 @@ ir.cpp:
# 2867| r2867_2(glval<bool>) = VariableAddress[b] :
# 2867| r2867_3(bool) = Load[b] : &:r2867_2, ~m?
# 2867| v2867_4(void) = ConditionalBranch : r2867_3
#-----| False -> Block 3
#-----| True -> Block 2
#-----| False -> Block 5
#-----| True -> Block 4
# 2867| Block 1
# 2867| r2867_5(glval<int>) = VariableAddress[#temp2867:13] :
# 2867| r2867_6(int) = Load[#temp2867:13] : &:r2867_5, ~m?
# 2867| mu2867_7(int) = Store[z] : &:r2867_1, r2867_6
# 2868| v2868_1(void) = NoOp :
# 2865| v2865_10(void) = ReturnVoid :
# 2865| v2865_11(void) = AliasedUse : ~m?
# 2865| v2865_12(void) = ExitFunction :
# 2865| Block 1
# 2865| v2865_10(void) = AliasedUse : ~m?
# 2865| v2865_11(void) = ExitFunction :
# 2867| Block 2
# 2867| r2867_8(int) = Constant[0] :
# 2867| r2867_9(glval<int>) = VariableAddress[#temp2867:13] :
# 2867| mu2867_10(int) = Store[#temp2867:13] : &:r2867_9, r2867_8
# 2865| Block 2
# 2865| v2865_12(void) = Unwind :
#-----| Goto -> Block 1
# 2867| Block 3
# 2867| r2867_5(glval<int>) = VariableAddress[#temp2867:13] :
# 2867| r2867_6(int) = Load[#temp2867:13] : &:r2867_5, ~m?
# 2867| mu2867_7(int) = Store[z] : &:r2867_1, r2867_6
# 2870| r2870_1(glval<int>) = VariableAddress[#throw2870:9] :
# 2870| r2870_2(int) = Constant[41] :
# 2870| mu2870_3(int) = Store[#throw2870:9] : &:r2870_1, r2870_2
# 2870| v2870_4(void) = ThrowValue : &:r2870_1, ~m?
#-----| C++ Exception -> Block 6
# 2867| Block 4
# 2867| r2867_8(int) = Constant[0] :
# 2867| r2867_9(glval<int>) = VariableAddress[#temp2867:13] :
# 2867| mu2867_10(int) = Store[#temp2867:13] : &:r2867_9, r2867_8
#-----| Goto -> Block 3
# 2867| Block 5
# 2867| r2867_11(int) = Constant[1] :
# 2867| r2867_12(glval<int>) = VariableAddress[#temp2867:13] :
# 2867| mu2867_13(int) = Store[#temp2867:13] : &:r2867_12, r2867_11
#-----| Goto -> Block 3
# 2871| Block 6
# 2871| v2871_1(void) = CatchByType[int] :
#-----| C++ Exception -> Block 2
#-----| Goto -> Block 7
# 2871| Block 7
# 2871| r2871_2(glval<int>) = VariableAddress[c] :
# 2871| mu2871_3(int) = InitializeParameter[c] : &:r2871_2
# 2872| r2872_1(int) = Constant[0] :
# 2872| v2872_2(void) = Convert : r2872_1
# 2874| v2874_1(void) = NoOp :
# 2865| v2865_13(void) = ReturnVoid :
#-----| Goto -> Block 1
ir23.cpp: