Skip to content

Commit

Permalink
Drop dependency on lexical_cast and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
apolukhin committed Mar 14, 2018
1 parent e99f858 commit 87b4789
Show file tree
Hide file tree
Showing 21 changed files with 172 additions and 26 deletions.
7 changes: 4 additions & 3 deletions include/boost/stacktrace/detail/addr2line_impls.hpp
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand All @@ -13,8 +13,9 @@
#endif

#include <boost/stacktrace/detail/to_hex_array.hpp>
#include <boost/stacktrace/detail/to_dec_array.hpp>
#include <boost/stacktrace/detail/try_dec_convert.hpp>
#include <boost/core/demangle.hpp>
#include <boost/lexical_cast.hpp>
#include <cstdio>

#include <sys/types.h>
Expand Down Expand Up @@ -212,7 +213,7 @@ std::size_t frame::source_line() const {
}
res = res.substr(last + 1);

if (!boost::conversion::try_lexical_convert(res, line_num)) {
if (!boost::stacktrace::detail::try_dec_convert(res.c_str(), line_num)) {
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion include/boost/stacktrace/detail/collect_msvc.ipp
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
2 changes: 1 addition & 1 deletion include/boost/stacktrace/detail/collect_noop.ipp
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
2 changes: 1 addition & 1 deletion include/boost/stacktrace/detail/collect_unwind.ipp
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
2 changes: 1 addition & 1 deletion include/boost/stacktrace/detail/frame_decl.hpp
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
8 changes: 4 additions & 4 deletions include/boost/stacktrace/detail/frame_msvc.ipp
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand All @@ -16,7 +16,7 @@

#include <boost/core/demangle.hpp>
#include <boost/core/noncopyable.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/stacktrace/detail/to_dec_array.hpp>
#include <boost/stacktrace/detail/to_hex_array.hpp>
#include <windows.h>
#include "dbgeng.h"
Expand Down Expand Up @@ -329,7 +329,7 @@ public:
res += " at ";
res += source_line.first;
res += ':';
res += boost::lexical_cast<boost::array<char, 40> >(source_line.second).data();
res += boost::stacktrace::detail::to_dec_array(source_line.second).data();
} else if (!module_name.empty()) {
res += " in ";
res += module_name;
Expand All @@ -349,7 +349,7 @@ std::string to_string(const frame* frames, std::size_t size) {
if (i < 10) {
res += ' ';
}
res += boost::lexical_cast<boost::array<char, 40> >(i).data();
res += boost::stacktrace::detail::to_dec_array(i).data();
res += '#';
res += ' ';
idebug.to_string_impl(frames[i].address(), res);
Expand Down
2 changes: 1 addition & 1 deletion include/boost/stacktrace/detail/frame_noop.ipp
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
6 changes: 3 additions & 3 deletions include/boost/stacktrace/detail/frame_unwind.ipp
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand All @@ -16,8 +16,8 @@

#include <boost/stacktrace/detail/to_hex_array.hpp>
#include <boost/stacktrace/detail/location_from_symbol.hpp>
#include <boost/stacktrace/detail/to_dec_array.hpp>
#include <boost/core/demangle.hpp>
#include <boost/lexical_cast.hpp>

#include <cstdio>

Expand Down Expand Up @@ -67,7 +67,7 @@ std::string to_string(const frame* frames, std::size_t size) {
if (i < 10) {
res += ' ';
}
res += boost::lexical_cast<boost::array<char, 40> >(i).data();
res += boost::stacktrace::detail::to_dec_array(i).data();
res += '#';
res += ' ';
res += impl(frames[i].address());
Expand Down
6 changes: 3 additions & 3 deletions include/boost/stacktrace/detail/libbacktrace_impls.hpp
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand All @@ -13,9 +13,9 @@
#endif

#include <boost/stacktrace/detail/to_hex_array.hpp>
#include <boost/stacktrace/detail/to_dec_array.hpp>
#include <boost/stacktrace/detail/location_from_symbol.hpp>
#include <boost/core/demangle.hpp>
#include <boost/lexical_cast.hpp>

#include <backtrace.h>

Expand Down Expand Up @@ -99,7 +99,7 @@ struct to_string_using_backtrace {
res += " at ";
res += filename;
res += ':';
res += boost::lexical_cast<boost::array<char, 40> >(line).data();
res += boost::stacktrace::detail::to_dec_array(line).data();
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion include/boost/stacktrace/detail/location_from_symbol.hpp
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
2 changes: 1 addition & 1 deletion include/boost/stacktrace/detail/pop_options.h
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
2 changes: 1 addition & 1 deletion include/boost/stacktrace/detail/push_options.h
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
2 changes: 1 addition & 1 deletion include/boost/stacktrace/detail/safe_dump_noop.ipp
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
2 changes: 1 addition & 1 deletion include/boost/stacktrace/detail/safe_dump_posix.ipp
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
46 changes: 46 additions & 0 deletions include/boost/stacktrace/detail/to_dec_array.hpp
@@ -0,0 +1,46 @@
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_STACKTRACE_DETAIL_TO_DEC_ARRAY_HPP
#define BOOST_STACKTRACE_DETAIL_TO_DEC_ARRAY_HPP

#include <boost/config.hpp>
#ifdef BOOST_HAS_PRAGMA_ONCE
# pragma once
#endif

#include <boost/array.hpp>

namespace boost { namespace stacktrace { namespace detail {

// We do not use boost::lexical_cast in this function to reduce module dependencies
inline boost::array<char, 40> to_dec_array(std::size_t value) BOOST_NOEXCEPT {
boost::array<char, 40> ret;
if (!value) {
ret[0] = '0';
ret[1] = '\0';
return ret;
}

std::size_t digits = 0;
for (std::size_t value_copy = value; value_copy; value_copy /= 10) {
++ digits;
}

for (std::size_t i = 1; i <= digits; ++i) {
ret[digits - i] = '0' + (value % 10);
value /= 10;
}

ret[digits] = '\0';

return ret;
}


}}} // namespace boost::stacktrace::detail

#endif // BOOST_STACKTRACE_DETAIL_TO_DEC_ARRAY_HPP
2 changes: 1 addition & 1 deletion include/boost/stacktrace/detail/to_hex_array.hpp
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
29 changes: 29 additions & 0 deletions include/boost/stacktrace/detail/try_dec_convert.hpp
@@ -0,0 +1,29 @@
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef BOOST_STACKTRACE_DETAIL_TRY_DEC_CONVERT_HPP
#define BOOST_STACKTRACE_DETAIL_TRY_DEC_CONVERT_HPP

#include <boost/config.hpp>
#ifdef BOOST_HAS_PRAGMA_ONCE
# pragma once
#endif

#include <cstdlib>

namespace boost { namespace stacktrace { namespace detail {

// We do not use boost::lexical_cast in this function to reduce module dependencies
inline bool try_dec_convert(const char* s, std::size_t& res) BOOST_NOEXCEPT {
char* end_ptr = 0;
res = std::strtoul(s, &end_ptr, 10);
return *end_ptr == '\0';
}


}}} // namespace boost::stacktrace::detail

#endif // BOOST_STACKTRACE_DETAIL_TRY_DEC_CONVERT_HPP
2 changes: 1 addition & 1 deletion include/boost/stacktrace/detail/unwind_base_impls.hpp
@@ -1,4 +1,4 @@
// Copyright Antony Polukhin, 2016-2017.
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
Expand Down
2 changes: 1 addition & 1 deletion include/boost/stacktrace/detail/void_ptr_cast.hpp
@@ -1,5 +1,5 @@
// Copyright 2014 Renato Tegon Forti, Antony Polukhin.
// Copyright 2015-2017 Antony Polukhin.
// Copyright 2015-2018 Antony Polukhin.
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile.v2
Expand Up @@ -182,6 +182,7 @@ test-suite stacktrace_tests
: windbg_cached_lib_threaded_com_st ]

[ run test_void_ptr_cast.cpp ]
[ run test_num_conv.cpp ]
;

# Assuring that examples compile and run. Adding sources from `examples` directory to the `type_index` test suite.
Expand Down
69 changes: 69 additions & 0 deletions test/test_num_conv.cpp
@@ -0,0 +1,69 @@
// Copyright Antony Polukhin, 2016-2018.
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <boost/stacktrace/detail/to_dec_array.hpp>
#include <boost/stacktrace/detail/to_hex_array.hpp>
#include <boost/stacktrace/detail/try_dec_convert.hpp>

#include <boost/core/lightweight_test.hpp>


void test_to_hex_array() {
const void* ptr = 0;
BOOST_TEST(std::string(boost::stacktrace::detail::to_hex_array(ptr).data()).find("0x0") != std::string::npos);

ptr = reinterpret_cast<const void*>(0x10);
BOOST_TEST(std::string(boost::stacktrace::detail::to_hex_array(ptr).data()).find("10") != std::string::npos);

ptr = reinterpret_cast<void*>(0x19);
BOOST_TEST(std::string(boost::stacktrace::detail::to_hex_array(ptr).data()).find("19") != std::string::npos);

ptr = reinterpret_cast<void*>(0x999999);
BOOST_TEST(std::string(boost::stacktrace::detail::to_hex_array(ptr).data()).find("999999") != std::string::npos);
}

void test_to_dec_array() {
BOOST_TEST_EQ(std::string(boost::stacktrace::detail::to_dec_array(0).data()), std::string("0"));
BOOST_TEST_EQ(std::string(boost::stacktrace::detail::to_dec_array(10).data()), std::string("10"));
BOOST_TEST_EQ(std::string(boost::stacktrace::detail::to_dec_array(19).data()), std::string("19"));
BOOST_TEST_EQ(std::string(boost::stacktrace::detail::to_dec_array(999999).data()), std::string("999999"));
}

void test_try_dec_convert() {
std::size_t res = 0;

BOOST_TEST(boost::stacktrace::detail::try_dec_convert("0", res));
BOOST_TEST(res == 0);

BOOST_TEST(boost::stacktrace::detail::try_dec_convert("+0", res));
BOOST_TEST(res == 0);

BOOST_TEST(boost::stacktrace::detail::try_dec_convert("10", res));
BOOST_TEST(res == 10);

BOOST_TEST(boost::stacktrace::detail::try_dec_convert("19", res));
BOOST_TEST(res == 19);

BOOST_TEST(boost::stacktrace::detail::try_dec_convert("+19", res));
BOOST_TEST(res == 19);

BOOST_TEST(boost::stacktrace::detail::try_dec_convert("9999", res));
BOOST_TEST(res == 9999);

BOOST_TEST(!boost::stacktrace::detail::try_dec_convert("q", res));
BOOST_TEST(!boost::stacktrace::detail::try_dec_convert("0z", res));
BOOST_TEST(!boost::stacktrace::detail::try_dec_convert("0u", res));
BOOST_TEST(!boost::stacktrace::detail::try_dec_convert("+0u", res));
}


int main() {
test_to_hex_array();
test_to_dec_array();
test_try_dec_convert();

return boost::report_errors();
}

0 comments on commit 87b4789

Please sign in to comment.