<commit_before>#=========================================================================================
# Enabling code coverage
#=========================================================================================
#
#   1. For each target that you want to enable coverage reporting for
#      invoke enable_coverage(target_name)
#
#   2. Automatically create coverage generation targets with:
#     create_coverage_targets(target_base_name run_target bin_dir source_dir output_path)
#     where:
#       targer_base_name -- will create *_html *_xml and * which makes both
#       run_target -- name of target that runs the tests
#       bin_dir -- root directory of unit test binaries
#       source_dir -- root directory of source for which to make reports
#       output_path -- where reports should be written to
#
#   3. when building, invoke cmake with -DCOV=1 option
#

option(COV "Enable code coverage reporting for unit tests (if possible)." OFF)

function(configure_coverage_gcc)
  set(COVERAGE_COMPILE_FLAGS "-g -O0 -coverage -fprofile-generate -ftest-coverage" PARENT_SCOPE)
  set(COVERAGE_LINK_FLAGS "-coverage -fprofile-generate -ftest-coverage" PARENT_SCOPE)
  find_program(COVERAGE_GCOV_PATH gcov)
  find_program(COVERAGE_GCOVR_PATH gcovr PATHS ${CMAKE_CURRENT_LIST_DIR})
  if (NOT COVERAGE_GCOV_PATH)
    message(WARNING "Cannot enable coverage reporting; gcov not found.")
    set(COVERAGE_ENABLED OFF PARENT_SCOPE)
  elseif (NOT COVERAGE_GCOVR_PATH)
    message(WARNING "Cannot enable coverage reporting; gocvr not found.")
    set(COVERAGE_ENABLED OFF PARENT_SCOPE)
  else ()
    set(COVERAGE_LIBRARIES gcov PARENT_SCOPE)
    message(STATUS "Enabled coverage reporting for GCC")
    set(COVERAGE_ENABLED ON PARENT_SCOPE)
  endif ()
endfunction()

function(configure_coverage_llvm)
  set(COVERAGE_COMPILE_FLAGS "-g -O0 -fprofile-instr-generate -fcoverage-mapping" PARENT_SCOPE)
  set(COVERAGE_LINK_FLAGS "-g -O0 -fprofile-instr-generate -fcoverage-mapping" PARENT_SCOPE)
  find_program(COVERAGE_LLVM_COV_PATH NAMES "llvm-cov-mp-4.0" "llvm-cov")
  find_program(COVERAGE_LLVM_PROF_PATH NAMES "llvm-profdata-mp-4.0" "llvm-profdata")
  if (NOT COVERAGE_LLVM_PROF_PATH OR NOT COVERAGE_LLVM_COV_PATH)
    message(WARNING "Cannot enable coverage reporting; llvm-cov and/or llvm-profdata not found.")
    set(COVERAGE_ENABLED OFF PARENT_SCOPE)
  else ()
    message(STATUS "Enabled coverage reporting for Clang")
    set(COVERAGE_ENABLED ON PARENT_SCOPE)
  endif ()
endfunction()

if (${COV})
  if (${CMAKE_CXX_COMPILER_ID} STREQUAL Clang OR ${CMAKE_CXX_COMPILER_ID} STREQUAL AppleClang)
    configure_coverage_llvm()
  elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
    configure_coverage_gcc()
  else ()
    message(WARNING "Cannot enable coverage reporting. Unsupported compiler: (${CMAKE_CXX_COMPILER_ID}).")
    set(COVERAGE_ENABLED OFF)
  endif ()

  if (${COVERAGE_ENABLED})
    if (NOT CMAKE_BUILD_TYPE)
      message(STATUS "Coverage requested but build type was not set. Using Debug mode.")
      set(CMAKE_BUILD_TYPE "Debug")
    elseif (CMAKE_BUILD_TYPE STREQUAL "Release")
      message(WARNING "Cannot provide coverage reporting with Release build. Disabling coverage.")
      set(COVERAGE_ENABLED OFF)
    endif ()
  endif ()

endif ()

function(enable_coverage_flags coverage_target)
  if (${COVERAGE_ENABLED})
    set_target_properties(${coverage_target} PROPERTIES
            COMPILE_FLAGS ${COVERAGE_COMPILE_FLAGS})
    set_target_properties(${coverage_target} PROPERTIES
            LINK_FLAGS ${COVERAGE_LINK_FLAGS})
  else ()
    message(STATUS "Cannot enable coverage flags for target: ${coverage_target}. Code coverage feature not available.")
  endif ()
