Check translator comments with clang-tidy #34243
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
SUMMARY: Build "Check translator comments with clang-tidy"
Purpose of change
Translator comments need to be preceding the extracted string and on the same or adjacent line to be properly recognized by xgettext. However, sometimes the translation calls need to be split into multiple lines due to styling, and newly added code can also unintentionally separate the comments from the extracted string. Because this kind of errors are not always obvious, this PR adds a clang-tidy check to check translator comments that does not have a matching string.
Describe the solution
Translator comments and macros are first recorded. AST matchers are then used to match strings in translation function calls and macros. Comments correctly preceding the found strings are then marked. After all strings are processed, unmarked comments are considered to be misplaced, and reported as errors.
Describe alternatives you've considered
A regex solution would be complicated and hard to maintain, due to the fact that a translator comment can be positioned after the translation function and before the extracted string.