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
14 changes: 10 additions & 4 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,10 +589,16 @@
if (nameToken->str() == ">" && nameToken->link())
nameToken = nameToken->link()->previous();
if (Token::Match(nameToken, "]|*")) {
const Token* newTok = nameToken->link() ? nameToken->link()->previous() : nameToken->previous();
while (Token::Match(newTok, "%type%|::|*") && !newTok->isKeyword())
newTok = newTok->previous();
if (Token::simpleMatch(newTok, "new"))
const Token* tok2 = nameToken;
if (tok2->link()) {
while (tok2 && tok2->link())
tok2 = tok2->link()->previous();
}
else
tok2 = tok2->previous();
while (Token::Match(tok2, "%type%|::|*") && !tok2->isKeyword())

Check failure on line 599 in lib/tokenlist.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Called C++ object pointer is null

See more on https://sonarcloud.io/project/issues?id=danmar_cppcheck&issues=AZ1Dwooolt8rz-9IKgM8&open=AZ1Dwooolt8rz-9IKgM8&pullRequest=8396
tok2 = tok2->previous();
if (Token::Match(tok2, "new|%var%"))
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions test/testastutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ class TestAstUtils : public TestFixture {
ASSERT_EQUALS(true, findLambdaEndToken("[](void) mutable -> const * int { return x; }"));
ASSERT_EQUALS(true, findLambdaEndToken("[](void) constexpr -> const ** int { return x; }"));
ASSERT_EQUALS(true, findLambdaEndToken("[](void) constexpr -> const * const* int { return x; }"));
ASSERT_EQUALS(false, findLambdaEndToken("int** a[] { new int*[2] { new int, new int} }", "[ ]"));
ASSERT_EQUALS(false, findLambdaEndToken("int** a[] { new int*[2] { new int, new int} }", "[ 2"));
ASSERT_EQUALS(false, findLambdaEndToken("int** a[] { new int*[2] { new int, new int} };", "[ ]"));
ASSERT_EQUALS(false, findLambdaEndToken("int** a[] { new int*[2] { new int, new int} };", "[ 2"));
ASSERT_EQUALS(false, findLambdaEndToken("shared_ptr<Type *[]> sp{ new Type *[2] {new Type, new Type}, Deleter<Type>{ 2 } };", "[ 2"));
ASSERT_EQUALS(true, findLambdaEndToken("int i = 5 * []{ return 7; }();", "[", /*checkNext*/ false));
}
Expand Down
10 changes: 10 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ class TestTokenizer : public TestFixture {
TEST_CASE(astenumdecl);
TEST_CASE(astcompound);
TEST_CASE(astfuncdecl);
TEST_CASE(astarrayinit);

TEST_CASE(startOfExecutableScope);

Expand Down Expand Up @@ -7492,6 +7493,11 @@ class TestTokenizer : public TestFixture {
ASSERT_EQUALS("", testAst("::int32_t f();"));
}

void astarrayinit() { // #11738
ASSERT_EQUALS("a2[12,{", testAst("int a[2]{ 1, 2 };"));
ASSERT_EQUALS("a2[2[ 12, 34,{", testAst("int a[2][2]{ { 1, 2 }, { 3, 4 } };"));
}

#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 Expand Up @@ -8641,6 +8647,10 @@ class TestTokenizer : public TestFixture {
"{ 1",
Token::Cpp11init::CPP11INIT);

testIsCpp11init("int a[2]{ 1, 2 }; \n",
"{ 1",
Token::Cpp11init::CPP11INIT);

ASSERT_NO_THROW(tokenizeAndStringify("template<typename U> struct X {};\n" // don't crash
"template<typename T> auto f(T t) -> X<decltype(t + 1)> {}\n"));
ASSERT_EQUALS("[test.cpp:2:22]: (debug) auto token with no type. [autoNoType]\n", errout_str());
Expand Down
Loading