Skip to content

Commit ba318c1

Browse files
authored
Add std:string overload for Debug (#255)
Add an overload in debug for std:string based on the pattern for std::string_view
1 parent de10585 commit ba318c1

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

sdk/include/debug.hh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,19 @@ struct DebugFormatArgumentAdaptor<std::string_view>
284284
}
285285
};
286286

287+
/**
288+
* String view specialisation, use the C string handler.
289+
*/
290+
template<>
291+
struct DebugFormatArgumentAdaptor<std::string>
292+
{
293+
__always_inline static DebugFormatArgument construct(std::string &value)
294+
{
295+
return DebugFormatArgumentAdaptor<const char *>::construct(
296+
value.c_str());
297+
}
298+
};
299+
287300
/**
288301
* Enum specialisation, prints the enum as a string and then the numeric value.
289302
*

tests/test-runner.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
#include "tests.hh"
55
#include <compartment.h>
66
#include <simulator.h>
7+
#include <string>
78

89
using namespace CHERI;
10+
using namespace std::string_literals;
911

1012
namespace
1113
{
@@ -104,10 +106,10 @@ void __cheri_compartment("test_runner") run_tests()
104106
0x123456789012345ULL);
105107
const char *testString = "Hello, world! with some trailing characters";
106108
// Make sure that we don't print the trailing characters
107-
debug_log("Trying to print string: {}", std::string_view{testString, 13});
108-
std::string hi = "Hello world";
109-
debug_log("Trying to print std::string: {}", hi.c_str());
110-
109+
debug_log("Trying to print std::string_view: {}",
110+
std::string_view{testString, 13});
111+
const std::string S = "I am a walrus"s;
112+
debug_log("Trying to print std::string: {}", S);
111113
run_timed("All tests", []() {
112114
run_timed("Debug helpers (C++)", test_debug_cxx);
113115
run_timed("Debug helpers (C)", test_debug_c);

0 commit comments

Comments
 (0)