Skip to content

Commit c6e0ee2

Browse files
committed
C++: fix duplicated UnreachedInstruction in raw IR
1 parent 6dfc598 commit c6e0ee2

14 files changed

+220
-10
lines changed

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/internal/IRConstruction.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,9 @@ predicate hasUnreachedInstruction(IRFunction func) {
410410
exists(Call c |
411411
c.getEnclosingFunction() = func.getFunction() and
412412
any(Options opt).exits(c.getTarget())
413+
) and
414+
not exists(TranslatedUnreachableReturnStmt return |
415+
return.getEnclosingFunction().getFunction() = func.getFunction()
413416
)
414417
}
415418

cpp/ql/test/library-tests/ir/ir/PrintAST.expected

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14437,6 +14437,31 @@ ir.cpp:
1443714437
# 1903| Type = [VoidType] void
1443814438
# 1903| ValueCategory = prvalue
1443914439
# 1905| getStmt(1): [ReturnStmt] return ...
14440+
# 1907| [TopLevelFunction] int noreturnTest2(int)
14441+
# 1907| <params>:
14442+
# 1907| getParameter(0): [Parameter] x
14443+
# 1907| Type = [IntType] int
14444+
# 1907| getEntryPoint(): [BlockStmt] { ... }
14445+
# 1908| getStmt(0): [IfStmt] if (...) ...
14446+
# 1908| getCondition(): [LTExpr] ... < ...
14447+
# 1908| Type = [BoolType] bool
14448+
# 1908| ValueCategory = prvalue
14449+
# 1908| getLesserOperand(): [VariableAccess] x
14450+
# 1908| Type = [IntType] int
14451+
# 1908| ValueCategory = prvalue(load)
14452+
# 1908| getGreaterOperand(): [Literal] 10
14453+
# 1908| Type = [IntType] int
14454+
# 1908| Value = [Literal] 10
14455+
# 1908| ValueCategory = prvalue
14456+
# 1908| getThen(): [BlockStmt] { ... }
14457+
# 1909| getStmt(0): [ExprStmt] ExprStmt
14458+
# 1909| getExpr(): [FunctionCall] call to noreturnFunc
14459+
# 1909| Type = [VoidType] void
14460+
# 1909| ValueCategory = prvalue
14461+
# 1911| getStmt(1): [ReturnStmt] return ...
14462+
# 1911| getExpr(): [VariableAccess] x
14463+
# 1911| Type = [IntType] int
14464+
# 1911| ValueCategory = prvalue(load)
1444014465
perf-regression.cpp:
1444114466
# 4| [CopyAssignmentOperator] Big& Big::operator=(Big const&)
1444214467
# 4| <params>:

cpp/ql/test/library-tests/ir/ir/aliased_ssa_consistency.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ missingOperandType
66
duplicateChiOperand
77
sideEffectWithoutPrimary
88
instructionWithoutSuccessor
9-
| ir.cpp:1754:41:1754:42 | Chi: call to CopyConstructorTestVirtualClass | Instruction 'Chi: call to CopyConstructorTestVirtualClass' has no successors in function '$@'. | ir.cpp:1750:5:1750:34 | int implicit_copy_constructor_test(CopyConstructorTestNonVirtualClass const&, CopyConstructorTestVirtualClass const&) | int implicit_copy_constructor_test(CopyConstructorTestNonVirtualClass const&, CopyConstructorTestVirtualClass const&) |
10-
| ir.cpp:1903:9:1903:20 | Chi: call to noreturnFunc | Instruction 'Chi: call to noreturnFunc' has no successors in function '$@'. | ir.cpp:1899:5:1899:16 | int noreturnTest(int) | int noreturnTest(int) |
119
ambiguousSuccessors
1210
unexplainedLoop
1311
unnecessaryPhiInstruction

cpp/ql/test/library-tests/ir/ir/aliased_ssa_consistency_unsound.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ missingOperandType
66
duplicateChiOperand
77
sideEffectWithoutPrimary
88
instructionWithoutSuccessor
9-
| ir.cpp:1754:41:1754:42 | Chi: call to CopyConstructorTestVirtualClass | Instruction 'Chi: call to CopyConstructorTestVirtualClass' has no successors in function '$@'. | ir.cpp:1750:5:1750:34 | int implicit_copy_constructor_test(CopyConstructorTestNonVirtualClass const&, CopyConstructorTestVirtualClass const&) | int implicit_copy_constructor_test(CopyConstructorTestNonVirtualClass const&, CopyConstructorTestVirtualClass const&) |
10-
| ir.cpp:1903:9:1903:20 | Chi: call to noreturnFunc | Instruction 'Chi: call to noreturnFunc' has no successors in function '$@'. | ir.cpp:1899:5:1899:16 | int noreturnTest(int) | int noreturnTest(int) |
119
ambiguousSuccessors
1210
unexplainedLoop
1311
unnecessaryPhiInstruction

