<commit_before># coding=utf-8
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

"""Allows detection of support for various command-line flags.
"""

from functools import lru_cache
import multiprocessing
import random
import re

from . import inspect_shell
from .build_options import chance


@lru_cache(maxsize=None)
def shell_supports_flag(shell_path, flag):
    """Returns whether a particular flag is supported by a shell.

    Args:
        shell_path (str): Path to the required shell.
        flag (str): Intended flag to test.

    Returns:
        bool: True if the flag is supported, i.e. does not cause the shell to throw an error, False otherwise.
    """
    out = inspect_shell.shellSupports(shell_path, [flag, "-e", "42"])
    return out


def add_random_arch_flags(shell_path, input_list=False):
    """Returns a list with probably additional architecture-related flags added.

    Args:
        shell_path (str): Path to the required shell.
        input_list (list): List of flags to eventually be tested against a shell.

    Returns:
        list: List of flags to be tested, with probable architecture-related flags added.
    """
    # m-c rev 330353:bb868860dfc3 is the earliest known working revision, so stop testing prior existence of flag

    if inspect_shell.queryBuildConfiguration(shell_path, "arm-simulator") and chance(.7):
        # m-c rev 165993:c450eb3abde4, see bug 965247
        input_list.append("--arm-sim-icache-checks")
    if inspect_shell.queryBuildConfiguration(shell_path, "arm-simulator") and chance(.7):
        # Added due to fuzz-flags.txt addition: m-c rev 418682:5bba65880a66, see bug 1461689
        # m-c rev 192164:f1bacafe789c, see bug 1020834
        input_list.append("--arm-asm-nop-fill=1")
    if inspect_shell.queryBuildConfiguration(shell_path, "arm-simulator") and chance(.7):
        # Added due to fuzz-flags.txt addition: m-c rev 418682:5bba65880a66, see bug 1461689
        # m-c rev 190582:5399dc155c3b, see bug 1028008
        input_list.append("--arm-hwcap=vfp")

    if chance(.2):
        # m-c rev 223959:5e6e959f0043, see bug 1118235
        input_list.append("--enable-avx")
    elif chance(.2):
        # m-c rev 223959:5e6e959f0043, see bug 1118235
        input_list.append("--no-avx")

    if chance(.2):  # m-c rev 154600:526ba3ace37a, see bug 935791
        input_list.append("--no-sse" + ("3" if chance(.5) else "4"))

    return input_list


