|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +set(config_arg) |
| 16 | +if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}.clang-tidy") |
| 17 | + set(config_arg "--config-file=${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}.clang-tidy") |
| 18 | +endif() |
| 19 | + |
| 20 | +if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}-stdout.txt") |
| 21 | + file(READ "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}-stdout.txt" expect_stdout) |
| 22 | + string(REGEX REPLACE "\n+$" "" expect_stdout "${expect_stdout}") |
| 23 | +else() |
| 24 | + set(expect_stdout "") |
| 25 | +endif() |
| 26 | + |
| 27 | +set(source_file "${RunClangTidy_BINARY_DIR}/${CHECK_NAME}.cc") |
| 28 | +configure_file("${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}.cc" "${source_file}" COPYONLY) |
| 29 | + |
| 30 | +file(GLOB header_files RELATIVE "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}" "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}/*") |
| 31 | +file(REMOVE_RECURSE "${RunClangTiy_BINARY_DIR}/${CHECK_NAME}") |
| 32 | +foreach(header_file IN LISTS header_files) |
| 33 | + if(NOT header_file MATCHES "-fixit\\.h\$") |
| 34 | + file(MAKE_DIRECTORY "${RunClangTidy_BINARY_DIR}/${CHECK_NAME}") |
| 35 | + configure_file("${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}/${header_file}" "${RunClangTidy_BINARY_DIR}/${CHECK_NAME}/${header_file}" COPYONLY) |
| 36 | + endif() |
| 37 | +endforeach() |
| 38 | + |
| 39 | +set(command |
| 40 | + "${CLANG_TIDY_COMMAND}" |
| 41 | + "--load=${CLANG_TIDY_MODULE}" |
| 42 | + "--checks=-*,${CHECK_NAME}" |
| 43 | + "--fix" |
| 44 | + "--format-style=file" |
| 45 | + "--header-filter=/${CHECK_NAME}/" |
| 46 | + ${config_arg} |
| 47 | + "${source_file}" |
| 48 | + -- |
| 49 | + ) |
| 50 | +execute_process( |
| 51 | + COMMAND ${command} |
| 52 | + RESULT_VARIABLE result |
| 53 | + OUTPUT_VARIABLE actual_stdout |
| 54 | + ERROR_VARIABLE actual_stderr |
| 55 | + ) |
| 56 | +string(REPLACE "${RunClangTidy_BINARY_DIR}/" "" actual_stdout "${actual_stdout}") |
| 57 | + |
| 58 | +set(RunClangTidy_TEST_FAILED) |
| 59 | + |
| 60 | +if(NOT result EQUAL 0) |
| 61 | + string(APPEND RunClangTidy_TEST_FAILED "Expected result: 0, actual result: ${result}\n") |
| 62 | +endif() |
| 63 | + |
| 64 | +string(REGEX REPLACE "\n+$" "" actual_stdout "${actual_stdout}") |
| 65 | +if(NOT actual_stdout STREQUAL expect_stdout) |
| 66 | + string(REPLACE "\n" "\n " expect_stdout_formatted " ${expect_stdout}") |
| 67 | + string(REPLACE "\n" "\n " actual_stdout_formatted " ${actual_stdout}") |
| 68 | + string(APPEND RunClangTidy_TEST_FAILED "Expected stdout:\n${expect_stdout_formatted}\nActual stdout:\n${actual_stdout_formatted}\n") |
| 69 | +endif() |
| 70 | + |
| 71 | +function(check_fixit expected fallback_expected actual) |
| 72 | + if(EXISTS "${expected}") |
| 73 | + set(expect_fixit_file "${expected}") |
| 74 | + else() |
| 75 | + set(expect_fixit_file "${fallback_expected}") |
| 76 | + endif() |
| 77 | + file(READ "${expect_fixit_file}" expect_fixit) |
| 78 | + file(READ "${actual}" actual_fixit) |
| 79 | + if(NOT expect_fixit STREQUAL actual_fixit) |
| 80 | + string(REPLACE "\n" "\n " expect_fixit_formatted " ${expect_fixit}") |
| 81 | + string(REPLACE "\n" "\n " actual_fixit_formatted " ${actual_fixit}") |
| 82 | + string(APPEND RunClangTidy_TEST_FAILED "Expected fixit for ${actual}:\n${expect_fixit_formatted}\nActual fixit:\n${actual_fixit_formatted}\n") |
| 83 | + set(RunClangTidy_TEST_FAILED "${RunClangTidy_TEST_FAILED}" PARENT_SCOPE) |
| 84 | + endif() |
| 85 | +endfunction() |
| 86 | + |
| 87 | +check_fixit( |
| 88 | + "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}-fixit.cc" |
| 89 | + "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}.cc" |
| 90 | + "${source_file}" |
| 91 | + ) |
| 92 | + |
| 93 | +foreach(header_file IN LISTS header_files) |
| 94 | + if(NOT header_file MATCHES "-fixit\\.h\$") |
| 95 | + string(REGEX REPLACE "\\.h\$" "-fixit.h" header_fixit "${header_file}") |
| 96 | + check_fixit( |
| 97 | + "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}/${header_fixit}" |
| 98 | + "${CMAKE_CURRENT_LIST_DIR}/${CHECK_NAME}/${header_file}" |
| 99 | + "${RunClangTidy_BINARY_DIR}/${CHECK_NAME}/${header_file}" |
| 100 | + ) |
| 101 | + endif() |
| 102 | +endforeach() |
| 103 | + |
| 104 | +if(RunClangTidy_TEST_FAILED) |
| 105 | + string(REPLACE ";" " " command_formatted "${command}") |
| 106 | + message(FATAL_ERROR "Command:\n ${command_formatted}\n${RunClangTidy_TEST_FAILED}") |
| 107 | +endif() |
0 commit comments