Skip to content

Commit 933a605

Browse files
committed
Fixes for clang-17
More tests for magic_enum
1 parent fd4a964 commit 933a605

File tree

6 files changed

+66
-12
lines changed

6 files changed

+66
-12
lines changed

cmake/clang_warnings.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ set(WARNING_FLAGS_VERSION16
4747
-Wno-unsafe-buffer-usage
4848
)
4949

50-
set(WARNING_FLAGS_VERSION18
50+
set(WARNING_FLAGS_VERSION17
5151

5252
# Own parameter
5353
-Wno-switch-default
@@ -63,8 +63,8 @@ if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16)
6363
endforeach()
6464
endif()
6565

66-
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 18 AND NOT CMAKE_CXX_COMPILER_ID MATCHES AppleClang)
67-
foreach(WARNING_FLAG ${WARNING_FLAGS_VERSION18})
66+
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 17)
67+
foreach(WARNING_FLAG ${WARNING_FLAGS_VERSION17})
6868
set(WARNING_FLAGS_SPACED "${WARNING_FLAGS_SPACED} ${WARNING_FLAG}")
6969
endforeach()
7070
endif()

source/Demangle.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ namespace vx::demangle {
6969
const std::unique_ptr<char, void ( * )( char * )> res {
7070

7171
abi::__cxa_demangle( _name.c_str(), nullptr, nullptr, &status ),
72-
[]( char *_toFree ) { std::free( _toFree ); } // NOSONAR raii is nor possible here.
72+
[]( char *_toFree ) { std::free( _toFree ); } // NOSONAR raii is not possible here.
7373
};
7474

7575
if ( status == 0 ) {

source/StringUtils.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,15 @@ namespace vx::string_utils {
215215
return result;
216216
}
217217

218-
std::optional<std::string> fromUnsignedChar( const unsigned char *_uchr ) noexcept {
218+
std::optional<std::string> MAYBE_BAD_fromUnsignedChar( const unsigned char *_uchr ) noexcept {
219219

220220
/* nullptr check is mandatory */
221221
if ( !_uchr ) { return {}; }
222222

223-
std::basic_string<unsigned char> string {};
223+
std::string string {};
224224
try {
225225

226-
string = _uchr;
226+
string = reinterpret_cast<const char *>( _uchr );
227227
}
228228
catch( const std::exception &_exception ) {
229229

@@ -244,10 +244,10 @@ namespace vx::string_utils {
244244
#if defined __linux__ && defined __clang__ && __clang_major__ >= 18
245245
size = std::strlen( reinterpret_cast<const char *>( _uchr ) );
246246
#else
247-
std::basic_string<unsigned char> string {};
247+
std::string string {};
248248
try {
249249

250-
string = _uchr;
250+
string = reinterpret_cast<const char *>( _uchr );
251251
}
252252
catch( const std::exception &_exception ) {
253253

source/StringUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ namespace vx::string_utils {
146146
* @return Standard string from unsigned char.
147147
* @note This function may throw an exception by the constructor of std::string.
148148
*/
149-
[[nodiscard]] std::optional<std::string> fromUnsignedChar( const unsigned char *_uchr ) noexcept;
149+
[[nodiscard]] std::optional<std::string> MAYBE_BAD_fromUnsignedChar( const unsigned char *_uchr ) noexcept;
150150

151151
/**
152152
* @brief Returns standard string from unsigned char.

tests/test_magic_enum.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,60 @@ namespace vx {
338338
EXPECT_TRUE( std::is_scoped_enum_v<Color> );
339339
EXPECT_FALSE( std::is_scoped_enum_v<int> );
340340
}
341+
342+
TEST( MagicEnum, Extend ) {
343+
344+
enum class QNetworkReply {
345+
346+
ConnectionRefusedError = 1,
347+
RemoteHostClosedError = 2,
348+
HostNotFoundError = 3,
349+
TimeoutError = 4,
350+
OperationCanceledError = 5,
351+
SslHandshakeFailedError = 6,
352+
TemporaryNetworkFailureError = 7,
353+
NetworkSessionFailedError = 8,
354+
BackgroundRequestNotAllowedError = 9,
355+
TooManyRedirectsError = 10,
356+
InsecureRedirectError = 11,
357+
ProxyConnectionRefusedError = 101,
358+
ProxyConnectionClosedError = 102,
359+
ProxyNotFoundError = 103,
360+
ProxyTimeoutError = 104,
361+
ProxyAuthenticationRequiredError = 105,
362+
ContentAccessDenied = 201,
363+
ContentOperationNotPermittedError = 202,
364+
ContentNotFoundError = 203,
365+
AuthenticationRequiredError = 204,
366+
ContentReSendError = 205,
367+
ContentConflictError = 206,
368+
ContentGoneError = 207,
369+
InternalServerError = 401,
370+
OperationNotImplementedError = 402,
371+
ServiceUnavailableError = 403,
372+
ProtocolUnknownError = 301,
373+
ProtocolInvalidOperationError = 302,
374+
UnknownNetworkError = 99,
375+
UnknownProxyError = 199,
376+
UnknownContentError = 299,
377+
ProtocolFailure = 399,
378+
UnknownServerError = 499
379+
};
380+
381+
const std::optional seven = magic_enum::enum_cast<QNetworkReply>( 7 );
382+
EXPECT_TRUE( seven.has_value() );
383+
384+
/* Default maximum value of magic_enum is 128 - so this test is fale, without configuration */
385+
const std::optional threeZeroOne = magic_enum::enum_cast<QNetworkReply>( 301 );
386+
EXPECT_FALSE( threeZeroOne.has_value() );
387+
388+
const std::optional threeOneEight = magic_enum::enum_cast<QNetworkReply>( 318 );
389+
EXPECT_FALSE( threeOneEight.has_value() );
390+
391+
/* Magic_enum gives the name, but cannot handle the id */
392+
constexpr auto unkownServerError = magic_enum::enum_name<QNetworkReply::UnknownServerError>();
393+
EXPECT_EQ( unkownServerError, "UnknownServerError" );
394+
}
341395
}
342396
#ifdef __clang__
343397
#pragma clang diagnostic pop

tests/test_string_utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,15 @@ namespace vx {
180180

181181
unsigned char chrArray[] = "The answer is 42."; // NOSONAR just for testing purpose.
182182
const unsigned char *chrPointer = chrArray;
183-
EXPECT_EQ( string_utils::fromUnsignedChar( chrPointer ), "The answer is 42." );
183+
EXPECT_EQ( string_utils::MAYBE_BAD_fromUnsignedChar( chrPointer ), "The answer is 42." );
184184
EXPECT_EQ( string_utils::MAYBE_BAD_fromUnsignedChar( chrPointer, 0 ), "The answer is 42." );
185185
EXPECT_EQ( string_utils::MAYBE_BAD_fromUnsignedChar( chrPointer, 17 ), "The answer is 42." );
186186
EXPECT_EQ( string_utils::MAYBE_BAD_fromUnsignedChar( chrPointer, 10 ), "The answer" );
187187
EXPECT_NE( string_utils::MAYBE_BAD_fromUnsignedChar( chrPointer, 5 ), "The answer is 42." );
188188

189189
/* nullptr unsigned char */
190190
const unsigned char *chrPointerNull = nullptr;
191-
EXPECT_EQ( string_utils::fromUnsignedChar( chrPointerNull ), std::nullopt );
191+
EXPECT_EQ( string_utils::MAYBE_BAD_fromUnsignedChar( chrPointerNull ), std::nullopt );
192192
EXPECT_EQ( string_utils::MAYBE_BAD_fromUnsignedChar( chrPointerNull, 0 ), std::nullopt );
193193

194194
/* Wrong size check - more than expected. */

0 commit comments

Comments
 (0)