From c30dffc7b95c937aa570d10169fc8f0d562d1882 Mon Sep 17 00:00:00 2001 From: Mathias Vorreiter Pedersen Date: Wed, 21 Jan 2026 13:24:54 +0000 Subject: [PATCH] C++: Add more tests. --- .../raw/internal/TranslatedAssertion.qll | 2 +- .../library-tests/ir/ir/PrintAST.expected | 243 +++++++++++------- .../library-tests/ir/ir/aliased_ir.expected | 148 +++++++---- .../ir/assertion_variable_resolution.expected | 0 .../ir/ir/assertion_variable_resolution.ql | 20 ++ cpp/ql/test/library-tests/ir/ir/ir.cpp | 53 ++-- .../test/library-tests/ir/ir/raw_ir.expected | 144 +++++++---- 7 files changed, 388 insertions(+), 222 deletions(-) create mode 100644 cpp/ql/test/library-tests/ir/ir/assertion_variable_resolution.expected create mode 100644 cpp/ql/test/library-tests/ir/ir/assertion_variable_resolution.ql diff --git a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedAssertion.qll b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedAssertion.qll index 0eebcf60951..f2b33de9512 100644 --- a/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedAssertion.qll +++ b/cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/TranslatedAssertion.qll @@ -198,7 +198,7 @@ abstract private class TranslatedAssertionOperand extends TranslatedElement, } /** An operand of an assertion that is a variable access. */ -private class TranslatedAssertionVarAccess extends TranslatedAssertionOperand { +class TranslatedAssertionVarAccess extends TranslatedAssertionOperand { TranslatedAssertionVarAccess() { hasVarAccessMacroArgument(mi, _, index, _) } Variable getVariable() { hasVarAccessMacroArgument(mi, result, index, _) } diff --git a/cpp/ql/test/library-tests/ir/ir/PrintAST.expected b/cpp/ql/test/library-tests/ir/ir/PrintAST.expected index ff5bb76bf6b..f4e9fa38d6d 100644 --- a/cpp/ql/test/library-tests/ir/ir/PrintAST.expected +++ b/cpp/ql/test/library-tests/ir/ir/PrintAST.expected @@ -25492,103 +25492,156 @@ ir.cpp: # 2859| Type = [VoidType] void # 2859| ValueCategory = prvalue # 2860| getStmt(7): [ReturnStmt] return ... -# 2865| [TopLevelFunction] void complex_assertions(int, bool, int) -# 2865| : -# 2865| getParameter(0): [Parameter] x -# 2865| Type = [IntType] int -# 2865| getParameter(1): [Parameter] b -# 2865| Type = [BoolType] bool -# 2865| getParameter(2): [Parameter] max -# 2865| Type = [IntType] int -# 2865| getEntryPoint(): [BlockStmt] { ... } -# 2866| getStmt(0): [DeclStmt] declaration -# 2866| getDeclarationEntry(0): [VariableDeclarationEntry] definition of y -# 2866| Type = [IntType] int -# 2866| getVariable().getInitializer(): [Initializer] initializer for y -# 2866| getExpr(): [CommaExpr] ... , ... -# 2866| Type = [IntType] int -# 2866| ValueCategory = prvalue(load) -# 2866| getLeftOperand(): [Literal] 0 -# 2866| Type = [IntType] int -# 2866| Value = [Literal] 0 -# 2866| ValueCategory = prvalue -# 2866| getRightOperand(): [VariableAccess] x -# 2866| Type = [IntType] int -# 2866| ValueCategory = prvalue(load) -# 2866| getLeftOperand().getFullyConverted(): [ParenthesisExpr] (...) -# 2866| Type = [VoidType] void -# 2866| ValueCategory = prvalue -# 2866| getExpr(): [CStyleCast] (void)... -# 2866| Conversion = [VoidConversion] conversion to void -# 2866| Type = [VoidType] void -# 2866| ValueCategory = prvalue -# 2866| getExpr().getFullyConverted(): [ParenthesisExpr] (...) -# 2866| Type = [IntType] int -# 2866| ValueCategory = prvalue(load) -# 2867| getStmt(1): [DeclStmt] declaration -# 2867| getDeclarationEntry(0): [VariableDeclarationEntry] definition of z -# 2867| Type = [IntType] int -# 2867| getVariable().getInitializer(): [Initializer] initializer for z -# 2867| getExpr(): [ConditionalExpr] ... ? ... : ... -# 2867| Type = [IntType] int -# 2867| ValueCategory = prvalue -# 2867| getCondition(): [VariableAccess] b -# 2867| Type = [BoolType] bool -# 2867| ValueCategory = prvalue(load) -# 2867| getThen(): [CommaExpr] ... , ... -# 2867| Type = [IntType] int -# 2867| Value = [CommaExpr] 0 -# 2867| ValueCategory = prvalue -# 2867| getLeftOperand(): [Literal] 0 -# 2867| Type = [IntType] int -# 2867| Value = [Literal] 0 -# 2867| ValueCategory = prvalue -# 2867| getRightOperand(): [Literal] 0 -# 2867| Type = [IntType] int -# 2867| Value = [Literal] 0 -# 2867| ValueCategory = prvalue -# 2867| getLeftOperand().getFullyConverted(): [ParenthesisExpr] (...) -# 2867| Type = [VoidType] void -# 2867| ValueCategory = prvalue -# 2867| getExpr(): [CStyleCast] (void)... -# 2867| Conversion = [VoidConversion] conversion to void -# 2867| Type = [VoidType] void -# 2867| ValueCategory = prvalue -# 2867| getElse(): [Literal] 1 -# 2867| Type = [IntType] int -# 2867| Value = [Literal] 1 -# 2867| ValueCategory = prvalue -# 2867| getThen().getFullyConverted(): [ParenthesisExpr] (...) -# 2867| Type = [IntType] int -# 2867| Value = [ParenthesisExpr] 0 -# 2867| ValueCategory = prvalue -# 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] -# 2871| getParameter(): [Parameter] c -# 2871| Type = [IntType] int -# 2871| getBlock(): [CatchBlock] { ... } -# 2872| getStmt(0): [ExprStmt] ExprStmt -# 2872| getExpr(): [Literal] 0 +# 2867| [TopLevelFunction] void (unnamed namespace)::complex_assertions(int, bool, int) +# 2867| : +# 2867| getParameter(0): [Parameter] x +# 2867| Type = [IntType] int +# 2867| getParameter(1): [Parameter] b +# 2867| Type = [BoolType] bool +# 2867| getParameter(2): [Parameter] max +# 2867| Type = [IntType] int +# 2867| getEntryPoint(): [BlockStmt] { ... } +# 2868| getStmt(0): [DeclStmt] declaration +# 2868| getDeclarationEntry(0): [VariableDeclarationEntry] definition of y +# 2868| Type = [IntType] int +# 2868| getVariable().getInitializer(): [Initializer] initializer for y +# 2868| getExpr(): [CommaExpr] ... , ... +# 2868| Type = [IntType] int +# 2868| ValueCategory = prvalue(load) +# 2868| getLeftOperand(): [Literal] 0 +# 2868| Type = [IntType] int +# 2868| Value = [Literal] 0 +# 2868| ValueCategory = prvalue +# 2868| getRightOperand(): [VariableAccess] x +# 2868| Type = [IntType] int +# 2868| ValueCategory = prvalue(load) +# 2868| getLeftOperand().getFullyConverted(): [ParenthesisExpr] (...) +# 2868| Type = [VoidType] void +# 2868| ValueCategory = prvalue +# 2868| getExpr(): [CStyleCast] (void)... +# 2868| Conversion = [VoidConversion] conversion to void +# 2868| Type = [VoidType] void +# 2868| ValueCategory = prvalue +# 2868| getExpr().getFullyConverted(): [ParenthesisExpr] (...) +# 2868| Type = [IntType] int +# 2868| ValueCategory = prvalue(load) +# 2869| getStmt(1): [DeclStmt] declaration +# 2869| getDeclarationEntry(0): [VariableDeclarationEntry] definition of z +# 2869| Type = [IntType] int +# 2869| getVariable().getInitializer(): [Initializer] initializer for z +# 2869| getExpr(): [ConditionalExpr] ... ? ... : ... +# 2869| Type = [IntType] int +# 2869| ValueCategory = prvalue +# 2869| getCondition(): [VariableAccess] b +# 2869| Type = [BoolType] bool +# 2869| ValueCategory = prvalue(load) +# 2869| getThen(): [CommaExpr] ... , ... +# 2869| Type = [IntType] int +# 2869| Value = [CommaExpr] 0 +# 2869| ValueCategory = prvalue +# 2869| getLeftOperand(): [Literal] 0 +# 2869| Type = [IntType] int +# 2869| Value = [Literal] 0 +# 2869| ValueCategory = prvalue +# 2869| getRightOperand(): [Literal] 0 +# 2869| Type = [IntType] int +# 2869| Value = [Literal] 0 +# 2869| ValueCategory = prvalue +# 2869| getLeftOperand().getFullyConverted(): [ParenthesisExpr] (...) +# 2869| Type = [VoidType] void +# 2869| ValueCategory = prvalue +# 2869| getExpr(): [CStyleCast] (void)... +# 2869| Conversion = [VoidConversion] conversion to void +# 2869| Type = [VoidType] void +# 2869| ValueCategory = prvalue +# 2869| getElse(): [Literal] 1 +# 2869| Type = [IntType] int +# 2869| Value = [Literal] 1 +# 2869| ValueCategory = prvalue +# 2869| getThen().getFullyConverted(): [ParenthesisExpr] (...) +# 2869| Type = [IntType] int +# 2869| Value = [ParenthesisExpr] 0 +# 2869| ValueCategory = prvalue +# 2871| getStmt(2): [TryStmt] try { ... } +# 2871| getStmt(): [BlockStmt] { ... } +# 2872| getStmt(0): [ExprStmt] ExprStmt +# 2872| getExpr(): [ThrowExpr] throw ... +# 2872| Type = [IntType] int +# 2872| ValueCategory = prvalue +# 2872| getExpr(): [Literal] 41 # 2872| Type = [IntType] int -# 2872| Value = [Literal] 0 +# 2872| Value = [Literal] 41 # 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 ... +# 2873| getChild(1): [Handler] +# 2873| getParameter(): [Parameter] c +# 2873| Type = [IntType] int +# 2873| getBlock(): [CatchBlock] { ... } +# 2874| getStmt(0): [ExprStmt] ExprStmt +# 2874| getExpr(): [Literal] 0 +# 2874| Type = [IntType] int +# 2874| Value = [Literal] 0 +# 2874| ValueCategory = prvalue +# 2874| getExpr().getFullyConverted(): [ParenthesisExpr] (...) +# 2874| Type = [VoidType] void +# 2874| ValueCategory = prvalue +# 2874| getExpr(): [CStyleCast] (void)... +# 2874| Conversion = [VoidConversion] conversion to void +# 2874| Type = [VoidType] void +# 2874| ValueCategory = prvalue +# 2875| getStmt(1): [ExprStmt] ExprStmt +# 2875| getExpr(): [Literal] 0 +# 2875| Type = [IntType] int +# 2875| Value = [Literal] 0 +# 2875| ValueCategory = prvalue +# 2875| getExpr().getFullyConverted(): [ParenthesisExpr] (...) +# 2875| Type = [VoidType] void +# 2875| ValueCategory = prvalue +# 2875| getExpr(): [CStyleCast] (void)... +# 2875| Conversion = [VoidConversion] conversion to void +# 2875| Type = [VoidType] void +# 2875| ValueCategory = prvalue +# 2878| getStmt(3): [ExprStmt] ExprStmt +# 2878| getExpr(): [Literal] 0 +# 2878| Type = [IntType] int +# 2878| Value = [Literal] 0 +# 2878| ValueCategory = prvalue +# 2878| getExpr().getFullyConverted(): [ParenthesisExpr] (...) +# 2878| Type = [VoidType] void +# 2878| ValueCategory = prvalue +# 2878| getExpr(): [CStyleCast] (void)... +# 2878| Conversion = [VoidConversion] conversion to void +# 2878| Type = [VoidType] void +# 2878| ValueCategory = prvalue +# 2879| getStmt(4): [DeclStmt] declaration +# 2879| getDeclarationEntry(0): [VariableDeclarationEntry] definition of shadowed +# 2879| Type = [IntType] int +# 2881| getStmt(5): [TryStmt] try { ... } +# 2881| getStmt(): [BlockStmt] { ... } +# 2882| getStmt(0): [ExprStmt] ExprStmt +# 2882| getExpr(): [ThrowExpr] throw ... +# 2882| Type = [IntType] int +# 2882| ValueCategory = prvalue +# 2882| getExpr(): [Literal] 41 +# 2882| Type = [IntType] int +# 2882| Value = [Literal] 41 +# 2882| ValueCategory = prvalue +# 2883| getChild(1): [Handler] +# 2883| getParameter(): [Parameter] shadowed +# 2883| Type = [IntType] int +# 2883| getBlock(): [CatchBlock] { ... } +# 2884| getStmt(0): [ExprStmt] ExprStmt +# 2884| getExpr(): [Literal] 0 +# 2884| Type = [IntType] int +# 2884| Value = [Literal] 0 +# 2884| ValueCategory = prvalue +# 2884| getExpr().getFullyConverted(): [ParenthesisExpr] (...) +# 2884| Type = [VoidType] void +# 2884| ValueCategory = prvalue +# 2884| getExpr(): [CStyleCast] (void)... +# 2884| Conversion = [VoidConversion] conversion to void +# 2884| Type = [VoidType] void +# 2884| ValueCategory = prvalue +# 2886| getStmt(6): [ReturnStmt] return ... ir23.cpp: # 1| [TopLevelFunction] bool consteval_1() # 1| : diff --git a/cpp/ql/test/library-tests/ir/ir/aliased_ir.expected b/cpp/ql/test/library-tests/ir/ir/aliased_ir.expected index 2e183c02f85..3df68744f42 100644 --- a/cpp/ql/test/library-tests/ir/ir/aliased_ir.expected +++ b/cpp/ql/test/library-tests/ir/ir/aliased_ir.expected @@ -20967,75 +20967,115 @@ ir.cpp: # 2846| v2846_12(void) = AliasedUse : m2846_3 # 2846| v2846_13(void) = ExitFunction : -# 2865| void complex_assertions(int, bool, int) -# 2865| Block 0 -# 2865| v2865_1(void) = EnterFunction : -# 2865| m2865_2(unknown) = AliasedDefinition : -# 2865| m2865_3(unknown) = InitializeNonLocal : -# 2865| m2865_4(unknown) = Chi : total:m2865_2, partial:m2865_3 -# 2865| r2865_5(glval) = VariableAddress[x] : -# 2865| m2865_6(int) = InitializeParameter[x] : &:r2865_5 -# 2865| r2865_7(glval) = VariableAddress[b] : -# 2865| m2865_8(bool) = InitializeParameter[b] : &:r2865_7 -# 2865| r2865_9(glval) = VariableAddress[max] : -# 2865| m2865_10(int) = InitializeParameter[max] : &:r2865_9 -# 2866| r2866_1(glval) = VariableAddress[y] : -# 2866| r2866_2(int) = Constant[0] : -# 2866| v2866_3(void) = Convert : r2866_2 -# 2866| r2866_4(glval) = VariableAddress[x] : -# 2866| r2866_5(int) = Load[x] : &:r2866_4, m2865_6 -# 2866| r2866_6(int) = CopyValue : r2866_5 -# 2866| m2866_7(int) = Store[y] : &:r2866_1, r2866_6 -# 2867| r2867_1(glval) = VariableAddress[z] : -# 2867| r2867_2(glval) = VariableAddress[b] : -# 2867| r2867_3(bool) = Load[b] : &:r2867_2, m2865_8 -# 2867| v2867_4(void) = ConditionalBranch : r2867_3 +# 2867| void (unnamed namespace)::complex_assertions(int, bool, int) +# 2867| Block 0 +# 2867| v2867_1(void) = EnterFunction : +# 2867| m2867_2(unknown) = AliasedDefinition : +# 2867| m2867_3(unknown) = InitializeNonLocal : +# 2867| m2867_4(unknown) = Chi : total:m2867_2, partial:m2867_3 +# 2867| r2867_5(glval) = VariableAddress[x] : +# 2867| m2867_6(int) = InitializeParameter[x] : &:r2867_5 +# 2867| r2867_7(glval) = VariableAddress[b] : +# 2867| m2867_8(bool) = InitializeParameter[b] : &:r2867_7 +# 2867| r2867_9(glval) = VariableAddress[max] : +# 2867| m2867_10(int) = InitializeParameter[max] : &:r2867_9 +# 2868| r2868_1(glval) = VariableAddress[y] : +# 2868| r2868_2(int) = Constant[0] : +# 2868| v2868_3(void) = Convert : r2868_2 +# 2868| r2868_4(glval) = VariableAddress[x] : +# 2868| r2868_5(int) = Load[x] : &:r2868_4, m2867_6 +# 2868| r2868_6(int) = CopyValue : r2868_5 +# 2868| m2868_7(int) = Store[y] : &:r2868_1, r2868_6 +# 2869| r2869_1(glval) = VariableAddress[z] : +# 2869| r2869_2(glval) = VariableAddress[b] : +# 2869| r2869_3(bool) = Load[b] : &:r2869_2, m2867_8 +# 2869| v2869_4(void) = ConditionalBranch : r2869_3 #-----| False -> Block 5 #-----| True -> Block 4 -# 2865| Block 1 -# 2865| v2865_11(void) = AliasedUse : m2865_3 -# 2865| v2865_12(void) = ExitFunction : +# 2867| Block 1 +# 2867| v2867_11(void) = AliasedUse : m2867_3 +# 2867| v2867_12(void) = ExitFunction : -# 2865| Block 2 -# 2865| v2865_13(void) = Unwind : +# 2867| Block 2 +# 2867| v2867_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) = 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) = 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 +# 2869| Block 3 +# 2869| m2869_5(int) = Phi : from 4:m2869_11, from 5:m2869_14 +# 2869| r2869_6(glval) = VariableAddress[#temp2869:17] : +# 2869| r2869_7(int) = Load[#temp2869:17] : &:r2869_6, m2869_5 +# 2869| m2869_8(int) = Store[z] : &:r2869_1, r2869_7 +# 2872| r2872_1(glval) = VariableAddress[#throw2872:13] : +# 2872| r2872_2(int) = Constant[41] : +# 2872| m2872_3(int) = Store[#throw2872:13] : &:r2872_1, r2872_2 +# 2872| v2872_4(void) = ThrowValue : &:r2872_1, m2872_3 #-----| C++ Exception -> Block 6 -# 2867| Block 4 -# 2867| r2867_9(int) = Constant[0] : -# 2867| r2867_10(glval) = VariableAddress[#temp2867:13] : -# 2867| m2867_11(int) = Store[#temp2867:13] : &:r2867_10, r2867_9 +# 2869| Block 4 +# 2869| r2869_9(int) = Constant[0] : +# 2869| r2869_10(glval) = VariableAddress[#temp2869:17] : +# 2869| m2869_11(int) = Store[#temp2869:17] : &:r2869_10, r2869_9 #-----| Goto -> Block 3 -# 2867| Block 5 -# 2867| r2867_12(int) = Constant[1] : -# 2867| r2867_13(glval) = VariableAddress[#temp2867:13] : -# 2867| m2867_14(int) = Store[#temp2867:13] : &:r2867_13, r2867_12 +# 2869| Block 5 +# 2869| r2869_12(int) = Constant[1] : +# 2869| r2869_13(glval) = VariableAddress[#temp2869:17] : +# 2869| m2869_14(int) = Store[#temp2869:17] : &:r2869_13, r2869_12 #-----| Goto -> Block 3 -# 2871| Block 6 -# 2871| v2871_1(void) = CatchByType[int] : +# 2873| Block 6 +# 2873| v2873_1(void) = CatchByType[int] : #-----| C++ Exception -> Block 2 #-----| Goto -> Block 7 -# 2871| Block 7 -# 2871| r2871_2(glval) = 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 : +# 2873| Block 7 +# 2873| r2873_2(glval) = VariableAddress[c] : +# 2873| m2873_3(int) = InitializeParameter[c] : &:r2873_2 +# 2874| r2874_1(int) = Constant[0] : +# 2874| v2874_2(void) = Convert : r2874_1 +# 2875| r2875_1(glval) = VariableAddress[shadowed] : +# 2875| r2875_2(int) = Load[shadowed] : &:r2875_1, ~m2867_4 +# 2875| r2875_3(int) = Constant[42] : +# 2875| r2875_4(bool) = CompareLT : r2875_2, r2875_3 +# 2875| v2875_5(void) = ConditionalBranch : r2875_4 +#-----| True -> Block 8 + +# 2878| Block 8 +# 2878| r2878_1(glval) = VariableAddress[shadowed] : +# 2878| r2878_2(int) = Load[shadowed] : &:r2878_1, ~m2867_4 +# 2878| r2878_3(int) = Constant[0] : +# 2878| r2878_4(bool) = CompareGT : r2878_2, r2878_3 +# 2878| v2878_5(void) = ConditionalBranch : r2878_4 +#-----| True -> Block 9 + +# 2879| Block 9 +# 2879| r2879_1(glval) = VariableAddress[shadowed] : +# 2879| m2879_2(int) = Uninitialized[shadowed] : &:r2879_1 +# 2882| r2882_1(glval) = VariableAddress[#throw2882:13] : +# 2882| r2882_2(int) = Constant[41] : +# 2882| m2882_3(int) = Store[#throw2882:13] : &:r2882_1, r2882_2 +# 2882| v2882_4(void) = ThrowValue : &:r2882_1, m2882_3 +#-----| C++ Exception -> Block 10 + +# 2883| Block 10 +# 2883| v2883_1(void) = CatchByType[int] : +#-----| C++ Exception -> Block 2 +#-----| Goto -> Block 11 + +# 2883| Block 11 +# 2883| r2883_2(glval) = VariableAddress[shadowed] : +# 2883| m2883_3(int) = InitializeParameter[shadowed] : &:r2883_2 +# 2884| r2884_1(glval) = VariableAddress[shadowed] : +# 2884| r2884_2(int) = Load[shadowed] : &:r2884_1, m2879_2 +# 2884| r2884_3(int) = Constant[42] : +# 2884| r2884_4(bool) = CompareLT : r2884_2, r2884_3 +# 2884| v2884_5(void) = ConditionalBranch : r2884_4 +#-----| True -> Block 12 + +# 2886| Block 12 +# 2886| v2886_1(void) = NoOp : +# 2867| v2867_14(void) = ReturnVoid : #-----| Goto -> Block 1 ir23.cpp: diff --git a/cpp/ql/test/library-tests/ir/ir/assertion_variable_resolution.expected b/cpp/ql/test/library-tests/ir/ir/assertion_variable_resolution.expected new file mode 100644 index 00000000000..e69de29bb2d diff --git a/cpp/ql/test/library-tests/ir/ir/assertion_variable_resolution.ql b/cpp/ql/test/library-tests/ir/ir/assertion_variable_resolution.ql new file mode 100644 index 00000000000..fae5baae8be --- /dev/null +++ b/cpp/ql/test/library-tests/ir/ir/assertion_variable_resolution.ql @@ -0,0 +1,20 @@ +import cpp +import semmle.code.cpp.ir.IR +import semmle.code.cpp.ir.implementation.raw.internal.TranslatedAssertion +import utils.test.InlineExpectationsTest + +module Test implements TestSig { + string getARelevantTag() { result = "var" } + + predicate hasActualResult(Location location, string element, string tag, string value) { + exists(TranslatedAssertionVarAccess tava, Variable v | + v = tava.getVariable() and + location = tava.getLocation() and + tava.toString() = element and + tag = "var" and + value = v.getLocation().getStartLine().toString() + ) + } +} + +import MakeTest diff --git a/cpp/ql/test/library-tests/ir/ir/ir.cpp b/cpp/ql/test/library-tests/ir/ir/ir.cpp index 0eb6ef50df0..7b25ed244c2 100644 --- a/cpp/ql/test/library-tests/ir/ir/ir.cpp +++ b/cpp/ql/test/library-tests/ir/ir/ir.cpp @@ -2828,48 +2828,61 @@ void vla_sizeof_test5(int len1, size_t len2) { #define __analysis_assume(x) void test_assert_simple(int x, int y, unsigned u, int shadowed) { - assert(x > 0); - assert(0 < x); - assert(x < y); + assert(x > 0); // $ var=2830 + assert(0 < x); // $ var=2830 + assert(x < y); // $ var=2830 - __analysis_assume(x != 2); + __analysis_assume(x != 2); // $ var=2830 - assert(u < x); + assert(u < x); // $ var=2830 { int shadowed = x; - assert(shadowed > 0); + assert(shadowed > 0); // no assertion generated since the variable is shadowed } } template void test_assert_in_template(T x, int y, unsigned u) { - assert(x > 0); - assert(0 < x); - assert(x < y); + assert(x > 0); // $ var=2846 + assert(0 < x); // $ var=2846 + assert(x < y); // $ var=2846 - __analysis_assume(x != 2); + __analysis_assume(x != 2); // $ var=2846 - assert(u < x); + assert(u < x); // $ var=2846 { int shadowed = x; - assert(shadowed > 0); + assert(shadowed > 0); // $ var=2856 } - assert(x> 0); + assert(x> 0); // $ var=2846 } template void test_assert_in_template(int, int, unsigned); template void test_assert_in_template(short, int, unsigned); +namespace { + int shadowed; -void complex_assertions(int x, bool b, int max) { - int y = (assert(x > 0), x); - int z = b ? (assert(x != 0), 0) : 1; + void complex_assertions(int x, bool b, int max) { + int y = (assert(x > 0), x); // no assertion generated + int z = b ? (assert(x != 0), 0) : 1; // no assertion generated - try { - throw 41; - } catch (int c) { - assert(c < 42); + try { + throw 41; + } catch (int c) { + assert(c < 42); // $ MISSING: var=2873 + assert(shadowed < 42); // $ SPURIOUS: var=2879 + } + + assert(shadowed > 0); // $ SPURIOUS: var=2879 + int shadowed; + + try { + throw 41; + } catch (int shadowed) { + assert(shadowed < 42); // $ SPURIOUS: var=2879 + } } } diff --git a/cpp/ql/test/library-tests/ir/ir/raw_ir.expected b/cpp/ql/test/library-tests/ir/ir/raw_ir.expected index 431ba110344..e2683619687 100644 --- a/cpp/ql/test/library-tests/ir/ir/raw_ir.expected +++ b/cpp/ql/test/library-tests/ir/ir/raw_ir.expected @@ -19103,73 +19103,113 @@ ir.cpp: # 2846| v2846_11(void) = AliasedUse : ~m? # 2846| v2846_12(void) = ExitFunction : -# 2865| void complex_assertions(int, bool, int) -# 2865| Block 0 -# 2865| v2865_1(void) = EnterFunction : -# 2865| mu2865_2(unknown) = AliasedDefinition : -# 2865| mu2865_3(unknown) = InitializeNonLocal : -# 2865| r2865_4(glval) = VariableAddress[x] : -# 2865| mu2865_5(int) = InitializeParameter[x] : &:r2865_4 -# 2865| r2865_6(glval) = VariableAddress[b] : -# 2865| mu2865_7(bool) = InitializeParameter[b] : &:r2865_6 -# 2865| r2865_8(glval) = VariableAddress[max] : -# 2865| mu2865_9(int) = InitializeParameter[max] : &:r2865_8 -# 2866| r2866_1(glval) = VariableAddress[y] : -# 2866| r2866_2(int) = Constant[0] : -# 2866| v2866_3(void) = Convert : r2866_2 -# 2866| r2866_4(glval) = VariableAddress[x] : -# 2866| r2866_5(int) = Load[x] : &:r2866_4, ~m? -# 2866| r2866_6(int) = CopyValue : r2866_5 -# 2866| mu2866_7(int) = Store[y] : &:r2866_1, r2866_6 -# 2867| r2867_1(glval) = VariableAddress[z] : -# 2867| r2867_2(glval) = VariableAddress[b] : -# 2867| r2867_3(bool) = Load[b] : &:r2867_2, ~m? -# 2867| v2867_4(void) = ConditionalBranch : r2867_3 +# 2867| void (unnamed namespace)::complex_assertions(int, bool, int) +# 2867| Block 0 +# 2867| v2867_1(void) = EnterFunction : +# 2867| mu2867_2(unknown) = AliasedDefinition : +# 2867| mu2867_3(unknown) = InitializeNonLocal : +# 2867| r2867_4(glval) = VariableAddress[x] : +# 2867| mu2867_5(int) = InitializeParameter[x] : &:r2867_4 +# 2867| r2867_6(glval) = VariableAddress[b] : +# 2867| mu2867_7(bool) = InitializeParameter[b] : &:r2867_6 +# 2867| r2867_8(glval) = VariableAddress[max] : +# 2867| mu2867_9(int) = InitializeParameter[max] : &:r2867_8 +# 2868| r2868_1(glval) = VariableAddress[y] : +# 2868| r2868_2(int) = Constant[0] : +# 2868| v2868_3(void) = Convert : r2868_2 +# 2868| r2868_4(glval) = VariableAddress[x] : +# 2868| r2868_5(int) = Load[x] : &:r2868_4, ~m? +# 2868| r2868_6(int) = CopyValue : r2868_5 +# 2868| mu2868_7(int) = Store[y] : &:r2868_1, r2868_6 +# 2869| r2869_1(glval) = VariableAddress[z] : +# 2869| r2869_2(glval) = VariableAddress[b] : +# 2869| r2869_3(bool) = Load[b] : &:r2869_2, ~m? +# 2869| v2869_4(void) = ConditionalBranch : r2869_3 #-----| False -> Block 5 #-----| True -> Block 4 -# 2865| Block 1 -# 2865| v2865_10(void) = AliasedUse : ~m? -# 2865| v2865_11(void) = ExitFunction : +# 2867| Block 1 +# 2867| v2867_10(void) = AliasedUse : ~m? +# 2867| v2867_11(void) = ExitFunction : -# 2865| Block 2 -# 2865| v2865_12(void) = Unwind : +# 2867| Block 2 +# 2867| v2867_12(void) = Unwind : #-----| Goto -> Block 1 -# 2867| Block 3 -# 2867| r2867_5(glval) = 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) = 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? +# 2869| Block 3 +# 2869| r2869_5(glval) = VariableAddress[#temp2869:17] : +# 2869| r2869_6(int) = Load[#temp2869:17] : &:r2869_5, ~m? +# 2869| mu2869_7(int) = Store[z] : &:r2869_1, r2869_6 +# 2872| r2872_1(glval) = VariableAddress[#throw2872:13] : +# 2872| r2872_2(int) = Constant[41] : +# 2872| mu2872_3(int) = Store[#throw2872:13] : &:r2872_1, r2872_2 +# 2872| v2872_4(void) = ThrowValue : &:r2872_1, ~m? #-----| C++ Exception -> Block 6 -# 2867| Block 4 -# 2867| r2867_8(int) = Constant[0] : -# 2867| r2867_9(glval) = VariableAddress[#temp2867:13] : -# 2867| mu2867_10(int) = Store[#temp2867:13] : &:r2867_9, r2867_8 +# 2869| Block 4 +# 2869| r2869_8(int) = Constant[0] : +# 2869| r2869_9(glval) = VariableAddress[#temp2869:17] : +# 2869| mu2869_10(int) = Store[#temp2869:17] : &:r2869_9, r2869_8 #-----| Goto -> Block 3 -# 2867| Block 5 -# 2867| r2867_11(int) = Constant[1] : -# 2867| r2867_12(glval) = VariableAddress[#temp2867:13] : -# 2867| mu2867_13(int) = Store[#temp2867:13] : &:r2867_12, r2867_11 +# 2869| Block 5 +# 2869| r2869_11(int) = Constant[1] : +# 2869| r2869_12(glval) = VariableAddress[#temp2869:17] : +# 2869| mu2869_13(int) = Store[#temp2869:17] : &:r2869_12, r2869_11 #-----| Goto -> Block 3 -# 2871| Block 6 -# 2871| v2871_1(void) = CatchByType[int] : +# 2873| Block 6 +# 2873| v2873_1(void) = CatchByType[int] : #-----| C++ Exception -> Block 2 #-----| Goto -> Block 7 -# 2871| Block 7 -# 2871| r2871_2(glval) = 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 : +# 2873| Block 7 +# 2873| r2873_2(glval) = VariableAddress[c] : +# 2873| mu2873_3(int) = InitializeParameter[c] : &:r2873_2 +# 2874| r2874_1(int) = Constant[0] : +# 2874| v2874_2(void) = Convert : r2874_1 +# 2875| r2875_1(glval) = VariableAddress[shadowed] : +# 2875| r2875_2(int) = Load[shadowed] : &:r2875_1, ~m? +# 2875| r2875_3(int) = Constant[42] : +# 2875| r2875_4(bool) = CompareLT : r2875_2, r2875_3 +# 2875| v2875_5(void) = ConditionalBranch : r2875_4 +#-----| True -> Block 8 + +# 2878| Block 8 +# 2878| r2878_1(glval) = VariableAddress[shadowed] : +# 2878| r2878_2(int) = Load[shadowed] : &:r2878_1, ~m? +# 2878| r2878_3(int) = Constant[0] : +# 2878| r2878_4(bool) = CompareGT : r2878_2, r2878_3 +# 2878| v2878_5(void) = ConditionalBranch : r2878_4 +#-----| True -> Block 9 + +# 2879| Block 9 +# 2879| r2879_1(glval) = VariableAddress[shadowed] : +# 2879| mu2879_2(int) = Uninitialized[shadowed] : &:r2879_1 +# 2882| r2882_1(glval) = VariableAddress[#throw2882:13] : +# 2882| r2882_2(int) = Constant[41] : +# 2882| mu2882_3(int) = Store[#throw2882:13] : &:r2882_1, r2882_2 +# 2882| v2882_4(void) = ThrowValue : &:r2882_1, ~m? +#-----| C++ Exception -> Block 10 + +# 2883| Block 10 +# 2883| v2883_1(void) = CatchByType[int] : +#-----| C++ Exception -> Block 2 +#-----| Goto -> Block 11 + +# 2883| Block 11 +# 2883| r2883_2(glval) = VariableAddress[shadowed] : +# 2883| mu2883_3(int) = InitializeParameter[shadowed] : &:r2883_2 +# 2884| r2884_1(glval) = VariableAddress[shadowed] : +# 2884| r2884_2(int) = Load[shadowed] : &:r2884_1, ~m? +# 2884| r2884_3(int) = Constant[42] : +# 2884| r2884_4(bool) = CompareLT : r2884_2, r2884_3 +# 2884| v2884_5(void) = ConditionalBranch : r2884_4 +#-----| True -> Block 12 + +# 2886| Block 12 +# 2886| v2886_1(void) = NoOp : +# 2867| v2867_13(void) = ReturnVoid : #-----| Goto -> Block 1 ir23.cpp: