Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5269,7 +5269,16 @@ void Tokenizer::setVarIdPass2()
std::map<const Token *, std::string> endOfScope;
std::list<std::string> scope;
std::list<const Token *> usingnamespaces;
const Token *enumEnd = nullptr;
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (isEnumStart(tok)) {
enumEnd = tok->link();
continue;
}
if (tok == enumEnd) {
enumEnd = nullptr;
continue;
}
if (!tok->previous() || Token::Match(tok->previous(), "[;{}]")) {
if (Token::Match(tok, "using namespace %name% ::|;")) {
Token *endtok = tok->tokAt(2);
Expand Down Expand Up @@ -5299,7 +5308,8 @@ void Tokenizer::setVarIdPass2()
tok = tok->next()->findClosingBracket()->next();
else if (usingnamespaces.empty() || tok->varId() || !tok->isName() || tok->isStandardType() || tok->tokType() == Token::eKeyword || tok->tokType() == Token::eBoolean ||
Token::Match(tok->previous(), ".|namespace|class|struct|&|&&|*|> %name%") || Token::Match(tok->previous(), "%type%| %name% ( %type%|)") || Token::Match(tok, "public:|private:|protected:") ||
(!tok->next() && Token::Match(tok->previous(), "}|; %name%")))
(!tok->next() && Token::Match(tok->previous(), "}|; %name%")) ||
(enumEnd && Token::Match(tok->previous(), "{|, %name% =|,|}")))
continue;

if (tok->strAt(-1) == "::" && tok->tokAt(-2) && tok->tokAt(-2)->isName())
Expand Down
17 changes: 17 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class TestTokenizer : public TestFixture {
TEST_CASE(tokenize41); // #13847
TEST_CASE(tokenize42); // #13861
TEST_CASE(tokenize43); // #13861
TEST_CASE(tokenize44); // #14955

TEST_CASE(validate);

Expand Down Expand Up @@ -949,6 +950,22 @@ class TestTokenizer : public TestFixture {
(void)errout_str();
}

void tokenize44() { // #14955
const char code[] = "namespace O {}\n"
"namespace N {\n"
" using namespace O;\n"
" enum class E { E0 };\n"
" int E0 = E::E0;\n"
"}\n";
const char expected[] = "2: namespace N {\n"
"3: using namespace O ;\n"
"4: enum class E { E0 } ;\n"
"5: int E0@1 ; E0@1 = E :: E0 ;\n"
"6: }\n";
ASSERT_EQUALS(expected, tokenizeDebugListing(code));
(void)errout_str();

@chrchr-github chrchr-github Aug 4, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ignore_errout()? But is there anything to ignore without auto?
It also looks like a cast is missing.

}

void validate() {
// C++ code in C file
ASSERT_THROW_INTERNAL(tokenizeAndStringify(";using namespace std;",dinit(TokenizeOptions, $.expand = false, $.cpp = false)), SYNTAX);
Expand Down
Loading