def add_random_ion_flags(shell_path, input_list=False):  # pylint: disable=too-complex,too-many-branches
    # pylint: disable=too-many-statements
    """Returns a list with probably additional IonMonkey flags added.

    The non-default options have a higher chance of being set, e.g. chance(.9)

    Args:
        shell_path (str): Path to the required shell.
        input_list (list): List of flags to eventually be tested against a shell.

    Returns:
        list: List of flags to be tested, with probable IonMonkey flags added.
    """
    if shell_supports_flag(shell_path, "--ion-optimization-levels=on") and chance(.2):
        # m-c rev 467738:3134740d831c, see bug 1382650
        input_list.append("--ion-optimization-levels=" + ("on" if chance(.1) else "off"))
    if shell_supports_flag(shell_path, "--ion-full-warmup-threshold=0") and chance(.2):
        # m-c rev 466417:a812f6daf98e, see bug 1382650
        input_list.append("--ion-full-warmup-threshold=0")
    elif shell_supports_flag(shell_path, "--ion-full-warmup-threshold=0") and chance(.2):
        # m-c rev 466417:a812f6daf98e, see bug 1382650
        input_list.append("--ion-full-warmup-threshold=10")
    elif shell_supports_flag(shell_path, "--ion-full-warmup-threshold=0") and chance(.2):
        # m-c rev 466417:a812f6daf98e, see bug 1382650
        input_list.append("--ion-full-warmup-threshold=100")
    elif shell_supports_flag(shell_path, "--ion-full-warmup-threshold=0") and chance(.2):
        # m-c rev 466417:a812f6daf98e, see bug 1382650
        input_list.append("--ion-full-warmup-threshold=1000")
    elif shell_supports_flag(shell_path, "--ion-full-warmup-threshold=0") and chance(.2):
        # m-c rev 466417:a812f6daf98e, see bug 1382650
        input_list.append("--ion-full-warmup-threshold=1500")
    elif shell_supports_flag(shell_path, "--ion-full-warmup-threshold=0") and chance(.2):
        # m-c rev 466417:a812f6daf98e, see bug 1382650
        input_list.append("--ion-full-warmup-threshold=5000")
    if shell_supports_flag(shell_path, "--no-bigint") and chance(.2):
        # m-c rev 461970:e262ebb01282, see bug 1527900
        input_list.append("--no-bigint")

    # m-c rev 330353:bb868860dfc3 is the earliest known working revision, so stop testing prior existence of flag

    if chance(.2):
        # m-c rev 308931:1c5b92144e1e, see bug 1292659
        input_list.append("--cache-ir-stubs=" + ("on" if chance(.1) else "off"))
    if chance(.2):
        # m-c rev 272274:b0a0ff5fa705, see bug 1209515
        input_list.append("--ion-pgo=" + ("on" if chance(.1) else "off"))
    if chance(.2):
        # m-c rev 262544:3dec2b935295, see bug 984018
        input_list.append("--ion-sincos=" + ("on" if chance(.5) else "off"))
    if chance(.2):
        # m-c rev 259672:59d2f2e62420, see bug 1195545
        input_list.append("--ion-instruction-reordering=" + ("on" if chance(.9) else "off"))
    if chance(.2):
        # m-c rev 248962:47e92bae09fd, see bug 1170840
        input_list.append("--ion-regalloc=testbed")

    forceinline_cmd = '--execute=\"setJitCompilerOption(\\\"ion.forceinlineCaches\\\",1)\"'
    # The only way I could make nested double quotes work with subprocess. See https://stackoverflow.com/a/35913597
    forceinline_shell_cmd = re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+', forceinline_cmd)[0]
    if chance(.2):
        # m-c rev 247709:ea9608e33abe, see bug 923717
        input_list.append(forceinline_shell_cmd)
    if chance(.2):
        # m-c rev 234228:cdf93416b39a, see bug 1139152
        input_list.append("--ion-extra-checks")

    # --ion-sink=on is still not ready to be fuzzed
    # if chance(.2):  # m-c rev 217242:9188c8b7962b, see bug 1093674
    #     input_list.append("--ion-sink=" + ("on" if chance(.1) else "off"))
    if chance(.7):  # m-c rev 204669:891d587c19c4, see bug 1063816
        # Added due to fuzz-flags.txt addition: m-c rev 460895:e0f409bac9bd, see bug 1529072
        input_list.append("--ion-warmup-threshold=0")
    elif chance(.5):  # m-c rev 204669:891d587c19c4, see bug 1063816
        # Added due to fuzz-flags.txt addition: m-c rev 418682:5bba65880a66, see bug 1461689
        input_list.append("--ion-warmup-threshold=100")
    if chance(.2):  # m-c rev 194672:b2a822934b97, see bug 992845
        input_list.append("--ion-scalar-replacement=" + ("on" if chance(.1) else "off"))
    if chance(.2):  # m-c rev 142933:f08e4a699011, see bug 894813
        input_list.append("--ion-check-range-analysis")
    # The stupid allocator isn't used by default and devs prefer not to have to fix fuzzbugs
    # if chance(.2):  # m-c rev 114120:7e97c5392d81, see bug 812945
        # input_list.append("--ion-regalloc=stupid")
    if chance(.2):  # m-c rev 106493:6688ede89a36, see bug 699883
        input_list.append("--ion-range-analysis=" + ("on" if chance(.1) else "off"))
    if chance(.2):  # m-c rev 106491:6c870a497ea4, see bug 699883
        input_list.append("--ion-edgecase-analysis=" + ("on" if chance(.1) else "off"))
    if chance(.2):  # m-c rev 106247:feac7727629c, see bug 755010
        input_list.append("--ion-limit-script-size=" + ("on" if chance(.1) else "off"))
    if chance(.2):  # m-c rev 105351:9fb668f0baca, see bug 700108
        input_list.append("--ion-osr=" + ("on" if chance(.1) else "off"))
    if chance(.2):  # m-c rev 105338:01ebfabf29e2, see bug 687901
        input_list.append("--ion-inlining=" + ("on" if chance(.1) else "off"))
    if chance(.2):  # m-c rev 105173:4ceb3e9961e4, see bug 683039
        input_list.append("--ion-eager")
    if chance(.2):  # m-c rev 104923:8db8eef79b8c, see bug 670816
        input_list.append("--ion-gvn=" + ("on" if chance(.1) else "off"))
    if chance(.2):  # m-c rev 104923:8db8eef79b8c, see bug 670816
        input_list.append("--ion-licm=" + ("on" if chance(.1) else "off"))

    return input_list


