summaryrefslogtreecommitdiff
path: root/Source/Core
diff options
context:
space:
mode:
authorJosJuice <josjuice@gmail.com>2020-11-20 17:29:11 +0100
committerJosJuice <josjuice@gmail.com>2020-11-20 20:24:30 +0100
commite4ceed2bed247c18e3c4d72eca24fa9f7ef64bb1 (patch)
tree523dc852cbfa1609f59e8ef754b263e2795ae02d /Source/Core
parente63b00e562e5641fa4a24c5eb63f3d754a7d8ca5 (diff)
Common: Assert that translatable strings use positional arguments
Like PR 9260, but for a different requirement (see PR 9253).
Diffstat (limited to 'Source/Core')
-rw-r--r--Source/Core/Common/FormatUtil.h35
-rw-r--r--Source/Core/Common/MsgHandler.h20
2 files changed, 50 insertions, 5 deletions
diff --git a/Source/Core/Common/FormatUtil.h b/Source/Core/Common/FormatUtil.h
index 3c0bc3f262..1b33b74256 100644
--- a/Source/Core/Common/FormatUtil.h
+++ b/Source/Core/Common/FormatUtil.h
@@ -38,4 +38,39 @@ constexpr std::size_t CountFmtReplacementFields(std::string_view s)
static_assert(CountFmtReplacementFields("") == 0);
static_assert(CountFmtReplacementFields("{} test {:x}") == 2);
static_assert(CountFmtReplacementFields("{} {{}} test {{{}}}") == 2);
+
+constexpr bool ContainsNonPositionalArguments(std::string_view s)
+{
+ for (std::size_t i = 0; i < s.size(); ++i)
+ {
+ if (s[i] != '{' || i + 1 == s.size())
+ continue;
+
+ const char next = s[i + 1];
+
+ // If the opening brace is followed by another brace, what we have is
+ // an escaped brace, not a replacement field.
+ if (next == '{')
+ {
+ // Skip the second brace.
+ // This ensures that e.g. {{{}}} is counted correctly: when the first brace character
+ // is read and detected as being part of an '{{' escape sequence, the second character
+ // is skipped so the most inner brace (the third character) is not detected
+ // as the end of an '{{' pair.
+ ++i;
+ }
+ else if (next == '}' || next == ':')
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
+static_assert(!ContainsNonPositionalArguments(""));
+static_assert(ContainsNonPositionalArguments("{}"));
+static_assert(!ContainsNonPositionalArguments("{0}"));
+static_assert(ContainsNonPositionalArguments("{:x}"));
+static_assert(!ContainsNonPositionalArguments("{0:x}"));
+static_assert(!ContainsNonPositionalArguments("{0} {{}} test {{{1}}}"));
} // namespace Common
diff --git a/Source/Core/Common/MsgHandler.h b/Source/Core/Common/MsgHandler.h
index b06d2e63a9..76164a629b 100644
--- a/Source/Core/Common/MsgHandler.h
+++ b/Source/Core/Common/MsgHandler.h
@@ -102,6 +102,16 @@ std::string FmtFormatT(const char* string, Args&&... args)
##__VA_ARGS__); \
}()
+#define GenericAlertFmtT(yes_no, style, format, ...) \
+ [&] { \
+ static_assert(!Common::ContainsNonPositionalArguments(format), \
+ "Translatable strings must use positional arguments (e.g. {0} instead of {})"); \
+ /* 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, ...) \
GenericAlertFmt(false, Common::MsgType::Information, format, ##__VA_ARGS__)
@@ -119,16 +129,16 @@ std::string FmtFormatT(const char* string, Args&&... args)
// Use these macros (that do the same thing) if the message should be translated.
#define SuccessAlertFmtT(format, ...) \
- GenericAlertFmt(false, Common::MsgType::Information, format, ##__VA_ARGS__)
+ GenericAlertFmtT(false, Common::MsgType::Information, format, ##__VA_ARGS__)
#define PanicAlertFmtT(format, ...) \
- GenericAlertFmt(false, Common::MsgType::Warning, format, ##__VA_ARGS__)
+ GenericAlertFmtT(false, Common::MsgType::Warning, format, ##__VA_ARGS__)
#define PanicYesNoFmtT(format, ...) \
- GenericAlertFmt(true, Common::MsgType::Warning, format, ##__VA_ARGS__)
+ GenericAlertFmtT(true, Common::MsgType::Warning, format, ##__VA_ARGS__)
#define AskYesNoFmtT(format, ...) \
- GenericAlertFmt(true, Common::MsgType::Question, format, ##__VA_ARGS__)
+ GenericAlertFmtT(true, Common::MsgType::Question, format, ##__VA_ARGS__)
#define CriticalAlertFmtT(format, ...) \
- GenericAlertFmt(false, Common::MsgType::Critical, format, ##__VA_ARGS__)
+ GenericAlertFmtT(false, Common::MsgType::Critical, format, ##__VA_ARGS__)