Common/MsgHandler: Add compile-time format string checks

This commit is contained in:
Léo Lam 2020-11-19 12:56:46 +01:00
parent 62eeb05519
commit dde6090e98
No known key found for this signature in database
GPG Key ID: 0DF30F9081000741
1 changed files with 26 additions and 12 deletions

View File

@ -4,11 +4,14 @@
#pragma once #pragma once
#include <cstdint>
#include <string> #include <string>
#include <utility> #include <utility>
#include <fmt/format.h> #include <fmt/format.h>
#include "Common/FormatUtil.h"
namespace Common namespace Common
{ {
// Message alerts // Message alerts
@ -37,9 +40,12 @@ bool MsgAlert(bool yes_no, MsgType style, const char* format, ...)
bool MsgAlertFmtImpl(bool yes_no, MsgType style, fmt::string_view format, bool MsgAlertFmtImpl(bool yes_no, MsgType style, fmt::string_view format,
const fmt::format_args& args); const fmt::format_args& args);
template <typename... Args> template <std::size_t NumFields, typename S, typename... Args>
bool MsgAlertFmt(bool yes_no, MsgType style, fmt::string_view format, const Args&... args) bool MsgAlertFmt(bool yes_no, MsgType style, const S& format, const Args&... args)
{ {
static_assert(NumFields == sizeof...(args),
"Unexpected number of replacement fields in format string; did you pass too few or "
"too many arguments?");
return MsgAlertFmtImpl(yes_no, style, format, fmt::make_args_checked<Args...>(format, args...)); return MsgAlertFmtImpl(yes_no, style, format, fmt::make_args_checked<Args...>(format, args...));
} }
@ -88,33 +94,41 @@ std::string FmtFormatT(const char* string, Args&&... args)
// Fmt-capable variants of the macros // Fmt-capable variants of the macros
#define GenericAlertFmt(yes_no, style, format, ...) \
[&] { \
/* Use a macro-like name to avoid shadowing warnings */ \
constexpr auto GENERIC_ALERT_FMT_N = Common::CountFmtReplacementFields(format); \
return Common::MsgAlertFmt<GENERIC_ALERT_FMT_N>(yes_no, style, FMT_STRING(format), \
##__VA_ARGS__); \
}()
#define SuccessAlertFmt(format, ...) \ #define SuccessAlertFmt(format, ...) \
Common::MsgAlertFmt(false, Common::MsgType::Information, FMT_STRING(format), ##__VA_ARGS__) GenericAlertFmt(false, Common::MsgType::Information, format, ##__VA_ARGS__)
#define PanicAlertFmt(format, ...) \ #define PanicAlertFmt(format, ...) \
Common::MsgAlertFmt(false, Common::MsgType::Warning, FMT_STRING(format), ##__VA_ARGS__) GenericAlertFmt(false, Common::MsgType::Warning, format, ##__VA_ARGS__)
#define PanicYesNoFmt(format, ...) \ #define PanicYesNoFmt(format, ...) \
Common::MsgAlertFmt(true, Common::MsgType::Warning, FMT_STRING(format), ##__VA_ARGS__) GenericAlertFmt(true, Common::MsgType::Warning, format, ##__VA_ARGS__)
#define AskYesNoFmt(format, ...) \ #define AskYesNoFmt(format, ...) \
Common::MsgAlertFmt(true, Common::MsgType::Question, FMT_STRING(format), ##__VA_ARGS__) GenericAlertFmt(true, Common::MsgType::Question, format, ##__VA_ARGS__)
#define CriticalAlertFmt(format, ...) \ #define CriticalAlertFmt(format, ...) \
Common::MsgAlertFmt(false, Common::MsgType::Critical, FMT_STRING(format), ##__VA_ARGS__) GenericAlertFmt(false, Common::MsgType::Critical, format, ##__VA_ARGS__)
// Use these macros (that do the same thing) if the message should be translated. // Use these macros (that do the same thing) if the message should be translated.
#define SuccessAlertFmtT(format, ...) \ #define SuccessAlertFmtT(format, ...) \
Common::MsgAlertFmt(false, Common::MsgType::Information, FMT_STRING(format), ##__VA_ARGS__) GenericAlertFmt(false, Common::MsgType::Information, format, ##__VA_ARGS__)
#define PanicAlertFmtT(format, ...) \ #define PanicAlertFmtT(format, ...) \
Common::MsgAlertFmt(false, Common::MsgType::Warning, FMT_STRING(format), ##__VA_ARGS__) GenericAlertFmt(false, Common::MsgType::Warning, format, ##__VA_ARGS__)
#define PanicYesNoFmtT(format, ...) \ #define PanicYesNoFmtT(format, ...) \
Common::MsgAlertFmt(true, Common::MsgType::Warning, FMT_STRING(format), ##__VA_ARGS__) GenericAlertFmt(true, Common::MsgType::Warning, format, ##__VA_ARGS__)
#define AskYesNoFmtT(format, ...) \ #define AskYesNoFmtT(format, ...) \
Common::MsgAlertFmt(true, Common::MsgType::Question, FMT_STRING(format), ##__VA_ARGS__) GenericAlertFmt(true, Common::MsgType::Question, format, ##__VA_ARGS__)
#define CriticalAlertFmtT(format, ...) \ #define CriticalAlertFmtT(format, ...) \
Common::MsgAlertFmt(false, Common::MsgType::Critical, FMT_STRING(format), ##__VA_ARGS__) GenericAlertFmt(false, Common::MsgType::Critical, format, ##__VA_ARGS__)