Skip to content

Commit e23b88b

Browse files
authored
Merge pull request github#6052 from jsinglet/jsinglet/stdtypes
Implementation of standard C/C++ fixed width, minimum width, and maximum width types
2 parents 85467ad + 8c6c011 commit e23b88b

13 files changed

+405
-10
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
lgtm,codescanning
2+
* Added definitions for types found in `cstdint`. Added types `FixedWidthIntegralType`, `MinimumWidthIntegralType`, `FastestMinimumWidthIntegralType`, and `MaximumWidthIntegralType` to describe types such as `int8_t`, `int_least8_t`, `int_fast8_t`, and `intmax_t` respectively.
3+
* Changed definition of `Intmax_t` and `Uintmax_t` to be part of the new type structure.
4+
* Added a type `FixedWidthEnumType` which describes enums based on a fixed-width integer type. For instance, `enum e: uint8_t = { a, b };`.

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

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

66+
/**
67+
* A parent class representing C/C++ a typedef'd `UserType` such as `int8_t`.
68+
*/
69+
abstract private class IntegralUnderlyingUserType extends UserType {
70+
IntegralUnderlyingUserType() { this.getUnderlyingType() instanceof IntegralType }
71+
}
72+
73+
abstract private class TFixedWidthIntegralType extends IntegralUnderlyingUserType { }
74+
75+
/**
76+
* A C/C++ fixed-width numeric type, such as `int8_t`.
77+
*/
78+
class FixedWidthIntegralType extends TFixedWidthIntegralType {
79+
FixedWidthIntegralType() { this instanceof TFixedWidthIntegralType }
80+
}
81+
82+
abstract private class TMinimumWidthIntegralType extends IntegralUnderlyingUserType { }
83+
84+
/**
85+
* A C/C++ minimum-width numeric type, such as `int_least8_t`.
86+
*/
87+
class MinimumWidthIntegralType extends TMinimumWidthIntegralType {
88+
MinimumWidthIntegralType() { this instanceof TMinimumWidthIntegralType }
89+
}
90+
91+
abstract private class TFastestMinimumWidthIntegralType extends IntegralUnderlyingUserType { }
92+
93+
/**
94+
* A C/C++ minimum-width numeric type, representing the fastest integer type with a
95+
* width of at least `N` such as `int_fast8_t`.
96+
*/
97+
class FastestMinimumWidthIntegralType extends TFastestMinimumWidthIntegralType {
98+
FastestMinimumWidthIntegralType() { this instanceof TFastestMinimumWidthIntegralType }
99+
}
100+
101+
abstract private class TMaximumWidthIntegralType extends IntegralUnderlyingUserType { }
102+
103+
/**
104+
* A C/C++ maximum-width numeric type, either `intmax_t` or `uintmax_t`.
105+
*/
106+
class MaximumWidthIntegralType extends TMaximumWidthIntegralType {
107+
MaximumWidthIntegralType() { this instanceof TMaximumWidthIntegralType }
108+
}
109+
110+
/**
111+
* An enum type based on a fixed-width integer type. For instance, `enum e: uint8_t = { a, b };`
112+
*/
113+
class FixedWidthEnumType extends UserType {
114+
FixedWidthEnumType() { this.(Enum).getExplicitUnderlyingType() instanceof FixedWidthIntegralType }
115+
}
116+
117+
/**
118+
* The C/C++ `int8_t` type.
119+
*/
120+
class Int8_t extends TFixedWidthIntegralType {
121+
Int8_t() { this.hasGlobalOrStdName("int8_t") }
122+
123+
override string getAPrimaryQlClass() { result = "Int8_t" }
124+
}
125+
126+
/**
127+
* The C/C++ `int16_t` type.
128+
*/
129+
class Int16_t extends TFixedWidthIntegralType {
130+
Int16_t() { this.hasGlobalOrStdName("int16_t") }
131+
132+
override string getAPrimaryQlClass() { result = "Int16_t" }
133+
}
134+
135+
/**
136+
* The C/C++ `int32_t` type.
137+
*/
138+
class Int32_t extends TFixedWidthIntegralType {
139+
Int32_t() { this.hasGlobalOrStdName("int32_t") }
140+
141+
override string getAPrimaryQlClass() { result = "Int32_t" }
142+
}
143+
144+
/**
145+
* The C/C++ `int64_t` type.
146+
*/
147+
class Int64_t extends TFixedWidthIntegralType {
148+
Int64_t() { this.hasGlobalOrStdName("int64_t") }
149+
150+
override string getAPrimaryQlClass() { result = "Int64_t" }
151+
}
152+
153+
/**
154+
* The C/C++ `uint8_t` type.
155+
*/
156+
class UInt8_t extends TFixedWidthIntegralType {
157+
UInt8_t() { this.hasGlobalOrStdName("uint8_t") }
158+
159+
override string getAPrimaryQlClass() { result = "UInt8_t" }
160+
}
161+
162+
/**
163+
* The C/C++ `uint16_t` type.
164+
*/
165+
class UInt16_t extends TFixedWidthIntegralType {
166+
UInt16_t() { this.hasGlobalOrStdName("uint16_t") }
167+
168+
override string getAPrimaryQlClass() { result = "UInt16_t" }
169+
}
170+
171+
/**
172+
* The C/C++ `uint32_t` type.
173+
*/
174+
class UInt32_t extends TFixedWidthIntegralType {
175+
UInt32_t() { this.hasGlobalOrStdName("uint32_t") }
176+
177+
override string getAPrimaryQlClass() { result = "UInt32_t" }
178+
}
179+
180+
/**
181+
* The C/C++ `uint64_t` type.
182+
*/
183+
class UInt64_t extends TFixedWidthIntegralType {
184+
UInt64_t() { this.hasGlobalOrStdName("uint64_t") }
185+
186+
override string getAPrimaryQlClass() { result = "UInt64_t" }
187+
}
188+
189+
/**
190+
* The C/C++ `int_least8_t` type.
191+
*/
192+
class Int_least8_t extends TMinimumWidthIntegralType {
193+
Int_least8_t() { this.hasGlobalOrStdName("int_least8_t") }
194+
195+
override string getAPrimaryQlClass() { result = "Int_least8_t" }
196+
}
197+
198+
/**
199+
* The C/C++ `int_least16_t` type.
200+
*/
201+
class Int_least16_t extends TMinimumWidthIntegralType {
202+
Int_least16_t() { this.hasGlobalOrStdName("int_least16_t") }
203+
204+
override string getAPrimaryQlClass() { result = "Int_least16_t" }
205+
}
206+
207+
/**
208+
* The C/C++ `int_least32_t` type.
209+
*/
210+
class Int_least32_t extends TMinimumWidthIntegralType {
211+
Int_least32_t() { this.hasGlobalOrStdName("int_least32_t") }
212+
213+
override string getAPrimaryQlClass() { result = "Int_least32_t" }
214+
}
215+
216+
/**
217+
* The C/C++ `int_least64_t` type.
218+
*/
219+
class Int_least64_t extends TMinimumWidthIntegralType {
220+
Int_least64_t() { this.hasGlobalOrStdName("int_least64_t") }
221+
222+
override string getAPrimaryQlClass() { result = "Int_least64_t" }
223+
}
224+
225+
/**
226+
* The C/C++ `uint_least8_t` type.
227+
*/
228+
class UInt_least8_t extends TMinimumWidthIntegralType {
229+
UInt_least8_t() { this.hasGlobalOrStdName("uint_least8_t") }
230+
231+
override string getAPrimaryQlClass() { result = "UInt_least8_t" }
232+
}
233+
234+
/**
235+
* The C/C++ `uint_least16_t` type.
236+
*/
237+
class UInt_least16_t extends TMinimumWidthIntegralType {
238+
UInt_least16_t() { this.hasGlobalOrStdName("uint_least16_t") }
239+
240+
override string getAPrimaryQlClass() { result = "UInt_least16_t" }
241+
}
242+
243+
/**
244+
* The C/C++ `uint_least32_t` type.
245+
*/
246+
class UInt_least32_t extends TMinimumWidthIntegralType {
247+
UInt_least32_t() { this.hasGlobalOrStdName("uint_least32_t") }
248+
249+
override string getAPrimaryQlClass() { result = "UInt_least32_t" }
250+
}
251+
252+
/**
253+
* The C/C++ `uint_least64_t` type.
254+
*/
255+
class UInt_least64_t extends TMinimumWidthIntegralType {
256+
UInt_least64_t() { this.hasGlobalOrStdName("uint_least64_t") }
257+
258+
override string getAPrimaryQlClass() { result = "UInt_least64_t" }
259+
}
260+
261+
/**
262+
* The C/C++ `int_fast8_t` type.
263+
*/
264+
class Int_fast8_t extends TFastestMinimumWidthIntegralType {
265+
Int_fast8_t() { this.hasGlobalOrStdName("int_fast8_t") }
266+
267+
override string getAPrimaryQlClass() { result = "Int_fast8_t" }
268+
}
269+
270+
/**
271+
* The C/C++ `int_fast16_t` type.
272+
*/
273+
class Int_fast16_t extends TFastestMinimumWidthIntegralType {
274+
Int_fast16_t() { this.hasGlobalOrStdName("int_fast16_t") }
275+
276+
override string getAPrimaryQlClass() { result = "Int_fast16_t" }
277+
}
278+
279+
/**
280+
* The C/C++ `int_fast32_t` type.
281+
*/
282+
class Int_fast32_t extends TFastestMinimumWidthIntegralType {
283+
Int_fast32_t() { this.hasGlobalOrStdName("int_fast32_t") }
284+
285+
override string getAPrimaryQlClass() { result = "Int_fast32_t" }
286+
}
287+
288+
/**
289+
* The C/C++ `int_fast64_t` type.
290+
*/
291+
class Int_fast64_t extends TFastestMinimumWidthIntegralType {
292+
Int_fast64_t() { this.hasGlobalOrStdName("int_fast64_t") }
293+
294+
override string getAPrimaryQlClass() { result = "Int_fast64_t" }
295+
}
296+
297+
/**
298+
* The C/C++ `uint_fast8_t` type.
299+
*/
300+
class UInt_fast8_t extends TFastestMinimumWidthIntegralType {
301+
UInt_fast8_t() { this.hasGlobalOrStdName("uint_fast8_t") }
302+
303+
override string getAPrimaryQlClass() { result = "UInt_fast8_t" }
304+
}
305+
306+
/**
307+
* The C/C++ `uint_fast16_t` type.
308+
*/
309+
class UInt_fast16_t extends TFastestMinimumWidthIntegralType {
310+
UInt_fast16_t() { this.hasGlobalOrStdName("uint_fast16_t") }
311+
312+
override string getAPrimaryQlClass() { result = "UInt_fast16_t" }
313+
}
314+
315+
/**
316+
* The C/C++ `uint_fast32_t` type.
317+
*/
318+
class UInt_fast32_t extends TFastestMinimumWidthIntegralType {
319+
UInt_fast32_t() { this.hasGlobalOrStdName("uint_fast32_t") }
320+
321+
override string getAPrimaryQlClass() { result = "UInt_fast32_t" }
322+
}
323+
324+
/**
325+
* The C/C++ `uint_fast64_t` type.
326+
*/
327+
class UInt_fast64_t extends TFastestMinimumWidthIntegralType {
328+
UInt_fast64_t() { this.hasGlobalOrStdName("uint_fast64_t") }
329+
330+
override string getAPrimaryQlClass() { result = "UInt_fast64_t" }
331+
}
332+
66333
/**
67334
* The C/C++ `intmax_t` type.
68335
*/
69-
class Intmax_t extends Type {
70-
Intmax_t() {
71-
this.getUnderlyingType() instanceof IntegralType and
72-
this.hasName("intmax_t")
73-
}
336+
class Intmax_t extends TMaximumWidthIntegralType {
337+
Intmax_t() { this.hasGlobalOrStdName("intmax_t") }
74338

75339
override string getAPrimaryQlClass() { result = "Intmax_t" }
76340
}
77341

78342
/**
79343
* The C/C++ `uintmax_t` type.
80344
*/
81-
class Uintmax_t extends Type {
82-
Uintmax_t() {
83-
this.getUnderlyingType() instanceof IntegralType and
84-
this.hasName("uintmax_t")
85-
}
345+
class Uintmax_t extends TMaximumWidthIntegralType {
346+
Uintmax_t() { this.hasGlobalOrStdName("uintmax_t") }
86347

87348
override string getAPrimaryQlClass() { result = "Uintmax_t" }
88349
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
typedef signed char int8_t;
2+
typedef short int16_t;
3+
typedef int int32_t;
4+
typedef long long int64_t;
5+
typedef unsigned char uint8_t;
6+
typedef unsigned short uint16_t;
7+
typedef unsigned int uint32_t;
8+
typedef unsigned long long uint64_t;
9+
10+
typedef signed char int_least8_t;
11+
typedef short int_least16_t;
12+
typedef int int_least32_t;
13+
typedef long long int_least64_t;
14+
typedef unsigned char uint_least8_t;
15+
typedef unsigned short uint_least16_t;
16+
typedef unsigned int uint_least32_t;
17+
typedef unsigned long long uint_least64_t;
18+
19+
typedef signed char int_fast8_t;
20+
typedef int int_fast16_t;
21+
typedef int int_fast32_t;
22+
typedef long long int_fast64_t;
23+
typedef unsigned char uint_fast8_t;
24+
typedef unsigned int uint_fast16_t;
25+
typedef unsigned int uint_fast32_t;
26+
typedef unsigned long long uint_fast64_t;
27+
28+
typedef long long intmax_t;
29+
typedef unsigned long long uintmax_t;
30+
31+
int8_t i8;
32+
int16_t i16;
33+
int32_t i32;
34+
int64_t i64;
35+
uint8_t ui8;
36+
uint16_t ui16;
37+
uint32_t ui32;
38+
uint64_t ui64;
39+
int_least8_t l8;
40+
int_least16_t l16;
41+
int_least32_t l32;
42+
int_least64_t l64;
43+
uint_least8_t ul8;
44+
uint_least16_t ul16;
45+
uint_least32_t ul32;
46+
uint_least64_t ul64;
47+
int_fast8_t if8;
48+
int_fast16_t if16;
49+
int_fast32_t if32;
50+
int_fast64_t if64;
51+
uint_fast8_t uf8;
52+
uint_fast16_t uf16;
53+
uint_fast32_t uf32;
54+
uint_fast64_t uf64;
55+
intmax_t im;
56+
uintmax_t uim;
57+
58+
enum E0 : int8_t {
59+
e0
60+
};
61+
62+
enum class E1 : int8_t {
63+
e1
64+
};
65+
66+
enum E2 {
67+
e2
68+
};
69+
70+
enum class E3 {
71+
e3
72+
};
73+
74+
E0 _e0;
75+
E1 _e1;
76+
E2 _e2;
77+
E3 _e3;
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(), ", ")

0 commit comments

Comments
 (0)