cpp/ql/test/library-tests/ir/ir/ir.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,4 +1904,11 @@ int noreturnTest(int x) {
19041904
}
19051905
}
19061906

1907+
int noreturnTest2(int x) {
1908+
if (x < 10) {
1909+
noreturnFunc();
1910+
}
1911+
return x;
1912+
}
1913+
19071914
// semmle-extractor-options: -std=c++17 --clang

cpp/ql/test/library-tests/ir/ir/operand_locations.expected

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8802,6 +8802,25 @@
88028802
| ir.cpp:1903:9:1903:20 | ChiPartial | partial:m1903_3 |
88038803
| ir.cpp:1903:9:1903:20 | ChiTotal | total:m1899_4 |
88048804
| ir.cpp:1903:9:1903:20 | SideEffect | ~m1899_4 |
8805+
| ir.cpp:1907:5:1907:17 | Address | &:r1907_8 |
8806+
| ir.cpp:1907:5:1907:17 | ChiPartial | partial:m1907_3 |
8807+
| ir.cpp:1907:5:1907:17 | ChiTotal | total:m1907_2 |
8808+
| ir.cpp:1907:5:1907:17 | Load | m1911_4 |
8809+
| ir.cpp:1907:5:1907:17 | SideEffect | m1907_3 |
8810+
| ir.cpp:1907:23:1907:23 | Address | &:r1907_5 |
8811+
| ir.cpp:1908:9:1908:9 | Address | &:r1908_1 |
8812+
| ir.cpp:1908:9:1908:9 | Left | r1908_2 |
8813+
| ir.cpp:1908:9:1908:9 | Load | m1907_6 |
8814+
| ir.cpp:1908:9:1908:14 | Condition | r1908_4 |
8815+
| ir.cpp:1908:13:1908:14 | Right | r1908_3 |
8816+
| ir.cpp:1909:9:1909:20 | CallTarget | func:r1909_1 |
8817+
| ir.cpp:1909:9:1909:20 | ChiPartial | partial:m1909_3 |
8818+
| ir.cpp:1909:9:1909:20 | ChiTotal | total:m1907_4 |
8819+
| ir.cpp:1909:9:1909:20 | SideEffect | ~m1907_4 |
8820+
| ir.cpp:1911:5:1911:13 | Address | &:r1911_1 |
8821+
| ir.cpp:1911:12:1911:12 | Address | &:r1911_2 |
8822+
| ir.cpp:1911:12:1911:12 | Load | m1907_6 |
8823+
| ir.cpp:1911:12:1911:12 | StoreValue | r1911_3 |
88058824
| perf-regression.cpp:6:3:6:5 | Address | &:r6_5 |
88068825
| perf-regression.cpp:6:3:6:5 | Address | &:r6_5 |
88078826
| perf-regression.cpp:6:3:6:5 | Address | &:r6_7 |

cpp/ql/test/library-tests/ir/ir/raw_ir.expected

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10136,6 +10136,37 @@ ir.cpp:
1013610136
# 1903| mu1903_3(unknown) = ^CallSideEffect : ~m?
1013710137
# 1905| v1905_1(void) = Unreached :
1013810138

10139+
# 1907| int noreturnTest2(int)
10140+
# 1907| Block 0
10141+
# 1907| v1907_1(void) = EnterFunction :
10142+
# 1907| mu1907_2(unknown) = AliasedDefinition :
10143+
# 1907| mu1907_3(unknown) = InitializeNonLocal :
10144+
# 1907| r1907_4(glval<int>) = VariableAddress[x] :
10145+
# 1907| mu1907_5(int) = InitializeParameter[x] : &:r1907_4
10146+
# 1908| r1908_1(glval<int>) = VariableAddress[x] :
10147+
# 1908| r1908_2(int) = Load[x] : &:r1908_1, ~m?
10148+
# 1908| r1908_3(int) = Constant[10] :
10149+
# 1908| r1908_4(bool) = CompareLT : r1908_2, r1908_3
10150+
# 1908| v1908_5(void) = ConditionalBranch : r1908_4
10151+
#-----| False -> Block 2
10152+
#-----| True -> Block 1
10153+
10154+
# 1909| Block 1
10155+
# 1909| r1909_1(glval<unknown>) = FunctionAddress[noreturnFunc] :
10156+
# 1909| v1909_2(void) = Call[noreturnFunc] : func:r1909_1
10157+
# 1909| mu1909_3(unknown) = ^CallSideEffect : ~m?
10158+
# 1907| v1907_6(void) = Unreached :
10159+
10160+
# 1911| Block 2
10161+
# 1911| r1911_1(glval<int>) = VariableAddress[#return] :
10162+
# 1911| r1911_2(glval<int>) = VariableAddress[x] :
10163+
# 1911| r1911_3(int) = Load[x] : &:r1911_2, ~m?
10164+
# 1911| mu1911_4(int) = Store[#return] : &:r1911_1, r1911_3
10165+
# 1907| r1907_7(glval<int>) = VariableAddress[#return] :
10166+
# 1907| v1907_8(void) = ReturnValue : &:r1907_7, ~m?
10167+
# 1907| v1907_9(void) = AliasedUse : ~m?
10168+
# 1907| v1907_10(void) = ExitFunction :
10169+
1013910170
perf-regression.cpp:
1014010171
# 6| void Big::Big()
1014110172
# 6| Block 0