def add_random_wasm_flags(shell_path, input_list=False):
    """Returns a list with probably additional WebAssembly (wasm/asmjs) flags added.

    Args:
        shell_path (str): Path to the required shell.
        input_list (list): List of flags to eventually be tested against a shell.

    Returns:
        list: List of flags to be tested, with probable wasm flags added.
    """
    if shell_supports_flag(shell_path, "--wasm-compiler=none") and chance(.9):
        if chance(.4):
            wasm_compiler_option = "baseline+ion"
        elif chance(.5):  # pragma: no cover
            wasm_compiler_option = "baseline"
        elif chance(.6):  # pragma: no cover
            wasm_compiler_option = "ion"
        # elif chance(.7):  # pragma: no cover
        #     wasm_compiler_option = "baseline+cranelift"
        # elif chance(.8):  # pragma: no cover
        #     wasm_compiler_option = "cranelift"
        else:  # pragma: no cover
            wasm_compiler_option = "none"
        # m-c rev 455252:48dc14f79fb0, see bug 1509441
        input_list.append("--wasm-compiler=" + wasm_compiler_option)
    # --wasm-gc is now unstable as of bug 1488205
    # if shell_supports_flag(shell_path, "--wasm-gc") and chance(.8):
    #     # m-c rev 413255:302befe7689a, see bug 1445272
    #     input_list.append("--wasm-gc")
    if shell_supports_flag(shell_path, "--test-wasm-await-tier2") and chance(.8):
        # m-c rev 387188:b1dc87a94262, see bug 1388785
        input_list.append("--test-wasm-await-tier2")

    # m-c rev 330353:bb868860dfc3 is the earliest known working revision, so stop testing prior existence of flag

    if chance(.7):  # m-c rev 124920:b3d85b68449d, see bug 840282
        input_list.append("--no-asmjs")

    return input_list


def random_flag_set(shell_path):  # pylint: disable=too-complex,too-many-branches,too-many-statements
    """Returns a random list of CLI flags appropriate for the given shell.

    Args:
        shell_path (str): Path to the required shell.

    Returns:
        list: List of flags to be tested.
    """
    args = []
<commit_msg>Tweak cranelift flag testing in shell_flags in preparation for its re-activation.<commit_after># coding=utf-8
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

"""Allows detection of support for various command-line flags.
"""

from functools import lru_cache
import multiprocessing
import random
import re

from . import inspect_shell
from .build_options import chance


@lru_cache(maxsize=None)
def shell_supports_flag(shell_path, flag):
    """Returns whether a particular flag is supported by a shell.

    Args:
        shell_path (str): Path to the required shell.
        flag (str): Intended flag to test.

    Returns:
        bool: True if the flag is supported, i.e. does not cause the shell to throw an error, False otherwise.
    """
    out = inspect_shell.shellSupports(shell_path, [flag, "-e", "42"])
    return out


def add_random_arch_flags(shell_path, input_list=False):
    """Returns a list with probably additional architecture-related flags added.

    Args:
        shell_path (str): Path to the required shell.
        input_list (list): List of flags to eventually be tested against a shell.

    Returns:
        list: List of flags to be tested, with probable architecture-related flags added.
    """
    # m-c rev 330353:bb868860dfc3 is the earliest known working revision, so stop testing prior existence of flag

    if inspect_shell.queryBuildConfiguration(shell_path, "arm-simulator") and chance(.7):
        # m-c rev 165993:c450eb3abde4, see bug 965247
        input_list.append("--arm-sim-icache-checks")
    if inspect_shell.queryBuildConfiguration(shell_path, "arm-simulator") and chance(.7):
        # Added due to fuzz-flags.txt addition: m-c rev 418682:5bba65880a66, see bug 1461689
        # m-c rev 192164:f1bacafe789c, see bug 1020834
        input_list.append("--arm-asm-nop-fill=1")
    if inspect_shell.queryBuildConfiguration(shell_path, "arm-simulator") and chance(.7):
        # Added due to fuzz-flags.txt addition: m-c rev 418682:5bba65880a66, see bug 1461689
        # m-c rev 190582:5399dc155c3b, see bug 1028008
        input_list.append("--arm-hwcap=vfp")

    if chance(.2):
        # m-c rev 223959:5e6e959f0043, see bug 1118235
        input_list.append("--enable-avx")
    elif chance(.2):
        # m-c rev 223959:5e6e959f0043, see bug 1118235
        input_list.append("--no-avx")

    if chance(.2):  # m-c rev 154600:526ba3ace37a, see bug 935791
        input_list.append("--no-sse" + ("3" if chance(.5) else "4"))

    return input_list


