Skip to content

Commit 028bffa

Browse files
authored
Update checks for dynamic_cast usage when compiled with no rtti (#3963)
* Rename FMT_USE_TYPEID to FMT_HAS_RTTI and use it as check to enable dynamic_cast usage * FMT_HAS_RTTI->FMT_USE_RTTI
1 parent 86741b3 commit 028bffa

File tree

3 files changed

+15
-15
lines changed

3 files changed

+15
-15
lines changed

include/fmt/base.h

+11
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,17 @@ import std;
279279
# define FMT_UNICODE 1
280280
#endif
281281

282+
// Check if rtti is available.
283+
#ifndef FMT_USE_RTTI
284+
// __RTTI is for EDG compilers. _CPPRTTI is for MSVC.
285+
# if defined(__GXX_RTTI) || FMT_HAS_FEATURE(cxx_rtti) || defined(_CPPRTTI) || \
286+
defined(__INTEL_RTTI__) || defined(__RTTI)
287+
# define FMT_USE_RTTI 1
288+
# else
289+
# define FMT_USE_RTTI 0
290+
# endif
291+
#endif
292+
282293
#define FMT_FWD(...) static_cast<decltype(__VA_ARGS__)&&>(__VA_ARGS__)
283294

284295
// Enable minimal optimizations for more compact code in debug mode.

include/fmt/ostream.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ auto get_file(std::filebuf&) -> FILE*;
4444
inline auto write_ostream_unicode(std::ostream& os, fmt::string_view data)
4545
-> bool {
4646
FILE* f = nullptr;
47-
#if FMT_MSC_VERSION
47+
#if FMT_MSC_VERSION && FMT_USE_RTTI
4848
if (auto* buf = dynamic_cast<std::filebuf*>(os.rdbuf()))
4949
f = get_file(*buf);
5050
else
5151
return false;
52-
#elif defined(_WIN32) && defined(__GLIBCXX__)
52+
#elif defined(_WIN32) && defined(__GLIBCXX__) && FMT_USE_RTTI
5353
auto* rdbuf = os.rdbuf();
5454
if (auto* sfbuf = dynamic_cast<__gnu_cxx::stdio_sync_filebuf<char>*>(rdbuf))
5555
f = sfbuf->file();

include/fmt/std.h

+2-13
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,6 @@
6262
# endif
6363
#endif
6464

65-
// Check if typeid is available.
66-
#ifndef FMT_USE_TYPEID
67-
// __RTTI is for EDG compilers. _CPPRTTI is for MSVC.
68-
# if defined(__GXX_RTTI) || FMT_HAS_FEATURE(cxx_rtti) || defined(_CPPRTTI) || \
69-
defined(__INTEL_RTTI__) || defined(__RTTI)
70-
# define FMT_USE_TYPEID 1
71-
# else
72-
# define FMT_USE_TYPEID 0
73-
# endif
74-
#endif
75-
7665
// For older Xcode versions, __cpp_lib_xxx flags are inaccurately defined.
7766
#ifndef FMT_CPP_LIB_FILESYSTEM
7867
# ifdef __cpp_lib_filesystem
@@ -443,7 +432,7 @@ struct formatter<
443432
if (it == end || *it == '}') return it;
444433
if (*it == 't') {
445434
++it;
446-
with_typename_ = FMT_USE_TYPEID != 0;
435+
with_typename_ = FMT_USE_RTTI != 0;
447436
}
448437
return it;
449438
}
@@ -455,7 +444,7 @@ struct formatter<
455444
if (!with_typename_)
456445
return detail::write_bytes<Char>(out, string_view(ex.what()));
457446

458-
#if FMT_USE_TYPEID
447+
#if FMT_USE_RTTI
459448
const std::type_info& ti = typeid(ex);
460449
# ifdef FMT_HAS_ABI_CXA_DEMANGLE
461450
int status = 0;

0 commit comments

Comments
 (0)