Skip to content

Commit

Permalink
Only look at strings in << streams if going into a known function tha…
Browse files Browse the repository at this point in the history
…t expects <<
  • Loading branch information
Blake-Madden committed Jun 15, 2024
1 parent 1f24c69 commit 36f5976
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/i18n_review.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,9 @@ namespace i18n_check
L"PERR", L"PWARN", L"PINFO", L"ENTER", L"LEAVE"
};

m_streamable_functions = { L"qDebug", L"qInfo", L"qWarning", L"qCritical", L"qFatal",
L"qCDebug", L"qCInfo", L"qCWarning", L"qCCritical" };

m_exceptions = {
// std exceptions
L"logic_error", L"std::logic_error", L"domain_error", L"std::domain_error",
Expand Down Expand Up @@ -890,7 +893,12 @@ namespace i18n_check
}
else if (functionName.length() > 0)
{
if (is_diagnostic_function(functionName))
if (m_is_in_stream &&
m_streamable_functions.find(functionName) == m_streamable_functions.cend())
{
return;
}
else if (is_diagnostic_function(functionName))
{
m_internal_strings.emplace_back(
std::wstring(currentTextPos, quoteEnd - currentTextPos),
Expand Down Expand Up @@ -1434,6 +1442,7 @@ namespace i18n_check
variableType.clear();
parameterPosition = 0;
deprecatedMacroEncountered.clear();
m_is_in_stream = false;
int32_t closeParenCount{ 0 };
int32_t closeBraseCount{ 0 };
bool quoteWrappedInCTOR{ false };
Expand Down Expand Up @@ -1697,8 +1706,8 @@ namespace i18n_check
{
break;
}
// << stream operator in some languages,
// skip over it and skip over ')' in front of it if there is one
// << stream operator in some languages.
// Skip over it and skip over ')' in front of it if there is one
// to allow things like:
// gDebug() << "message"
else if (*startPos == L'<')
Expand All @@ -1715,6 +1724,7 @@ namespace i18n_check
{
std::advance(startPos, -1);
}
m_is_in_stream = true;
}
}
else
Expand Down
3 changes: 3 additions & 0 deletions src/i18n_review.h
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,7 @@ namespace i18n_check
std::set<std::wstring_view> m_non_localizable_functions;
std::set<std::wstring_view> m_internal_functions;
std::set<std::wstring_view> m_log_functions;
std::set<std::wstring_view> m_streamable_functions;
std::set<std::wstring_view> m_exceptions;
std::set<std::wstring_view> m_ctors_to_ignore;
std::vector<std::wregex> m_variable_name_patterns_to_ignore;
Expand Down Expand Up @@ -1031,6 +1032,8 @@ namespace i18n_check
// helpers
mutable std::vector<parse_messages> m_error_log;

bool m_is_in_stream{ false };

// bookkeeping diagnostics
#ifndef NDEBUG
mutable std::pair<std::wstring, std::wregex> m_longest_internal_string;
Expand Down
10 changes: 10 additions & 0 deletions tests/cpptests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ TEST_CASE("<<", "[cpp][i18n]")
cpp.review_strings();
REQUIRE(cpp.get_internal_strings().size() == 1);
}

SECTION("Doesn't support <<")
{
cpp_i18n_review cpp;
const wchar_t* code = LR"(wxGetTranslation("Hello there") << "################### THERE IS A MESSAGE #################";)";
cpp(code, L"");
cpp.review_strings();
REQUIRE(cpp.get_localizable_strings().size() == 1);
CHECK(cpp.get_localizable_strings()[0].m_string == L"Hello there");
}
}

TEST_CASE("Place holders", "[cpp][i18n]")
Expand Down

0 comments on commit 36f5976

Please sign in to comment.