From 36f5976ccf92603bade68c40fea2f390e9fea98c Mon Sep 17 00:00:00 2001 From: Blake-Madden <66873089+Blake-Madden@users.noreply.github.com> Date: Sat, 15 Jun 2024 13:45:35 -0400 Subject: [PATCH] Only look at strings in << streams if going into a known function that expects << --- src/i18n_review.cpp | 16 +++++++++++++--- src/i18n_review.h | 3 +++ tests/cpptests.cpp | 10 ++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/i18n_review.cpp b/src/i18n_review.cpp index 77f3dae..646d84b 100644 --- a/src/i18n_review.cpp +++ b/src/i18n_review.cpp @@ -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", @@ -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), @@ -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 }; @@ -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'<') @@ -1715,6 +1724,7 @@ namespace i18n_check { std::advance(startPos, -1); } + m_is_in_stream = true; } } else diff --git a/src/i18n_review.h b/src/i18n_review.h index f62767d..62fad72 100644 --- a/src/i18n_review.h +++ b/src/i18n_review.h @@ -934,6 +934,7 @@ namespace i18n_check std::set m_non_localizable_functions; std::set m_internal_functions; std::set m_log_functions; + std::set m_streamable_functions; std::set m_exceptions; std::set m_ctors_to_ignore; std::vector m_variable_name_patterns_to_ignore; @@ -1031,6 +1032,8 @@ namespace i18n_check // helpers mutable std::vector m_error_log; + bool m_is_in_stream{ false }; + // bookkeeping diagnostics #ifndef NDEBUG mutable std::pair m_longest_internal_string; diff --git a/tests/cpptests.cpp b/tests/cpptests.cpp index fd13469..41f9f3d 100644 --- a/tests/cpptests.cpp +++ b/tests/cpptests.cpp @@ -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]")