From 848062302fe76a168b43622bec9064ecb5047cbb Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Fri, 18 Feb 2022 23:19:46 -0800 Subject: [PATCH] feat: add clang-tidy for C --- src/StaticAnalyzers.cmake | 43 +++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/StaticAnalyzers.cmake b/src/StaticAnalyzers.cmake index ae15e080..4ec3f275 100644 --- a/src/StaticAnalyzers.cmake +++ b/src/StaticAnalyzers.cmake @@ -43,18 +43,36 @@ endmacro() macro(enable_clang_tidy) find_program(CLANGTIDY clang-tidy) if(CLANGTIDY) - if(NOT - CMAKE_CXX_COMPILER_ID - MATCHES - ".*Clang" + + # clang-tidy only works with clang when PCH is enabled + if((NOT + CMAKE_CXX_COMPILER_ID + MATCHES + ".*Clang" + OR (NOT + CMAKE_C_COMPILER_ID + MATCHES + ".*Clang" + ) + ) AND ${ProjectOptions_ENABLE_PCH}) message( SEND_ERROR "clang-tidy cannot be enabled with non-clang compiler and PCH, clang-tidy fails to handle gcc's PCH file") endif() + # construct the clang-tidy command line set(CMAKE_CXX_CLANG_TIDY ${CLANGTIDY} -extra-arg=-Wno-unknown-warning-option) - # set standard + + # set warnings as errors + if(WARNINGS_AS_ERRORS) + list(APPEND CMAKE_CXX_CLANG_TIDY -warnings-as-errors=*) + endif() + + # C clang-tidy + set(CMAKE_C_CLANG_TIDY ${CMAKE_C_CLANG_TIDY}) + + # set C++ standard if(NOT "${CMAKE_CXX_STANDARD}" STREQUAL @@ -65,10 +83,19 @@ macro(enable_clang_tidy) set(CMAKE_CXX_CLANG_TIDY ${CMAKE_CXX_CLANG_TIDY} -extra-arg=-std=c++${CMAKE_CXX_STANDARD}) endif() endif() - # set warnings as errors - if(WARNINGS_AS_ERRORS) - list(APPEND CMAKE_CXX_CLANG_TIDY -warnings-as-errors=*) + + # set C standard + if(NOT + "${CMAKE_C_STANDARD}" + STREQUAL + "") + if("${CMAKE_C_CLANG_TIDY_DRIVER_MODE}" STREQUAL "cl") + set(CMAKE_C_CLANG_TIDY ${CMAKE_C_CLANG_TIDY} -extra-arg=/std:c${CMAKE_C_STANDARD}) + else() + set(CMAKE_C_CLANG_TIDY ${CMAKE_C_CLANG_TIDY} -extra-arg=-std=c${CMAKE_C_STANDARD}) + endif() endif() + else() message(${WARNING_MESSAGE} "clang-tidy requested but executable not found") endif()