cpp/ql/test/library-tests/ir/ssa/aliased_ssa_consistency.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ missingOperandType
66
duplicateChiOperand
77
sideEffectWithoutPrimary
88
instructionWithoutSuccessor
9-
| ssa.cpp:427:9:427:20 | Chi: call to noreturnFunc | Instruction 'Chi: call to noreturnFunc' has no successors in function '$@'. | ssa.cpp:423:5:423:16 | int noreturnTest(int) | int noreturnTest(int) |
109
ambiguousSuccessors
1110
unexplainedLoop
1211
unnecessaryPhiInstruction

cpp/ql/test/library-tests/ir/ssa/aliased_ssa_consistency_unsound.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ missingOperandType
66
duplicateChiOperand
77
sideEffectWithoutPrimary
88
instructionWithoutSuccessor
9-
| ssa.cpp:427:9:427:20 | Chi: call to noreturnFunc | Instruction 'Chi: call to noreturnFunc' has no successors in function '$@'. | ssa.cpp:423:5:423:16 | int noreturnTest(int) | int noreturnTest(int) |
109
ambiguousSuccessors
1110
unexplainedLoop
1211
unnecessaryPhiInstruction

cpp/ql/test/library-tests/ir/ssa/aliased_ssa_ir.expected

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,6 +2123,37 @@ ssa.cpp:
21232123
# 427| v427_2(void) = Call[noreturnFunc] : func:r427_1
21242124
# 427| m427_3(unknown) = ^CallSideEffect : ~m423_4
21252125
# 427| m427_4(unknown) = Chi : total:m423_4, partial:m427_3
2126+
# 423| v423_11(void) = Unreached :
2127+
2128+
# 431| int noreturnTest2(int)
2129+
# 431| Block 0
2130+
# 431| v431_1(void) = EnterFunction :
2131+
# 431| m431_2(unknown) = AliasedDefinition :
2132+
# 431| m431_3(unknown) = InitializeNonLocal :
2133+
# 431| m431_4(unknown) = Chi : total:m431_2, partial:m431_3
2134+
# 431| r431_5(glval<int>) = VariableAddress[x] :
2135+
# 431| m431_6(int) = InitializeParameter[x] : &:r431_5
2136+
# 432| r432_1(glval<int>) = VariableAddress[x] :
2137+
# 432| r432_2(int) = Load[x] : &:r432_1, m431_6
2138+
# 432| r432_3(int) = Constant[10] :
2139+
# 432| r432_4(bool) = CompareLT : r432_2, r432_3
2140+
# 432| v432_5(void) = ConditionalBranch : r432_4
2141+
#-----| False -> Block 2
2142+
#-----| True -> Block 1
21262143

2127-
# 423| Block 3
2128-
# 423| v423_11(void) = Unreached :
2144+
# 433| Block 1
2145+
# 433| r433_1(glval<unknown>) = FunctionAddress[noreturnFunc] :
2146+
# 433| v433_2(void) = Call[noreturnFunc] : func:r433_1
2147+
# 433| m433_3(unknown) = ^CallSideEffect : ~m431_4
2148+
# 433| m433_4(unknown) = Chi : total:m431_4, partial:m433_3
2149+
# 431| v431_7(void) = Unreached :
2150+
2151+
# 435| Block 2
2152+
# 435| r435_1(glval<int>) = VariableAddress[#return] :
2153+
# 435| r435_2(glval<int>) = VariableAddress[x] :
2154+
# 435| r435_3(int) = Load[x] : &:r435_2, m431_6
2155+
# 435| m435_4(int) = Store[#return] : &:r435_1, r435_3
2156+
# 431| r431_8(glval<int>) = VariableAddress[#return] :
2157+
# 431| v431_9(void) = ReturnValue : &:r431_8, m435_4
2158+
# 431| v431_10(void) = AliasedUse : m431_3
2159+
# 431| v431_11(void) = ExitFunction :

0 commit comments

Comments
 (0)