Skip to content

Commit bf3be95

Browse files
Fix #11453, #11490 internalAstError with templates (#4986)
* Fix #11490 internalAstError with unknown template in index expression * Comment * Fix #11490
1 parent 938ad99 commit bf3be95

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

lib/tokenize.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5178,22 +5178,10 @@ void Tokenizer::createLinks2()
51785178
if (!top2 || top2->str() != "<") {
51795179
if (token->str() == ">>")
51805180
continue;
5181-
if (!Token::Match(token->next(), "%name%|%cop%|%assign%|::|,|(|)|{|}|;|[|:|.|=|...") &&
5181+
if (!Token::Match(token->next(), "%name%|%cop%|%assign%|::|,|(|)|{|}|;|[|]|:|.|=|...") &&
51825182
!Token::Match(token->next(), "&& %name% ="))
51835183
continue;
51845184
}
5185-
// if > is followed by [ .. "new a<b>[" is expected
5186-
// unless this is from varidiac expansion
5187-
if (token->strAt(1) == "[" && !Token::simpleMatch(token->tokAt(-1), "... >") &&
5188-
!Token::Match(token->tokAt(1), "[ ]")) {
5189-
Token *prev = type.top()->previous();
5190-
while (prev && Token::Match(prev->previous(), ":: %name%"))
5191-
prev = prev->tokAt(-2);
5192-
if (prev && prev->str() != "new")
5193-
prev = prev->previous();
5194-
if (!prev || prev->str() != "new")
5195-
continue;
5196-
}
51975185

51985186
if (token->str() == ">>" && top1 && top2) {
51995187
type.pop();

test/testtokenize.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3454,6 +3454,39 @@ class TestTokenizer : public TestFixture {
34543454
ASSERT_EQUALS(true, tok1->link() == tok2);
34553455
ASSERT_EQUALS(true, tok2->link() == tok1);
34563456
}
3457+
3458+
{
3459+
// #11453
3460+
const char code[] = "template<typename T>\n"
3461+
"std::array<T, 1> a{};\n"
3462+
"void f() {\n"
3463+
" if (a<int>[0]) {}\n"
3464+
"}\n";
3465+
errout.str("");
3466+
Tokenizer tokenizer(&settings0, this);
3467+
std::istringstream istr(code);
3468+
ASSERT(tokenizer.tokenize(istr, "test.cpp"));
3469+
const Token* tok1 = Token::findsimplematch(tokenizer.tokens(), "< int");
3470+
const Token* tok2 = Token::findsimplematch(tok1, "> [");
3471+
ASSERT_EQUALS(true, tok1->link() == tok2);
3472+
ASSERT_EQUALS(true, tok2->link() == tok1);
3473+
}
3474+
3475+
{
3476+
// #11490
3477+
const char code[] = "void g() {\n"
3478+
" int b[2] = {};\n"
3479+
" if (b[idx<1>]) {}\n"
3480+
"}\n";
3481+
errout.str("");
3482+
Tokenizer tokenizer(&settings0, this);
3483+
std::istringstream istr(code);
3484+
ASSERT(tokenizer.tokenize(istr, "test.cpp"));
3485+
const Token* tok1 = Token::findsimplematch(tokenizer.tokens(), "< 1");
3486+
const Token* tok2 = Token::findsimplematch(tok1, "> ]");
3487+
ASSERT_EQUALS(true, tok1->link() == tok2);
3488+
ASSERT_EQUALS(true, tok2->link() == tok1);
3489+
}
34573490
}
34583491

34593492
void simplifyString() {

0 commit comments

Comments
 (0)