Skip to content
Draft
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
5 changes: 4 additions & 1 deletion lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1837,8 +1837,11 @@
}
typetok = typetok->next();
}
if (Token::Match(typetok, "%var% [={]"))
if (Token::Match(typetok, "%var% =|{|[")) {
tok = typetok;
while (Token::Match(tok->tokAt(-2), "%name% ::"))

Check warning

Code scanning / Cppcheck Premium

Use meaningful symbolic constants to represent literal values. Warning

Use meaningful symbolic constants to represent literal values.
tok = tok->tokAt(-2);

Check warning

Code scanning / Cppcheck Premium

Use meaningful symbolic constants to represent literal values. Warning

Use meaningful symbolic constants to represent literal values.
}

// Do not create AST for function declaration
if (typetok &&
Expand Down
15 changes: 12 additions & 3 deletions lib/vf_settokenvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ namespace ValueFlow
void setTokenValue(Token* tok,
Value value,
const Settings& settings,
SourceLocation loc)
SourceLocation loc,
bool recurseNamespaces)
{
// Skip setting values that are too big since its ambiguous
if (!value.isImpossible() && value.isIntValue() && value.intvalue < 0 && astIsUnsigned(tok)
Expand All @@ -237,6 +238,14 @@ namespace ValueFlow
if (!tok->addValue(value))
return;

if (Token::simpleMatch(tok, "::") && recurseNamespaces) {
Token *inner = tok;
while (Token::simpleMatch(inner, "::"))
inner = inner->astOperand2();
if (inner)
setTokenValue(inner, value, settings, SourceLocation::current(), false);
}

if (value.path < 0)
return;

Expand Down Expand Up @@ -701,8 +710,8 @@ namespace ValueFlow
}
}

else if (Token::Match(parent, ":: %name%") && parent->astOperand2() == tok) {
setTokenValue(parent, std::move(value), settings);
else if (Token::Match(parent, ":: %name%") && parent->astOperand2() == tok && recurseNamespaces) {
setTokenValue(parent, std::move(value), settings, SourceLocation::current(), false);
}

// Calling std::size or std::empty on an array
Expand Down
3 changes: 2 additions & 1 deletion lib/vf_settokenvalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ namespace ValueFlow
void setTokenValue(Token* tok,
Value value,
const Settings& settings,
SourceLocation loc = SourceLocation::current());
SourceLocation loc = SourceLocation::current(),
bool recurseNamespaces = true);
}

#endif // vfSetTokenValueH
6 changes: 6 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ class TestTokenizer : public TestFixture {
TEST_CASE(astfuncdecl);
TEST_CASE(astarrayinit);
TEST_CASE(astbracedinit);
TEST_CASE(astarrayofptrs);

TEST_CASE(startOfExecutableScope);

Expand Down Expand Up @@ -7543,6 +7544,11 @@ class TestTokenizer : public TestFixture {
ASSERT_EQUALS("anullptr{", testAst("int *a { nullptr };", AstStyle::Simple, ListSimplification::Full));
}

void astarrayofptrs() {
ASSERT_EQUALS("a1[", testAst("int *a[1];", AstStyle::Simple, ListSimplification::Full));
ASSERT_EQUALS("a1[", testAst("int **a[1];", AstStyle::Simple, ListSimplification::Full));
}

#define isStartOfExecutableScope(offset, code) isStartOfExecutableScope_(offset, code, __FILE__, __LINE__)
template<size_t size>
bool isStartOfExecutableScope_(int offset, const char (&code)[size], const char* file, int line) {
Expand Down
Loading