Skip to content

Commit 192c309

Browse files
authored
annotation for const and enum (#251)
Add type annotations for C++ enumerations and constants literals. Signed-off-by: mohapatr3 <[email protected]>
1 parent 8c83903 commit 192c309

File tree

11 files changed

+57
-36
lines changed

11 files changed

+57
-36
lines changed

flatdata-cpp/include/flatdata/DebugDataAccessStatistics.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <shared_mutex>
2121
#include <utility>
2222
#include <vector>
23+
#include <algorithm>
2324

2425
namespace flatdata
2526
{

flatdata-generator/flatdata/generator/templates/cpp/constant.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ enum : {{ node.type|cpp_base_type}}
44
{% if node.doc %}
55
{{- node.doc|cpp_doc }}
66
{% endif %}
7-
{{ node.name }} = {{ node.value }}
7+
{{ node.name }} = {{ node.value }}{{ node.type.annotation }}
88
};
99
{%- endmacro %}

flatdata-generator/flatdata/generator/templates/cpp/enumeration.jinja2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ enum class {{ enum.name }} : {{ enum.type|cpp_base_type }}
99
{% if value.doc %}
1010
{{ value.doc|cpp_doc }}
1111
{% endif %}
12-
{{ value.name }} = {{ value.value }}{{ "," if not loop.last }}
12+
{{ value.name }} = {{ value.value }}{{ enum.type.annotation }}{{ "," if not loop.last }}
1313
{% endfor %}
1414
};
1515

flatdata-generator/flatdata/generator/tree/helpers/basictype.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ class BasicType:
1616
"i64": 64
1717
}
1818

19+
_TYPE_ANNOTATION = {
20+
"bool": "",
21+
"u8": "",
22+
"i8": "",
23+
"u16": "",
24+
"i16": "",
25+
"u32": "UL",
26+
"i32": "L",
27+
"u64": "ULL",
28+
"i64": "LL"
29+
}
30+
1931
@staticmethod
2032
def is_basic_type(name):
2133
return name in grammar.BASIC_TYPES
@@ -41,6 +53,10 @@ def width(self):
4153
def is_signed(self):
4254
return self._name[0] == 'i'
4355

56+
@property
57+
def annotation(self):
58+
return self._TYPE_ANNOTATION[self._name]
59+
4460
def bits_required(self, value):
4561
if self.is_signed:
4662
if value >= 0:

flatdata-generator/flatdata/generator/tree/helpers/enumtype.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ def name(self):
1515
def width(self):
1616
return self._type.width
1717

18+
@property
19+
def annotation(self):
20+
return self._type.annotation
21+
1822
@property
1923
def is_signed(self):
2024
return self._type.is_signed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
enum : uint32_t
22
{
33
// This is a comment about foo
4-
FOO = 0
4+
FOO = 0UL
55
};

flatdata-generator/tests/generators/cpp_expectations/constants/comments.h.2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ enum : uint64_t
33
/*
44
* This is a comment about bar
55
*/
6-
BAR = 0
6+
BAR = 0ULL
77
};

flatdata-generator/tests/generators/cpp_expectations/constants/values.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,139 +141,139 @@ enum : uint16_t
141141
namespace n {
142142
enum : int32_t
143143
{
144-
FOO_I32_NEG = -2147483648
144+
FOO_I32_NEG = -2147483648L
145145
};
146146
} // namespace n
147147

148148
namespace n {
149149
enum : int32_t
150150
{
151-
FOO_I32_POS = 2147483647
151+
FOO_I32_POS = 2147483647L
152152
};
153153
} // namespace n
154154

155155
namespace n {
156156
enum : int32_t
157157
{
158-
FOO_I32_ZERO = 0
158+
FOO_I32_ZERO = 0L
159159
};
160160
} // namespace n
161161

162162
namespace n {
163163
enum : int32_t
164164
{
165-
FOO_I32_NEG_HEX = -2147483648
165+
FOO_I32_NEG_HEX = -2147483648L
166166
};
167167
} // namespace n
168168

169169
namespace n {
170170
enum : int32_t
171171
{
172-
FOO_I32_POS_HEX = 2147483647
172+
FOO_I32_POS_HEX = 2147483647L
173173
};
174174
} // namespace n
175175

176176
namespace n {
177177
enum : int32_t
178178
{
179-
FOO_I32_ZERO_HEX = 0
179+
FOO_I32_ZERO_HEX = 0L
180180
};
181181
} // namespace n
182182

183183
namespace n {
184184
enum : uint32_t
185185
{
186-
FOO_U32_POS = 4294967295
186+
FOO_U32_POS = 4294967295UL
187187
};
188188
} // namespace n
189189

190190
namespace n {
191191
enum : uint32_t
192192
{
193-
FOO_U32_ZERO = 0
193+
FOO_U32_ZERO = 0UL
194194
};
195195
} // namespace n
196196

