Skip to content

Commit

Permalink
Enable compiler warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
binary1248 committed Apr 20, 2021
1 parent ce992ee commit 1c03786
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 300 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
@@ -1,5 +1,8 @@
cmake_minimum_required(VERSION 3.7.2)

# customize the compiler options CMake uses to initialize variables with
set(CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/CompilerOptionsOverride.cmake")

# define a macro that helps defining an option
macro(sfml_set_option var default type docstring)
if(NOT DEFINED ${var})
Expand Down
5 changes: 5 additions & 0 deletions cmake/CompilerOptionsOverride.cmake
@@ -0,0 +1,5 @@
if(MSVC)
# remove default warning level from CMAKE_CXX_FLAGS_INIT
string(REGEX REPLACE "/W[0-4]" "" CMAKE_CXX_FLAGS_INIT "${CMAKE_CXX_FLAGS_INIT}")
string(REGEX REPLACE "/W[0-4]" "" CMAKE_C_FLAGS_INIT "${CMAKE_C_FLAGS_INIT}")
endif()
88 changes: 88 additions & 0 deletions cmake/CompilerWarnings.cmake
@@ -0,0 +1,88 @@
# from here:
#
# https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md

# Helper function to enable compiler warnings for a specific set of files
function(set_file_warnings)
option(WARNINGS_AS_ERRORS "Treat compiler warnings as errors" TRUE)

set(MSVC_WARNINGS
/W4 # Baseline reasonable warnings
/w14242 # 'identifier': conversion from 'type1' to 'type1', possible loss of data
/w14254 # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data
/w14263 # 'function': member function does not override any base class virtual member function
/w14265 # 'classname': class has virtual functions, but destructor is not virtual instances of this class may not be destructed correctly
/w14287 # 'operator': unsigned/negative constant mismatch
/we4289 # nonstandard extension used: 'variable': loop control variable declared in the for-loop is used outside the for-loop scope
/w14296 # 'operator': expression is always 'boolean_value'
/w14311 # 'variable': pointer truncation from 'type1' to 'type2'
/w14545 # expression before comma evaluates to a function which is missing an argument list
/w14546 # function call before comma missing argument list
/w14547 # 'operator': operator before comma has no effect; expected operator with side-effect
/w14549 # 'operator': operator before comma has no effect; did you intend 'operator'?
/w14555 # expression has no effect; expected expression with side- effect
/w14619 # pragma warning: there is no warning number 'number'
/w14640 # Enable warning on thread un-safe static member initialization
/w14826 # Conversion from 'type1' to 'type_2' is sign-extended. This may cause unexpected runtime behavior.
/w14905 # wide string literal cast to 'LPSTR'
/w14906 # string literal cast to 'LPWSTR'
/w14928 # illegal copy-initialization; more than one user-defined conversion has been implicitly applied
# /permissive- # standards conformance mode for MSVC compiler. Disabled until all out-of-the-box Windows SDKs can successfully build with it.

# Disables, remove when appropriate
/wd4996 # disable warnings about deprecated functions
)

set(CLANG_WARNINGS
-Wall
-Wextra # reasonable and standard
-Wshadow # warn the user if a variable declaration shadows one from a parent context
-Wnon-virtual-dtor # warn the user if a class with virtual functions has a non-virtual destructor. This helps catch hard to track down memory errors
-Wold-style-cast # warn for c-style casts
-Wcast-align # warn for potential performance problem casts
-Wunused # warn on anything being unused
-Woverloaded-virtual # warn if you overload (not override) a virtual function
-Wpedantic # warn if non-standard C++ is used
-Wconversion # warn on type conversions that may lose data
-Wsign-conversion # warn on sign conversions
-Wnull-dereference # warn if a null dereference is detected
-Wdouble-promotion # warn if float is implicit promoted to double
-Wformat=2 # warn on security issues around functions that format output (ie printf)
)

if(WARNINGS_AS_ERRORS)
set(CLANG_WARNINGS ${CLANG_WARNINGS} -Werror)
set(MSVC_WARNINGS ${MSVC_WARNINGS} /WX)
endif()

set(GCC_WARNINGS
${CLANG_WARNINGS}
-Wmisleading-indentation # warn if indentation implies blocks where blocks do not exist
-Wduplicated-cond # warn if if / else chain has duplicated conditions
-Wlogical-op # warn about logical operations being used where bitwise were probably wanted
-Wuseless-cast # warn if you perform a cast to the same type
)

# Don't enable -Wduplicated-branches for GCC < 8.1 since it will lead to false positives
# https://github.com/gcc-mirror/gcc/commit/6bebae75035889a4844eb4d32a695bebf412bcd7
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 8.1)
set(GCC_WARNINGS
${GCC_WARNINGS}
-Wduplicated-branches # warn if if / else branches have duplicated code
)
endif()

