Skip to content

Commit 28e2cdb

Browse files
committed
adding standard C/C++ fixed width, minimum width, and maximum width types
1 parent b14fa8b commit 28e2cdb

11 files changed

+378
-6
lines changed

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

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

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

75320
override string getAPrimaryQlClass() { result = "Intmax_t" }
@@ -78,10 +323,9 @@ class Intmax_t extends Type {
78323
/**
79324
* The C/C++ `uintmax_t` type.
80325
*/
81-
class Uintmax_t extends Type {
326+
class Uintmax_t extends MaximumWidthIntegralType {
82327
Uintmax_t() {
83-
this.getUnderlyingType() instanceof IntegralType and
84-
this.hasName("uintmax_t")
328+
this.hasGlobalOrStdName("uintmax_t")
85329
}
86330

87331
override string getAPrimaryQlClass() { result = "Uintmax_t" }
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: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 |
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, FixedWidthIntegralType t
4+
where v.getType() = t
5+
select v, concat(t.getAQlClass(), ", ")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| cstd_types.cpp:74:4:74:6 | _e0 | Enum, FixedWidthEnumType |
2+
| cstd_types.cpp:75:4:75:6 | _e1 | FixedWidthEnumType, ScopedEnum |
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, FixedWidthEnumType t
4+
where v.getType() = t
5+
select v, concat(t.getAQlClass(), ", ")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +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 |
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, MaximumWidthIntegralType t
4+
where v.getType() = t
5+
select v, concat(t.getAQlClass(), ", ")

0 commit comments

Comments
 (0)