Skip to content

Commit

Permalink
Merge pull request #45250 from Qrox/translator-comments-check
Browse files Browse the repository at this point in the history
Properly implement translator comments check for the _() macro
  • Loading branch information
ZhilkinSerg authored Nov 5, 2020
2 parents f390470 + 69c8333 commit 146d314
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
9 changes: 1 addition & 8 deletions src/translations.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,14 @@ static inline local_translation_cache<std::string> get_local_translation_cache(
return local_translation_cache<std::string>();
}

// this function is used as a marker for clang-tidy check (see TranslatorCommentsCheck.cpp)
template <typename T>
static inline auto translation_macro_marker_func( T &&arg )
{
return std::forward<T>( arg );
}

} // namespace detail

// Note: in case of std::string argument, the result is copied, this is intended (for safety)
#define _( msg ) \
( ( []( const auto & arg ) { \
static auto cache = detail::get_local_translation_cache( arg ); \
return cache( arg ); \
} )( detail::translation_macro_marker_func( msg ) ) )
} )( msg ) )

// ngettext overload taking an unsigned long long so that people don't need
// to cast at call sites. This is particularly relevant on 64-bit Windows where
Expand Down
14 changes: 11 additions & 3 deletions tools/clang-tidy-plugin/TranslatorCommentsCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,16 @@ class TranslatorCommentsCheck::TranslationMacroCallback : public PPCallbacks

StringRef MacroName = MacroNameTok.getIdentifierInfo()->getName();

bool is_marker;
unsigned int RawStringInd;
if( MacroName == "translate_marker" ) {
if( MacroName == "_" ) {
is_marker = false;
RawStringInd = 0;
} else if( MacroName == "translate_marker" ) {
is_marker = true;
RawStringInd = 0;
} else if( MacroName == "translate_marker_context" ) {
is_marker = true;
RawStringInd = 1;
} else {
return;
Expand All @@ -173,7 +179,9 @@ class TranslatorCommentsCheck::TranslationMacroCallback : public PPCallbacks
}
for( ; Tok->isNot( tok::eof ); ++Tok ) {
if( !tok::isStringLiteral( Tok->getKind() ) ) {
Check.diag( Tok->getLocation(), "Translation marker macros only accepts string literal arguments" );
if( is_marker ) {
Check.diag( Tok->getLocation(), "Translation marker macros only accepts string literal arguments" );
}
return;
}
}
Expand Down Expand Up @@ -220,7 +228,7 @@ void TranslatorCommentsCheck::registerMatchers( MatchFinder *Finder )
);
Finder->addMatcher(
callExpr(
callee( functionDecl( hasAnyName( "_", "gettext", "translation_macro_marker_func" ) ) ),
callee( functionDecl( hasAnyName( "_", "gettext" ) ) ),
hasImmediateArgument( 0, stringLiteralArgumentBound )
),
this
Expand Down
6 changes: 5 additions & 1 deletion tools/clang-tidy-plugin/test/translator-comments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ using string = basic_string<char>;
// check_clang_tidy uses -nostdinc++, so we add dummy translation interface here instead of including translations.h
#define translate_marker( s ) ( s )
#define translate_marker_context( c, s ) ( s )
// mimic how it's declared in translation.h
#define _( msg ) \
( ( []( const auto & arg ) { \
return arg; \
} )( msg ) )

const char *_( const char * );
const char *gettext( const char * );
const char *pgettext( const char *, const char * );
const char *ngettext( const char *, const char *, int );
Expand Down

0 comments on commit 146d314

Please sign in to comment.