Skip to content

Commit 5814349

Browse files
committed
C++: Give names in structured binding declarations correct IR types
1 parent 73f0366 commit 5814349

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

cpp/ql/lib/semmle/code/cpp/ir/internal/IRUtilities.qll

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ private Type getDecayedType(Type type) {
1111
result.(PointerType).getBaseType() = type.(ArrayType).getBaseType()
1212
}
1313

14+
/**
15+
* Holds if the sepcified variable is a structured binding with a non-reference
16+
* type.
17+
*/
18+
predicate isNonReferenceStructuredBinding(Variable v) {
19+
v.isStructuredBinding() and
20+
not v.getUnderlyingType() instanceof ReferenceType
21+
}
22+
1423
/**
1524
* Get the actual type of the specified variable, as opposed to the declared type.
1625
* This returns the type of the variable after any pointer decay is applied, and
@@ -30,7 +39,10 @@ Type getVariableType(Variable v) {
3039
result = v.getInitializer().getExpr().getType()
3140
or
3241
not exists(v.getInitializer()) and result = v.getType()
33-
else result = v.getType()
42+
else
43+
if isNonReferenceStructuredBinding(v)
44+
then exists(ReferenceType r | r.getBaseType() = v.getUnderlyingType() | result = r)
45+
else result = v.getType()
3446
)
3547
}
3648

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6694,6 +6694,8 @@
66946694
| ir.cpp:1466:26:1466:27 | StoreValue | r1466_3 |
66956695
| ir.cpp:1466:26:1466:27 | Unary | r1466_2 |
66966696
| ir.cpp:1467:9:1467:10 | Address | &:r1467_2 |
6697+
| ir.cpp:1467:9:1467:14 | ChiPartial | partial:m1467_3 |
6698+
| ir.cpp:1467:9:1467:14 | ChiTotal | total:m0_14 |
66976699
| ir.cpp:1467:14:1467:14 | StoreValue | r1467_1 |
66986700
| ir.cpp:1468:14:1468:16 | Address | &:r1468_1 |
66996701
| ir.cpp:1468:20:1468:21 | StoreValue | r1468_3 |
@@ -6801,9 +6803,11 @@
68016803
| ir.cpp:1500:22:1500:22 | Unary | r1500_2 |
68026804
| ir.cpp:1501:13:1501:13 | Address | &:r1501_1 |
68036805
| ir.cpp:1501:17:1501:17 | Address | &:r1501_2 |
6804-
| ir.cpp:1501:17:1501:17 | Load | m1498_8 |
6806+
| ir.cpp:1501:17:1501:17 | Load | ~m1498_8 |
68056807
| ir.cpp:1501:17:1501:17 | StoreValue | r1501_3 |
68066808
| ir.cpp:1502:9:1502:9 | Address | &:r1502_2 |
6809+
| ir.cpp:1502:9:1502:13 | ChiPartial | partial:m1502_3 |
6810+
| ir.cpp:1502:9:1502:13 | ChiTotal | total:m1498_22 |
68076811
| ir.cpp:1502:13:1502:13 | StoreValue | r1502_1 |
68086812
| ir.cpp:1503:9:1503:10 | Address | &:r1503_4 |
68096813
| ir.cpp:1503:9:1503:14 | ChiPartial | partial:m1503_5 |

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7915,22 +7915,22 @@ ir.cpp:
79157915
# 1466| r1466_2(glval<int[2]>) = VariableAddress[xs] :
79167916
# 1466| r1466_3(int(&)[2]) = CopyValue : r1466_2
79177917
# 1466| mu1466_4(int(&)[2]) = Store[(unnamed local variable)] : &:r1466_1, r1466_3
7918-
# 1466| r1466_5(glval<int>) = VariableAddress[x0] :
7918+
# 1466| r1466_5(glval<int &>) = VariableAddress[x0] :
79197919
#-----| r0_1(glval<int(&)[2]>) = VariableAddress[(unnamed local variable)] :
79207920
#-----| r0_2(int(&)[2]) = Load[(unnamed local variable)] : &:r0_1, ~m?
79217921
#-----| r0_3(glval<int[2]>) = CopyValue : r0_2
79227922
#-----| r0_4(int *) = Convert : r0_3
79237923
#-----| r0_5(unsigned long) = Constant[0] :
79247924
#-----| r0_6(glval<int>) = PointerAdd[4] : r0_4, r0_5
7925-
#-----| mu0_7(int) = Store[x0] : &:r1466_5, r0_6
7926-
# 1466| r1466_6(glval<int>) = VariableAddress[x1] :
7925+
#-----| mu0_7(int &) = Store[x0] : &:r1466_5, r0_6
7926+
# 1466| r1466_6(glval<int &>) = VariableAddress[x1] :
79277927
#-----| r0_8(glval<int(&)[2]>) = VariableAddress[(unnamed local variable)] :
79287928
#-----| r0_9(int(&)[2]) = Load[(unnamed local variable)] : &:r0_8, ~m?
79297929
#-----| r0_10(glval<int[2]>) = CopyValue : r0_9
79307930
#-----| r0_11(int *) = Convert : r0_10
79317931
#-----| r0_12(unsigned long) = Constant[1] :
79327932
#-----| r0_13(glval<int>) = PointerAdd[4] : r0_11, r0_12
7933-
#-----| mu0_14(int) = Store[x1] : &:r1466_6, r0_13
7933+
#-----| mu0_14(int &) = Store[x1] : &:r1466_6, r0_13
79347934
# 1467| r1467_1(int) = Constant[3] :
79357935
# 1467| r1467_2(glval<int>) = VariableAddress[x1] :
79367936
# 1467| mu1467_3(int) = Store[x1] : &:r1467_2, r1467_1
@@ -8015,38 +8015,38 @@ ir.cpp:
80158015
# 1498| r1498_2(glval<StructuredBindingDataMemberStruct>) = VariableAddress[s] :
80168016
# 1498| r1498_3(StructuredBindingDataMemberStruct) = Load[s] : &:r1498_2, ~m?
80178017
# 1498| mu1498_4(StructuredBindingDataMemberStruct) = Store[(unnamed local variable)] : &:r1498_1, r1498_3
8018-
# 1498| r1498_5(glval<int>) = VariableAddress[i] :
8018+
# 1498| r1498_5(glval<int &>) = VariableAddress[i] :
80198019
# 1498| r1498_6(glval<StructuredBindingDataMemberStruct>) = VariableAddress[(unnamed local variable)] :
80208020
# 1498| r1498_7(glval<int>) = FieldAddress[i] : r1498_6
8021-
# 1498| mu1498_8(int) = Store[i] : &:r1498_5, r1498_7
8022-
# 1498| r1498_9(glval<double>) = VariableAddress[d] :
8021+
# 1498| mu1498_8(int &) = Store[i] : &:r1498_5, r1498_7
8022+
# 1498| r1498_9(glval<double &>) = VariableAddress[d] :
80238023
# 1498| r1498_10(glval<StructuredBindingDataMemberStruct>) = VariableAddress[(unnamed local variable)] :
80248024
# 1498| r1498_11(glval<double>) = FieldAddress[d] : r1498_10
8025-
# 1498| mu1498_12(double) = Store[d] : &:r1498_9, r1498_11
8026-
# 1498| r1498_13(glval<unsigned int>) = VariableAddress[b] :
8025+
# 1498| mu1498_12(double &) = Store[d] : &:r1498_9, r1498_11
8026+
# 1498| r1498_13(glval<unsigned int &>) = VariableAddress[b] :
80278027
# 1498| r1498_14(glval<StructuredBindingDataMemberStruct>) = VariableAddress[(unnamed local variable)] :
80288028
# 1498| r1498_15(glval<unsigned int>) = FieldAddress[b] : r1498_14
8029-
# 1498| mu1498_16(unsigned int) = Store[b] : &:r1498_13, r1498_15
8030-
# 1498| r1498_17(glval<int>) = VariableAddress[r] :
8029+
# 1498| mu1498_16(unsigned int &) = Store[b] : &:r1498_13, r1498_15
8030+
# 1498| r1498_17(glval<int &>) = VariableAddress[r] :
80318031
# 1498| r1498_18(glval<StructuredBindingDataMemberStruct>) = VariableAddress[(unnamed local variable)] :
80328032
# 1498| r1498_19(glval<int &>) = FieldAddress[r] : r1498_18
80338033
# 1498| r1498_20(int &) = Load[?] : &:r1498_19, ~m?
80348034
# 1498| r1498_21(glval<int>) = CopyValue : r1498_20
8035-
# 1498| mu1498_22(int) = Store[r] : &:r1498_17, r1498_21
8036-
# 1498| r1498_23(glval<int *>) = VariableAddress[p] :
8035+
# 1498| mu1498_22(int &) = Store[r] : &:r1498_17, r1498_21
8036+
# 1498| r1498_23(glval<int *&>) = VariableAddress[p] :
80378037
# 1498| r1498_24(glval<StructuredBindingDataMemberStruct>) = VariableAddress[(unnamed local variable)] :
80388038
# 1498| r1498_25(glval<int *>) = FieldAddress[p] : r1498_24
8039-
# 1498| mu1498_26(int *) = Store[p] : &:r1498_23, r1498_25
8040-
# 1498| r1498_27(glval<int[2]>) = VariableAddress[xs] :
8039+
# 1498| mu1498_26(int *&) = Store[p] : &:r1498_23, r1498_25
8040+
# 1498| r1498_27(glval<int(&)[2]>) = VariableAddress[xs] :
80418041
# 1498| r1498_28(glval<StructuredBindingDataMemberStruct>) = VariableAddress[(unnamed local variable)] :
80428042
# 1498| r1498_29(glval<int[2]>) = FieldAddress[xs] : r1498_28
8043-
# 1498| mu1498_30(int[2]) = Store[xs] : &:r1498_27, r1498_29
8044-
# 1498| r1498_31(glval<int>) = VariableAddress[r_alt] :
8043+
# 1498| mu1498_30(int(&)[2]) = Store[xs] : &:r1498_27, r1498_29
8044+
# 1498| r1498_31(glval<int &>) = VariableAddress[r_alt] :
80458045
# 1498| r1498_32(glval<StructuredBindingDataMemberStruct>) = VariableAddress[(unnamed local variable)] :
80468046
# 1498| r1498_33(glval<int &>) = FieldAddress[r_alt] : r1498_32
80478047
# 1498| r1498_34(int &) = Load[?] : &:r1498_33, ~m?
80488048
# 1498| r1498_35(glval<int>) = CopyValue : r1498_34
8049-
# 1498| mu1498_36(int) = Store[r_alt] : &:r1498_31, r1498_35
8049+
# 1498| mu1498_36(int &) = Store[r_alt] : &:r1498_31, r1498_35
80508050
# 1499| r1499_1(double) = Constant[4.0] :
80518051
# 1499| r1499_2(glval<double>) = VariableAddress[d] :
80528052
# 1499| mu1499_3(double) = Store[d] : &:r1499_2, r1499_1

0 commit comments

Comments
 (0)