RunClangTidy.cmake 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. set(config_arg)
  2. if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}.clang-tidy")
  3. set(config_arg "--config-file=${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}.clang-tidy")
  4. endif()
  5. if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}-stdout.txt")
  6. file(READ "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}-stdout.txt" expect_stdout)
  7. string(REGEX REPLACE "\n+$" "" expect_stdout "${expect_stdout}")
  8. else()
  9. set(expect_stdout "")
  10. endif()
  11. set(source_file "${RunClangTidy_BINARY_DIR}/${CHECK_NAME}.cxx")
  12. configure_file("${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}.cxx" "${source_file}" COPYONLY)
  13. file(GLOB header_files RELATIVE "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}" "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}/*")
  14. file(REMOVE_RECURSE "${RunClangTiy_BINARY_DIR}/${CHECK_NAME}")
  15. foreach(header_file IN LISTS header_files)
  16. if(NOT header_file MATCHES "-fixit\\.h\$")
  17. file(MAKE_DIRECTORY "${RunClangTidy_BINARY_DIR}/${CHECK_NAME}")
  18. configure_file("${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}/${header_file}" "${RunClangTidy_BINARY_DIR}/${CHECK_NAME}/${header_file}" COPYONLY)
  19. endif()
  20. endforeach()
  21. set(command
  22. "${CLANG_TIDY_COMMAND}"
  23. "--load=${CLANG_TIDY_MODULE}"
  24. "--checks=-*,${CHECK_NAME}"
  25. "--fix"
  26. "--format-style=file"
  27. "--header-filter=/${CHECK_NAME}/"
  28. ${config_arg}
  29. "${source_file}"
  30. --
  31. )
  32. execute_process(
  33. COMMAND ${command}
  34. RESULT_VARIABLE result
  35. OUTPUT_VARIABLE actual_stdout
  36. ERROR_VARIABLE actual_stderr
  37. )
  38. string(REPLACE "${RunClangTidy_BINARY_DIR}/" "" actual_stdout "${actual_stdout}")
  39. set(RunClangTidy_TEST_FAILED)
  40. if(NOT result EQUAL 0)
  41. string(APPEND RunClangTidy_TEST_FAILED "Expected result: 0, actual result: ${result}\n")
  42. endif()
  43. string(REGEX REPLACE "\n+$" "" actual_stdout "${actual_stdout}")
  44. if(NOT actual_stdout STREQUAL expect_stdout)
  45. string(REPLACE "\n" "\n " expect_stdout_formatted " ${expect_stdout}")
  46. string(REPLACE "\n" "\n " actual_stdout_formatted " ${actual_stdout}")
  47. string(APPEND RunClangTidy_TEST_FAILED "Expected stdout:\n${expect_stdout_formatted}\nActual stdout:\n${actual_stdout_formatted}\n")
  48. endif()
  49. function(check_fixit expected fallback_expected actual)
  50. if(EXISTS "${expected}")
  51. set(expect_fixit_file "${expected}")
  52. else()
  53. set(expect_fixit_file "${fallback_expected}")
  54. endif()
  55. file(READ "${expect_fixit_file}" expect_fixit)
  56. file(READ "${actual}" actual_fixit)
  57. if(NOT expect_fixit STREQUAL actual_fixit)
  58. string(REPLACE "\n" "\n " expect_fixit_formatted " ${expect_fixit}")
  59. string(REPLACE "\n" "\n " actual_fixit_formatted " ${actual_fixit}")
  60. string(APPEND RunClangTidy_TEST_FAILED "Expected fixit for ${actual}:\n${expect_fixit_formatted}\nActual fixit:\n${actual_fixit_formatted}\n")
  61. set(RunClangTidy_TEST_FAILED "${RunClangTidy_TEST_FAILED}" PARENT_SCOPE)
  62. endif()
  63. endfunction()
  64. check_fixit(
  65. "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}-fixit.cxx"
  66. "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}.cxx"
  67. "${source_file}"
  68. )
  69. foreach(header_file IN LISTS header_files)
  70. if(NOT header_file MATCHES "-fixit\\.h\$")
  71. string(REGEX REPLACE "\\.h\$" "-fixit.h" header_fixit "${header_file}")
  72. check_fixit(
  73. "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}/${header_fixit}"
  74. "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}/${header_file}"
  75. "${RunClangTidy_BINARY_DIR}/${CHECK_NAME}/${header_file}"
  76. )
  77. endif()
  78. endforeach()
  79. if(RunClangTidy_TEST_FAILED)
  80. string(REPLACE ";" " " command_formatted "${command}")
  81. message(FATAL_ERROR "Command:\n ${command_formatted}\n${RunClangTidy_TEST_FAILED}")
  82. endif()