Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
25 changes: 24 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5269,7 +5269,29 @@ 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 (tok->str() == "enum") {
Comment thread
ludviggunne marked this conversation as resolved.
Outdated
tok = tok->next();
if (tok->str() == "class")
tok = tok->next();
if (tok->isName())
tok = tok->next();
if (tok->str() == ":") {
tok = tok->next();
if (tok->str() == "::")
tok = tok->next();
while (Token::Match(tok, "%name% ::"))
tok = tok->tokAt(2);
tok = tok->next();
}
if (tok->str() == "{")
enumEnd = tok->link();
}
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 +5321,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();
Comment thread
ludviggunne marked this conversation as resolved.
Outdated
}

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