From c505f5ea232cba51a600632b178df0310c2f5753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 2 Nov 2013 19:18:58 +0100 Subject: [PATCH] Fixed AST problem for 'if ((a.b&c)==d){}' --- lib/token.cpp | 9 +++++++-- test/testtokenize.cpp | 13 +++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index 26811549d..ede52f5cf 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1160,9 +1160,14 @@ void Token::astHandleParentheses() innerTop = _previous; else if (_next && _next->_str == ")") return; - else // _str = "(" + else { // _str = "(" innerTop = _next; - while (innerTop->_astParent) + while (Token::simpleMatch(innerTop->link(),") )")) + innerTop = innerTop->_next; + if (innerTop && innerTop->_str == "(") + innerTop = innerTop->_link->_next; + } + while (innerTop && innerTop->_astParent) innerTop = innerTop->_astParent; if (_astParent) { diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 1d7624f91..327a8bf37 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -23,6 +23,7 @@ #include "settings.h" #include "path.h" #include +#include extern std::ostringstream errout; class TestTokenizer : public TestFixture { @@ -9926,6 +9927,17 @@ private: if (!tokenList.createTokens(istr,"test.cpp")) return "ERROR"; + // Set links.. + std::stack links; + for (Token *tok = tokenList.front(); tok; tok = tok->next()) { + if (Token::Match(tok, "(|[")) + links.push(tok); + else if (!links.empty() && Token::Match(tok,")|]")) { + Token::createMutualLinks(links.top(), tok); + links.pop(); + } + } + // Create AST.. tokenList.createAst(); @@ -9956,6 +9968,7 @@ private: ASSERT_EQUALS("12+3*", testAst("(1+2)*3")); ASSERT_EQUALS("123+*", testAst("1*(2+3)")); ASSERT_EQUALS("123+*4*", testAst("1*(2+3)*4")); + ASSERT_EQUALS("ab.c&d==if", testAst("if((a.b&c)==d){}")); } void astbrackets() const { // []