if(MSVC)
set(FILE_WARNINGS ${MSVC_WARNINGS})
elseif(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
set(FILE_WARNINGS ${CLANG_WARNINGS})
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(FILE_WARNINGS ${GCC_WARNINGS})
else()
message(AUTHOR_WARNING "No compiler warnings set for '${CMAKE_CXX_COMPILER_ID}' compiler.")
endif()

foreach(WARNING ${FILE_WARNINGS})
set_property(SOURCE ${ARGV} APPEND_STRING PROPERTY COMPILE_FLAGS " ${WARNING}")
endforeach()
endfunction()
7 changes: 7 additions & 0 deletions cmake/Macros.cmake
@@ -1,5 +1,8 @@
include(CMakeParseArguments)

# include the compiler warnings helpers
include(${CMAKE_CURRENT_LIST_DIR}/CompilerWarnings.cmake)

# This little macro lets you set any Xcode specific property
macro (sfml_set_xcode_property TARGET XCODE_PROPERTY XCODE_VALUE)
set_property (TARGET ${TARGET} PROPERTY XCODE_ATTRIBUTE_${XCODE_PROPERTY} ${XCODE_VALUE})
Expand Down Expand Up @@ -63,6 +66,8 @@ macro(sfml_add_library target)
add_library(${target} ${THIS_SOURCES})
endif()

set_file_warnings(${THIS_SOURCES})

# define the export symbol of the module
string(REPLACE "-" "_" NAME_UPPER "${target}")
string(TOUPPER "${NAME_UPPER}" NAME_UPPER)
Expand Down Expand Up @@ -260,6 +265,8 @@ macro(sfml_add_example target)
add_executable(${target} ${target_input})
endif()

set_file_warnings(${target_input})

# set the debug suffix
set_target_properties(${target} PROPERTIES DEBUG_POSTFIX -d)

Expand Down
22 changes: 0 additions & 22 deletions extlibs/headers/glad/include/glad/egl.h
Expand Up @@ -1224,18 +1224,6 @@ static void sf_glad_egl_load_EGL_KHR_reusable_sync( GLADuserptrloadfunc load, vo
sf_glad_eglSignalSyncKHR = (PFNEGLSIGNALSYNCKHRPROC) load(userptr, "eglSignalSyncKHR");
}


static void sf_glad_egl_resolve_aliases(void) {
if (sf_glad_eglClientWaitSync == NULL && sf_glad_eglClientWaitSyncKHR != NULL) sf_glad_eglClientWaitSync = (PFNEGLCLIENTWAITSYNCPROC)sf_glad_eglClientWaitSyncKHR;
if (sf_glad_eglClientWaitSyncKHR == NULL && sf_glad_eglClientWaitSync != NULL) sf_glad_eglClientWaitSyncKHR = (PFNEGLCLIENTWAITSYNCKHRPROC)sf_glad_eglClientWaitSync;
if (sf_glad_eglCreateSync == NULL && sf_glad_eglCreateSync64KHR != NULL) sf_glad_eglCreateSync = (PFNEGLCREATESYNCPROC)sf_glad_eglCreateSync64KHR;
if (sf_glad_eglCreateSync64KHR == NULL && sf_glad_eglCreateSync != NULL) sf_glad_eglCreateSync64KHR = (PFNEGLCREATESYNC64KHRPROC)sf_glad_eglCreateSync;
if (sf_glad_eglDestroyImage == NULL && sf_glad_eglDestroyImageKHR != NULL) sf_glad_eglDestroyImage = (PFNEGLDESTROYIMAGEPROC)sf_glad_eglDestroyImageKHR;
if (sf_glad_eglDestroyImageKHR == NULL && sf_glad_eglDestroyImage != NULL) sf_glad_eglDestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC)sf_glad_eglDestroyImage;
if (sf_glad_eglDestroySync == NULL && sf_glad_eglDestroySyncKHR != NULL) sf_glad_eglDestroySync = (PFNEGLDESTROYSYNCPROC)sf_glad_eglDestroySyncKHR;
if (sf_glad_eglDestroySyncKHR == NULL && sf_glad_eglDestroySync != NULL) sf_glad_eglDestroySyncKHR = (PFNEGLDESTROYSYNCKHRPROC)sf_glad_eglDestroySync;
}

static int sf_glad_egl_get_extensions(EGLDisplay display, const char **extensions) {
*extensions = eglQueryString(display, EGL_EXTENSIONS);

Expand All @@ -1262,10 +1250,6 @@ static int sf_glad_egl_has_extension(const char *extensions, const char *ext) {
}
}

static GLADapiproc sf_glad_egl_get_proc_from_userptr(void *userptr, const char *name) {
return (GLAD_GNUC_EXTENSION (GLADapiproc (*)(const char *name)) userptr)(name);
}

static int sf_glad_egl_find_extensions_egl(EGLDisplay display) {
const char *extensions;
if (!sf_glad_egl_get_extensions(display, &extensions)) return 0;
Expand Down Expand Up @@ -1344,12 +1328,6 @@ static int gladLoadEGLUserPtr(EGLDisplay display, GLADuserptrloadfunc load, void
return version;
}

static int gladLoadEGL(EGLDisplay display, GLADloadfunc load) {
return gladLoadEGLUserPtr(display, sf_glad_egl_get_proc_from_userptr, GLAD_GNUC_EXTENSION (void*) load);
}



#ifdef SF_GLAD_EGL

#ifndef GLAD_LOADER_LIBRARY_C_
Expand Down

0 comments on commit 1c03786

Please sign in to comment.