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

Check warning

Code scanning / Cppcheck Premium

Use meaningful symbolic constants to represent literal values. Warning

Use meaningful symbolic constants to represent literal values.
tok = typetok;

Check warning

Code scanning / Cppcheck Premium

Use meaningful symbolic constants to represent literal values. Warning

Use meaningful symbolic constants to represent literal values.
while (Token::Match(tok->tokAt(-2), "%name% ::"))
Comment thread
ludviggunne marked this conversation as resolved.
tok = tok->tokAt(-2);
Comment thread
ludviggunne marked this conversation as resolved.
}

// 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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The SourceLocation should always be the last parameter.

bool recurseNamespaces)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do we pass this as a flag? It is never set to false.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

It's set to false here: https://github.com/cppcheck-opensource/cppcheck/pull/8610/changes#diff-49c0f9d6942f6808cf091d306b42ae4639ff837acf2dcad90c28ed5c70cf850dR246. I wanted a way to propagate values both up and down namespace "stacks" without causing infinite recursion. I'm not super familiar with the value flow code though so there might be a better way.

{
// 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 @@ -444,6 +444,7 @@ class TestTokenizer : public TestFixture {
TEST_CASE(astfuncdecl);
TEST_CASE(astarrayinit);
TEST_CASE(astbracedinit);
TEST_CASE(astarrayofptrs);

TEST_CASE(startOfExecutableScope);

Expand Down Expand Up @@ -7613,6 +7614,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