Skip to content

Commit

Permalink
refactor: precisely determine GCC compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
zchrissirhcz committed Jan 11, 2025
1 parent f810a6b commit 1b0a42d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
42 changes: 21 additions & 21 deletions plugins/overlook/overlook.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ endif()

# rule0: don't ignore all that warnings
# If `-w` specified for GCC/Clang, report an error
if((CMAKE_C_COMPILER_ID MATCHES "GNU") OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
if((CMAKE_C_COMPILER_ID STREQUAL "GNU") OR (CMAKE_C_COMPILER_ID MATCHES "Clang"))
get_directory_property(overlook_detected_global_compile_options COMPILE_OPTIONS)
message("Detected Global Compile Options: ${overlook_detected_global_compile_options}")
string(REGEX MATCH "-w" ignore_all_warnings "${overlook_detected_global_compile_options}" )
Expand All @@ -65,7 +65,7 @@ endif()
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS /we4013)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS /we4013)
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU")
elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -Werror=implicit-function-declaration)
if(CMAKE_CXX_COMPILER_VERSION LESS 9.1)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=implicit-function-declaration)
Expand All @@ -82,7 +82,7 @@ endif()
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS /we4431)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS /we4431)
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU")
elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -Werror=implicit-int)
if(CMAKE_CXX_COMPILER_VERSION LESS 9.1)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=implicit-int)
Expand Down Expand Up @@ -116,7 +116,7 @@ endif()
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS /we4172)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS /we4172)
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU")
elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -Werror=return-local-addr)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=return-local-addr)
elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
Expand All @@ -141,7 +141,7 @@ endif()
# rule14: 避免 void* 类型的指针参参与算术运算
# MSVC C/C++ 默认会报错, Linux gcc 不报 warning 和 error, Linux g++ 只报 warning
# Linux 下 Clang 开 -Wpedentric 才报 warning, Clang++ 报 error
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -Werror=pointer-arith)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=pointer-arith)
elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
Expand All @@ -153,7 +153,7 @@ endif()
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS /we4133)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS /we4133)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION GREATER 4.8) # gcc/g++ 4.8.3 not ok
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -Werror=incompatible-pointer-types)
if(CMAKE_CXX_COMPILER_VERSION LESS 9.1)
Expand All @@ -169,7 +169,7 @@ endif()
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS /we4047)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS /we4047)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION GREATER 4.8)
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -Werror=int-conversion)
if(CMAKE_CXX_COMPILER_VERSION LESS 9.1)
Expand Down Expand Up @@ -200,7 +200,7 @@ endif()
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS /we4312)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS /we4312)
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU")
elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -Werror=int-to-pointer-cast)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=int-to-pointer-cast)
elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
Expand Down Expand Up @@ -248,22 +248,22 @@ endif()
# rule16: 释放非堆内存
# TODO: 检查 MSVC
# Linux Clang8.0 无法检测到
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -Werror=free-nonheap-object)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=free-nonheap-object)
endif()

# rule32: 用 memset 等 C 函数设置 非 POD class 对象
# Linux下, GCC9.3 能发现此问题, 但clang10 不能发现
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
if(CMAKE_CXX_COMPILER_VERSION GREATER 7.5)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=class-memaccess)
endif()
endif()

# rule15: 避免符号重复定义(变量对应的强弱符号)。只在 C 中出现。
# 暂时没找到 MSVC 的对应编译选项
if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_C_COMPILER_ID MATCHES "Clang")
if((CMAKE_C_COMPILER_ID STREQUAL "GNU") OR CMAKE_C_COMPILER_ID MATCHES "Clang")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -fno-common)
endif()

Expand Down Expand Up @@ -304,7 +304,7 @@ endif()
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS /we4552 /we4555)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS /we4552 /we4555)
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU")
elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -Werror=unused-value)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=unused-value)
elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
Expand All @@ -326,7 +326,7 @@ endif()
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS /we4006)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS /we4006)
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU")
elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -Werror=comment)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=comment)
elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
Expand All @@ -351,7 +351,7 @@ endif()

# rule29: C++中,禁止把字符串常量赋值给 char* 变量
# VS2019 开启 /Wall 后也查不到
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=write-strings)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# Linux Clang 和 AppleClang 不太一样
Expand All @@ -364,7 +364,7 @@ endif()

# rule31: multi-char constant
# MSVC 没有对应的选项
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -Werror=multichar)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=multichar)
elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
Expand All @@ -380,7 +380,7 @@ endif()
if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS /we4018)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS /we4018)
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU")
elseif(CMAKE_C_COMPILER_ID STREQUAL "GNU")
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -Werror=sign-compare)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=sign-compare)
elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
Expand All @@ -403,7 +403,7 @@ if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
endif()

## rule37: float 转换隐式转换为 int,可能改变变量的值
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=float-conversion)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=float-conversion)
Expand All @@ -428,14 +428,14 @@ endif()
# Warn when a class has virtual functions and an accessible non-virtual destructor itself or in an accessible polymorphic base
# class, in which case it is possible but unsafe to delete an instance of a derived class through a pointer to the class
# itself or base class. This warning is automatically enabled if -Weffc++ is specified.
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=non-virtual-dtor)
elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=non-virtual-dtor)
endif()

## rule36: switch case 忘记写 break, 会 fallthrough 执行, 可能导致数组越界(具体取决于你的代码). 不写break我们视为错误。
if((CMAKE_C_COMPILER_ID MATCHES "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "GNU"))
if((CMAKE_C_COMPILER_ID STREQUAL "GNU") OR (CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -Werror=implicit-fallthrough)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Werror=implicit-fallthrough)
elseif((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
Expand All @@ -444,7 +444,7 @@ elseif((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_CXX_COMPILER_ID MATCHES "
endif()

## rule37: 遇到第一个报错就停止编译
if((CMAKE_C_COMPILER_ID MATCHES "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "GNU"))
if((CMAKE_C_COMPILER_ID STREQUAL "GNU") OR (CMAKE_CXX_COMPILER_ID STREQUAL "GNU"))
list(APPEND OVERLOOK_C_COMPILE_OPTIONS -Wfatal-errors)
list(APPEND OVERLOOK_CXX_COMPILE_OPTIONS -Wfatal-errors)
elseif((CMAKE_C_COMPILER_ID MATCHES "Clang") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
Expand Down Expand Up @@ -496,7 +496,7 @@ function(overlook_add_whole_archive_flag lib output_var)
overlook_warn("MSVC version is ${MSVC_VERSION}, /WHOLEARCHIVE flag cannot be set")
set(${output_var} ${lib} PARENT_SCOPE)
endif()
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(${output_var} -Wl,--whole-archive ${lib} -Wl,--no-whole-archive PARENT_SCOPE)
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND CMAKE_SYSTEM_NAME MATCHES "Linux")
# set(${output_var} -Wl,--whole-archive ${lib} -Wl,--no-whole-archive PARENT_SCOPE)
Expand Down
4 changes: 3 additions & 1 deletion plugins/overlook/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.10)
project(test_overlook)

message("CMAKE_C_COMPILER_ID: ${CMAKE_C_COMPILER_ID}")
message("CMAKE_CXX_COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}")
include(../overlook.cmake)

add_executable(test1 test1.c)
Expand Down Expand Up @@ -39,4 +41,4 @@ add_executable(test33 test33.cpp)
add_executable(test34 test34.cpp)
add_executable(test35 test35.cpp)
add_executable(test36 test36.cpp)
add_executable(test37 test37.cpp)
add_executable(test37 test37.cpp)

0 comments on commit 1b0a42d

Please sign in to comment.