Skip to content

Commit 9005657

Browse files
committed
add test
1 parent 3deb36d commit 9005657

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
@@ -1839,8 +1839,11 @@ static Token * createAstAtToken(Token *tok)
18391839
}
18401840
typetok = typetok->next();
18411841
}
1842-
if (Token::Match(typetok, "%var% [={]"))
1842+
if (Token::Match(typetok, "%var% =|{|[")) {
18431843
tok = typetok;
1844+
while (Token::Match(tok->tokAt(-2), "%name% ::"))
1845+
tok = tok->tokAt(-2);
1846+
}
18441847

18451848
// Do not create AST for function declaration
18461849
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
@@ -444,6 +444,7 @@ class TestTokenizer : public TestFixture {
444444
TEST_CASE(astfuncdecl);
445445
TEST_CASE(astarrayinit);
446446
TEST_CASE(astbracedinit);
447+
TEST_CASE(astarrayofptrs);
447448

448449
TEST_CASE(startOfExecutableScope);
449450

@@ -7613,6 +7614,11 @@ class TestTokenizer : public TestFixture {
76137614
ASSERT_EQUALS("anullptr{", testAst("int *a { nullptr };", AstStyle::Simple, ListSimplification::Full));
76147615
}
76157616

7617+
void astarrayofptrs() {
7618+
ASSERT_EQUALS("a1[", testAst("int *a[1];", AstStyle::Simple, ListSimplification::Full));
7619+
ASSERT_EQUALS("a1[", testAst("int **a[1];", AstStyle::Simple, ListSimplification::Full));
7620+
}
7621+
76167622
#define isStartOfExecutableScope(offset, code) isStartOfExecutableScope_(offset, code, __FILE__, __LINE__)
76177623
template<size_t size>
76187624
bool isStartOfExecutableScope_(int offset, const char (&code)[size], const char* file, int line) {

0 commit comments

Comments
 (0)