From cdee62c0320961e34daba2c0c619c38473b0c95e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 8 Mar 2020 16:21:22 +0100 Subject: [PATCH] AST: Fixed AST for struct cast '(struct T){...}' --- lib/tokenlist.cpp | 10 ++++++++++ test/testsymboldatabase.cpp | 3 --- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index c01146fac..fd3e5f09e 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -528,6 +528,12 @@ static bool iscast(const Token *tok) if (tok2->str() == ")") { if (Token::simpleMatch(tok2, ") (") && Token::simpleMatch(tok2->linkAt(1), ") .")) return true; + if (Token::simpleMatch(tok2, ") {") && !type) { + const Token *tok3 = tok2->linkAt(1); + while (tok3 != tok2 && Token::Match(tok3, "[{}]")) + tok3 = tok3->previous(); + return tok3 != tok2 && tok3->str() != ";"; + } return type || tok2->strAt(-1) == "*" || Token::simpleMatch(tok2, ") ~") || (Token::Match(tok2, ") %any%") && !tok2->next()->isOp() && @@ -987,7 +993,11 @@ static void compilePrecedence3(Token *&tok, AST_state& state) Token* castTok = tok; castTok->isCast(true); tok = tok->link()->next(); + const int inArrayAssignment = state.inArrayAssignment; + if (Token::Match(tok, "{ . %name% =")) + state.inArrayAssignment = 1; compilePrecedence3(tok, state); + state.inArrayAssignment = inArrayAssignment; compileUnaryOp(castTok, state, nullptr); } else if (state.cpp && Token::Match(tok, "new %name%|::|(")) { Token* newtok = tok; diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index c8ac39076..6e3002aae 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -2952,8 +2952,6 @@ private: } void symboldatabase45() { - // TODO: Do not throw AST validation exception -/* GET_SYMBOL_DB("typedef struct {\n" " unsigned long bits;\n" "} S;\n" @@ -2982,7 +2980,6 @@ private: ASSERT_EQUALS(Scope::eStruct, scope->type); ++scope; ASSERT_EQUALS(Scope::eFunction, scope->type); -*/ } void symboldatabase46() { // #6171 (anonymous namespace)