<commit_before># -*- Python -*-

import os
import platform

import lit.formats

def get_required_attr(config, attr_name):
  attr_value = getattr(config, attr_name, None)
  if attr_value == None:
    lit_config.fatal(
      "No attribute %r in test configuration! You may need to run "
      "tests from your build directory or add this attribute "
      "to lit.site.cfg " % attr_name)
  return attr_value

def push_dynamic_library_lookup_path(config, new_path):
  if platform.system() == 'Windows':
    dynamic_library_lookup_var = 'PATH'
  elif platform.system() == 'Darwin':
    dynamic_library_lookup_var = 'DYLD_LIBRARY_PATH'
  else:
    dynamic_library_lookup_var = 'LD_LIBRARY_PATH'

  new_ld_library_path = os.path.pathsep.join(
    (new_path, config.environment.get(dynamic_library_lookup_var, '')))
  config.environment[dynamic_library_lookup_var] = new_ld_library_path

# Setup config name.
config.name = 'AddressSanitizer' + config.name_suffix

# Platform-specific default ASAN_OPTIONS for lit tests.
default_asan_opts = ''
if config.host_os == 'Darwin':
  # On Darwin, we default to `abort_on_error=1`, which would make tests run
  # much slower. Let's override this and run lit tests with 'abort_on_error=0'.
  # Also, make sure we do not overwhelm the syslog while testing.
  default_asan_opts = 'abort_on_error=0'
  default_asan_opts += ':log_to_syslog=0'
elif config.android:
  # The same as on Darwin, we default to "abort_on_error=1" which slows down
  # testing. Also, all existing tests are using "not" instead of "not --crash"
  # which does not work for abort()-terminated programs.
  default_asan_opts = 'abort_on_error=0'

if default_asan_opts:
  config.environment['ASAN_OPTIONS'] = default_asan_opts
  default_asan_opts += ':'
config.substitutions.append(('%env_asan_opts=',
                             'env ASAN_OPTIONS=' + default_asan_opts))

# Setup source root.
config.test_source_root = os.path.dirname(__file__)

# There is no libdl on FreeBSD.
if config.host_os != 'FreeBSD':
  libdl_flag = "-ldl"
else:
  libdl_flag = ""

# GCC-ASan doesn't link in all the necessary libraries automatically, so
# we have to do it ourselves.
if config.compiler_id == 'GNU':
  extra_linkflags = ["-pthread", "-lstdc++", libdl_flag]
else:
  extra_linkflags = []

# BFD linker in 64-bit android toolchains fails to find libm.so, which is a
# transitive shared library dependency (via asan runtime).
if config.android:
  extra_linkflags += ["-lm"]

# Setup default compiler flags used with -fsanitize=address option.
# FIXME: Review the set of required flags and check if it can be reduced.
target_cflags = [get_required_attr(config, "target_cflags")] + extra_linkflags
target_cxxflags = config.cxx_mode_flags + target_cflags
clang_asan_static_cflags = (["-fsanitize=address",
                            "-mno-omit-leaf-frame-pointer",
                            "-fno-omit-frame-pointer",
                            "-fno-optimize-sibling-calls"] +
                            config.debug_info_flags + target_cflags)
if config.target_arch == 's390x':
  clang_asan_static_cflags.append("-mbackchain")
clang_asan_static_cxxflags = config.cxx_mode_flags + clang_asan_static_cflags

if config.asan_dynamic:
  clang_asan_cflags = clang_asan_static_cflags + ['-shared-libasan']
  clang_asan_cxxflags = clang_asan_static_cxxflags + ['-shared-libasan']
  config.available_features.add("asan-dynamic-runtime")
else:
  clang_asan_cflags = clang_asan_static_cflags
  clang_asan_cxxflags = clang_asan_static_cxxflags
  config.available_features.add("asan-static-runtime")

asan_lit_source_dir = get_required_attr(config, "asan_lit_source_dir")
if config.android == "1":
  config.available_features.add('android')
  clang_wrapper = os.path.join(asan_lit_source_dir,
                               "android_commands", "android_compile.py") + " "
else:
  config.available_features.add('not-android')
  clang_wrapper = ""

def build_invocation(compile_flags):
  return " " + " ".join([clang_wrapper, config.clang] + compile_flags) + " "

