Skip to content

Commit c4780d1

Browse files
committed
Revert ":art: Autodetect scoped enums in generated .cpp catalog file"
This reverts commit a696f42.
1 parent a4bffd8 commit c4780d1

File tree

6 files changed

+37
-125
lines changed

6 files changed

+37
-125
lines changed

include/log/catalog/arguments.hpp

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,67 +10,48 @@ template <typename> struct encode_32;
1010
template <typename> struct encode_64;
1111
template <typename> struct encode_u32;
1212
template <typename> struct encode_u64;
13-
template <typename...> struct encode_enum;
1413

1514
namespace logging {
1615
template <typename T>
17-
concept signed_packable =
18-
std::signed_integral<T> and sizeof(T) <= sizeof(std::int64_t);
16+
concept signed_packable = std::signed_integral<stdx::underlying_type_t<T>> and
17+
sizeof(T) <= sizeof(std::int64_t);
1918

2019
template <typename T>
2120
concept unsigned_packable =
22-
std::unsigned_integral<T> and sizeof(T) <= sizeof(std::int64_t);
21+
std::unsigned_integral<stdx::underlying_type_t<T>> and
22+
sizeof(T) <= sizeof(std::int64_t);
2323

2424
template <typename T>
25-
concept float_packable =
26-
std::floating_point<T> and sizeof(T) <= sizeof(std::int64_t);
25+
concept float_packable = std::floating_point<stdx::underlying_type_t<T>> and
26+
sizeof(T) <= sizeof(std::int64_t);
2727

2828
template <typename T>
29-
concept enum_packable = std::is_enum_v<T> and sizeof(T) <= sizeof(std::int64_t);
30-
31-
template <typename T>
32-
concept packable = signed_packable<T> or unsigned_packable<T> or
33-
float_packable<T> or enum_packable<T>;
29+
concept packable =
30+
signed_packable<T> or unsigned_packable<T> or float_packable<T>;
3431

3532
template <typename T> struct encoding;
3633

37-
namespace detail {
38-
template <typename T>
39-
using signed_encode_t = stdx::conditional_t<sizeof(T) <= sizeof(std::int32_t),
40-
encode_32<T>, encode_64<T>>;
41-
template <typename T>
42-
using unsigned_encode_t =
43-
stdx::conditional_t<sizeof(T) <= sizeof(std::uint32_t), encode_u32<T>,
44-
encode_u64<T>>;
45-
} // namespace detail
46-
4734
template <signed_packable T> struct encoding<T> {
48-
using encode_t = detail::signed_encode_t<T>;
35+
using encode_t = stdx::conditional_t<sizeof(T) <= sizeof(std::int32_t),
36+
encode_32<T>, encode_64<T>>;
4937
using pack_t = stdx::conditional_t<sizeof(T) <= sizeof(std::int32_t),
5038
std::int32_t, std::int64_t>;
5139
};
5240

5341
template <unsigned_packable T> struct encoding<T> {
54-
using encode_t = detail::unsigned_encode_t<T>;
42+
using encode_t = stdx::conditional_t<sizeof(T) <= sizeof(std::uint32_t),
43+
encode_u32<T>, encode_u64<T>>;
5544
using pack_t = stdx::conditional_t<sizeof(T) <= sizeof(std::uint32_t),
5645
std::uint32_t, std::uint64_t>;
5746
};
5847

5948
template <float_packable T> struct encoding<T> {
60-
using encode_t = detail::unsigned_encode_t<T>;
49+
using encode_t = stdx::conditional_t<sizeof(T) <= sizeof(std::uint32_t),
50+
encode_u32<T>, encode_u64<T>>;
6151
using pack_t = stdx::conditional_t<sizeof(T) <= sizeof(std::uint32_t),
6252
std::uint32_t, std::uint64_t>;
6353
};
6454

65-
template <enum_packable T>
66-
struct encoding<T> : encoding<stdx::underlying_type_t<T>> {
67-
using encode_t = stdx::conditional_t<
68-
stdx::is_scoped_enum_v<T>, encode_enum<T, stdx::underlying_type_t<T>>,
69-
stdx::conditional_t<std::signed_integral<stdx::underlying_type_t<T>>,
70-
detail::signed_encode_t<T>,
71-
detail::unsigned_encode_t<T>>>;
72-
};
73-
7455
struct default_arg_packer {
7556
template <packable T> using pack_as_t = typename encoding<T>::pack_t;
7657
template <packable T> using encode_as_t = typename encoding<T>::encode_t;

test/log/catalog2b_lib.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,14 @@ using log_env2b = stdx::make_env_t<logging::get_level, logging::level::TRACE>;
2626
} // namespace
2727

2828
auto log_rt_enum_arg() -> void;
29-
auto log_rt_auto_scoped_enum_arg() -> void;
3029
auto log_rt_float_arg() -> void;
3130
auto log_rt_double_arg() -> void;
3231

3332
auto log_rt_enum_arg() -> void {
3433
auto cfg = logging::binary::config{test_log_destination{}};
3534
using namespace ns;
3635
cfg.logger.log_msg<log_env2b>(
37-
stdx::ct_format<"E string with {} placeholder">(VAL));
38-
}
39-
40-
namespace some_ns {
41-
enum struct E { A, B, C };
42-
}
43-
44-
auto log_rt_auto_scoped_enum_arg() -> void {
45-
auto cfg = logging::binary::config{test_log_destination{}};
46-
using namespace some_ns;
47-
cfg.logger.log_msg<log_env2b>(
48-
stdx::ct_format<"E (scoped) string with {} placeholder">(E::A));
36+
stdx::ct_format<"E string with {} placeholder">(E::value));
4937
}
5038

