Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use C++20 consteval to verify translation strings #30379

Closed
maflcko opened this issue Jul 3, 2024 · 3 comments
Closed

Use C++20 consteval to verify translation strings #30379

maflcko opened this issue Jul 3, 2024 · 3 comments

Comments

@maflcko
Copy link
Member

maflcko commented Jul 3, 2024

It is possible to write _(str_ptr), which is wrong, see #30267 (comment).

I am not familiar with the translation build system integration, but it would be good to effectively change the signature of _ from auto _(const char*) to consteval auto _(const char*).

My understanding is that this would catch this error at compile time, saving review effort and follow-up fixup changes.

Assumed compiler error message:

error: the value of 'reason' is not usable in a constant expression
      |   _(reason);
      |     ^~~~~~
@maflcko
Copy link
Member Author

maflcko commented Jul 3, 2024

diff --git a/src/util/translation.h b/src/util/translation.h
index d33fd2d0a0..7d82264a43 100644
--- a/src/util/translation.h
+++ b/src/util/translation.h
@@ -71,7 +71,7 @@ const extern std::function<std::string(const char*)> G_TRANSLATION_FUN;
  * Translation function.
  * If no translation function is set, simply return the input.
  */
-inline bilingual_str _(const char* psz)
+inline consteval bilingual_str _(const char* psz)
 {
     return bilingual_str{psz, G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(psz) : psz};
 }

does not work:

./util/translation.h:74:32: error: consteval function never produces a constant expression [-Winvalid-constexpr]
   74 | inline consteval bilingual_str _(const char* psz)
      |                                ^
./util/translation.h:76:31: note: non-constexpr function 'operator bool' cannot be used in a constant expression
   76 |     return bilingual_str{psz, G_TRANSLATION_FUN ? (G_TRANSLATION_FUN)(psz) : psz};
      |                               ^

So I guess this needs a clang-tidy plugin.

@maflcko
Copy link
Member Author

maflcko commented Jul 3, 2024

nvm, I'll open a pull request soon.

@maflcko maflcko closed this as completed Jul 3, 2024
@ryanofsky
Copy link
Contributor

So I guess this needs a clang-tidy plugin.

Clang-tidy plugin is not actually required because #30383 can catch the errors, IIUC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants