From 3af3d7fc06f0acfc602cdda94ace273e170788e5 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Thu, 20 May 2021 02:27:07 -0400 Subject: [PATCH] fix #10281 (Tokenizer; Wrong simplification for 'namespace ef = ::a::b::c::d::ef') (#3263) --- lib/tokenize.cpp | 14 +++++++++++++- test/testsimplifytokens.cpp | 16 ++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index c4e88a1d8..eb62097db 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -12545,6 +12545,18 @@ static bool sameTokens(const Token *first, const Token *last, const Token *other return false; } +static bool alreadyHasNamespace(const Token *first, const Token *last, const Token *end) +{ + while (end && last->str() == end->str()) { + if (first == last) + return true; + last = last->previous(); + end = end->previous(); + } + + return false; +} + static Token * deleteAlias(Token * tok) { Token::eraseTokens(tok, Token::findsimplematch(tok, ";")); @@ -12615,7 +12627,7 @@ void Tokenizer::simplifyNamespaceAliases() } } - if (tok2->strAt(1) == "::") { + if (tok2->strAt(1) == "::" && !alreadyHasNamespace(tokNameStart, tokNameEnd, tok2)) { tok2->str(tokNameStart->str()); Token * tok3 = tokNameStart; while (tok3 != tokNameEnd) { diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 3307bd661..00bfdc16e 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -265,7 +265,8 @@ private: TEST_CASE(simplifyCharAt); TEST_CASE(simplifyOverride); // ticket #5069 TEST_CASE(simplifyNestedNamespace); - TEST_CASE(simplifyNamespaceAliases); + TEST_CASE(simplifyNamespaceAliases1); + TEST_CASE(simplifyNamespaceAliases2); // ticket #10281 TEST_CASE(simplifyKnownVariables1); TEST_CASE(simplifyKnownVariables2); @@ -5156,7 +5157,7 @@ private: ASSERT_EQUALS("namespace A { namespace B { namespace C { int i ; } } }", tok("namespace A::B::C { int i; }")); } - void simplifyNamespaceAliases() { + void simplifyNamespaceAliases1() { ASSERT_EQUALS(";", tok("namespace ios = boost::iostreams;")); ASSERT_EQUALS("boost :: iostreams :: istream foo ( \"foo\" ) ;", @@ -5222,6 +5223,17 @@ private: "}")); } + void simplifyNamespaceAliases2() { + ASSERT_EQUALS("void foo ( ) " + "{ " + "int maxResults ; maxResults = :: a :: b :: c :: d :: ef :: MAX ; " + "}", + tok("namespace ef = ::a::b::c::d::ef;" + "void foo()" + "{" + " int maxResults = ::a::b::c::d::ef::MAX;" + "}")); + } std::string simplifyKnownVariables(const char code[]) { errout.str("");