Enable log macros to work with no args (#4475)

This is enabled by the C++20 __VA_OPT__ feature.
Uses of "{}" format string were updated.
Some warning suppressions were required for older clang versions.
Also improve codegen of wpi::Logger::Log(), frc::ReportError(), and frc::MakeError();
these generate better and less redundant code if they use fmt::string_view for the
format string instead of templating on it.
This commit is contained in:
Tyler Veness
2022-10-19 10:49:27 -07:00
committed by GitHub
parent 878cc8defb
commit 1fc098e696
70 changed files with 373 additions and 338 deletions

View File

@@ -74,9 +74,10 @@ void ReportErrorV(int32_t status, const char* fileName, int lineNumber,
* @param[in] format error message format
* @param[in] args error message format args
*/
template <typename S, typename... Args>
template <typename... Args>
inline void ReportError(int32_t status, const char* fileName, int lineNumber,
const char* funcName, const S& format, Args&&... args) {
const char* funcName, fmt::string_view format,
Args&&... args) {
ReportErrorV(status, fileName, lineNumber, funcName, format,
fmt::make_format_args(args...));
}
@@ -99,12 +100,10 @@ inline void ReportError(int32_t status, const char* fileName, int lineNumber,
fmt::string_view format,
fmt::format_args args);
template <typename S, typename... Args>
[[nodiscard]] inline RuntimeError MakeError(int32_t status,
const char* fileName,
int lineNumber,
const char* funcName,
const S& format, Args&&... args) {
template <typename... Args>
[[nodiscard]] inline RuntimeError MakeError(
int32_t status, const char* fileName, int lineNumber, const char* funcName,
fmt::string_view format, Args&&... args) {
return MakeErrorV(status, fileName, lineNumber, funcName, format,
fmt::make_format_args(args...));
}
@@ -122,18 +121,24 @@ namespace warn {
} // namespace warn
} // namespace frc
// C++20 relaxed the number of arguments to variadics, but Apple Clang's
// warnings haven't caught up yet: https://stackoverflow.com/a/67996331
#ifdef __clang__
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments"
#endif
/**
* Reports an error to the driver station (using HAL_SendError).
*
* @param[out] status error code
* @param[in] format error message format
*/
#define FRC_ReportError(status, format, ...) \
do { \
if ((status) != 0) { \
::frc::ReportError(status, __FILE__, __LINE__, __FUNCTION__, \
FMT_STRING(format), __VA_ARGS__); \
} \
#define FRC_ReportError(status, format, ...) \
do { \
if ((status) != 0) { \
::frc::ReportError(status, __FILE__, __LINE__, __FUNCTION__, \
FMT_STRING(format) __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
/**
@@ -146,7 +151,7 @@ namespace warn {
*/
#define FRC_MakeError(status, format, ...) \
::frc::MakeError(status, __FILE__, __LINE__, __FUNCTION__, \
FMT_STRING(format), __VA_ARGS__)
FMT_STRING(format) __VA_OPT__(, ) __VA_ARGS__)
/**
* Checks a status code and depending on its value, either throws a
@@ -155,23 +160,24 @@ namespace warn {
* @param[out] status error code
* @param[in] format error message format
*/
#define FRC_CheckErrorStatus(status, format, ...) \
do { \
if ((status) < 0) { \
throw ::frc::MakeError(status, __FILE__, __LINE__, __FUNCTION__, \
FMT_STRING(format), __VA_ARGS__); \
} else if ((status) > 0) { \
::frc::ReportError(status, __FILE__, __LINE__, __FUNCTION__, \
FMT_STRING(format), __VA_ARGS__); \
} \
#define FRC_CheckErrorStatus(status, format, ...) \
do { \
if ((status) < 0) { \
throw ::frc::MakeError(status, __FILE__, __LINE__, __FUNCTION__, \
FMT_STRING(format) __VA_OPT__(, ) __VA_ARGS__); \
} else if ((status) > 0) { \
::frc::ReportError(status, __FILE__, __LINE__, __FUNCTION__, \
FMT_STRING(format) __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
#define FRC_AssertMessage(condition, format, ...) \
do { \
if (!(condition)) { \
throw ::frc::MakeError(err::AssertionFailure, __FILE__, __LINE__, \
__FUNCTION__, FMT_STRING(format), __VA_ARGS__); \
__FUNCTION__, \
FMT_STRING(format) __VA_OPT__(, ) __VA_ARGS__); \
} \
} while (0)
#define FRC_Assert(condition) FRC_AssertMessage(condition, "{}", #condition)
#define FRC_Assert(condition) FRC_AssertMessage(condition, #condition)

View File

@@ -34,7 +34,7 @@ void RunRobot(wpi::mutex& m, Robot** robot) {
} catch (const frc::RuntimeError& e) {
e.Report();
FRC_ReportError(
err::Error, "{}",
err::Error,
"The robot program quit unexpectedly."
" This is usually due to a code error.\n"
" The above stacktrace can help determine where the error occurred.\n"