Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ if (ARGS_BUILD_UNITTESTS)
args_configure_test(${target_name})
add_test(NAME ${test_name} COMMAND ${target_name})
endforeach ()

# ARGS_NOEXCEPT exists so the header can be used where exceptions are off,
# so build this one with them off to keep that working.
if (NOT MSVC)
target_compile_options(argstest-no_exceptions PRIVATE -fno-exceptions)
endif ()
endif()

add_library(taywee::args ALIAS args)
Expand Down
8 changes: 1 addition & 7 deletions args.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,7 @@ namespace args

if (can_reserve && total > 0)
{
try
{
res.reserve(total);
}
catch (...) {
// Fall back to default allocation
}
res.reserve(total);
}

bool first = true;
Expand Down
7 changes: 7 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,11 @@ endforeach
test('multiple_inclusion', executable('argstest-multiple_inclusion',
sources: ['test/multiple_inclusion.cxx', 'test/multiple_inclusion.aux.cxx'],
dependencies: args_dep))

# ARGS_NOEXCEPT exists so the header can be used where exceptions are off, so
# build this one with them off to keep that working.
test('no_exceptions', executable('argstest-no_exceptions',
sources: 'test/no_exceptions.cxx',
cpp_args: meson.get_compiler('cpp').get_supported_arguments('-fno-exceptions'),
dependencies: args_dep))
endif
34 changes: 34 additions & 0 deletions test/no_exceptions.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* Copyright (c) Taylor Richberger <taylor@axfive.net>
* This code is released under the license described in the LICENSE file
*
* Compiled with the compiler's exception support switched off (see
* CMakeLists.txt and meson.build), so anything in args.hxx that needs
* exceptions outside of the ARGS_NOEXCEPT guards breaks this test at compile
* time.
*/

#define ARGS_NOEXCEPT
#include "test_common.hxx"

#include <args.hxx>

#include "test_helpers.hxx"

int main()
{
args::ArgumentParser p("parser");
args::CompletionFlag c(p, {"completion"});
args::Flag f(p, "foo", "description", {'f', "foo"});
args::MapFlag<std::string, int> m(p, "map", "description", {'m', "map"}, {{"alpha", 1}, {"beta", 2}});

p.ParseArgs(std::vector<std::string>{"--map", "beta", "-f"});
test::require(p.GetError() == args::Error::None);
test::require(args::get(m) == 2);

// Completion replies are assembled by args::detail::Join.
p.ParseArgs(std::vector<std::string>{"--completion", "bash", "2", "test", "--map", ""});
test::require(p.GetError() == args::Error::Completion);
test::require(args::get(c) == "alpha\nbeta");

return 0;
}
6 changes: 6 additions & 0 deletions test/test_helpers.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ inline void require_false(bool cond)
}
}

// The throwing helpers are unusable in a translation unit built without
// exception support, and merely parsing them is an error there.
#ifdef __cpp_exceptions

template <typename F>
void require_nothrow(F &&f)
{
Expand Down Expand Up @@ -101,6 +105,8 @@ void require_throws_with(F &&f, const std::string &expected)
fail("require_throws_with: nothing thrown");
}

#endif

template <typename ContainerT, typename TargetT>
void require_contains(const ContainerT& container, const TargetT& target)
{
Expand Down