Skip to content

Commit

Permalink
feat: add clang-tidy for C
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Feb 19, 2022
1 parent eee4115 commit 8480623
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/StaticAnalyzers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 8480623

Please sign in to comment.