Skip to content

Commit 0d3f53b

Browse files
committed
Changes to structure per feedback of @jbj
1 parent f174d7a commit 0d3f53b

File tree

6 files changed

+109
-59
lines changed

6 files changed

+109
-59
lines changed

cpp/ql/src/semmle/code/cpp/commons/CommonType.qll

Lines changed: 78 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,70 @@ class Ptrdiff_t extends Type {
6363
override string getAPrimaryQlClass() { result = "Ptrdiff_t" }
6464
}
6565

66+
/**
67+
* A parent class representing C/C++ typedef'd `UserTypes` such as `int8_t`.
68+
*/
69+
private class IntegralUnderlyingUserType extends UserType {
70+
IntegralUnderlyingUserType() { this.getUnderlyingType() instanceof IntegralType }
71+
}
72+
6673
/**
6774
* A C/C++ fixed-width numeric type, such as `int8_t`.
6875
*/
69-
abstract class FixedWidthIntegralType extends UserType {
70-
FixedWidthIntegralType() { this.getUnderlyingType() instanceof IntegralType }
76+
class FixedWidthIntegralType extends UserType {
77+
FixedWidthIntegralType() {
78+
this instanceof Int8_t or
79+
this instanceof Int16_t or
80+
this instanceof Int32_t or
81+
this instanceof Int64_t or
82+
this instanceof UInt8_t or
83+
this instanceof UInt16_t or
84+
this instanceof UInt32_t or
85+
this instanceof UInt64_t
86+
}
87+
}
88+
89+
/**
90+
* A C/C++ minimum-width numeric type, such as `int_least8_t`.
91+
*/
92+
class MinimumWidthIntegralType extends UserType {
93+
MinimumWidthIntegralType() {
94+
this instanceof Int_least8_t or
95+
this instanceof Int_least16_t or
96+
this instanceof Int_least32_t or
97+
this instanceof Int_least64_t or
98+
this instanceof UInt_least8_t or
99+
this instanceof UInt_least16_t or
100+
this instanceof UInt_least32_t or
101+
this instanceof UInt_least64_t
102+
}
71103
}
72104

73105
/**
74-
* A C/C++ minimum-width numeric type, such as `int_least8_t` or `uint_fast16_t`.
106+
* A C/C++ minimum-width numeric type, representing the fastest integer type with a
107+
* width of at least `N` such as `int_fast8_t`.
75108
*/
76-
abstract class MinimumWidthIntegralType extends UserType {
77-
MinimumWidthIntegralType() { this.getUnderlyingType() instanceof IntegralType }
109+
class FastestMinimumWidthIntegralType extends UserType {
110+
FastestMinimumWidthIntegralType() {
111+
this instanceof Int_fast8_t or
112+
this instanceof Int_fast16_t or
113+
this instanceof Int_fast32_t or
114+
this instanceof Int_fast64_t or
115+
this instanceof UInt_fast8_t or
116+
this instanceof UInt_fast16_t or
117+
this instanceof UInt_fast32_t or
118+
this instanceof UInt_fast64_t
119+
}
78120
}
79121

80122
/**
81123
* A C/C++ maximum-width numeric type, either `intmax_t` or `uintmax_t`.
82124
*/
83-
abstract class MaximumWidthIntegralType extends UserType {
84-
MaximumWidthIntegralType() { this.getUnderlyingType() instanceof IntegralType }
125+
class MaximumWidthIntegralType extends UserType {
126+
MaximumWidthIntegralType() {
127+
this instanceof Intmax_t or
128+
this instanceof Uintmax_t
129+
}
85130
}
86131

87132
/**
@@ -94,7 +139,7 @@ class FixedWidthEnumType extends UserType {
94139
/**
95140
* The C/C++ `int8_t` type.
96141
*/
97-
class Int8_t extends FixedWidthIntegralType {
142+
class Int8_t extends IntegralUnderlyingUserType {
98143
Int8_t() { this.hasGlobalOrStdName("int8_t") }
99144

100145
override string getAPrimaryQlClass() { result = "Int8_t" }
@@ -103,7 +148,7 @@ class Int8_t extends FixedWidthIntegralType {
103148
/**
104149
* The C/C++ `int16_t` type.
105150
*/
106-
class Int16_t extends FixedWidthIntegralType {
151+
class Int16_t extends IntegralUnderlyingUserType {
107152
Int16_t() { this.hasGlobalOrStdName("int16_t") }
108153

109154
override string getAPrimaryQlClass() { result = "Int16_t" }
@@ -112,7 +157,7 @@ class Int16_t extends FixedWidthIntegralType {
112157
/**
113158
* The C/C++ `int32_t` type.
114159
*/
115-
class Int32_t extends FixedWidthIntegralType {
160+
class Int32_t extends IntegralUnderlyingUserType {
116161
Int32_t() { this.hasGlobalOrStdName("int32_t") }
117162

118163
override string getAPrimaryQlClass() { result = "Int32_t" }
@@ -121,7 +166,7 @@ class Int32_t extends FixedWidthIntegralType {
121166
/**
122167
* The C/C++ `int64_t` type.
123168
*/
124-
class Int64_t extends FixedWidthIntegralType {
169+
class Int64_t extends IntegralUnderlyingUserType {
125170
Int64_t() { this.hasGlobalOrStdName("int64_t") }
126171

127172
override string getAPrimaryQlClass() { result = "Int64_t" }
@@ -130,7 +175,7 @@ class Int64_t extends FixedWidthIntegralType {
130175
/**
131176
* The C/C++ `uint8_t` type.
132177
*/
133-
class UInt8_t extends FixedWidthIntegralType {
178+
class UInt8_t extends IntegralUnderlyingUserType {
134179
UInt8_t() { this.hasGlobalOrStdName("uint8_t") }
135180

136181
override string getAPrimaryQlClass() { result = "UInt8_t" }
@@ -139,7 +184,7 @@ class UInt8_t extends FixedWidthIntegralType {
139184
/**
140185
* The C/C++ `uint16_t` type.
141186
*/
142-
class UInt16_t extends FixedWidthIntegralType {
187+
class UInt16_t extends IntegralUnderlyingUserType {
143188
UInt16_t() { this.hasGlobalOrStdName("uint16_t") }
144189

145190
override string getAPrimaryQlClass() { result = "UInt16_t" }
@@ -148,7 +193,7 @@ class UInt16_t extends FixedWidthIntegralType {
148193
/**
149194
* The C/C++ `uint32_t` type.
150195
*/
151-
class UInt32_t extends FixedWidthIntegralType {
196+
class UInt32_t extends IntegralUnderlyingUserType {
152197
UInt32_t() { this.hasGlobalOrStdName("uint32_t") }
153198

154199
override string getAPrimaryQlClass() { result = "UInt32_t" }
@@ -157,7 +202,7 @@ class UInt32_t extends FixedWidthIntegralType {
157202
/**
158203
* The C/C++ `uint64_t` type.
159204
*/
160-
class UInt64_t extends FixedWidthIntegralType {
205+
class UInt64_t extends IntegralUnderlyingUserType {
161206
UInt64_t() { this.hasGlobalOrStdName("uint64_t") }
162207

163208
override string getAPrimaryQlClass() { result = "UInt64_t" }
@@ -166,7 +211,7 @@ class UInt64_t extends FixedWidthIntegralType {
166211
/**
167212
* The C/C++ `int_least8_t` type.
168213
*/
169-
class Int_least8_t extends MinimumWidthIntegralType {
214+
class Int_least8_t extends IntegralUnderlyingUserType {
170215
Int_least8_t() { this.hasGlobalOrStdName("int_least8_t") }
171216

172217
override string getAPrimaryQlClass() { result = "Int_least8_t" }
@@ -175,7 +220,7 @@ class Int_least8_t extends MinimumWidthIntegralType {
175220
/**
176221
* The C/C++ `int_least16_t` type.
177222
*/
178-
class Int_least16_t extends MinimumWidthIntegralType {
223+
class Int_least16_t extends IntegralUnderlyingUserType {
179224
Int_least16_t() { this.hasGlobalOrStdName("int_least16_t") }
180225

181226
override string getAPrimaryQlClass() { result = "Int_least16_t" }
@@ -184,7 +229,7 @@ class Int_least16_t extends MinimumWidthIntegralType {
184229
/**
185230
* The C/C++ `int_least32_t` type.
186231
*/
187-
class Int_least32_t extends MinimumWidthIntegralType {
232+
class Int_least32_t extends IntegralUnderlyingUserType {
188233
Int_least32_t() { this.hasGlobalOrStdName("int_least32_t") }
189234

190235
override string getAPrimaryQlClass() { result = "Int_least32_t" }
@@ -193,7 +238,7 @@ class Int_least32_t extends MinimumWidthIntegralType {
193238
/**
194239
* The C/C++ `int_least64_t` type.
195240
*/
196-
class Int_least64_t extends MinimumWidthIntegralType {
241+
class Int_least64_t extends IntegralUnderlyingUserType {
197242
Int_least64_t() { this.hasGlobalOrStdName("int_least64_t") }
198243

199244
override string getAPrimaryQlClass() { result = "Int_least64_t" }
@@ -202,7 +247,7 @@ class Int_least64_t extends MinimumWidthIntegralType {
202247
/**
203248
* The C/C++ `uint_least8_t` type.
204249
*/
205-
class UInt_least8_t extends MinimumWidthIntegralType {
250+
class UInt_least8_t extends IntegralUnderlyingUserType {
206251
UInt_least8_t() { this.hasGlobalOrStdName("uint_least8_t") }
207252

208253
override string getAPrimaryQlClass() { result = "UInt_least8_t" }
@@ -211,7 +256,7 @@ class UInt_least8_t extends MinimumWidthIntegralType {
211256
/**
212257
* The C/C++ `uint_least16_t` type.
213258
*/
214-
class UInt_least16_t extends MinimumWidthIntegralType {
259+
class UInt_least16_t extends IntegralUnderlyingUserType {
215260
UInt_least16_t() { this.hasGlobalOrStdName("uint_least16_t") }
216261

217262
override string getAPrimaryQlClass() { result = "UInt_least16_t" }
@@ -220,7 +265,7 @@ class UInt_least16_t extends MinimumWidthIntegralType {
220265
/**
221266
* The C/C++ `uint_least32_t` type.
222267
*/
223-
class UInt_least32_t extends MinimumWidthIntegralType {
268+
class UInt_least32_t extends IntegralUnderlyingUserType {
224269
UInt_least32_t() { this.hasGlobalOrStdName("uint_least32_t") }
225270

226271
override string getAPrimaryQlClass() { result = "UInt_least32_t" }
@@ -229,7 +274,7 @@ class UInt_least32_t extends MinimumWidthIntegralType {
229274
/**
230275
* The C/C++ `uint_least64_t` type.
231276
*/
232-
class UInt_least64_t extends MinimumWidthIntegralType {
277+
class UInt_least64_t extends IntegralUnderlyingUserType {
233278
UInt_least64_t() { this.hasGlobalOrStdName("uint_least64_t") }
234279

235280
override string getAPrimaryQlClass() { result = "UInt_least64_t" }
@@ -238,7 +283,7 @@ class UInt_least64_t extends MinimumWidthIntegralType {
238283
/**
239284
* The C/C++ `int_fast8_t` type.
240285
*/
241-
class Int_fast8_t extends MinimumWidthIntegralType {
286+
class Int_fast8_t extends IntegralUnderlyingUserType {
242287
Int_fast8_t() { this.hasGlobalOrStdName("int_fast8_t") }
243288

244289
override string getAPrimaryQlClass() { result = "Int_fast8_t" }
@@ -247,7 +292,7 @@ class Int_fast8_t extends MinimumWidthIntegralType {
247292
/**
248293
* The C/C++ `int_fast16_t` type.
249294
*/
250-
class Int_fast16_t extends MinimumWidthIntegralType {
295+
class Int_fast16_t extends IntegralUnderlyingUserType {
251296
Int_fast16_t() { this.hasGlobalOrStdName("int_fast16_t") }
252297

253298
override string getAPrimaryQlClass() { result = "Int_fast16_t" }
@@ -256,7 +301,7 @@ class Int_fast16_t extends MinimumWidthIntegralType {
256301
/**
257302
* The C/C++ `int_fast32_t` type.
258303
*/
259-
class Int_fast32_t extends MinimumWidthIntegralType {
304+
class Int_fast32_t extends IntegralUnderlyingUserType {
260305
Int_fast32_t() { this.hasGlobalOrStdName("int_fast32_t") }
261306

262307
override string getAPrimaryQlClass() { result = "Int_fast32_t" }
@@ -265,7 +310,7 @@ class Int_fast32_t extends MinimumWidthIntegralType {
265310
/**
266311
* The C/C++ `int_fast64_t` type.
267312
*/
268-
class Int_fast64_t extends MinimumWidthIntegralType {
313+
class Int_fast64_t extends IntegralUnderlyingUserType {
269314
Int_fast64_t() { this.hasGlobalOrStdName("int_fast64_t") }
270315

271316
override string getAPrimaryQlClass() { result = "Int_fast64_t" }
@@ -274,7 +319,7 @@ class Int_fast64_t extends MinimumWidthIntegralType {
274319
/**
275320
* The C/C++ `uint_fast8_t` type.
276321
*/
277-
class UInt_fast8_t extends MinimumWidthIntegralType {
322+
class UInt_fast8_t extends IntegralUnderlyingUserType {
278323
UInt_fast8_t() { this.hasGlobalOrStdName("uint_fast8_t") }
279324

280325
override string getAPrimaryQlClass() { result = "UInt_fast8_t" }
@@ -283,7 +328,7 @@ class UInt_fast8_t extends MinimumWidthIntegralType {
283328
/**
284329
* The C/C++ `uint_fast16_t` type.
285330
*/
286-
class UInt_fast16_t extends MinimumWidthIntegralType {
331+
class UInt_fast16_t extends IntegralUnderlyingUserType {
287332
UInt_fast16_t() { this.hasGlobalOrStdName("uint_fast16_t") }
288333

289334
override string getAPrimaryQlClass() { result = "UInt_fast16_t" }
@@ -292,7 +337,7 @@ class UInt_fast16_t extends MinimumWidthIntegralType {
292337
/**
293338
* The C/C++ `uint_fast32_t` type.
294339
*/
295-
class UInt_fast32_t extends MinimumWidthIntegralType {
340+
class UInt_fast32_t extends IntegralUnderlyingUserType {
296341
UInt_fast32_t() { this.hasGlobalOrStdName("uint_fast32_t") }
297342

298343
override string getAPrimaryQlClass() { result = "UInt_fast32_t" }
@@ -301,7 +346,7 @@ class UInt_fast32_t extends MinimumWidthIntegralType {
301346
/**
302347
* The C/C++ `uint_fast64_t` type.
303348
*/
304-
class UInt_fast64_t extends MinimumWidthIntegralType {
349+
class UInt_fast64_t extends IntegralUnderlyingUserType {
305350
UInt_fast64_t() { this.hasGlobalOrStdName("uint_fast64_t") }
306351

307352
override string getAPrimaryQlClass() { result = "UInt_fast64_t" }
@@ -310,7 +355,7 @@ class UInt_fast64_t extends MinimumWidthIntegralType {
310355
/**
311356
* The C/C++ `intmax_t` type.
312357
*/
313-
class Intmax_t extends MaximumWidthIntegralType {
358+
class Intmax_t extends IntegralUnderlyingUserType {
314359
Intmax_t() { this.hasGlobalOrStdName("intmax_t") }
315360

316361
override string getAPrimaryQlClass() { result = "Intmax_t" }
@@ -319,7 +364,7 @@ class Intmax_t extends MaximumWidthIntegralType {
319364
/**
320365
* The C/C++ `uintmax_t` type.
321366
*/
322-
class Uintmax_t extends MaximumWidthIntegralType {
367+
class Uintmax_t extends IntegralUnderlyingUserType {
323368
Uintmax_t() { this.hasGlobalOrStdName("uintmax_t") }
324369

325370
override string getAPrimaryQlClass() { result = "Uintmax_t" }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
| cstd_types.cpp:47:13:47:15 | if8 | CTypedefType, FastestMinimumWidthIntegralType, Int_fast8_t |
2+
| cstd_types.cpp:48:14:48:17 | if16 | CTypedefType, FastestMinimumWidthIntegralType, Int_fast16_t |
3+
| cstd_types.cpp:49:14:49:17 | if32 | CTypedefType, FastestMinimumWidthIntegralType, Int_fast32_t |
4+
| cstd_types.cpp:50:14:50:17 | if64 | CTypedefType, FastestMinimumWidthIntegralType, Int_fast64_t |
5+
| cstd_types.cpp:51:14:51:16 | uf8 | CTypedefType, FastestMinimumWidthIntegralType, UInt_fast8_t |
6+
| cstd_types.cpp:52:15:52:18 | uf16 | CTypedefType, FastestMinimumWidthIntegralType, UInt_fast16_t |
7+
| cstd_types.cpp:53:15:53:18 | uf32 | CTypedefType, FastestMinimumWidthIntegralType, UInt_fast32_t |
8+
| cstd_types.cpp:54:15:54:18 | uf64 | CTypedefType, FastestMinimumWidthIntegralType, UInt_fast64_t |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import cpp
2+
3+
from Variable v, FastestMinimumWidthIntegralType t
4+
where v.getType() = t
5+
select v, concat(t.getAQlClass(), ", ")
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
| cstd_types.cpp:31:8:31:9 | i8 | CTypedefType, Int8_t |
2-
| cstd_types.cpp:32:9:32:11 | i16 | CTypedefType, Int16_t |
3-
| cstd_types.cpp:33:9:33:11 | i32 | CTypedefType, Int32_t |
4-
| cstd_types.cpp:34:9:34:11 | i64 | CTypedefType, Int64_t |
5-
| cstd_types.cpp:35:9:35:11 | ui8 | CTypedefType, UInt8_t |
6-
| cstd_types.cpp:36:10:36:13 | ui16 | CTypedefType, UInt16_t |
7-
| cstd_types.cpp:37:10:37:13 | ui32 | CTypedefType, UInt32_t |
8-
| cstd_types.cpp:38:10:38:13 | ui64 | CTypedefType, UInt64_t |
1+
| cstd_types.cpp:31:8:31:9 | i8 | CTypedefType, FixedWidthIntegralType, Int8_t |
2+
| cstd_types.cpp:32:9:32:11 | i16 | CTypedefType, FixedWidthIntegralType, Int16_t |
3+
| cstd_types.cpp:33:9:33:11 | i32 | CTypedefType, FixedWidthIntegralType, Int32_t |
4+
| cstd_types.cpp:34:9:34:11 | i64 | CTypedefType, FixedWidthIntegralType, Int64_t |
5+
| cstd_types.cpp:35:9:35:11 | ui8 | CTypedefType, FixedWidthIntegralType, UInt8_t |
6+
| cstd_types.cpp:36:10:36:13 | ui16 | CTypedefType, FixedWidthIntegralType, UInt16_t |
7+
| cstd_types.cpp:37:10:37:13 | ui32 | CTypedefType, FixedWidthIntegralType, UInt32_t |
8+
| cstd_types.cpp:38:10:38:13 | ui64 | CTypedefType, FixedWidthIntegralType, UInt64_t |
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| cstd_types.cpp:55:10:55:11 | im | CTypedefType, Intmax_t |
2-
| cstd_types.cpp:56:11:56:13 | uim | CTypedefType, Uintmax_t |
1+
| cstd_types.cpp:55:10:55:11 | im | CTypedefType, Intmax_t, MaximumWidthIntegralType |
2+
| cstd_types.cpp:56:11:56:13 | uim | CTypedefType, MaximumWidthIntegralType, Uintmax_t |
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
1-
| cstd_types.cpp:39:15:39:16 | l8 | CTypedefType, Int_least8_t |
2-
| cstd_types.cpp:40:15:40:17 | l16 | CTypedefType, Int_least16_t |
3-
| cstd_types.cpp:41:15:41:17 | l32 | CTypedefType, Int_least32_t |
4-
| cstd_types.cpp:42:15:42:17 | l64 | CTypedefType, Int_least64_t |
5-
| cstd_types.cpp:43:15:43:17 | ul8 | CTypedefType, UInt_least8_t |
6-
| cstd_types.cpp:44:16:44:19 | ul16 | CTypedefType, UInt_least16_t |
7-
| cstd_types.cpp:45:16:45:19 | ul32 | CTypedefType, UInt_least32_t |
8-
| cstd_types.cpp:46:16:46:19 | ul64 | CTypedefType, UInt_least64_t |
9-
| cstd_types.cpp:47:13:47:15 | if8 | CTypedefType, Int_fast8_t |
10-
| cstd_types.cpp:48:14:48:17 | if16 | CTypedefType, Int_fast16_t |
11-
| cstd_types.cpp:49:14:49:17 | if32 | CTypedefType, Int_fast32_t |
12-
| cstd_types.cpp:50:14:50:17 | if64 | CTypedefType, Int_fast64_t |
13-
| cstd_types.cpp:51:14:51:16 | uf8 | CTypedefType, UInt_fast8_t |
14-
| cstd_types.cpp:52:15:52:18 | uf16 | CTypedefType, UInt_fast16_t |
15-
| cstd_types.cpp:53:15:53:18 | uf32 | CTypedefType, UInt_fast32_t |
16-
| cstd_types.cpp:54:15:54:18 | uf64 | CTypedefType, UInt_fast64_t |
1+
| cstd_types.cpp:39:15:39:16 | l8 | CTypedefType, Int_least8_t, MinimumWidthIntegralType |
2+
| cstd_types.cpp:40:15:40:17 | l16 | CTypedefType, Int_least16_t, MinimumWidthIntegralType |
3+
| cstd_types.cpp:41:15:41:17 | l32 | CTypedefType, Int_least32_t, MinimumWidthIntegralType |
4+
| cstd_types.cpp:42:15:42:17 | l64 | CTypedefType, Int_least64_t, MinimumWidthIntegralType |
5+
| cstd_types.cpp:43:15:43:17 | ul8 | CTypedefType, MinimumWidthIntegralType, UInt_least8_t |
6+
| cstd_types.cpp:44:16:44:19 | ul16 | CTypedefType, MinimumWidthIntegralType, UInt_least16_t |
7+
| cstd_types.cpp:45:16:45:19 | ul32 | CTypedefType, MinimumWidthIntegralType, UInt_least32_t |
8+
| cstd_types.cpp:46:16:46:19 | ul64 | CTypedefType, MinimumWidthIntegralType, UInt_least64_t |

0 commit comments

Comments
 (0)