RunClangTidy.cmake 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. set(command
  14. "${CLANG_TIDY_COMMAND}"
  15. "--load=${CLANG_TIDY_MODULE}"
  16. "--checks=-*,${CHECK_NAME}"
  17. "--fix"
  18. "--format-style=file"
  19. ${config_arg}
  20. "${source_file}"
  21. --
  22. )
  23. execute_process(
  24. COMMAND ${command}
  25. RESULT_VARIABLE result
  26. OUTPUT_VARIABLE actual_stdout
  27. ERROR_VARIABLE actual_stderr
  28. )
  29. string(REPLACE "${RunClangTidy_BINARY_DIR}/" "" actual_stdout "${actual_stdout}")
  30. set(RunClangTidy_TEST_FAILED)
  31. if(NOT result EQUAL 0)
  32. string(APPEND RunClangTidy_TEST_FAILED "Expected result: 0, actual result: ${result}\n")
  33. endif()
  34. string(REGEX REPLACE "\n+$" "" actual_stdout "${actual_stdout}")
  35. if(NOT actual_stdout STREQUAL expect_stdout)
  36. string(REPLACE "\n" "\n " expect_stdout_formatted " ${expect_stdout}")
  37. string(REPLACE "\n" "\n " actual_stdout_formatted " ${actual_stdout}")
  38. string(APPEND RunClangTidy_TEST_FAILED "Expected stdout:\n${expect_stdout_formatted}\nActual stdout:\n${actual_stdout_formatted}\n")
  39. endif()
  40. if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}-fixit.cxx")
  41. set(expect_fixit_file "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}-fixit.cxx")
  42. else()
  43. set(expect_fixit_file "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}.cxx")
  44. endif()
  45. file(READ "${expect_fixit_file}" expect_fixit)
  46. file(READ "${source_file}" actual_fixit)
  47. if(NOT expect_fixit STREQUAL actual_fixit)
  48. string(REPLACE "\n" "\n " expect_fixit_formatted " ${expect_fixit}")
  49. string(REPLACE "\n" "\n " actual_fixit_formatted " ${actual_fixit}")
  50. string(APPEND RunClangTidy_TEST_FAILED "Expected fixit:\n${expect_fixit_formatted}\nActual fixit:\n${actual_fixit_formatted}\n")
  51. endif()
  52. if(RunClangTidy_TEST_FAILED)
  53. string(REPLACE ";" " " command_formatted "${command}")
  54. message(FATAL_ERROR "Command:\n ${command_formatted}\n${RunClangTidy_TEST_FAILED}")
  55. endif()