Skip to content

Commit 4b6908c

Browse files
committed
C++: test template classes with MemberFunction::getTypeOfThis()
1 parent ab1dc64 commit 4b6908c

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

cpp/ql/test/library-tests/members/this/test.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,76 @@ class D {
5454
global++;
5555
}
5656
};
57+
58+
template<typename T>
59+
class InstantiatedTemplateClass {
60+
int x;
61+
62+
public:
63+
64+
void f1() {
65+
// Implicit dereference of `this.`
66+
x++;
67+
}
68+
69+
void f2() {
70+
// Explicit dereference of `this.`
71+
this->x++;
72+
}
73+
74+
int f3() const {
75+
// We expect the type of `this` to be const-qualified.
76+
return x;
77+
}
78+
79+
int f4() volatile {
80+
// We expect the type of `this` to be volatile-qualified.
81+
return x;
82+
}
83+
84+
int f5() const volatile {
85+
// We expect the type of `this` to be qualified as both const and volatile.
86+
return x;
87+
}
88+
89+
void f6() {
90+
// No use of `this`, but we still expect to be able to get its type.
91+
global++;
92+
}
93+
94+
float f7() const & {
95+
// We expect the type of `this` to be const-qualified.
96+
return x;
97+
}
98+
99+
float f8() && {
100+
// We expect the type of `this` to be unqualified.
101+
return x;
102+
}
103+
};
104+
105+
void instantiate() {
106+
InstantiatedTemplateClass<int> x;
107+
x.f1();
108+
x.f2();
109+
x.f3();
110+
x.f4();
111+
x.f5();
112+
x.f6();
113+
x.f7();
114+
115+
float val = InstantiatedTemplateClass<int>().f8();
116+
}
117+
118+
// Since there are no instantiations of this class, we don't expect
119+
// MemberFunction::getTypeOfThis() to hold.
120+
template<typename T>
121+
class UninstantiatedTemplateClass {
122+
int x;
123+
124+
public:
125+
126+
void f1() {
127+
x++;
128+
}
129+
};

cpp/ql/test/library-tests/members/this/this.expected

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ thisExprType
66
| test.cpp:31:12:31:12 | this | file://:0:0:0:0 | const volatile C * |
77
| test.cpp:41:12:41:12 | this | file://:0:0:0:0 | const C * |
88
| test.cpp:46:12:46:12 | this | file://:0:0:0:0 | C * |
9+
| test.cpp:66:5:66:5 | this | file://:0:0:0:0 | InstantiatedTemplateClass<T> * |
10+
| test.cpp:66:5:66:5 | this | file://:0:0:0:0 | InstantiatedTemplateClass<int> * |
11+
| test.cpp:71:5:71:8 | this | file://:0:0:0:0 | InstantiatedTemplateClass<T> * |
12+
| test.cpp:71:5:71:8 | this | file://:0:0:0:0 | InstantiatedTemplateClass<int> * |
13+
| test.cpp:76:12:76:12 | this | file://:0:0:0:0 | const InstantiatedTemplateClass<T> * |
14+
| test.cpp:76:12:76:12 | this | file://:0:0:0:0 | const InstantiatedTemplateClass<int> * |
15+
| test.cpp:81:12:81:12 | this | file://:0:0:0:0 | volatile InstantiatedTemplateClass<T> * |
16+
| test.cpp:81:12:81:12 | this | file://:0:0:0:0 | volatile InstantiatedTemplateClass<int> * |
17+
| test.cpp:86:12:86:12 | this | file://:0:0:0:0 | const volatile InstantiatedTemplateClass<T> * |
18+
| test.cpp:86:12:86:12 | this | file://:0:0:0:0 | const volatile InstantiatedTemplateClass<int> * |
19+
| test.cpp:96:12:96:12 | this | file://:0:0:0:0 | const InstantiatedTemplateClass<T> * |
20+
| test.cpp:96:12:96:12 | this | file://:0:0:0:0 | const InstantiatedTemplateClass<int> * |
21+
| test.cpp:101:12:101:12 | this | file://:0:0:0:0 | InstantiatedTemplateClass<T> * |
22+
| test.cpp:101:12:101:12 | this | file://:0:0:0:0 | InstantiatedTemplateClass<int> * |
923
#select
1024
| test.cpp:9:8:9:9 | f1 | file://:0:0:0:0 | C * |
1125
| test.cpp:14:8:14:9 | f2 | file://:0:0:0:0 | C * |
@@ -16,3 +30,19 @@ thisExprType
1630
| test.cpp:39:9:39:10 | f7 | file://:0:0:0:0 | const C * |
1731
| test.cpp:44:9:44:10 | f8 | file://:0:0:0:0 | C * |
1832
| test.cpp:53:8:53:8 | f | file://:0:0:0:0 | D * |
33+
| test.cpp:64:8:64:8 | f1 | file://:0:0:0:0 | InstantiatedTemplateClass<int> * |
34+
| test.cpp:64:8:64:9 | f1 | file://:0:0:0:0 | InstantiatedTemplateClass<T> * |
35+
| test.cpp:69:8:69:8 | f2 | file://:0:0:0:0 | InstantiatedTemplateClass<int> * |
36+
| test.cpp:69:8:69:9 | f2 | file://:0:0:0:0 | InstantiatedTemplateClass<T> * |
37+
| test.cpp:74:7:74:7 | f3 | file://:0:0:0:0 | const InstantiatedTemplateClass<int> * |
38+
| test.cpp:74:7:74:8 | f3 | file://:0:0:0:0 | const InstantiatedTemplateClass<T> * |
39+
| test.cpp:79:7:79:7 | f4 | file://:0:0:0:0 | volatile InstantiatedTemplateClass<int> * |
40+
| test.cpp:79:7:79:8 | f4 | file://:0:0:0:0 | volatile InstantiatedTemplateClass<T> * |
41+
| test.cpp:84:7:84:7 | f5 | file://:0:0:0:0 | const volatile InstantiatedTemplateClass<int> * |
42+
| test.cpp:84:7:84:8 | f5 | file://:0:0:0:0 | const volatile InstantiatedTemplateClass<T> * |
43+
| test.cpp:89:8:89:8 | f6 | file://:0:0:0:0 | InstantiatedTemplateClass<int> * |
44+
| test.cpp:89:8:89:9 | f6 | file://:0:0:0:0 | InstantiatedTemplateClass<T> * |
45+
| test.cpp:94:9:94:9 | f7 | file://:0:0:0:0 | const InstantiatedTemplateClass<int> * |
46+
| test.cpp:94:9:94:10 | f7 | file://:0:0:0:0 | const InstantiatedTemplateClass<T> * |
47+
| test.cpp:99:9:99:9 | f8 | file://:0:0:0:0 | InstantiatedTemplateClass<int> * |
48+
| test.cpp:99:9:99:10 | f8 | file://:0:0:0:0 | InstantiatedTemplateClass<T> * |

0 commit comments

Comments
 (0)