Skip to main content

reflection.h File

C++ header file with declarations for the µTest++ reflection utilities. More...

Included Headers

#include <string_view> #include "inlines/reflection-inlines.h"

Namespaces Index

namespacemicro_os_plus

The primary namespace for the µOS++ framework. More...

namespacemicro_test_plus

Primary namespace for the µTest++ testing framework. More...

namespacereflection

Reflection utilities for the µTest++ testing framework. More...

Classes Index

classsource_location

Local implementation of source location information for diagnostics. More...

Functions Index

const char *short_name (const char *name) noexcept

Extract a short type or function name from a fully qualified name. More...

template <class T>
constexpr std::string_viewtype_name (void)

Extract the type name from the __PRETTY_FUNCTION__ macro. More...

Description

C++ header file with declarations for the µTest++ reflection utilities.

This header provides the declarations for the reflection utilities used within the µTest++ framework. It defines interfaces for obtaining source location information and extracting type names at compile time, supporting advanced diagnostics and reporting capabilities.

The reflection utilities include a local implementation of source_location for environments lacking C++20 standard support, as well as functions for extracting concise type names using compiler-specific macros such as __PRETTY_FUNCTION__. These facilities enable precise identification of code locations and types in test reports, enhancing the clarity and professionalism of diagnostic output.

All definitions reside within the micro_os_plus::micro_test_plus::reflection namespace, ensuring clear separation from user code and minimising the risk of naming conflicts.

The header files are organised within the include/micro-os-plus/micro-test-plus folder to maintain a structured and modular codebase.

This file is intended solely for internal use within the framework and should not be included directly by user code.

Functions

short_name()

const char * micro_os_plus::micro_test_plus::reflection::short_name (const char * name)
noexcept

Extract a short type or function name from a fully qualified name.

Parameters
name

The fully qualified name as a C-string.

Returns

A pointer to the short name within the input string.

This function extracts the short name from a given file path by locating the final folder separator ('/'). If a separator is found, it returns a pointer to the character immediately following it, effectively providing the file or folder name. If no separator is present, the original input string is returned. This utility is useful for reporting concise file or folder names in test output.

Definition at line 74 of file reflection.cpp.

74 short_name (const char* name) noexcept
75 {
76#if defined(__GNUC__)
77#pragma GCC diagnostic push
78#if defined(__clang__)
79#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
80#endif
81#endif
82 const char* p = strrchr (name, '/');
83 if (p != nullptr)
84 return p + 1;
85 else
86 return name;
87#if defined(__GNUC__)
88#pragma GCC diagnostic pop
89#endif
90 }

Referenced by micro_os_plus::micro_test_plus::runner::abort, micro_os_plus::micro_test_plus::reporter_human::output_fail_prefix_ and micro_os_plus::micro_test_plus::reporter_tap::output_fail_suffix_.

type_name()

template <class T>
std::string_view micro_os_plus::micro_test_plus::reflection::type_name (void)
nodiscard constexpr

Extract the type name from the __PRETTY_FUNCTION__ macro.

This function template parses the compiler-specific __PRETTY_FUNCTION__ macro to extract a concise type name for the template parameter T.

Rather than relying on fixed character offsets (which are fragile across compiler versions and namespace changes), the implementation searches for well-known marker characters in the function signature string:

  • Clang formats the signature as "... [T = <typename>]", so the type name lies between the last '[' (skipping "[T = ") and the last ']'.
  • GCC formats the signature as "... [with T = <typename>]", so the type name lies between the last '=' (skipping the trailing space) and the last ']'.

This approach is resilient to namespace renaming, namespace nesting changes, and compiler format updates.

Template Parameters
T

The type whose name is to be extracted.

Parameters

None.

Returns

A std::string_view containing the extracted type name.

Definition at line 145 of file reflection-inlines.h.

145 type_name (void) -> std::string_view
146 {
147 const std::string_view sv = __PRETTY_FUNCTION__;
148#if defined(__clang__)
149 // Clang: "... [T = <typename>]"
150 // rfind('[') locates the opening bracket of "[T = ...]".
151 const auto start = sv.rfind ('[') + 5; // skip "[T = "
152 const auto end = sv.rfind (']');
153#elif defined(__GNUC__)
154 // GCC: "... [with T = <typename>]" or, on some versions,
155 // "... [with T = <typename>; std::string_view = ...]"
156 // Search for "T = " explicitly to avoid landing on a later '='.
157 const auto t_eq = sv.find ("T = ");
158 const auto start = t_eq + 4; // skip "T = "
159 const auto semi = sv.find (';', start);
160 const auto end
161 = (semi != std::string_view::npos) ? semi : sv.rfind (']');
162#else
163// Note: MSVC uses __FUNCSIG__ instead of __PRETTY_FUNCTION__.
164// MSVC is not a supported target for this framework.
165#error "Unsupported compiler"
166#endif
167 return sv.substr (start, end - start);
168 }

Referenced by micro_os_plus::micro_test_plus::detail::expression_formatter::operator<<.

File Listing

The file content with the documentation metadata removed is:

1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can be
9 * obtained from https://opensource.org/licenses/mit.
10 *
11 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
12 * released under the terms of the Boost Version 1 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
47
48#ifndef MICRO_TEST_PLUS_REFLECTION_H_
49#define MICRO_TEST_PLUS_REFLECTION_H_
50
51// ----------------------------------------------------------------------------
52
53#ifdef __cplusplus
54
55// ----------------------------------------------------------------------------
56
57#include <string_view>
58
59#if defined(__cpp_lib_source_location)
60#include <source_location>
61#endif
62
63// ----------------------------------------------------------------------------
64
65#if defined(__GNUC__)
66#pragma GCC diagnostic push
67#pragma GCC diagnostic ignored "-Wpadded"
68#pragma GCC diagnostic ignored "-Waggregate-return"
69#if defined(__clang__)
70#pragma clang diagnostic ignored "-Wc++98-compat"
71#endif
72#endif
73
74// =============================================================================
75
77{
78 // --------------------------------------------------------------------------
79
97 namespace reflection
98 {
99 // ------------------------------------------------------------------------
100
101#if defined(__cpp_lib_source_location)
115 using source_location = std::source_location;
116#else
138 {
139 public:
147 [[nodiscard]] static constexpr source_location
148 current (
149#if (__has_builtin(__builtin_FILE) and __has_builtin(__builtin_LINE))
150 const char* file = __builtin_FILE (),
151 unsigned int line = __builtin_LINE ()
152#else
153 const char* file = "unknown", unsigned int line = {}
154#endif
155 ) noexcept;
156
164 [[nodiscard]] constexpr auto
165 file_name (void) const noexcept;
166
174 [[nodiscard]] constexpr auto
175 line (void) const noexcept;
176
177 private:
181 const char* file_{ "unknown" };
182
186 unsigned int line_{};
187 };
188
189#endif
190
198 const char*
199 short_name (const char* name) noexcept;
200
210 template <class T>
211 [[nodiscard]] constexpr auto
212 type_name (void) -> std::string_view;
213
214 // ------------------------------------------------------------------------
215 } // namespace reflection
216
217 // --------------------------------------------------------------------------
218} // namespace micro_os_plus::micro_test_plus
219
220#if defined(__GNUC__)
221#pragma GCC diagnostic pop
222#endif
223
224// ----------------------------------------------------------------------------
225
226#endif // __cplusplus
227
228// ============================================================================
229// Templates & constexpr implementations.
230
232
233// ----------------------------------------------------------------------------
234
235#endif // MICRO_TEST_PLUS_REFLECTION_H_
236
237// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.2.0 by Doxygen 1.17.0.