endfunction()

function(enable_coverage coverage_target)
  if (${COVERAGE_ENABLED})
    set_target_properties(${coverage_target} PROPERTIES
      COMPILE_FLAGS ${COVERAGE_COMPILE_FLAGS})
    set_target_properties(${coverage_target} PROPERTIES
      LINK_FLAGS ${COVERAGE_LINK_FLAGS})
    target_link_libraries(${coverage_target} ${COVERAGE_LIBRARIES})
  else ()
    message(STATUS "Cannot enable coverage for target: ${coverage_target}. Code coverage feature not available.")
  endif ()
endfunction()

function(create_coverage_targets target_base_name run_target bin_dir source_dir output_path excl)
  if (${COVERAGE_ENABLED})
    file(MAKE_DIRECTORY ${output_path})
    separate_arguments(excl)
    if (${CMAKE_CXX_COMPILER_ID} STREQUAL Clang OR ${CMAKE_CXX_COMPILER_ID} STREQUAL AppleClang)
      add_custom_target(${target_base_name}_html
        COMMAND ${COVERAGE_LLVM_PROF_PATH}
        "merge" "default.profraw" "-o" "default.profdata" ";" ${COVERAGE_LLVM_COV_PATH}
        "show" "${bin_dir}/bin/unit_tests" "-instr-profile=default.profdata" "-format=html" ">${output_path}/coverage.html"
        DEPENDS ${run_target})
      # ";" ${COVERAGE_LLVM_COV_PATH} "report" "${bin_dir}/lib"
      add_custom_target(${target_base_name} DEPENDS ${target_base_name}_html)
    elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
      add_custom_target(${target_base_name}_xml
        COMMAND ${COVERAGE_GCOVR_PATH} "--xml" "--exclude-unreachable-branches" "-s"
        ${excl}
        "-r" "${source_dir}"
        "-o" "${output_path}/coverage.xml"
        "${bin_dir}"
        DEPENDS ${run_target})<commit_msg>fewer warnings related to coverage<commit_after>#=========================================================================================
# Enabling code coverage
#=========================================================================================
#
#   1. For each target that you want to enable coverage reporting for
#      invoke enable_coverage(target_name)
#
#   2. Automatically create coverage generation targets with:
#     create_coverage_targets(target_base_name run_target bin_dir source_dir output_path)
#     where:
#       targer_base_name -- will create *_html *_xml and * which makes both
#       run_target -- name of target that runs the tests
#       bin_dir -- root directory of unit test binaries
#       source_dir -- root directory of source for which to make reports
#       output_path -- where reports should be written to
#
#   3. when building, invoke cmake with -DCOV=1 option
#

option(COV "Enable code coverage reporting for unit tests (if possible)." OFF)

function(configure_coverage_gcc)
  set(COVERAGE_COMPILE_FLAGS "-g -O0 -coverage -fprofile-generate -ftest-coverage" PARENT_SCOPE)
  set(COVERAGE_LINK_FLAGS "-coverage -fprofile-generate -ftest-coverage" PARENT_SCOPE)
  find_program(COVERAGE_GCOV_PATH gcov)
  find_program(COVERAGE_GCOVR_PATH gcovr PATHS ${CMAKE_CURRENT_LIST_DIR})
  if (NOT COVERAGE_GCOV_PATH)
    message(WARNING "Cannot enable coverage reporting; gcov not found.")
    set(COVERAGE_ENABLED OFF PARENT_SCOPE)
  elseif (NOT COVERAGE_GCOVR_PATH)
    message(WARNING "Cannot enable coverage reporting; gocvr not found.")
    set(COVERAGE_ENABLED OFF PARENT_SCOPE)
  else ()
    set(COVERAGE_LIBRARIES gcov PARENT_SCOPE)
    message(STATUS "Enabled coverage reporting for GCC")
    set(COVERAGE_ENABLED ON PARENT_SCOPE)
  endif ()
endfunction()

