From b2b0962067111ba21841a1b3945dcb2adb941308 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 3 Apr 2023 15:41:19 +0200 Subject: [PATCH] Fix #11629 debug: Scope::checkVariable found variable 'np' with varid 0. (#4915) --- lib/tokenize.cpp | 5 ++++- test/testsimplifytokens.cpp | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index c66def4f0..7bf490f0c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -10169,11 +10169,14 @@ void Tokenizer::simplifyNamespaceAliases() int scope = 0; for (Token *tok = list.front(); tok; tok = tok->next()) { + bool isPrev{}; if (tok->str() == "{") scope++; else if (tok->str() == "}") scope--; - else if (Token::Match(tok, "namespace %name% =")) { + else if (Token::Match(tok, "namespace %name% =") || (isPrev = Token::Match(tok->previous(), "namespace %name% ="))) { + if (isPrev) + tok = tok->previous(); const std::string name(tok->next()->str()); Token * tokNameStart = tok->tokAt(3); Token * tokNameEnd = tokNameStart; diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index b425e28a2..1db88f2ba 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -1455,6 +1455,9 @@ private: "}" "namespace AB = A::B;" //duplicate declaration "}")); + ASSERT_EQUALS(";", + tok("namespace p = boost::python;\n" + "namespace np = boost::numpy;\n")); // redeclared nested namespace aliases TODO_ASSERT_EQUALS("namespace A { namespace B { void foo ( ) { bar ( A :: B :: ab ( ) ) ; { baz ( A :: a ( ) ) ; } bar ( A :: B :: ab ( ) ) ; } } }",