def add_random_ion_flags(shell_path, input_list=False):  # pylint: disable=too-complex,too-many-branches
    # pylint: disable=too-many-statements
    """Returns a list with probably additional IonMonkey flags added.

    The non-default options have a higher chance of being set, e.g. chance(.9)

    Args:
        shell_path (str): Path to the required shell.
        input_list (list): List of flags to eventually be tested against a shell.

    Returns:
        list: List of flags to be tested, with probable IonMonkey flags added.
    """
    if shell_supports_flag(shell_path, "--ion-optimization-levels=on") and chance(.2):
        # m-c rev 467738:3134740d831c, see bug 1382650
        input_list.append("--ion-optimization-levels=" + ("on" if chance(.1) else "off"))
    if shell_supports_flag(shell_path, "--ion-full-warmup-threshold=0") and chance(.2):
        # m-c rev 466417:a812f6daf98e, see bug 1382650
        input_list.append("--ion-full-warmup-threshold=0")
    elif shell_supports_flag(shell_path, "--ion-full-warmup-threshold=0") and chance(.2):
        # m-c rev 466417:a812f6daf98e, see bug 1382650
        input_list.append("--ion-full-warmup-threshold=10")
    elif shell_supports_flag(shell_path, "--ion-full-warmup-threshold=0") and chance(.2):
        # m-c rev 466417:a812f6daf98e, see bug 1382650
        input_list.append("--ion-full-warmup-threshold=100")
    elif shell_supports_flag(shell_path, "--ion-full-warmup-threshold=0") and chance(.2):
        # m-c rev 466417:a812f6daf98e, see bug 1382650
        input_list.append("--ion-full-warmup-threshold=1000")
    elif shell_supports_flag(shell_path, "--ion-full-warmup-threshold=0") and chance(.2):
        # m-c rev 466417:a812f6daf98e, see bug 1382650
        input_list.append("--ion-full-warmup-threshold=1500")
    elif shell_supports_flag(shell_path, "--ion-full-warmup-threshold=0") and chance(.2):
        # m-c rev 466417:a812f6daf98e, see bug 1382650
        input_list.append("--ion-full-warmup-threshold=5000")
    if shell_supports_flag(shell_path, "--no-bigint") and chance(.2):
        # m-c rev 461970:e262ebb01282, see bug 1527900
        input_list.append("--no-bigint")

    # m-c rev 330353:bb868860dfc3 is the earliest known working revision, so stop testing prior existence of flag

    if chance(.2):
        # m-c rev 308931:1c5b92144e1e, see bug 1292659
        input_list.append("--cache-ir-stubs=" + ("on" if chance(.1) else "off"))
    if chance(.2):
        # m-c rev 272274:b0a0ff5fa705, see bug 1209515
        input_list.append("--ion-pgo=" + ("on" if chance(.1) else "off"))
    if chance(.2):
        # m-c rev 262544:3dec2b935295, see bug 984018
        input_list.append("--ion-sincos=" + ("on" if chance(.5) else "off"))
    if chance(.2):
        # m-c rev 259672:59d2f2e62420, see bug 1195545
        input_list.append("--ion-instruction-reordering=" + ("on" if chance(.9) else "off"))
    if chance(.2):
        # m-c rev 248962:47e92bae09fd, see bug 1170840
        input_list.append("--ion-regalloc=testbed")

    forceinline_cmd = '--execute=\"setJitCompilerOption(\\\"ion.forceinlineCaches\\\",1)\"'
    # The only way I could make nested double quotes work with subprocess. See https://stackoverflow.com/a/35913597
    forceinline_shell_cmd = re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+', forceinline_cmd)[0]
    if chance(.2):
        # m-c rev 247709:ea9608e33abe, see bug 923717
        input_list.append(forceinline_shell_cmd)
    if chance(.2):
        # m-c rev 234228:cdf93416b39a, see bug 1139152
        input_list.append("--ion-extra-checks")

    # --ion-sink=on is still not ready to be fuzzed
    # if chance(.2):  # m-c rev 217242:9188c8b7962b, see bug 1093674
    #     input_list.append("--ion-sink=" + ("on" if chance(.1) else "off"))
    if chance(.7):  # m-c rev 204669:891d587c19c4, see bug 1063816
        # Added due to fuzz-flags.txt addition: m-c rev 460895:e0f409bac9bd, see bug 1529072
        input_list.append("--ion-warmup-threshold=0")
    elif chance(.5):  # m-c rev 204669:891d587c19c4, see bug 1063816
        # Added due to fuzz-flags.txt addition: m-c rev 418682:5bba65880a66, see bug 1461689
        input_list.append("--ion-warmup-threshold=100")
    if chance(.2):  # m-c rev 194672:b2a822934b97, see bug 992845
        input_list.append("--ion-scalar-replacement=" + ("on" if chance(.1) else "off"))
    if chance(.2):  # m-c rev 142933:f08e4a699011, see bug 894813
        input_list.append("--ion-check-range-analysis")
    # The stupid allocator isn't used by default and devs prefer not to have to fix fuzzbugs
    # if chance(.2):  # m-c rev 114120:7e97c5392d81, see bug 812945
        # input_list.append("--ion-regalloc=stupid")
    if chance(.2):  # m-c rev 106493:6688ede89a36, see bug 699883
        input_list.append("--ion-range-analysis=" + ("on" if chance(.1) else "off"))
    if chance(.2):  # m-c rev 106491:6c870a497ea4, see bug 699883
        input_list.append("--ion-edgecase-analysis=" + ("on" if chance(.1) else "off"))
    if chance(.2):  # m-c rev 106247:feac7727629c, see bug 755010
        input_list.append("--ion-limit-script-size=" + ("on" if chance(.1) else "off"))
    if chance(.2):  # m-c rev 105351:9fb668f0baca, see bug 700108
        input_list.append("--ion-osr=" + ("on" if chance(.1) else "off"))
    if chance(.2):  # m-c rev 105338:01ebfabf29e2, see bug 687901
        input_list.append("--ion-inlining=" + ("on" if chance(.1) else "off"))
    if chance(.2):  # m-c rev 105173:4ceb3e9961e4, see bug 683039
        input_list.append("--ion-eager")
    if chance(.2):  # m-c rev 104923:8db8eef79b8c, see bug 670816
        input_list.append("--ion-gvn=" + ("on" if chance(.1) else "off"))
    if chance(.2):  # m-c rev 104923:8db8eef79b8c, see bug 670816
        input_list.append("--ion-licm=" + ("on" if chance(.1) else "off"))

    return input_list


