From 9fc8bdcbac9ef14c94b0c175dfdb5954fd8aca09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 8 May 2014 06:48:53 +0200 Subject: [PATCH] Fixed #5770 (AST: better handling of 'a.b[c.d]==0') --- lib/tokenlist.cpp | 18 +++++++++++++++--- test/testtokenize.cpp | 1 + 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 9ea460b04..75faaa6e5 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -578,8 +578,20 @@ static void compileDot(Token *&tok, std::stack &op, unsigned int depth) compileBinOp(tok, compileScope, op, depth); if (depth==1U && Token::Match(tok,"++|--")) compileTerm(tok,op,depth); - } else if (tok->str() == "[") { + } else break; + } +} + +static void compileBrackets(Token *&tok, std::stack &op, unsigned int depth) +{ + compileDot(tok,op, depth); + while (tok) { + if (tok->str() == ".") { // compile dot and brackets from left to right. Example: "a.b[c]" compileBinOp(tok, compileScope, op, depth); + if (depth==1U && Token::Match(tok,"++|--")) + compileTerm(tok,op,depth); + } else if (tok->str() == "[") { + compileBinOp(tok, compileDot, op, depth); tok = tok->next(); } else break; } @@ -587,7 +599,7 @@ static void compileDot(Token *&tok, std::stack &op, unsigned int depth) static void compileMulDiv(Token *&tok, std::stack &op, unsigned int depth) { - compileDot(tok,op, depth); + compileBrackets(tok,op, depth); while (tok) { if (Token::Match(tok, "[*/%]")) { if (Token::Match(tok, "* [*,)]")) { @@ -595,7 +607,7 @@ static void compileMulDiv(Token *&tok, std::stack &op, unsigned int dept tok = tok->next(); break; } - compileBinOp(tok, compileDot, op, depth); + compileBinOp(tok, compileBrackets, op, depth); } else break; } } diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 064f70638..645225ac7 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -10389,6 +10389,7 @@ private: ASSERT_EQUALS("ifa(0[1==(", testAst("if(a()[0]==1){}")); ASSERT_EQUALS("ifbuff0[&(*1==(", testAst("if (*((DWORD*)&buff[0])==1){}")); ASSERT_EQUALS("ifp*0[1==(", testAst("if((*p)[0]==1)")); + ASSERT_EQUALS("ifab.cd.[e==(", testAst("if(a.b[c.d]==e){}")); // problems with: x=expr ASSERT_EQUALS("=\n"