RunClangTidy.cmake 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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" "\n" actual_stdout "${actual_stdout}")
  44. string(REGEX REPLACE "\n+$" "" actual_stdout "${actual_stdout}")
  45. if(NOT actual_stdout STREQUAL expect_stdout)
  46. string(REPLACE "\n" "\n " expect_stdout_formatted " ${expect_stdout}")
  47. string(REPLACE "\n" "\n " actual_stdout_formatted " ${actual_stdout}")
  48. string(APPEND RunClangTidy_TEST_FAILED "Expected stdout:\n${expect_stdout_formatted}\nActual stdout:\n${actual_stdout_formatted}\n")
  49. endif()
  50. function(check_fixit expected fallback_expected actual)
  51. if(EXISTS "${expected}")
  52. set(expect_fixit_file "${expected}")
  53. else()
  54. set(expect_fixit_file "${fallback_expected}")
  55. endif()
  56. file(READ "${expect_fixit_file}" expect_fixit)
  57. file(READ "${actual}" actual_fixit)
  58. if(NOT expect_fixit STREQUAL actual_fixit)
  59. string(REPLACE "\n" "\n " expect_fixit_formatted " ${expect_fixit}")
  60. string(REPLACE "\n" "\n " actual_fixit_formatted " ${actual_fixit}")
  61. string(APPEND RunClangTidy_TEST_FAILED "Expected fixit for ${actual}:\n${expect_fixit_formatted}\nActual fixit:\n${actual_fixit_formatted}\n")
  62. set(RunClangTidy_TEST_FAILED "${RunClangTidy_TEST_FAILED}" PARENT_SCOPE)
  63. endif()
  64. endfunction()
  65. check_fixit(
  66. "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}-fixit.cxx"
  67. "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}.cxx"
  68. "${source_file}"
  69. )
  70. foreach(header_file IN LISTS header_files)
  71. if(NOT header_file MATCHES "-fixit\\.h\$")
  72. string(REGEX REPLACE "\\.h\$" "-fixit.h" header_fixit "${header_file}")
  73. check_fixit(
  74. "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}/${header_fixit}"
  75. "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}/${header_file}"
  76. "${RunClangTidy_BINARY_DIR}/${CHECK_NAME}/${header_file}"
  77. )
  78. endif()
  79. endforeach()
  80. if(RunClangTidy_TEST_FAILED)
  81. string(REPLACE ";" " " command_formatted "${command}")
  82. message(FATAL_ERROR "Command:\n ${command_formatted}\n${RunClangTidy_TEST_FAILED}")
  83. endif()