Fixed #8034 (AST: better handling for c++ type initialization)

This commit is contained in:
Daniel Marjamäki 2017-05-24 20:18:31 +02:00
parent 099b4435c3
commit 5444875f89
2 changed files with 28 additions and 0 deletions

View File

@ -383,6 +383,26 @@ static bool iscast(const Token *tok)
return false;
}
// int(1), int*(2), ..
static Token * findCppTypeInitPar(Token *tok)
{
if (!tok || !Token::Match(tok->previous(), "[,(] %name%"))
return nullptr;
while (Token::Match(tok, "%name%|::|<")) {
if (tok->str() == "<") {
tok = tok->link();
if (!tok)
return nullptr;
}
tok = tok->next();
}
if (!Token::Match(tok, "[*&]"))
return nullptr;
while (Token::Match(tok, "[*&]"))
tok = tok->next();
return (tok && tok->str() == "(") ? tok : nullptr;
}
// X{} X<Y>{} etc
static bool iscpp11init(const Token * const tok)
{
@ -492,6 +512,10 @@ static void compileTerm(Token *&tok, AST_state& state)
} else if (Token::Match(tok, "sizeof !!(")) {
compileUnaryOp(tok, state, compileExpression);
state.op.pop();
} else if (state.cpp && findCppTypeInitPar(tok)) { // int(0), int*(123), ..
tok = findCppTypeInitPar(tok);
state.op.push(tok);
tok = tok->tokAt(2);
} else if (state.cpp && iscpp11init(tok)) { // X{} X<Y>{} etc
state.op.push(tok);
tok = tok->next();

View File

@ -7930,6 +7930,10 @@ private:
ASSERT_EQUALS("f\"A\"1,(",testAst("f(\"A\" B, 1);"));
ASSERT_EQUALS("fA1,(",testAst("f(A \"B\", 1);"));
// C++ : type()
ASSERT_EQUALS("fint(0,(", testAst("f(int(),0);"));
ASSERT_EQUALS("f(0,(", testAst("f(int *(),0);")); // typedef int* X; f(X(),0);
// for
ASSERT_EQUALS("for;;(", testAst("for(;;)"));
ASSERT_EQUALS("fora0=a8<a++;;(", testAst("for(a=0;a<8;a++)"));