Refactor CMake code related to compiler warnings, enable some additional warnings

This commit is contained in:
Siarhei Fedartsou 2022-09-01 23:05:03 +02:00
parent daacda9484
commit 60bd8ba812
11 changed files with 116 additions and 26 deletions

View File

@ -327,9 +327,9 @@ if (ENABLE_SANITIZER)
endif()
# Configuring compilers
set(OSRM_WARNING_FLAGS "-Wno-used-but-marked-unused -Wno-cast-function-type -Wno-non-virtual-dtor -Wno-undefined-reinterpret-cast -Wno-suggest-destructor-override -Wno-inconsistent-missing-destructor-override -Wno-undefined-func-template -Wno-unused-member-function -Wno-covered-switch-default -Wno-documentation -Wno-unused-template -Wno-global-constructors -Wno-exit-time-destructors -Wno-double-promotion -Wno-float-conversion -Wno-weak-template-vtables -Wno-weak-vtables -Wno-undef -Wno-float-equal -Wimplicit-float-conversion -Wno-shadow -Wno-switch-enum -Wno-shorten-64-to-32 -Wno-extra-semi -Wno-atomic-implicit-seq-cst -Wno-sign-conversion -Wno-suggest-override -Wno-implicit-int-conversion -Wno-missing-prototypes -Wno-missing-noreturn -Wno-disabled-macro-expansion -Wno-padded -Wno-shadow-field -Wno-newline-eof -Wno-old-style-cast -Wno-zero-as-null-pointer-constant -Wno-shadow-field-in-constructor -Wno-c++98-compat -Wno-c++98-compat-pedantic -Werror -Wunused -Werror=all -Werror=extra -Werror=uninitialized -Werror=unreachable-code -Werror=unused-variable -Werror=unreachable-code -Wno-error=cpp -Wpedantic")
include(cmake/warnings.cmake)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OSRM_WARNING_FLAGS} -Werror=strict-overflow=2 -Wno-error=unused-local-typedef -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fPIC -fcolor-diagnostics -ftemplate-depth=1024 -Wno-unused-command-line-argument")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fPIC -fcolor-diagnostics -ftemplate-depth=1024")
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(COLOR_FLAG "-fdiagnostics-color=auto")
check_cxx_compiler_flag("-fdiagnostics-color=auto" HAS_COLOR_FLAG)
@ -337,7 +337,7 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
set(COLOR_FLAG "")
endif()
# using GCC
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OSRM_WARNING_FLAGS} -Werror=strict-overflow=1 -Wno-error=maybe-uninitialized -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 ${COLOR_FLAG} -fPIC -ftemplate-depth=1024")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 ${COLOR_FLAG} -fPIC -ftemplate-depth=1024")
if(WIN32) # using mingw
add_dependency_defines(-DWIN32)
@ -430,6 +430,7 @@ include_directories(SYSTEM ${CHEAPRULER_INCLUDE_DIR})
add_library(MICROTAR OBJECT "${CMAKE_CURRENT_SOURCE_DIR}/third_party/microtar/src/microtar.c")
set_property(TARGET MICROTAR PROPERTY POSITION_INDEPENDENT_CODE ON)
target_no_warning(MICROTAR unused-variable)
set(PROTOZERO_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party/protozero/include")
include_directories(SYSTEM ${PROTOZERO_INCLUDE_DIR})

84
cmake/warnings.cmake Normal file
View File

@ -0,0 +1,84 @@
include (CheckCXXCompilerFlag)
include (CheckCCompilerFlag)
# Try to add -Wflag if compiler supports it
macro (add_warning flag)
string(REPLACE "-" "_" underscored_flag ${flag})
string(REPLACE "+" "x" underscored_flag ${underscored_flag})
check_cxx_compiler_flag("-W${flag}" SUPPORTS_CXXFLAG_${underscored_flag})
check_c_compiler_flag("-W${flag}" SUPPORTS_CFLAG_${underscored_flag})
if (SUPPORTS_CXXFLAG_${underscored_flag})
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W${flag}")
else()
message (STATUS "Flag -W${flag} is unsupported")
endif()
if (SUPPORTS_CFLAG_${underscored_flag})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W${flag}")
else()
message(STATUS "Flag -W${flag} is unsupported")
endif()
endmacro()
# Try to add -Wno flag if compiler supports it
macro (no_warning flag)
add_warning(no-${flag})
endmacro ()
# The same but only for specified target.
macro (target_add_warning target flag)
string (REPLACE "-" "_" underscored_flag ${flag})
string (REPLACE "+" "x" underscored_flag ${underscored_flag})
check_cxx_compiler_flag("-W${flag}" SUPPORTS_CXXFLAG_${underscored_flag})
if (SUPPORTS_CXXFLAG_${underscored_flag})
target_compile_options (${target} PRIVATE "-W${flag}")
else ()
message (STATUS "Flag -W${flag} is unsupported")
endif ()
endmacro ()
macro (target_no_warning target flag)
target_add_warning(${target} no-${flag})
endmacro ()
add_warning(all)
add_warning(extra)
add_warning(pedantic)
add_warning(error) # treat all warnings as errors
add_warning(strict-overflow=2)
add_warning(suggest-override)
add_warning(suggest-destructor-override)
add_warning(missing-noreturn)
add_warning(unused)
add_warning(unreachable-code)
add_warning(duplicated-cond)
add_warning(disabled-optimization)
add_warning(init-self)
add_warning(bool-compare)
add_warning(logical-not-parentheses)
add_warning(logical-op)
add_warning(maybe-uninitialized)
add_warning(misleading-indentation)
add_warning(no-return-local-addr)
add_warning(odr)
add_warning(reorder)
add_warning(shift-negative-value)
add_warning(sizeof-array-argument)
add_warning(switch-bool)
add_warning(tautological-compare)
add_warning(trampolines)
# TODO: these warnings are not enabled by default, but we consider them as useful and good to enable in the future
no_warning(implicit-int-conversion)
no_warning(implicit-float-conversion)
no_warning(unused-member-function)
no_warning(old-style-cast)
no_warning(non-virtual-dtor)
no_warning(float-conversion)
no_warning(sign-conversion)
no_warning(shorten-64-to-32)
no_warning(padded)

View File

@ -123,9 +123,9 @@ class BaseAPI
const auto &snapped_location = candidatesSnappedLocation(candidates);
const auto &input_location = candidatesInputLocation(candidates);
auto location =
fbresult::Position(static_cast<float>(static_cast<double>(util::toFloating(snapped_location.lon))),
static_cast<float>(static_cast<double>(util::toFloating(snapped_location.lat))));
auto location = fbresult::Position(
static_cast<float>(static_cast<double>(util::toFloating(snapped_location.lon))),
static_cast<float>(static_cast<double>(util::toFloating(snapped_location.lat))));
const auto toName = [this](const auto &phantom) {
return facade.GetNameForID(facade.GetNameIndex(phantom.forward_segment_id.id))
@ -156,8 +156,8 @@ class BaseAPI
auto waypoint = std::make_unique<fbresult::WaypointBuilder>(*builder);
waypoint->add_location(&location);
waypoint->add_distance(
static_cast<float>(util::coordinate_calculation::greatCircleDistance(snapped_location, input_location)));
waypoint->add_distance(static_cast<float>(
util::coordinate_calculation::greatCircleDistance(snapped_location, input_location)));
waypoint->add_name(name_string);
if (parameters.generate_hints)
{

View File

@ -83,7 +83,7 @@ template <typename Algorithm> class Engine final : public EngineInterface
Engine(const Engine &) = delete;
Engine &operator=(const Engine &) = delete;
virtual ~Engine() = default;
virtual ~Engine() override = default;
Status Route(const api::RouteParameters &params, api::ResultT &result) const override final
{

View File

@ -72,7 +72,7 @@ template <typename Algorithm> class RoutingAlgorithms final : public RoutingAlgo
{
}
virtual ~RoutingAlgorithms() = default;
virtual ~RoutingAlgorithms() override = default;
InternalManyRoutesResult
AlternativePathSearch(const PhantomEndpointCandidates &endpoint_candidates,

View File

@ -106,7 +106,7 @@ class RuntimeError : public exception
// This function exists to 'anchor' the class, and stop the compiler from
// copying vtable and RTTI info into every object file that includes
// this header. (Caught by -Wweak-vtables under Clang.)
virtual void anchor() const;
virtual void anchor() const override;
const ErrorCode code;
static std::string BuildMessage(const std::string &message,

View File

@ -87,11 +87,11 @@ template <typename Integer, typename Filter> class filtered_range
// convenience function to construct an integer range with type deduction
template <typename Integer, typename Filter>
filtered_range<Integer, Filter>
filtered_irange(const Integer first,
const Integer last,
const Filter &filter,
typename std::enable_if<std::is_integral<Integer>::value>::type * = 0) noexcept
filtered_range<Integer, Filter> filtered_irange(
const Integer first,
const Integer last,
const Filter &filter,
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr) noexcept
{
return filtered_range<Integer, Filter>(first, last, filter);
}

View File

@ -86,7 +86,7 @@ template <typename Integer>
range<Integer>
irange(const Integer first,
const Integer last,
typename std::enable_if<std::is_integral<Integer>::value>::type * = 0) noexcept
typename std::enable_if<std::is_integral<Integer>::value>::type * = nullptr) noexcept
{
return range<Integer>(first, last);
}

View File

@ -49,14 +49,16 @@ template <typename WordT, typename T>
inline T get_lower_half_value(WordT word,
WordT mask,
std::uint8_t offset,
typename std::enable_if_t<std::is_integral<T>::value> * = 0)
typename std::enable_if_t<std::is_integral<T>::value> * = nullptr)
{
return static_cast<T>((word & mask) >> offset);
}
template <typename WordT, typename T>
inline T
get_lower_half_value(WordT word, WordT mask, std::uint8_t offset, typename T::value_type * = 0)
inline T get_lower_half_value(WordT word,
WordT mask,
std::uint8_t offset,
typename T::value_type * = nullptr)
{
return T{static_cast<typename T::value_type>((word & mask) >> offset)};
}
@ -65,14 +67,16 @@ template <typename WordT, typename T>
inline T get_upper_half_value(WordT word,
WordT mask,
std::uint8_t offset,
typename std::enable_if_t<std::is_integral<T>::value> * = 0)
typename std::enable_if_t<std::is_integral<T>::value> * = nullptr)
{
return static_cast<T>((word & mask) << offset);
}
template <typename WordT, typename T>
inline T
get_upper_half_value(WordT word, WordT mask, std::uint8_t offset, typename T::value_type * = 0)
inline T get_upper_half_value(WordT word,
WordT mask,
std::uint8_t offset,
typename T::value_type * = nullptr)
{
static_assert(std::is_unsigned<WordT>::value, "Only unsigned word types supported for now.");
return T{static_cast<typename T::value_type>((word & mask) << offset)};

View File

@ -667,7 +667,7 @@ void ExtractionContainers::PrepareEdges(ScriptingEnvironment &scripting_environm
auto &edge = edge_iterator->result;
edge.weight = std::max<EdgeWeight>(1, std::round(segment.weight * weight_multiplier));
edge.duration = std::max<EdgeWeight>(1, std::round(segment.duration * 10.));
edge.distance = accurate_distance;
edge.distance = static_cast<float>(accurate_distance);
// assign new node id
const auto node_id = mapExternalToInternalNodeID(

View File

@ -23,11 +23,12 @@ namespace
// Boost.Assert only declares the following two functions and let's us define them here.
namespace boost
{
void assertion_failed(char const *expr, char const *function, char const *file, long line)
[[noreturn]] void
assertion_failed(char const *expr, char const *function, char const *file, long line)
{
::assertion_failed_msg_helper(expr, "", function, file, line);
}
void assertion_failed_msg(
[[noreturn]] void assertion_failed_msg(
char const *expr, char const *msg, char const *function, char const *file, long line)
{
::assertion_failed_msg_helper(expr, msg, function, file, line);