Skip to content

Commit d2c12a2

Browse files
committed
add test
1 parent 21ea669 commit d2c12a2

4 files changed

Lines changed: 24 additions & 5 deletions

File tree

lib/tokenlist.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1837,8 +1837,11 @@ static Token * createAstAtToken(Token *tok)
18371837
}
18381838
typetok = typetok->next();
18391839
}
1840-
if (Token::Match(typetok, "%var% [={]"))
1840+
if (Token::Match(typetok, "%var% =|{|[")) {
18411841
tok = typetok;
1842+
while (Token::Match(tok->tokAt(-2), "%name% ::"))
1843+
tok = tok->tokAt(-2);
1844+
}
18421845

18431846
// Do not create AST for function declaration
18441847
if (typetok &&

lib/vf_settokenvalue.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ namespace ValueFlow
220220
void setTokenValue(Token* tok,
221221
Value value,
222222
const Settings& settings,
223-
SourceLocation loc)
223+
SourceLocation loc,
224+
bool recurseNamespaces)
224225
{
225226
// Skip setting values that are too big since its ambiguous
226227
if (!value.isImpossible() && value.isIntValue() && value.intvalue < 0 && astIsUnsigned(tok)
@@ -237,6 +238,14 @@ namespace ValueFlow
237238
if (!tok->addValue(value))
238239
return;
239240

241+
if (Token::simpleMatch(tok, "::") && recurseNamespaces) {
242+
Token *inner = tok;
243+
while (Token::simpleMatch(inner, "::"))
244+
inner = inner->astOperand2();
245+
if (inner)
246+
setTokenValue(inner, value, settings, SourceLocation::current(), false);
247+
}
248+
240249
if (value.path < 0)
241250
return;
242251

@@ -701,8 +710,8 @@ namespace ValueFlow
701710
}
702711
}
703712

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

708717
// Calling std::size or std::empty on an array

lib/vf_settokenvalue.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ namespace ValueFlow
3030
void setTokenValue(Token* tok,
3131
Value value,
3232
const Settings& settings,
33-
SourceLocation loc = SourceLocation::current());
33+
SourceLocation loc = SourceLocation::current(),
34+
bool recurseNamespaces = true);
3435
}
3536

3637
#endif // vfSetTokenValueH

test/testtokenize.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ class TestTokenizer : public TestFixture {
440440
TEST_CASE(astfuncdecl);
441441
TEST_CASE(astarrayinit);
442442
TEST_CASE(astbracedinit);
443+
TEST_CASE(astarrayofptrs);
443444

444445
TEST_CASE(startOfExecutableScope);
445446

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

7547+
void astarrayofptrs() {
7548+
ASSERT_EQUALS("a1[", testAst("int *a[1];", AstStyle::Simple, ListSimplification::Full));
7549+
ASSERT_EQUALS("a1[", testAst("int **a[1];", AstStyle::Simple, ListSimplification::Full));
7550+
}
7551+
75467552
#define isStartOfExecutableScope(offset, code) isStartOfExecutableScope_(offset, code, __FILE__, __LINE__)
75477553
template<size_t size>
75487554
bool isStartOfExecutableScope_(int offset, const char (&code)[size], const char* file, int line) {

0 commit comments

Comments
 (0)