5139
auto log_rt_float_arg() -> void {

test/log/catalog_app.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ extern auto log_one_64bit_rt_arg() -> void;
1717
extern auto log_one_formatted_rt_arg() -> void;
1818
extern auto log_two_rt_args() -> void;
1919
extern auto log_rt_enum_arg() -> void;
20-
extern auto log_rt_auto_scoped_enum_arg() -> void;
2120
extern auto log_with_non_default_module() -> void;
2221
extern auto log_with_fixed_module() -> void;
2322
extern auto log_with_fixed_string_id() -> void;
@@ -101,15 +100,6 @@ TEST_CASE("log runtime enum argument", "[catalog]") {
101100
CHECK(log_calls == 1);
102101
}
103102

104-
TEST_CASE("log runtime scoped enum argument outside included header",
105-
"[catalog]") {
106-
log_calls = 0;
107-
test_critical_section::count = 0;
108-
log_rt_auto_scoped_enum_arg();
109-
CHECK(test_critical_section::count == 2);
110-
CHECK(log_calls == 1);
111-
}
112-
113103
TEST_CASE("log module ids change", "[catalog]") {
114104
// subtype 1, severity 7, type 3
115105
std::uint32_t expected_static = (1u << 24u) | (7u << 4u) | 3u;

test/log/catalog_enums.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
22

33
namespace ns {
4-
enum E { VAL = 42 };
5-
} // namespace ns
4+
enum struct E { value = 42 };
5+
}

test/log/encoder.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,6 @@ using log_env = stdx::make_env_t<logging::get_level, logging::level::TRACE>;
155155

156156
template <> inline auto conc::injected_policy<> = test_conc_policy{};
157157

158-
namespace {
159-
enum UnscopedE { A, B, C };
160-
enum struct ScopedE { A, B, C };
161-
} // namespace
162-
163158
TEST_CASE("argument packing", "[mipi]") {
164159
using P = logging::default_arg_packer;
165160
STATIC_REQUIRE(std::same_as<P::pack_as_t<std::int32_t>, std::int32_t>);
@@ -170,8 +165,6 @@ TEST_CASE("argument packing", "[mipi]") {
170165
STATIC_REQUIRE(std::same_as<P::pack_as_t<unsigned char>, std::uint32_t>);
171166
STATIC_REQUIRE(std::same_as<P::pack_as_t<float>, std::uint32_t>);
172167
STATIC_REQUIRE(std::same_as<P::pack_as_t<double>, std::uint64_t>);
173-
STATIC_REQUIRE(std::same_as<P::pack_as_t<UnscopedE>, std::uint32_t>);
174-
STATIC_REQUIRE(std::same_as<P::pack_as_t<ScopedE>, std::int32_t>);
175168
}
176169

177170
TEST_CASE("argument encoding", "[mipi]") {
@@ -189,10 +182,6 @@ TEST_CASE("argument encoding", "[mipi]") {
189182
std::same_as<P::encode_as_t<unsigned char>, encode_u32<unsigned char>>);
190183
STATIC_REQUIRE(std::same_as<P::encode_as_t<float>, encode_u32<float>>);
191184
STATIC_REQUIRE(std::same_as<P::encode_as_t<double>, encode_u64<double>>);
192-
STATIC_REQUIRE(
193-
std::same_as<P::encode_as_t<UnscopedE>, encode_u32<UnscopedE>>);
194-
STATIC_REQUIRE(
195-
std::same_as<P::encode_as_t<ScopedE>, encode_enum<ScopedE, int>>);
196185
}
197186

198187
TEST_CASE("log zero arguments", "[mipi]") {

tools/gen_str_catalog.py

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,6 @@ def read_file(filename):
202202
return assign_ids(unique_messages, unique_modules, stable_data)
203203

204204

205-
def make_cpp_scoped_enum_decl(e: str, ut: str) -> str:
206-
parts = e.split("::")
207-
enum = parts[-1]
208-
if "(anonymous namespace)" in parts or "{anonymous}" in parts:
209-
raise Exception(
210-
f"Scoped enum {e} is inside an anonymous namespace and cannot be forward declared."
211-
)
212-
if len(parts) > 1:
213-
ns = "::".join(parts[:-1])
214-
return f"namespace {ns} {{ enum struct {enum} : {ut}; }}"
215-
return f"enum struct {enum} : {ut};"
216-
217-
218205
def make_cpp_catalog_defn(m: Message) -> str:
219206
return f"""/*
220207
"{m.text}"
@@ -234,16 +221,11 @@ def make_cpp_module_defn(m: Module) -> str:
234221
}}"""
235222

236223

237-
def write_cpp(messages, modules, scoped_enums, extra_headers: list[str], filename: str):
224+
def write_cpp(messages, modules, extra_headers: list[str], filename: str):
238225
with open(filename, "w") as f:
239226
f.write("\n".join(f'#include "{h}"' for h in extra_headers))
240227
f.write("\n#include <log/catalog/arguments.hpp>\n")
241228
f.write("\n#include <log/catalog/catalog.hpp>\n\n")
242-
scoped_enum_decls = [
243-
make_cpp_scoped_enum_decl(e, ut) for e, ut in scoped_enums.items()
244-
]
245-
f.write("\n".join(scoped_enum_decls))
246-
f.write("\n\n")
247229
cpp_catalog_defns = [make_cpp_catalog_defn(m) for m in messages]
248230
f.write("\n".join(cpp_catalog_defns))
249231
f.write("\n\n")
@@ -269,15 +251,18 @@ def fq_name(node):
269251
index = Index.create()
270252
translation_unit = index.parse(filename)
271253

272-
enums: dict[str, dict] = {}
254+
enums = {}
273255
for node in translation_unit.cursor.walk_preorder():
274256
if node.kind == CursorKind.ENUM_DECL:
275-
new_decl = {
276-
e.spelling: e.enum_value
277-
for e in node.walk_preorder()
278-
if e.kind == CursorKind.ENUM_CONSTANT_DECL
279-
}
280-
enums[fq_name(node)] = enums.get(fq_name(node), {}) | new_decl
257+
enums.update(
258+
{
259+
fq_name(node): {
260+
e.spelling: e.enum_value
261+
for e in node.walk_preorder()
262+
if e.kind == CursorKind.ENUM_CONSTANT_DECL
263+
}
264+
}
265+
)
281266
return enums
282267

283268

@@ -289,11 +274,11 @@ def write_json(
289274
)
290275
for m in stable_data.get("messages"):
291276
j = m.to_json()
292-
if j not in d["messages"]:
277+
if not j in d["messages"]:
293278
d["messages"].append(j)
294279
for m in stable_data.get("modules"):
295280
j = m.to_json()
296-
if j not in d["modules"]:
281+
if not j in d["modules"]:
297282
d["modules"].append(j)
298283

299284
str_catalog = dict(**d, enums=dict())
@@ -362,13 +347,9 @@ def serialize_modules(client_node: et.Element, modules: list[Module]):
362347

363348

364349
def arg_type_encoding(arg):
365-
string_re = re.compile(r"encode_(32|u32|64|u64|enum)<(.*)>")
350+
string_re = re.compile(r"encode_(32|u32|64|u64)<(.*)>")
366351
m = string_re.match(arg)
367-
if "enum" in m.group(1):
368-
args_re = re.compile(r"(.*), (.*)")
369-
args_m = args_re.match(m.group(2))
370-
return (f"encode_{m.group(1)}", args_m.group(1), args_m.group(2))
371-
return (f"encode_{m.group(1)}", m.group(2), m.group(2))
352+
return (f"encode_{m.group(1)}", m.group(2))
372353

373354

374355
def arg_printf_spec(arg: str):
@@ -379,20 +360,9 @@ def arg_printf_spec(arg: str):
379360
"encode_u64": "%llu",
380361
"float": "%f",
381362
"double": "%f",
382-
"int": "%d",
383-
"unsigned int": "%u",
384-
"short": "%d",
385-
"unsigned short": "%u",
386-
"signed char": "%d",
387-
"unsigned char": "%u",
388-
"char": "%c",
389-
"long": "%ld",
390-
"unsigned long": "%lu",
391-
"long long": "%lld",
392-
"unsigned long long": "%llu",
393363
}
394-
enc, _, ut = arg_type_encoding(arg)
395-
return printf_dict.get(ut, printf_dict.get(enc, "%d"))
364+
enc, t = arg_type_encoding(arg)
365+
return printf_dict.get(t, printf_dict.get(enc, "%d"))
396366

397367

398368
def serialize_messages(
@@ -569,15 +539,8 @@ def main():
569539
except Exception as e:
570540
raise Exception(f"{str(e)} from file {args.input}")
571541

572-
scoped_enums = {}
573542
if args.cpp_output is not None:
574-
for m in messages:
575-
for i, arg_type in enumerate(m.args):
576-
enc, enum, ut = arg_type_encoding(arg_type)
577-
if "enum" in enc:
578-
scoped_enums.update({enum: ut})
579-
580-
write_cpp(messages, modules, scoped_enums, args.cpp_headers, args.cpp_output)
543+
write_cpp(messages, modules, args.cpp_headers, args.cpp_output)
581544

582545
stable_output = dict(messages=[], modules=[])
583546
if not args.forget_old_ids:
@@ -600,13 +563,14 @@ def main():
600563
}
601564
for m in messages:
602565
for i, arg_type in enumerate(m.args):
603-
_, enum, _ = arg_type_encoding(arg_type)
566+
_, enum = arg_type_encoding(arg_type)
604567
if enum in enums:
605568
m.enum_lookup = (i + 1, enums[enum][0])
606569
except Exception as e:
607570
print(
608571
f"Couldn't extract enum info ({str(e)}), enum lookup will not be available"
609572
)
573+
610574
else:
611575
print("XML output without C++ output: enum lookup will not be available")
612576

0 commit comments

Comments
 (0)