Skip to content

Commit deb2443

Browse files
committed
Added a first version for clang tidy integration
1 parent 40223c9 commit deb2443

6 files changed

+72
-9
lines changed

.clang-tidy

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ Checks: '*,
2323
-hicpp-no-array-decay,
2424
-modernize-pass-by-value,
2525
-cppcoreguidelines-pro-bounds-constant-array-index,
26-
-hicpp-signed-bitwise
26+
-hicpp-signed-bitwise,
27+
-llvmlibc-implementation-in-namespace,
28+
-llvmlibc-restrict-system-libc-headers
2729
'
2830
WarningsAsErrors: '*'
2931
HeaderFilterRegex: 'src/*.hpp'

CMakeLists.txt

+33-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ cpr_option(CPR_ENABLE_SSL "Enables or disables the SSL backend. Required to perf
3939
cpr_option(CPR_FORCE_OPENSSL_BACKEND "Force to use the OpenSSL backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
4040
cpr_option(CPR_FORCE_WINSSL_BACKEND "Force to use the WinSSL backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
4141
cpr_option(CPR_FORCE_DARWINSSL_BACKEND "Force to use the DarwinSSL backend. If CPR_FORCE_OPENSSL_BACKEND, CPR_FORCE_DARWINSSL_BACKEND, and CPR_FORCE_WINSSL_BACKEND are set to to OFF, cpr will try to automatically detect the best available SSL backend (WinSSL - Windows, OpenSSL - Linux, DarwinSSL - Mac ...)." OFF)
42+
cpr_option(CPR_ENABLE_LINTING "Set to ON to enable clang linting. If enabled, CPR_STATIC_ANALYZE has to be disabled." OFF)
43+
cpr_option(CPR_STATIC_ANALYZE "Set to ON to enable the GCC 10 static analysis. If enabled, CPR_ENABLE_LINTING has to be disabled." OFF)
4244
cpr_option(CPR_BUILD_TESTS "Set to ON to build cpr tests." ON)
4345
cpr_option(CPR_BUILD_TESTS_SSL "Set to ON to build cpr ssl tests" ${CPR_BUILD_TESTS})
4446
message(STATUS "=======================================================")
@@ -47,7 +49,20 @@ include(GNUInstallDirs)
4749
include(FetchContent)
4850
include(cmake/code_coverage.cmake)
4951
include(cmake/sanitizer.cmake)
50-
include(cmake/gcc_analyze.cmake)
52+
include(cmake/clear_variable.cmake)
53+
54+
# Linting and Static Analyze
55+
if(CPR_ENABLE_LINTING AND CPR_STATIC_ANALYZE)
56+
message(FATAL_ERROR "Linting and the GCC static analysis can not be enabled at the same time! Disable either CPR_STATIC_ANALYZE or CPR_ENABLE_LINTING.")
57+
endif()
58+
if(CPR_ENABLE_LINTING)
59+
include(cmake/clang-tidy.cmake)
60+
endif()
61+
62+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
63+
else()
64+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Werror")
65+
endif()
5166

5267
# SSL
5368
if(CPR_ENABLE_SSL)
@@ -184,12 +199,17 @@ else()
184199
set(CMAKE_USE_SECTRANSP ON CACHE INTERNAL "" FORCE)
185200
endif()
186201

202+
# Disable linting for curl
203+
clear_variable(DESTINATION CMAKE_CXX_CLANG_TIDY BACKUP CMAKE_CXX_CLANG_TIDY_BKP)
204+
187205
FetchContent_Declare(curl
188206
URL https://github.com/curl/curl/releases/download/curl-7_75_0/curl-7.75.0.tar.xz
189207
URL_HASH SHA256=fe0c49d8468249000bda75bcfdf9e30ff7e9a86d35f1a21f428d79c389d55675 # the file hash for curl-7.75.0.tar.xz
190208
USES_TERMINAL_DOWNLOAD TRUE) # <---- This is needed only for Ninja to show download progress
191209
FetchContent_MakeAvailable(curl)
192210

211+
restore_variable(DESTINATION CMAKE_CXX_CLANG_TIDY BACKUP CMAKE_CXX_CLANG_TIDY_BKP)
212+
193213
add_library(curl_int INTERFACE)
194214
target_link_libraries(curl_int INTERFACE libcurl)
195215
target_include_directories(curl_int INTERFACE ${curl_SOURCE_DIR}/include ${curl_BINARY_DIR}/include/curl)
@@ -216,11 +236,17 @@ if(CPR_BUILD_TESTS)
216236
# does.
217237
set(gtest_force_shared_crt ON CACHE BOOL "Force gtest to use the shared c runtime")
218238
endif()
239+
240+
# Disable linting for google test
241+
clear_variable(DESTINATION CMAKE_CXX_CLANG_TIDY BACKUP CMAKE_CXX_CLANG_TIDY_BKP)
242+
219243
FetchContent_Declare(googletest
220244
URL https://github.com/google/googletest/archive/release-1.10.0.tar.gz
221245
URL_HASH SHA256=9dc9157a9a1551ec7a7e43daea9a694a0bb5fb8bec81235d8a1e6ef64c716dcb # the file hash for release-1.10.0.tar.gz
222246
USES_TERMINAL_DOWNLOAD TRUE) # <---- This is needed only for Ninja to show download progress
223247
FetchContent_MakeAvailable(googletest)
248+
249+
restore_variable(DESTINATION CMAKE_CXX_CLANG_TIDY BACKUP CMAKE_CXX_CLANG_TIDY_BKP)
224250

225251
add_library(gtest_int INTERFACE)
226252
target_link_libraries(gtest_int INTERFACE gtest)
@@ -253,6 +279,9 @@ if(CPR_BUILD_TESTS)
253279
set(ENABLE_SSL_TESTS OFF CACHE INTERNAL "")
254280
endif()
255281

282+
# Disable linting for mongoose
283+
clear_variable(DESTINATION CMAKE_CXX_CLANG_TIDY BACKUP CMAKE_CXX_CLANG_TIDY_BKP)
284+
256285
FetchContent_Declare(mongoose
257286
URL https://github.com/cesanta/mongoose/archive/6.18.tar.gz
258287
URL_HASH SHA256=f5c10346abc9c72f7cac7885d853ca064fb09aad57580433941a8fd7a3543769 # the hash for 6.18.tar.gz
@@ -268,13 +297,15 @@ if(CPR_BUILD_TESTS)
268297
endif()
269298
# Group under the "external" project folder in IDEs such as Visual Studio.
270299
set_property(TARGET mongoose PROPERTY FOLDER "external")
300+
restore_variable(DESTINATION CMAKE_CXX_CLANG_TIDY BACKUP CMAKE_CXX_CLANG_TIDY_BKP)
271301
endif()
272302

273303

274304
add_subdirectory(cpr)
275305
add_subdirectory(include)
276306

277-
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND CPR_BUILD_TESTS)
307+
278308
enable_testing()
279309
add_subdirectory(test)
310+
restore_variable(DESTINATION CMAKE_CXX_CLANG_TIDY BACKUP CMAKE_CXX_CLANG_TIDY_BKP)
280311
endif()

cmake/clang-tidy.cmake

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
2+
find_program(CLANG_TIDY_EXECUTABLE NAMES clang-tidy-7 clang-tidy)
3+
mark_as_advanced(CLANG_TIDY_EXECUTABLE)
4+
5+
if (${CLANG_TIDY_EXECUTABLE})
6+
message(FATAL_ERROR "Clang-tidy not found")
7+
else()
8+
message(STATUS "Enabling clang-tidy")
9+
set(CMAKE_CXX_CLANG_TIDY "${CLANG_TIDY_EXECUTABLE};-warnings-as-errors=*")
10+
endif()
11+
else()
12+
message(FATAL_ERROR "Clang-tidy is not supported when building for windows")
13+
endif()

cmake/clear_variable.cmake

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
macro(clear_variable)
2+
cmake_parse_arguments(CLEAR_VAR "" "DESTINATION;BACKUP;REPLACE" "" ${ARGN})
3+
set(${CLEAR_VAR_BACKUP} ${${CLEAR_VAR_DESTINATION}})
4+
set(${CLEAR_VAR_DESTINATION} ${CLEAR_VAR_REPLACE})
5+
endmacro()
6+
7+
macro(restore_variable)
8+
cmake_parse_arguments(CLEAR_VAR "" "DESTINATION;BACKUP" "" ${ARGN})
9+
set(${CLEAR_VAR_DESTINATION} ${${CLEAR_VAR_BACKUP}})
10+
unset(${CLEAR_VAR_BACKUP})
11+
endmacro()

cmake/gcc_analyze.cmake

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ include(CheckCXXCompilerFlag)
22

33
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
44
check_cxx_compiler_flag("-fanalyzer" HAS_GCC_STATIC_ANALYZER)
5-
if(HAS_GCC_STATIC_ANALYZER AND NOT ENABLE_LINTING)
6-
option(ENABLE_GCC_STATIC_ANALYZER "Enable linting with the GCC static analyzer" OFF)
7-
if(ENABLE_GCC_STATIC_ANALYZER)
8-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fanalyzer")
9-
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -fanalyzer")
10-
endif()
5+
if(HAS_GCC_STATIC_ANALYZER)
6+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fanalyzer")
7+
set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -fanalyzer")
8+
message(STATUS "GCC static analysis successfully enabled.")
9+
else()
10+
message(FATAL_ERROR "Failed to enable GCC static analysis since the compiler does not support it.")
1111
endif()
12+
else()
13+
message(FATAL_ERROR "Failed to enable GCC static analysis since the compiler does not seam to be GCC (GNU).")
1214
endif()

cpr/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
cmake_minimum_required(VERSION 3.15)
22

3+
if(CPR_STATIC_ANALYZE)
4+
include(../cmake/gcc_analyze.cmake)
5+
endif()
6+
37
add_library(cpr
48
auth.cpp
59
bearer.cpp

0 commit comments

Comments
 (0)