Skip to content

Commit 4c7eaab

Browse files
authored
Use std::uintptr_t (#235)
And sync CMakeLists.txt with other modules
1 parent f114f11 commit 4c7eaab

8 files changed

Lines changed: 52 additions & 46 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
# Distributed under the Boost Software License, Version 1.0.
44
# https://www.boost.org/LICENSE_1_0.txt
55

6-
cmake_minimum_required(VERSION 3.8...4.20)
6+
cmake_minimum_required(VERSION 3.8...4.4)
77

88
project(boost_stacktrace VERSION "${BOOST_SUPERPROJECT_VERSION}" LANGUAGES CXX)
99

1010
function(stacktrace_add_module name import_std)
1111
target_sources(${name}
1212
PUBLIC
1313
FILE_SET CXX_MODULES
14-
BASE_DIRS "${CMAKE_CURRENT_LIST_DIR}/modules"
15-
FILES "${CMAKE_CURRENT_LIST_DIR}/modules/${name}.cppm"
14+
BASE_DIRS modules
15+
FILES "modules/${name}.cppm"
1616
)
1717
target_compile_definitions(${name} PUBLIC BOOST_USE_MODULES)
1818

1919
if(${import_std})
2020
target_compile_features(${name} PUBLIC cxx_std_23)
21-
target_compile_definitions(${name} PRIVATE BOOST_STACKTRACE_USE_STD_MODULE)
21+
target_compile_definitions(${name} PUBLIC BOOST_STACKTRACE_USE_STD_MODULE)
2222
else()
2323
target_compile_features(${name} PUBLIC cxx_std_20)
2424
endif()

‎include/boost/stacktrace/detail/addr2line_impls.hpp‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
# pragma once
1313
#endif
1414

15+
#include <cstring> // std::strchr
16+
#include <cstdio> // fclose, fdopen, dup2
17+
1518
#if !defined(BOOST_STACKTRACE_INTERFACE_UNIT)
1619
#include <boost/core/demangle.hpp>
1720

18-
#include <cstdio>
19-
#include <cstring>
2021

2122
#include <sys/types.h>
2223
#include <sys/wait.h>
@@ -163,11 +164,11 @@ inline std::string addr2line(const char* flag, const void* addr) {
163164
}
164165

165166
inline std::string source_location(const void* addr, bool position_independent) {
166-
uintptr_t addr_base = 0;
167+
std::uintptr_t addr_base = 0;
167168
if (position_independent) {
168169
addr_base = boost::stacktrace::detail::get_own_proc_addr_base(addr);
169170
}
170-
const void* offset = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) - addr_base);
171+
const void* offset = reinterpret_cast<void*>(reinterpret_cast<std::uintptr_t>(addr) - addr_base);
171172
std::string source_line = boost::stacktrace::detail::addr2line("-Cpe", reinterpret_cast<const void*>(offset));
172173
if (source_line.empty() || source_line[0] == '?') {
173174
return "";
@@ -210,11 +211,11 @@ template <class Base> class to_string_impl_base;
210211
typedef to_string_impl_base<to_string_using_addr2line> to_string_impl;
211212

212213
inline std::string name(const void* addr, bool position_independent) {
213-
uintptr_t addr_base = 0;
214+
std::uintptr_t addr_base = 0;
214215
if(position_independent){
215216
addr_base = boost::stacktrace::detail::get_own_proc_addr_base(addr);
216217
}
217-
const void* offset = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) - addr_base);
218+
const void* offset = reinterpret_cast<void*>(reinterpret_cast<std::uintptr_t>(addr) - addr_base);
218219
std::string res = boost::stacktrace::detail::addr2line("-fe", offset);
219220
res = res.substr(0, res.find_last_of('\n'));
220221
res = boost::core::demangle(res.c_str());
@@ -237,11 +238,11 @@ inline std::string name_impl(const void* addr) {
237238

238239
inline std::string source_file(const void* addr, bool position_independent) {
239240
std::string res;
240-
uintptr_t addr_base = 0;
241+
std::uintptr_t addr_base = 0;
241242
if(position_independent){
242243
addr_base = boost::stacktrace::detail::get_own_proc_addr_base(addr);
243244
}
244-
const void* offset = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) - addr_base);
245+
const void* offset = reinterpret_cast<void*>(reinterpret_cast<std::uintptr_t>(addr) - addr_base);
245246
res = boost::stacktrace::detail::addr2line("-e", offset);
246247
res = res.substr(0, res.find_last_of(':'));
247248
if (res == "??") {
@@ -253,11 +254,11 @@ inline std::string source_file(const void* addr, bool position_independent) {
253254

254255
inline std::size_t source_line(const void* addr, bool position_independent) {
255256
std::size_t line_num = 0;
256-
uintptr_t addr_base = 0;
257+
std::uintptr_t addr_base = 0;
257258
if(position_independent){
258259
addr_base = boost::stacktrace::detail::get_own_proc_addr_base(addr);
259260
}
260-
const void* offset = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) - addr_base);
261+
const void* offset = reinterpret_cast<void*>(reinterpret_cast<std::uintptr_t>(addr) - addr_base);
261262
std::string res = boost::stacktrace::detail::addr2line("-e", offset);
262263
const std::size_t last = res.find_last_of(':');
263264
if (last == std::string::npos) {

‎include/boost/stacktrace/detail/addr_base.hpp‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,26 @@
1515
#if !defined(BOOST_STACKTRACE_INTERFACE_UNIT) && !defined(BOOST_STACKTRACE_USE_STD_MODULE)
1616
#include <fstream>
1717
#include <sstream>
18-
#include <cstdint>
19-
#include <cstdlib>
18+
#include <cstdint> // std::uintptr_t
19+
#include <stdexcept>
20+
#include <string>
2021
#endif // !defined(BOOST_STACKTRACE_INTERFACE_UNIT) && !defined(BOOST_STACKTRACE_USE_STD_MODULE)
2122

2223
namespace boost { namespace stacktrace { namespace detail {
2324

2425
struct mapping_entry_t {
25-
uintptr_t start = 0;
26-
uintptr_t end = 0;
27-
uintptr_t offset_from_base = 0;
26+
std::uintptr_t start = 0;
27+
std::uintptr_t end = 0;
28+
std::uintptr_t offset_from_base = 0;
2829

2930
inline bool contains_addr(const void* addr) const {
30-
uintptr_t addr_uint = reinterpret_cast<uintptr_t>(addr);
31+
std::uintptr_t addr_uint = reinterpret_cast<std::uintptr_t>(addr);
3132
return addr_uint >= start && addr_uint < end;
3233
}
3334
};
3435

35-
inline uintptr_t hex_str_to_int(const std::string& str) {
36-
uintptr_t out;
36+
inline std::uintptr_t hex_str_to_int(const std::string& str) {
37+
std::uintptr_t out;
3738
std::stringstream ss;
3839
ss << std::hex << str;
3940
ss >> out;
@@ -70,12 +71,12 @@ inline mapping_entry_t parse_proc_maps_line(const std::string& line) {
7071
mapping.end = hex_str_to_int(mapping_end_str);
7172
mapping.offset_from_base = hex_str_to_int(offset_from_base_str);
7273
return mapping;
73-
} catch(std::invalid_argument& e) {
74+
} catch(const std::invalid_argument& e) {
7475
return mapping_entry_t{};
7576
}
7677
}
7778

78-
inline uintptr_t get_own_proc_addr_base(const void* addr) {
79+
inline std::uintptr_t get_own_proc_addr_base(const void* addr) {
7980
std::ifstream maps_file("/proc/self/maps");
8081
for (std::string line; std::getline(maps_file, line); ) {
8182
const mapping_entry_t mapping = parse_proc_maps_line(line);

‎include/boost/stacktrace/detail/frame_msvc.ipp‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public:
262262
nullptr,
263263
&base
264264
));
265-
265+
266266
if (res) {
267267
name[0] = '\0';
268268
size = 0;
@@ -303,7 +303,7 @@ public:
303303
}
304304
else if (res && size > 1) {
305305
module_name->assign(name, size - 1);
306-
}
306+
}
307307
}
308308
}
309309

@@ -402,8 +402,8 @@ public:
402402
res += to_hex_array(addr).data();
403403
#else
404404
// Get own base address
405-
const uintptr_t base_addr = get_own_proc_addr_base(addr);
406-
res += to_hex_array(reinterpret_cast<uintptr_t>(addr) - base_addr).data();
405+
const std::uintptr_t base_addr = get_own_proc_addr_base(addr);
406+
res += to_hex_array(reinterpret_cast<std::uintptr_t>(addr) - base_addr).data();
407407
#endif
408408
}
409409

‎include/boost/stacktrace/detail/frame_unwind.ipp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public:
5050
Base::res = to_hex_array(addr).data();
5151
#else
5252
const auto addr_base = boost::stacktrace::detail::get_own_proc_addr_base(addr);
53-
Base::res = to_hex_array(reinterpret_cast<uintptr_t>(addr) - addr_base).data();
53+
Base::res = to_hex_array(reinterpret_cast<std::uintptr_t>(addr) - addr_base).data();
5454
#endif
5555
}
5656

‎include/boost/stacktrace/detail/libbacktrace_impls.hpp‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ struct pc_data {
3535
std::size_t line;
3636
};
3737

38-
inline void libbacktrace_syminfo_callback(void *data, uintptr_t /*pc*/, const char *symname, uintptr_t /*symval*/, uintptr_t /*symsize*/) {
38+
inline void libbacktrace_syminfo_callback(void *data, std::uintptr_t /*pc*/, const char *symname, std::uintptr_t /*symval*/, std::uintptr_t /*symsize*/) {
3939
pc_data& d = *static_cast<pc_data*>(data);
4040
if (d.function && symname) {
4141
*d.function = symname;
4242
}
4343
}
4444

4545
// Old versions of libbacktrace have different signature for the callback
46-
inline void libbacktrace_syminfo_callback(void *data, uintptr_t pc, const char *symname, uintptr_t symval) {
46+
inline void libbacktrace_syminfo_callback(void *data, std::uintptr_t pc, const char *symname, std::uintptr_t symval) {
4747
boost::stacktrace::detail::libbacktrace_syminfo_callback(data, pc, symname, symval, 0);
4848
}
4949

50-
inline int libbacktrace_full_callback(void *data, uintptr_t /*pc*/, const char *filename, int lineno, const char *function) {
50+
inline int libbacktrace_full_callback(void *data, std::uintptr_t /*pc*/, const char *filename, int lineno, const char *function) {
5151
pc_data& d = *static_cast<pc_data*>(data);
5252
if (d.filename && filename) {
5353
*d.filename = filename;
@@ -129,15 +129,15 @@ struct to_string_using_backtrace {
129129
if (state) {
130130
::backtrace_pcinfo(
131131
state,
132-
reinterpret_cast<uintptr_t>(addr),
132+
reinterpret_cast<std::uintptr_t>(addr),
133133
boost::stacktrace::detail::libbacktrace_full_callback,
134134
boost::stacktrace::detail::libbacktrace_error_callback,
135135
&data
136-
)
136+
)
137137
||
138138
::backtrace_syminfo(
139139
state,
140-
reinterpret_cast<uintptr_t>(addr),
140+
reinterpret_cast<std::uintptr_t>(addr),
141141
boost::stacktrace::detail::libbacktrace_syminfo_callback,
142142
boost::stacktrace::detail::libbacktrace_error_callback,
143143
&data
@@ -176,15 +176,15 @@ inline std::string name_impl(const void* addr) {
176176
if (state) {
177177
::backtrace_pcinfo(
178178
state,
179-
reinterpret_cast<uintptr_t>(addr),
179+
reinterpret_cast<std::uintptr_t>(addr),
180180
boost::stacktrace::detail::libbacktrace_full_callback,
181181
boost::stacktrace::detail::libbacktrace_error_callback,
182182
&data
183183
)
184184
||
185185
::backtrace_syminfo(
186186
state,
187-
reinterpret_cast<uintptr_t>(addr),
187+
reinterpret_cast<std::uintptr_t>(addr),
188188
boost::stacktrace::detail::libbacktrace_syminfo_callback,
189189
boost::stacktrace::detail::libbacktrace_error_callback,
190190
&data
@@ -213,7 +213,7 @@ std::string frame::source_file() const {
213213
if (state) {
214214
::backtrace_pcinfo(
215215
state,
216-
reinterpret_cast<uintptr_t>(addr_),
216+
reinterpret_cast<std::uintptr_t>(addr_),
217217
boost::stacktrace::detail::libbacktrace_full_callback,
218218
boost::stacktrace::detail::libbacktrace_error_callback,
219219
&data
@@ -235,7 +235,7 @@ std::size_t frame::source_line() const {
235235
if (state) {
236236
::backtrace_pcinfo(
237237
state,
238-
reinterpret_cast<uintptr_t>(addr_),
238+
reinterpret_cast<std::uintptr_t>(addr_),
239239
boost::stacktrace::detail::libbacktrace_full_callback,
240240
boost::stacktrace::detail::libbacktrace_error_callback,
241241
&data

‎modules/boost_stacktrace_dump.cppm‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ module;
1515
#include <boost/predef.h>
1616

1717
#if !defined(BOOST_STACKTRACE_USE_STD_MODULE)
18-
#include <stdio.h>
18+
#include <cstdio>
19+
#include <cstdint> // std::uintptr_t
1920
#endif
2021

2122
#include <fcntl.h>
22-
#include <unwind.h>
2323
#include <sys/stat.h>
2424

25+
#if !defined(BOOST_WINDOWS)
26+
# include <unwind.h>
27+
#endif
28+
2529
#define BOOST_STACKTRACE_INTERFACE_UNIT
2630
#define BOOST_STACKTRACE_LINK
2731

‎test/test.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void test_nested(bool print = true) {
8585

8686
BOOST_TEST(ss1.str().find(" 1# ") != std::string::npos);
8787
BOOST_TEST(ss2.str().find(" 1# ") != std::string::npos);
88-
88+
8989
BOOST_TEST(ss1.str().find(" in ") != std::string::npos);
9090
BOOST_TEST(ss2.str().find(" in ") != std::string::npos);
9191

@@ -212,7 +212,7 @@ void test_frame() {
212212
BOOST_TEST(fv);
213213
if (i > 1 && i < min_size - 3) { // Begin ...and end of the trace may match, skipping
214214
BOOST_TEST(st[i] != fv);
215-
215+
216216
#if !(defined(BOOST_STACKTRACE_TEST_NO_DEBUG_AT_ALL) && defined(BOOST_MSVC))
217217
// MSVC can not get function name withhout debug symbols even if it is exported
218218
BOOST_TEST(st[i].name() != fv.name());
@@ -276,15 +276,15 @@ std::size_t get_file_size(const char* file_name) {
276276
return static_cast<std::size_t>(file_size);
277277
}
278278

279-
uintptr_t get_address_from_frame(const std::string& frame) {
279+
std::uintptr_t get_address_from_frame(const std::string& frame) {
280280
std::size_t address = 0;
281281
std::string hex_address;
282282
std::size_t pos = frame.find("0x");
283283

284284
if (pos != std::string::npos) {
285285
// Extract the hex address substring
286286
hex_address = frame.substr(pos + 2); // Skip "0x"
287-
287+
288288
// Convert hex string to std::size_t
289289
std::stringstream ss;
290290
ss << std::hex << hex_address;
@@ -295,7 +295,7 @@ uintptr_t get_address_from_frame(const std::string& frame) {
295295
}
296296

297297
void test_relative_virtual_address(const char* file_path)
298-
{
298+
{
299299
const auto frame = to_string(boost::stacktrace::stacktrace(0, 1).as_vector().front());
300300

301301
// Skip the test if the frame does not contain an address

0 commit comments

Comments
 (0)