197197
namespace n {
198198
enum : uint32_t
199199
{
200-
FOO_U32_POS_HEX = 4294967295
200+
FOO_U32_POS_HEX = 4294967295UL
201201
};
202202
} // namespace n
203203

204204
namespace n {
205205
enum : uint32_t
206206
{
207-
FOO_U32_ZERO_HEX = 0
207+
FOO_U32_ZERO_HEX = 0UL
208208
};
209209
} // namespace n
210210

211211
namespace n {
212212
enum : int64_t
213213
{
214-
FOO_I64_NEG = -9223372036854775808
214+
FOO_I64_NEG = -9223372036854775808LL
215215
};
216216
} // namespace n
217217

218218
namespace n {
219219
enum : int64_t
220220
{
221-
FOO_I64_POS = 9223372036854775807
221+
FOO_I64_POS = 9223372036854775807LL
222222
};
223223
} // namespace n
224224

225225
namespace n {
226226
enum : int64_t
227227
{
228-
FOO_I64_ZERO = 0
228+
FOO_I64_ZERO = 0LL
229229
};
230230
} // namespace n
231231

232232
namespace n {
233233
enum : int64_t
234234
{
235-
FOO_I64_NEG_HEX = -9223372036854775808
235+
FOO_I64_NEG_HEX = -9223372036854775808LL
236236
};
237237
} // namespace n
238238

239239
namespace n {
240240
enum : int64_t
241241
{
242-
FOO_I64_POS_HEX = 9223372036854775807
242+
FOO_I64_POS_HEX = 9223372036854775807LL
243243
};
244244
} // namespace n
245245

246246
namespace n {
247247
enum : int64_t
248248
{
249-
FOO_I64_ZERO_HEX = 0
249+
FOO_I64_ZERO_HEX = 0LL
250250
};
251251
} // namespace n
252252

253253
namespace n {
254254
enum : uint64_t
255255
{
256-
FOO_U64_POS = 18446744073709551615
256+
FOO_U64_POS = 18446744073709551615ULL
257257
};
258258
} // namespace n
259259

260260
namespace n {
261261
enum : uint64_t
262262
{
263-
FOO_U64_ZERO = 0
263+
FOO_U64_ZERO = 0ULL
264264
};
265265
} // namespace n
266266

267267
namespace n {
268268
enum : uint64_t
269269
{
270-
FOO_U64_POS_HEX = 18446744073709551615
270+
FOO_U64_POS_HEX = 18446744073709551615ULL
271271
};
272272
} // namespace n
273273

274274
namespace n {
275275
enum : uint64_t
276276
{
277-
FOO_U64_ZERO_HEX = 0
277+
FOO_U64_ZERO_HEX = 0ULL
278278
};
279279
} // namespace n

flatdata-generator/tests/generators/cpp_expectations/enums/comments.h.1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
enum class Foo : uint64_t
33
{
44
// This is a comment about Foo.a
5-
A = 0,
5+
A = 0ULL,
66
// This is a comment about Foo.b
7-
B = 1
7+
B = 1ULL
88
};

flatdata-generator/tests/generators/cpp_expectations/enums/comments.h.2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ enum class Bar : uint64_t
66
/*
77
* This is a comment about Bar.a
88
*/
9-
A = 0,
9+
A = 0ULL,
1010
/*
1111
* This is a comment about Bar.b
1212
*/
13-
B = 1
13+
B = 1ULL
1414
};

flatdata-generator/tests/generators/cpp_expectations/enums/structs.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ namespace n {
282282

283283
enum class EnumI32 : int32_t
284284
{
285-
VALUE = 0,
286-
UNKNOWN_VALUE_MINUS_1 = -1
285+
VALUE = 0L,
286+
UNKNOWN_VALUE_MINUS_1 = -1L
287287
};
288288

289289
inline
@@ -350,8 +350,8 @@ namespace n {
350350

351351
enum class EnumU32 : uint32_t
352352
{
353-
VALUE = 0,
354-
UNKNOWN_VALUE_1 = 1
353+
VALUE = 0UL,
354+
UNKNOWN_VALUE_1 = 1UL
355355
};
356356

357357
inline
@@ -418,8 +418,8 @@ namespace n {
418418

419419
enum class EnumI64 : int64_t
420420
{
421-
VALUE = 0,
422-
UNKNOWN_VALUE_MINUS_1 = -1
421+
VALUE = 0LL,
422+
UNKNOWN_VALUE_MINUS_1 = -1LL
423423
};
424424

425425
inline
@@ -486,8 +486,8 @@ namespace n {
486486

487487
enum class EnumU64 : uint64_t
488488
{
489-
VALUE = 0,
490-
UNKNOWN_VALUE_1 = 1
489+
VALUE = 0ULL,
490+
UNKNOWN_VALUE_1 = 1ULL
491491
};
492492

493493
inline

0 commit comments

Comments
 (0)