Skip to content

Commit 4bc0f7c

Browse files
authored
Merge pull request #575 from elbeno/dont-reuse-old-ids
✨ Allow old string IDs to be remembered by default
2 parents cfc6543 + a492c8b commit 4bc0f7c

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

cmake/string_catalog.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function(gen_str_catalog)
2-
set(options "")
2+
set(options FORGET_OLD_IDS)
33
set(oneValueArgs OUTPUT_CPP OUTPUT_XML OUTPUT_JSON GEN_STR_CATALOG
44
OUTPUT_LIB)
55
set(multiValueArgs INPUT_JSON INPUT_LIBS INPUT_HEADERS STABLE_JSON)
@@ -31,13 +31,18 @@ function(gen_str_catalog)
3131
list(TRANSFORM SC_STABLE_JSON PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/")
3232
list(TRANSFORM SC_INPUT_HEADERS PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/")
3333

34+
if(SC_FORGET_OLD_IDS)
35+
set(FORGET_ARG "--forget_old_ids")
36+
endif()
37+
3438
add_custom_command(
3539
OUTPUT ${SC_OUTPUT_CPP} ${SC_OUTPUT_JSON} ${SC_OUTPUT_XML}
3640
COMMAND
3741
${Python3_EXECUTABLE} ${SC_GEN_STR_CATALOG} --input ${UNDEFS}
3842
--json_input ${SC_INPUT_JSON} --cpp_headers ${SC_INPUT_HEADERS}
3943
--cpp_output ${SC_OUTPUT_CPP} --json_output ${SC_OUTPUT_JSON}
4044
--xml_output ${SC_OUTPUT_XML} --stable_json ${SC_STABLE_JSON}
45+
${FORGET_ARG}
4146
DEPENDS ${UNDEFS} ${INPUT_JSON} ${SC_GEN_STR_CATALOG} ${SC_STABLE_JSON}
4247
COMMAND_EXPAND_LISTS)
4348
if(SC_OUTPUT_LIB)

test/log/catalog_app.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ TEST_CASE("log one compile-time argument", "[catalog]") {
3434
log_one_ct_arg();
3535
CHECK(test_critical_section::count == 2);
3636
CHECK(log_calls == 1);
37+
// ID 0 is reserved by stable input
38+
CHECK(last_header >> 4u != 0);
3739
}
3840

3941
TEST_CASE("log one runtime argument", "[catalog]") {

test/log/stable_strings.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
{
22
"messages": [
3+
{
4+
"level": "TRACE",
5+
"msg": "An old string that can't be reused",
6+
"type": "msg",
7+
"arg_types": [],
8+
"arg_count": 0,
9+
"id": 0
10+
},
311
{
412
"level": "TRACE",
513
"msg": "A string with no placeholders",
@@ -10,6 +18,10 @@
1018
}
1119
],
1220
"modules": [
21+
{
22+
"string": "old_stable",
23+
"id": 42
24+
},
1325
{
1426
"string": "fixed",
1527
"id": 17

tools/gen_str_catalog.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,14 @@ def fq_name(node):
199199
return enums
200200

201201

202-
def write_json(messages, modules, extra_inputs: list[str], filename: str):
202+
def write_json(messages, modules, extra_inputs: list[str], filename: str, stable_ids):
203203
str_catalog = dict(messages=list(messages.values()), modules=list(modules.values()))
204+
for msg in stable_ids.get("messages"):
205+
if not msg in str_catalog["messages"]:
206+
str_catalog["messages"].append(msg)
207+
for mod in stable_ids.get("modules"):
208+
if not mod in str_catalog["modules"]:
209+
str_catalog["modules"].append(mod)
204210
for extra in extra_inputs:
205211
with open(extra, "r") as f:
206212
str_catalog.update(json.load(f))
@@ -213,10 +219,7 @@ def read_stable(stable_filenames: list[str]):
213219
for filename in stable_filenames:
214220
with open(filename, "r") as f:
215221
stable_catalog.update(json.load(f))
216-
return (
217-
{stable_msg_key(msg): msg["id"] for msg in stable_catalog["messages"]},
218-
{m["string"]: m["id"] for m in stable_catalog["modules"]},
219-
)
222+
return stable_catalog
220223

221224

222225
def serialize_guids(client_node: et.Element, guid_id: str, guid_mask: str):
@@ -371,6 +374,11 @@ def parse_cmdline():
371374
default=[],
372375
help="Input filename(s) for previously generated JSON; this is used to fix stable IDs.",
373376
)
377+
parser.add_argument(
378+
"--forget_old_ids",
379+
action="store_true",
380+
help="When on, stable IDs from a previous run are forgotten. By default, those strings are remembered in the output so that they will not be reused in future.",
381+
)
374382
return parser.parse_args()
375383

376384

@@ -388,17 +396,27 @@ def main():
388396
et._escape_cdata = _escape_cdata
389397
args = parse_cmdline()
390398

391-
stable_ids = read_stable(args.stable_json)
399+
stable_catalog = read_stable(args.stable_json)
392400
try:
401+
stable_ids = (
402+
{stable_msg_key(msg): msg["id"] for msg in stable_catalog["messages"]},
403+
{m["string"]: m["id"] for m in stable_catalog["modules"]},
404+
)
393405
modules, messages = read_input(args.input, stable_ids)
394406
except Exception as e:
395407
raise Exception(f"{str(e)} from file {args.input}")
396408

397409
if args.cpp_output is not None:
398410
write_cpp(messages, modules, args.cpp_headers, args.cpp_output)
399411

412+
stable_output = dict(messages=[], modules=[])
413+
if not args.forget_old_ids:
414+
stable_output = dict(
415+
messages=stable_catalog["messages"], modules=stable_catalog["modules"]
416+
)
417+
400418
if args.json_output is not None:
401-
write_json(messages, modules, args.json_input, args.json_output)
419+
write_json(messages, modules, args.json_input, args.json_output, stable_output)
402420

403421
if args.xml_output is not None:
404422
enums = {}

0 commit comments

Comments
 (0)