def add_random_wasm_flags(shell_path, input_list=False):
    """Returns a list with probably additional WebAssembly (wasm/asmjs) flags added.

    Args:
        shell_path (str): Path to the required shell.
        input_list (list): List of flags to eventually be tested against a shell.

    Returns:
        list: List of flags to be tested, with probable wasm flags added.
    """
    if shell_supports_flag(shell_path, "--wasm-compiler=none") and chance(.9):
        if chance(.2):
            wasm_compiler_option = "baseline+ion"
        else:  # pragma: no cover
            random_value = random.random()
            if 0 < random_value <= 0.2:  # pragma: no cover
                wasm_compiler_option = "ion"
            elif 0.2 < random_value <= 0.4:  # pragma: no cover
                wasm_compiler_option = "baseline+cranelift"
            elif 0.4 < random_value <= 0.6:  # pragma: no cover
                wasm_compiler_option = "cranelift"
            elif 0.6 < random_value <= 0.8:  # pragma: no cover
                wasm_compiler_option = "none"
            else:  # pragma: no cover
                wasm_compiler_option = "baseline"
        # m-c rev 455252:48dc14f79fb0, see bug 1509441
        input_list.append("--wasm-compiler=" + wasm_compiler_option)
    # --wasm-gc is now unstable as of bug 1488205
    # if shell_supports_flag(shell_path, "--wasm-gc") and chance(.8):
    #     # m-c rev 413255:302befe7689a, see bug 1445272
    #     input_list.append("--wasm-gc")
    if shell_supports_flag(shell_path, "--test-wasm-await-tier2") and chance(.8):
        # m-c rev 387188:b1dc87a94262, see bug 1388785
        input_list.append("--test-wasm-await-tier2")

    # m-c rev 330353:bb868860dfc3 is the earliest known working revision, so stop testing prior existence of flag

    if chance(.7):  # m-c rev 124920:b3d85b68449d, see bug 840282
        input_list.append("--no-asmjs")

    return input_list


def random_flag_set(shell_path):  # pylint: disable=too-complex,too-many-branches,too-many-statements
    """Returns a random list of CLI flags appropriate for the given shell.

    Args:
        shell_path (str): Path to the required shell.

    Returns:
        list: List of flags to be tested.
    """
    args = []

{'fraction_non_alphanumeric': 0.1270109281928615, 'fraction_numerical': 0.09497678345293373, 'mean_word_length': 2.9913889928865594, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4014168', 'n_tokens_mistral': 8769, 'n_tokens_neox': 7320, 'n_words': 1938}