function(configure_coverage_llvm)
  set(COVERAGE_COMPILE_FLAGS "-g -O0 -fprofile-instr-generate -fcoverage-mapping" PARENT_SCOPE)
  set(COVERAGE_LINK_FLAGS "-g -O0 -fprofile-instr-generate -fcoverage-mapping" PARENT_SCOPE)
  find_program(COVERAGE_LLVM_COV_PATH NAMES "llvm-cov-mp-4.0" "llvm-cov")
  find_program(COVERAGE_LLVM_PROF_PATH NAMES "llvm-profdata-mp-4.0" "llvm-profdata")
  if (NOT COVERAGE_LLVM_PROF_PATH OR NOT COVERAGE_LLVM_COV_PATH)
    message(WARNING "Cannot enable coverage reporting; llvm-cov and/or llvm-profdata not found.")
    set(COVERAGE_ENABLED OFF PARENT_SCOPE)
  else ()
    message(STATUS "Enabled coverage reporting for Clang")
    set(COVERAGE_ENABLED ON PARENT_SCOPE)
  endif ()
endfunction()

if (${COV})
  if (${CMAKE_CXX_COMPILER_ID} STREQUAL Clang OR ${CMAKE_CXX_COMPILER_ID} STREQUAL AppleClang)
    configure_coverage_llvm()
  elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
    configure_coverage_gcc()
  else ()
    message(WARNING "Cannot enable coverage reporting. Unsupported compiler: (${CMAKE_CXX_COMPILER_ID}).")
    set(COVERAGE_ENABLED OFF)
  endif ()

  if (${COVERAGE_ENABLED})
    if (NOT CMAKE_BUILD_TYPE)
      message(STATUS "Coverage requested but build type was not set. Using Debug mode.")
      set(CMAKE_BUILD_TYPE "Debug")
    elseif (CMAKE_BUILD_TYPE STREQUAL "Release")
      message(WARNING "Cannot provide coverage reporting with Release build. Disabling coverage.")
      set(COVERAGE_ENABLED OFF)
    endif ()
  endif ()
endif ()

function(enable_coverage_flags coverage_target)
  if (${COVERAGE_ENABLED})
    set_target_properties(${coverage_target} PROPERTIES
            COMPILE_FLAGS ${COVERAGE_COMPILE_FLAGS})
    set_target_properties(${coverage_target} PROPERTIES
            LINK_FLAGS ${COVERAGE_LINK_FLAGS})
  endif ()
endfunction()

function(enable_coverage coverage_target)
  if (${COVERAGE_ENABLED})
    set_target_properties(${coverage_target} PROPERTIES
      COMPILE_FLAGS ${COVERAGE_COMPILE_FLAGS})
    set_target_properties(${coverage_target} PROPERTIES
      LINK_FLAGS ${COVERAGE_LINK_FLAGS})
    target_link_libraries(${coverage_target} ${COVERAGE_LIBRARIES})
  endif ()
endfunction()

function(create_coverage_targets target_base_name run_target bin_dir source_dir output_path excl)
  if (${COVERAGE_ENABLED})
    file(MAKE_DIRECTORY ${output_path})
    separate_arguments(excl)
    if (${CMAKE_CXX_COMPILER_ID} STREQUAL Clang OR ${CMAKE_CXX_COMPILER_ID} STREQUAL AppleClang)
      add_custom_target(${target_base_name}_html
        COMMAND ${COVERAGE_LLVM_PROF_PATH}
        "merge" "default.profraw" "-o" "default.profdata" ";" ${COVERAGE_LLVM_COV_PATH}
        "show" "${bin_dir}/bin/unit_tests" "-instr-profile=default.profdata" "-format=html" ">${output_path}/coverage.html"
        DEPENDS ${run_target})
      # ";" ${COVERAGE_LLVM_COV_PATH} "report" "${bin_dir}/lib"
      add_custom_target(${target_base_name} DEPENDS ${target_base_name}_html)
    elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL GNU)
      add_custom_target(${target_base_name}_xml
        COMMAND ${COVERAGE_GCOVR_PATH} "--xml" "--exclude-unreachable-branches" "-s"
        ${excl}
        "-r" "${source_dir}"
        "-o" "${output_path}/coverage.xml"
        "${bin_dir}"
        DEPENDS ${run_target})
{'fraction_non_alphanumeric': 0.12900115696104897, 'fraction_numerical': 0.0021210952564596993, 'mean_word_length': 4.837366347777152, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, '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': '3728069', 'n_tokens_mistral': 3493, 'n_tokens_neox': 3389, 'n_words': 762}