diff --git a/CMakeLists.txt b/CMakeLists.txt index c46343712..66c524c48 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/cmake/warnings.cmake b/cmake/warnings.cmake new file mode 100644 index 000000000..7750c1011 --- /dev/null +++ b/cmake/warnings.cmake @@ -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) \ No newline at end of file diff --git a/include/engine/api/base_api.hpp b/include/engine/api/base_api.hpp index 8f7ce1de0..086c787de 100644 --- a/include/engine/api/base_api.hpp +++ b/include/engine/api/base_api.hpp @@ -123,9 +123,9 @@ class BaseAPI const auto &snapped_location = candidatesSnappedLocation(candidates); const auto &input_location = candidatesInputLocation(candidates); - auto location = - fbresult::Position(static_cast(static_cast(util::toFloating(snapped_location.lon))), - static_cast(static_cast(util::toFloating(snapped_location.lat)))); + auto location = fbresult::Position( + static_cast(static_cast(util::toFloating(snapped_location.lon))), + static_cast(static_cast(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(*builder); waypoint->add_location(&location); - waypoint->add_distance( - static_cast(util::coordinate_calculation::greatCircleDistance(snapped_location, input_location))); + waypoint->add_distance(static_cast( + util::coordinate_calculation::greatCircleDistance(snapped_location, input_location))); waypoint->add_name(name_string); if (parameters.generate_hints) { diff --git a/include/engine/engine.hpp b/include/engine/engine.hpp index 4fe63ec05..451253df6 100644 --- a/include/engine/engine.hpp +++ b/include/engine/engine.hpp @@ -83,7 +83,7 @@ template 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 ¶ms, api::ResultT &result) const override final { diff --git a/include/engine/routing_algorithms.hpp b/include/engine/routing_algorithms.hpp index f3610bbb2..0253ef13c 100644 --- a/include/engine/routing_algorithms.hpp +++ b/include/engine/routing_algorithms.hpp @@ -72,7 +72,7 @@ template class RoutingAlgorithms final : public RoutingAlgo { } - virtual ~RoutingAlgorithms() = default; + virtual ~RoutingAlgorithms() override = default; InternalManyRoutesResult AlternativePathSearch(const PhantomEndpointCandidates &endpoint_candidates, diff --git a/include/util/exception.hpp b/include/util/exception.hpp index 7a4b89ca4..3dee59ea4 100644 --- a/include/util/exception.hpp +++ b/include/util/exception.hpp @@ -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, diff --git a/include/util/filtered_integer_range.hpp b/include/util/filtered_integer_range.hpp index ec0ac596b..4960c9560 100644 --- a/include/util/filtered_integer_range.hpp +++ b/include/util/filtered_integer_range.hpp @@ -87,11 +87,11 @@ template class filtered_range // convenience function to construct an integer range with type deduction template -filtered_range -filtered_irange(const Integer first, - const Integer last, - const Filter &filter, - typename std::enable_if::value>::type * = 0) noexcept +filtered_range filtered_irange( + const Integer first, + const Integer last, + const Filter &filter, + typename std::enable_if::value>::type * = nullptr) noexcept { return filtered_range(first, last, filter); } diff --git a/include/util/integer_range.hpp b/include/util/integer_range.hpp index c8f56b8f7..2b16cc8b4 100644 --- a/include/util/integer_range.hpp +++ b/include/util/integer_range.hpp @@ -86,7 +86,7 @@ template range irange(const Integer first, const Integer last, - typename std::enable_if::value>::type * = 0) noexcept + typename std::enable_if::value>::type * = nullptr) noexcept { return range(first, last); } diff --git a/include/util/packed_vector.hpp b/include/util/packed_vector.hpp index ae79c3014..2cff90164 100644 --- a/include/util/packed_vector.hpp +++ b/include/util/packed_vector.hpp @@ -49,14 +49,16 @@ template inline T get_lower_half_value(WordT word, WordT mask, std::uint8_t offset, - typename std::enable_if_t::value> * = 0) + typename std::enable_if_t::value> * = nullptr) { return static_cast((word & mask) >> offset); } template -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((word & mask) >> offset)}; } @@ -65,14 +67,16 @@ template inline T get_upper_half_value(WordT word, WordT mask, std::uint8_t offset, - typename std::enable_if_t::value> * = 0) + typename std::enable_if_t::value> * = nullptr) { return static_cast((word & mask) << offset); } template -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::value, "Only unsigned word types supported for now."); return T{static_cast((word & mask) << offset)}; diff --git a/src/extractor/extraction_containers.cpp b/src/extractor/extraction_containers.cpp index 4730441b1..5c3a07309 100644 --- a/src/extractor/extraction_containers.cpp +++ b/src/extractor/extraction_containers.cpp @@ -667,7 +667,7 @@ void ExtractionContainers::PrepareEdges(ScriptingEnvironment &scripting_environm auto &edge = edge_iterator->result; edge.weight = std::max(1, std::round(segment.weight * weight_multiplier)); edge.duration = std::max(1, std::round(segment.duration * 10.)); - edge.distance = accurate_distance; + edge.distance = static_cast(accurate_distance); // assign new node id const auto node_id = mapExternalToInternalNodeID( diff --git a/src/util/assert.cpp b/src/util/assert.cpp index f8a9cc2e6..b91bf592a 100644 --- a/src/util/assert.cpp +++ b/src/util/assert.cpp @@ -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);