config.substitutions.append( ("%clang ", build_invocation(target_cflags)) )
config.substitutions.append( ("%clangxx ", build_invocation(target_cxxflags)) )
config.substitutions.append( ("%clang_asan ", build_invocation(clang_asan_cflags)) )
config.substitutions.append( ("%clangxx_asan ", build_invocation(clang_asan_cxxflags)) )
config.substitutions.append( ("%shared_libasan", "libclang_rt.asan-%s.so" % config.target_arch))
if config.asan_dynamic:
  config.substitutions.append( ("%clang_asan_static ", build_invocation(clang_asan_static_cflags)) )
  config.substitutions.append( ("%clangxx_asan_static ", build_invocation(clang_asan_static_cxxflags)) )

# Windows-specific tests might also use the clang-cl.exe driver.
if platform.system() == 'Windows':
  clang_cl_asan_cxxflags = ["-fsanitize=address",
                            "-Wno-deprecated-declarations",
                            "-WX",
                            "-D_HAS_EXCEPTIONS=0",
                            "-Zi"] + target_cflags
  if config.asan_dynamic:
    clang_cl_asan_cxxflags.append("-MD")
  clang_invocation = build_invocation(clang_cl_asan_cxxflags)
  clang_cl_invocation = clang_invocation.replace("clang.exe","clang-cl.exe")
  config.substitutions.append( ("%clang_cl_asan ", clang_cl_invocation) )
  base_lib = os.path.join(config.compiler_rt_libdir, "clang_rt.asan%%s-%s.lib" % config.target_arch)
  config.substitutions.append( ("%asan_lib", base_lib % "") )
  config.substitutions.append( ("%asan_cxx_lib", base_lib % "_cxx") )
  config.substitutions.append( ("%asan_dll_thunk", base_lib % "_dll_thunk") )

if platform.system() == 'Windows':
  # Don't use -std=c++11 on Windows, as the driver will detect the appropriate
  # default needed to use with the STL.
  config.substitutions.append(("%stdcxx11 ", ""))
else:
  # Some tests uses C++11 features such as lambdas and need to pass -std=c++11.
  config.substitutions.append(("%stdcxx11 ", "-std=c++11 "))

# FIXME: De-hardcode this path.<commit_msg>[compiler-rt] Fix a broken asan 64-bit test using ld_preload<commit_after># -*- Python -*-

import os
import platform

import lit.formats

def get_required_attr(config, attr_name):
  attr_value = getattr(config, attr_name, None)
  if attr_value == None:
    lit_config.fatal(
      "No attribute %r in test configuration! You may need to run "
      "tests from your build directory or add this attribute "
      "to lit.site.cfg " % attr_name)
  return attr_value

def push_dynamic_library_lookup_path(config, new_path):
  if platform.system() == 'Windows':
    dynamic_library_lookup_var = 'PATH'
  elif platform.system() == 'Darwin':
    dynamic_library_lookup_var = 'DYLD_LIBRARY_PATH'
  else:
    dynamic_library_lookup_var = 'LD_LIBRARY_PATH'

  new_ld_library_path = os.path.pathsep.join(
    (new_path, config.environment.get(dynamic_library_lookup_var, '')))
  config.environment[dynamic_library_lookup_var] = new_ld_library_path

# Setup config name.
config.name = 'AddressSanitizer' + config.name_suffix

# Platform-specific default ASAN_OPTIONS for lit tests.
default_asan_opts = ''
if config.host_os == 'Darwin':
  # On Darwin, we default to `abort_on_error=1`, which would make tests run
  # much slower. Let's override this and run lit tests with 'abort_on_error=0'.
  # Also, make sure we do not overwhelm the syslog while testing.
  default_asan_opts = 'abort_on_error=0'
  default_asan_opts += ':log_to_syslog=0'
elif config.android:
  # The same as on Darwin, we default to "abort_on_error=1" which slows down
  # testing. Also, all existing tests are using "not" instead of "not --crash"
  # which does not work for abort()-terminated programs.
  default_asan_opts = 'abort_on_error=0'

if default_asan_opts:
  config.environment['ASAN_OPTIONS'] = default_asan_opts
  default_asan_opts += ':'
config.substitutions.append(('%env_asan_opts=',
                             'env ASAN_OPTIONS=' + default_asan_opts))

# Setup source root.
config.test_source_root = os.path.dirname(__file__)

# There is no libdl on FreeBSD.
if config.host_os != 'FreeBSD':
  libdl_flag = "-ldl"
else:
  libdl_flag = ""

# GCC-ASan doesn't link in all the necessary libraries automatically, so
# we have to do it ourselves.
if config.compiler_id == 'GNU':
  extra_linkflags = ["-pthread", "-lstdc++", libdl_flag]
