Skip to content

Commit

Permalink
Add compiler warning flags
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgunter committed Aug 4, 2024
1 parent 96d159c commit 7eefcf8
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 89 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ target_include_directories(
target_link_libraries(whirlwind INTERFACE range-v3::range-v3 std::generator std::mdspan)

# When compiling with GCC<11, we need to add the `-fcoroutines` option. With LLVM
# Clang<17, we need `-fcoroutines-ts`.
# Clang<16, we need `-fcoroutines-ts`.
target_compile_options(
whirlwind
INTERFACE
$<$<AND:$<COMPILE_LANG_AND_ID:CXX,GNU>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,11>>:-fcoroutines>
$<$<AND:$<COMPILE_LANG_AND_ID:CXX,Clang>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,17>>:-fcoroutines-ts>
$<$<AND:$<COMPILE_LANG_AND_ID:CXX,Clang>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,16>>:-fcoroutines-ts>
)

if(WHIRLWIND_TEST)
Expand Down
71 changes: 71 additions & 0 deletions cmake/WhirlwindWarnings.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
include_guard(GLOBAL)

set(WHIRLWIND_CXX_WARNINGS)
if(CMAKE_CXX_COMPILER_ID STREQUAL GNU)
set(WHIRLWIND_CXX_WARNINGS
# cmake-format: sortable
-Wall
-Wcast-align
-Wcast-qual
-Wconversion
-Wdouble-promotion
-Wduplicated-branches
-Werror
-Wextra
-Wlogical-op
-Wnon-virtual-dtor
-Wold-style-cast
-Wpedantic
-Wshadow
-Wsign-conversion
-Wundef
)
elseif(CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$")
set(WHIRLWIND_CXX_WARNINGS
# cmake-format: sortable
-Wall
-Wbad-function-cast
-Wcast-function-type
-Wcast-qual
-Wconditional-uninitialized
-Wconversion
-Wcuda-compat
-Wdeprecated
-Wdouble-promotion
-Wduplicate-decl-specifier
-Wduplicate-method-arg
-Wduplicate-method-match
-Werror
-Wextra
-Wextra-semi
-Wheader-hygiene
-Wimplicit
-Winconsistent-missing-destructor-override
-Wloop-analysis
-Wnarrowing
-Wnon-virtual-dtor
-Wold-style-cast
-Woverriding-method-mismatch
-Wpedantic
-Wpointer-arith
-Wshadow
-Wstatic-in-inline
-Wswitch-enum
-Wundefined-reinterpret-cast
-Wunreachable-code-aggressive
-Wunused-member-function
-Wunused-template
-Wvector-conversion
)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL MSVC)
set(WHIRLWIND_CXX_WARNINGS /W3 /WX)
endif()

# Define an interface-only target with a list of desired C++ compiler warning flags
# enabled. The flags can then be applied to any compiled target by linking with the
# interface target via `target_link_libraries()`.
add_library(whirlwind-warnings INTERFACE)
add_library(whirlwind::warnings ALIAS whirlwind-warnings)
target_compile_options(
whirlwind-warnings INTERFACE $<$<COMPILE_LANGUAGE:CXX>:${WHIRLWIND_CXX_WARNINGS}>
)
12 changes: 6 additions & 6 deletions include/whirlwind/graph/shortest_path_forest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ class ShortestPathForest : public Base {

using base_type::graph;

explicit constexpr ShortestPathForest(const graph_type& graph)
: base_type(graph),
label_(graph.num_vertices(), label_type::unreached),
distance_(graph.num_vertices(), infinity<distance_type>())
explicit constexpr ShortestPathForest(const graph_type& g)
: base_type(g),
label_(g.num_vertices(), label_type::unreached),
distance_(g.num_vertices(), infinity<distance_type>())
{
WHIRLWIND_DEBUG_ASSERT(std::size(label_) == this->graph().num_vertices());
WHIRLWIND_DEBUG_ASSERT(std::size(distance_) == this->graph().num_vertices());
WHIRLWIND_DEBUG_ASSERT(std::size(label_) == g.num_vertices());
WHIRLWIND_DEBUG_ASSERT(std::size(distance_) == g.num_vertices());
}

[[nodiscard]] constexpr auto
Expand Down
6 changes: 5 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
find_package(Catch2 3 CONFIG REQUIRED)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include(WhirlwindWarnings)

# Add test executable.
add_executable(
test-whirlwind # cmake-format: sortable
Expand All @@ -12,7 +15,8 @@ add_executable(
math/test_numbers.cpp
)
target_link_libraries(
test-whirlwind PRIVATE whirlwind::whirlwind Catch2::Catch2WithMain
test-whirlwind PRIVATE Catch2::Catch2WithMain whirlwind::warnings
whirlwind::whirlwind
)
set_target_properties(test-whirlwind PROPERTIES CXX_SCAN_FOR_MODULES OFF)

Expand Down
110 changes: 55 additions & 55 deletions test/graph/csr_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ TEST_CASE("CSRGraph (empty)", "[graph]")

SECTION("num_{vertices,edges}")
{
CHECK(graph.num_vertices() == 0);
CHECK(graph.num_edges() == 0);
CHECK(graph.num_vertices() == 0U);
CHECK(graph.num_edges() == 0U);
}

SECTION("contains_{vertex,edge}")
{
CHECK(!graph.contains_vertex(0));
CHECK(!graph.contains_edge(0));
CHECK(!graph.contains_vertex(0U));
CHECK(!graph.contains_edge(0U));
}
}

TEST_CASE("CSRGraph", "[graph]")
{
auto edgelist = ww::EdgeList();
edgelist.add_edge(0, 1);
edgelist.add_edge(0, 2);
edgelist.add_edge(0, 3);
edgelist.add_edge(2, 1);
edgelist.add_edge(3, 0);
edgelist.add_edge(0U, 1U);
edgelist.add_edge(0U, 2U);
edgelist.add_edge(0U, 3U);
edgelist.add_edge(2U, 1U);
edgelist.add_edge(3U, 0U);

const auto graph = ww::CSRGraph(edgelist);

const auto vertices = {0, 1, 2, 3};
const auto edges = {0, 1, 2, 3, 4};
const auto vertices = {0U, 1U, 2U, 3U};
const auto edges = {0U, 1U, 2U, 3U, 4U};

SECTION("{vertex,edge,size}_type")
{
Expand All @@ -56,8 +56,8 @@ TEST_CASE("CSRGraph", "[graph]")

SECTION("num_{vertices,edges}")
{
CHECK(graph.num_vertices() == 4);
CHECK(graph.num_edges() == 5);
CHECK(graph.num_vertices() == 4U);
CHECK(graph.num_edges() == 5U);
}

SECTION("get_{vertex,edge}_id")
Expand All @@ -78,47 +78,47 @@ TEST_CASE("CSRGraph", "[graph]")

SECTION("contains_{vertex,edge}")
{
CHECK(graph.contains_vertex(0));
CHECK(graph.contains_vertex(3));
CHECK(!graph.contains_vertex(-1));
CHECK(!graph.contains_vertex(4));
CHECK(graph.contains_vertex(0U));
CHECK(graph.contains_vertex(3U));
CHECK(!graph.contains_vertex(999U));
CHECK(!graph.contains_vertex(4U));

CHECK(graph.contains_edge(0));
CHECK(graph.contains_edge(4));
CHECK(!graph.contains_edge(-1));
CHECK(!graph.contains_edge(5));
CHECK(graph.contains_edge(0U));
CHECK(graph.contains_edge(4U));
CHECK(!graph.contains_edge(999U));
CHECK(!graph.contains_edge(5U));
}

SECTION("outdegree")
{
CHECK(graph.outdegree(0) == 3);
CHECK(graph.outdegree(1) == 0);
CHECK(graph.outdegree(2) == 1);
CHECK(graph.outdegree(3) == 1);
CHECK(graph.outdegree(0U) == 3U);
CHECK(graph.outdegree(1U) == 0U);
CHECK(graph.outdegree(2U) == 1U);
CHECK(graph.outdegree(3U) == 1U);
}
}

TEST_CASE("CSRGraph (nonconsecutive vertices)", "[graph]")
{
auto edgelist = ww::EdgeList();
edgelist.add_edge(0, 1);
edgelist.add_edge(1, 2);
edgelist.add_edge(4, 5);
edgelist.add_edge(0U, 1U);
edgelist.add_edge(1U, 2U);
edgelist.add_edge(4U, 5U);

const auto graph = ww::CSRGraph(edgelist);

SECTION("num_{vertices,edges}")
{
CHECK(graph.num_vertices() == 6);
CHECK(graph.num_edges() == 3);
CHECK(graph.num_vertices() == 6U);
CHECK(graph.num_edges() == 3U);
}

SECTION("{vertices,edges}")
{
const auto vertices = {0, 1, 2, 3, 4, 5};
const auto vertices = {0U, 1U, 2U, 3U, 4U, 5U};
CHECK(ranges::equal(graph.vertices(), vertices));

const auto edges = {0, 1, 2};
const auto edges = {0U, 1U, 2U};
CHECK(ranges::equal(graph.edges(), edges));
}

Expand All @@ -134,70 +134,70 @@ TEST_CASE("CSRGraph (nonconsecutive vertices)", "[graph]")
TEST_CASE("CSRGraph (unsorted edges)", "[graph]")
{
auto edgelist = ww::EdgeList();
edgelist.add_edge(0, 3);
edgelist.add_edge(2, 1);
edgelist.add_edge(0, 2);
edgelist.add_edge(3, 0);
edgelist.add_edge(0, 1);
edgelist.add_edge(0U, 3U);
edgelist.add_edge(2U, 1U);
edgelist.add_edge(0U, 2U);
edgelist.add_edge(3U, 0U);
edgelist.add_edge(0U, 1U);

const auto graph = ww::CSRGraph(edgelist);

SECTION("num_{vertices,edges}")
{
CHECK(graph.num_vertices() == 4);
CHECK(graph.num_edges() == 5);
CHECK(graph.num_vertices() == 4U);
CHECK(graph.num_edges() == 5U);
}

SECTION("{vertices,edges}")
{
const auto vertices = {0, 1, 2, 3};
const auto vertices = {0U, 1U, 2U, 3U};
CHECK(ranges::equal(graph.vertices(), vertices));

const auto edges = {0, 1, 2, 3, 4};
const auto edges = {0U, 1U, 2U, 3U, 4U};
CHECK(ranges::equal(graph.edges(), edges));
}
}

TEST_CASE("CSRGraph (parallel edges)", "[graph]")
{
auto edgelist = ww::EdgeList();
edgelist.add_edge(0, 1);
edgelist.add_edge(0, 1);
edgelist.add_edge(0U, 1U);
edgelist.add_edge(0U, 1U);

const auto graph = ww::CSRGraph(edgelist);

SECTION("num_{vertices,edges}")
{
CHECK(graph.num_vertices() == 2);
CHECK(graph.num_edges() == 2);
CHECK(graph.num_vertices() == 2U);
CHECK(graph.num_edges() == 2U);
}

SECTION("outdegree") { CHECK(graph.outdegree(0) == 2); }
SECTION("outdegree") { CHECK(graph.outdegree(0) == 2U); }
}

TEST_CASE("CSRGraph (self loops)", "[graph]")
{
auto edgelist = ww::EdgeList();
edgelist.add_edge(1, 0);
edgelist.add_edge(1, 1);
edgelist.add_edge(1, 1);
edgelist.add_edge(1, 2);
edgelist.add_edge(1U, 0U);
edgelist.add_edge(1U, 1U);
edgelist.add_edge(1U, 1U);
edgelist.add_edge(1U, 2U);

const auto graph = ww::CSRGraph(edgelist);

SECTION("num_{vertices,edges}")
{
CHECK(graph.num_vertices() == 3);
CHECK(graph.num_edges() == 4);
CHECK(graph.num_vertices() == 3U);
CHECK(graph.num_edges() == 4U);
}

SECTION("contains_vertex")
{
CHECK(graph.contains_vertex(0));
CHECK(graph.contains_vertex(2));
CHECK(graph.contains_vertex(0U));
CHECK(graph.contains_vertex(2U));
}

SECTION("outdegree") { CHECK(graph.outdegree(1) == 4); }
SECTION("outdegree") { CHECK(graph.outdegree(1U) == 4U); }
}

} // namespace
Loading

0 comments on commit 7eefcf8

Please sign in to comment.