Repository: jerryscript-project/jerryscript
File: tools/gen-magic-strings.py (Python)

Code before:
def calculate_magic_string_guards(defs, uses, debug=False):
    extended_defs = []

    for str_ref, str_value in defs:
        if str_ref not in uses:
            print('warning: unused magic string {str_ref}'
                  .format(str_ref=str_ref))
            continue

        # Calculate the most compact guard, i.e., if a magic string is
        # referenced under various guards, keep the one that is more generic.
        # E.g.,
        # guard1 = A and B and C and D and E and F
        # guard2 = A and B and C
        # then guard1 or guard2 == guard2.
        guards = [set(guard_tuple) for guard_tuple in uses[str_ref].keys()]
        for i, guard_i in enumerate(guards):
            if guard_i is None:
                continue
            for guard in guard_i.copy():
                neg_guard = "!(" + guard[1:] + ")"
                for guard_j in guards:
                    if guard_j is not None and neg_guard in guard_j:
                        guards.remove(guard_i)
                        guard_i.remove(guard)
                        guards.append(guard_i)
            for j, guard_j in enumerate(guards):
                if j == i or guard_j is None:
                    continue
                if guard_i < guard_j:
                    guards[j] = None
        guards = {tuple(sorted(guard)) for guard in guards if guard is not None}

        extended_defs.append((str_ref, str_value, guards))

    if debug:
        print('debug: magic string definitions (with guards): {dump}'
              .format(dump=debug_dump(extended_defs)))

    return extended_defs


def guards_to_str(guards):
    return ' \\\n|| '.join(' && '.join(g.strip() for g in sorted(guard))
                           for guard in sorted(guards))


def generate_header(gen_file):
    header = \
"""/* Copyright JS Foundation and other contributors, http://js.foundation
 *

Review comment (suggestion):
I'm not sure if we need the `guards.remove` and `guards.append` at all:
* guard_i is an object reference (I think it is a set) in the guards array(?).
* So if we remove an element from the guard_i then (as it is a reference) the change is reflected in the guard array also.



Code after:
def calculate_magic_string_guards(defs, uses, debug=False):
    extended_defs = []

    for str_ref, str_value in defs:
        if str_ref not in uses:
            print('warning: unused magic string {str_ref}'
                  .format(str_ref=str_ref))
            continue

        # Calculate the most compact guard, i.e., if a magic string is
        # referenced under various guards, keep the one that is more generic.
        # E.g.,
        # guard1 = A and B and C and D and E and F
        # guard2 = A and B and C
        # then guard1 or guard2 == guard2.
        guards = [set(guard_tuple) for guard_tuple in uses[str_ref].keys()]
        for i, guard_i in enumerate(guards):
            if guard_i is None:
                continue
            for guard in guard_i.copy():
                neg_guard = "!(" + guard[1:] + ")"
                for guard_j in guards:
                    if guard_j is not None and neg_guard in guard_j:
                        guard_i.remove(guard)
            for j, guard_j in enumerate(guards):
                if j == i or guard_j is None:
                    continue
                if guard_i < guard_j:
                    guards[j] = None
        guards = {tuple(sorted(guard)) for guard in guards if guard is not None}

        extended_defs.append((str_ref, str_value, guards))

    if debug:
        print('debug: magic string definitions (with guards): {dump}'
              .format(dump=debug_dump(extended_defs)))

    return extended_defs


def guards_to_str(guards):
    return ' \\\n|| '.join(' && '.join(g.strip() for g in sorted(guard))
                           for guard in sorted(guards))


def generate_header(gen_file):
    header = \
"""/* Copyright JS Foundation and other contributors, http://js.foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.