Skip to content

Commit ad352da

Browse files
pfultz2danmar
authored andcommitted
Fix issue 9535: Syntax Error: AST broken, 'if' doesn't have two operands. (#2450)
1 parent 890d11c commit ad352da

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

lib/tokenlist.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,9 @@ static bool iscast(const Token *tok)
516516
return false;
517517
}
518518

519-
static const Token* findTypeEnd(const Token* tok)
519+
static Token* findTypeEnd(Token* tok)
520520
{
521-
while (Token::Match(tok, "%name%|.|::|<|(|template|decltype|sizeof")) {
521+
while (Token::Match(tok, "%name%|.|::|*|&|<|(|template|decltype|sizeof")) {
522522
if (Token::Match(tok, "(|<"))
523523
tok = tok->link();
524524
if (!tok)
@@ -528,6 +528,11 @@ static const Token* findTypeEnd(const Token* tok)
528528
return tok;
529529
}
530530

531+
static const Token* findTypeEnd(const Token* tok)
532+
{
533+
return findTypeEnd(const_cast<Token*>(tok));
534+
}
535+
531536
static const Token * findLambdaEndScope(const Token *tok)
532537
{
533538
if (!Token::simpleMatch(tok, "["))
@@ -867,10 +872,7 @@ static void compilePrecedence2(Token *&tok, AST_state& state)
867872
if (Token::Match(curlyBracket, "mutable|const"))
868873
curlyBracket = curlyBracket->next();
869874
if (curlyBracket && curlyBracket->originalName() == "->") {
870-
while (Token::Match(curlyBracket, "%name%|.|::|&|*"))
871-
curlyBracket = curlyBracket->next();
872-
if (curlyBracket && curlyBracket->str() == "<" && curlyBracket->link())
873-
curlyBracket = curlyBracket->link()->next();
875+
curlyBracket = findTypeEnd(curlyBracket->next());
874876
}
875877
if (curlyBracket && curlyBracket->str() == "{") {
876878
squareBracket->astOperand1(roundBracket);

test/testtokenize.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7811,6 +7811,7 @@ class TestTokenizer : public TestFixture {
78117811
ASSERT_EQUALS("{([(return 0return", testAst("return []() -> int { return 0; }();"));
78127812
ASSERT_EQUALS("{([(return 0return", testAst("return [something]() -> int { return 0; }();"));
78137813
ASSERT_EQUALS("{([cd,(return 0return", testAst("return [](int a, int b) -> int { return 0; }(c, d);"));
7814+
ASSERT_EQUALS("{([return", testAst("return []() -> decltype(0) {};"));
78147815
ASSERT_EQUALS("x{([=", testAst("x = [&]()->std::string const & {};"));
78157816
ASSERT_EQUALS("f{([=", testAst("f = []() -> foo* {};"));
78167817
ASSERT_EQUALS("f{([=", testAst("f = [](void) mutable -> foo* {};"));
@@ -8159,6 +8160,13 @@ class TestTokenizer : public TestFixture {
81598160
" return 0;\n"
81608161
" }};\n"
81618162
"}\n"))
8163+
// #0535
8164+
ASSERT_NO_THROW(tokenizeAndStringify("template <typename, typename> struct a;\n"
8165+
"template <typename, typename b> void c() {\n"
8166+
" ([]() -> decltype(0) {\n"
8167+
" if (a<b, decltype(0)>::d) {}\n"
8168+
" });\n"
8169+
"}\n"))
81628170
}
81638171
void checkIfCppCast() {
81648172
ASSERT_NO_THROW(tokenizeAndStringify("struct a {\n"

0 commit comments

Comments
 (0)