else:
  extra_linkflags = []

# BFD linker in 64-bit android toolchains fails to find libm.so, which is a
# transitive shared library dependency (via asan runtime).
if config.android:
  extra_linkflags += ["-lm"]

# Setup default compiler flags used with -fsanitize=address option.
# FIXME: Review the set of required flags and check if it can be reduced.
target_cflags = [get_required_attr(config, "target_cflags")] + extra_linkflags
target_cxxflags = config.cxx_mode_flags + target_cflags
clang_asan_static_cflags = (["-fsanitize=address",
                            "-mno-omit-leaf-frame-pointer",
                            "-fno-omit-frame-pointer",
                            "-fno-optimize-sibling-calls"] +
                            config.debug_info_flags + target_cflags)
if config.target_arch == 's390x':
  clang_asan_static_cflags.append("-mbackchain")
clang_asan_static_cxxflags = config.cxx_mode_flags + clang_asan_static_cflags

if config.asan_dynamic:
  clang_asan_cflags = clang_asan_static_cflags + ['-shared-libasan']
  clang_asan_cxxflags = clang_asan_static_cxxflags + ['-shared-libasan']
  config.available_features.add("asan-dynamic-runtime")
else:
  clang_asan_cflags = clang_asan_static_cflags
  clang_asan_cxxflags = clang_asan_static_cxxflags
  config.available_features.add("asan-static-runtime")

asan_lit_source_dir = get_required_attr(config, "asan_lit_source_dir")
if config.android == "1":
  config.available_features.add('android')
  clang_wrapper = os.path.join(asan_lit_source_dir,
                               "android_commands", "android_compile.py") + " "
else:
  config.available_features.add('not-android')
  clang_wrapper = ""

def build_invocation(compile_flags):
  return " " + " ".join([clang_wrapper, config.clang] + compile_flags) + " "

# Clang driver link 'x86' (i686) architecture to 'i386'.
target_arch = config.target_arch
if (target_arch == "i686"):
  target_arch = "i386"

config.substitutions.append( ("%clang ", build_invocation(target_cflags)) )
config.substitutions.append( ("%clangxx ", build_invocation(target_cxxflags)) )
config.substitutions.append( ("%clang_asan ", build_invocation(clang_asan_cflags)) )
config.substitutions.append( ("%clangxx_asan ", build_invocation(clang_asan_cxxflags)) )
config.substitutions.append( ("%shared_libasan", "libclang_rt.asan-%s.so" % target_arch))
if config.asan_dynamic:
  config.substitutions.append( ("%clang_asan_static ", build_invocation(clang_asan_static_cflags)) )
  config.substitutions.append( ("%clangxx_asan_static ", build_invocation(clang_asan_static_cxxflags)) )

# Windows-specific tests might also use the clang-cl.exe driver.
if platform.system() == 'Windows':
  clang_cl_asan_cxxflags = ["-fsanitize=address",
                            "-Wno-deprecated-declarations",
                            "-WX",
                            "-D_HAS_EXCEPTIONS=0",
                            "-Zi"] + target_cflags
  if config.asan_dynamic:
    clang_cl_asan_cxxflags.append("-MD")
  clang_invocation = build_invocation(clang_cl_asan_cxxflags)
  clang_cl_invocation = clang_invocation.replace("clang.exe","clang-cl.exe")
  config.substitutions.append( ("%clang_cl_asan ", clang_cl_invocation) )
  base_lib = os.path.join(config.compiler_rt_libdir, "clang_rt.asan%%s-%s.lib" % config.target_arch)
  config.substitutions.append( ("%asan_lib", base_lib % "") )
  config.substitutions.append( ("%asan_cxx_lib", base_lib % "_cxx") )
  config.substitutions.append( ("%asan_dll_thunk", base_lib % "_dll_thunk") )

if platform.system() == 'Windows':
  # Don't use -std=c++11 on Windows, as the driver will detect the appropriate
  # default needed to use with the STL.
  config.substitutions.append(("%stdcxx11 ", ""))
else:
  # Some tests uses C++11 features such as lambdas and need to pass -std=c++11.
  config.substitutions.append(("%stdcxx11 ", "-std=c++11 "))

# FIXME: De-hardcode this path.
{'fraction_non_alphanumeric': 0.11647078028077049, 'fraction_numerical': 0.0053868756121449556, 'mean_word_length': 4.227389078498294, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '304220', 'n_tokens_mistral': 4278, 'n_tokens_neox': 4089, 'n_words': 858}