From 2a78db4c06d067565c65b6dbe49c394257f7582c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 31 Jul 2013 10:30:20 +0200 Subject: [PATCH] Refactoring Token::findClosingBracket. --- lib/templatesimplifier.cpp | 5 ++--- lib/token.cpp | 20 ++++++++++++++------ lib/token.h | 12 +++--------- lib/tokenize.cpp | 10 +++++----- test/testtoken.cpp | 29 +++++++++-------------------- 5 files changed, 33 insertions(+), 43 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index a90612c84..f13d204c5 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -448,7 +448,7 @@ std::list TemplateSimplifier::getTemplateInstantiations(Token *tokens) for (Token *tok = tokens; tok; tok = tok->next()) { // template definition.. skip it if (Token::simpleMatch(tok, "template <")) { - tok->next()->findClosingBracket(tok); + tok = tok->next()->findClosingBracket(); if (!tok) break; } else if (Token::Match(tok->previous(), "[({};=] %var% <") || @@ -456,8 +456,7 @@ std::list TemplateSimplifier::getTemplateInstantiations(Token *tokens) // Add inner template instantiations first => go to the ">" // and then parse backwards, adding all seen instantiations - const Token *tok2; - tok->next()->findClosingBracket(tok2); + const Token *tok2 = tok->next()->findClosingBracket(); // parse backwards and add template instantiations for (; tok2 && tok2 != tok; tok2 = tok2->previous()) { diff --git a/lib/token.cpp b/lib/token.cpp index 5818df9b2..97a9ac07d 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -836,30 +836,38 @@ Token* Token::nextArgument() const return 0; } -bool Token::findClosingBracket(const Token*& closing) const +const Token * Token::findClosingBracket() const { + const Token *closing = 0; + if (_str == "<") { unsigned int depth = 0; for (closing = this; closing != NULL; closing = closing->next()) { if (closing->str() == "{" || closing->str() == "[" || closing->str() == "(") closing = closing->link(); else if (closing->str() == "}" || closing->str() == "]" || closing->str() == ")" || closing->str() == ";" || closing->str() == "=") - return false; + break; else if (closing->str() == "<") ++depth; else if (closing->str() == ">") { if (--depth == 0) - return true; + break; } else if (closing->str() == ">>") { if (--depth == 0) - return true; + break; if (--depth == 0) - return true; + break; } } } - return false; + return closing; +} + +Token * Token::findClosingBracket() +{ + // return value of const function + return const_cast(const_cast(this)->findClosingBracket()); } //--------------------------------------------------------------------------- diff --git a/lib/token.h b/lib/token.h index 6715c79b4..01749cb56 100644 --- a/lib/token.h +++ b/lib/token.h @@ -546,16 +546,10 @@ public: /** * Returns the closing bracket of opening '<'. Should only be used if link() * is unavailable. - * @param closing The closing token is stored in that parameter - * @return success + * @return closing '>', ')', ']' or '}'. if no closing bracket is found, NULL is returned */ - bool findClosingBracket(const Token*& closing) const; - bool findClosingBracket(Token*& closing) const { - const Token* tok; - bool retVal = findClosingBracket(tok); - closing = const_cast(tok); - return retVal; - } + const Token* findClosingBracket() const; + Token* findClosingBracket(); private: void next(Token *nextToken) { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 14782f53a..81f9cb903 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -661,7 +661,7 @@ void Tokenizer::simplifyTypedef() // check for template if (tokOffset->str() == "<") { - tokOffset->findClosingBracket(typeEnd); + typeEnd = tokOffset->findClosingBracket(); while (typeEnd && Token::Match(typeEnd->next(), ":: %type%")) typeEnd = typeEnd->tokAt(2); @@ -2662,8 +2662,8 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::mapstr() == "<" && TemplateSimplifier::templateParameters(tok2) > 0) { - bool ok = tok2->findClosingBracket(tok2); - if (!ok || !tok2) + tok2 = tok2->findClosingBracket(); + if (!Token::Match(tok2, ">|>>")) break; } else if (tok2->str() == "&" || tok2->str() == "&&") { ref = true; @@ -5251,7 +5251,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_ if (tok2->str() == "{" || tok2->str() == "(" || tok2->str() == "[") tok2 = tok2->link(); if (tok2->str() == "<" && TemplateSimplifier::templateParameters(tok2) > 0) - tok2->findClosingBracket(tok2); + tok2 = tok2->findClosingBracket(); tok2 = tok2->next(); } if (tok2 && tok2->str() == ";") @@ -5305,7 +5305,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_ tok2 = tok2->link(); else if (tok2->str() == "<" && tok2->previous()->isName() && !tok2->previous()->varId()) - tok2->findClosingBracket(tok2); + tok2 = tok2->findClosingBracket(); else if (std::strchr(";,", tok2->str()[0])) { // "type var =" => "type var; var =" diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 9ed71c0ce..cdf92ee4a 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -832,18 +832,14 @@ private: void canFindMatchingBracketsNeedsOpen() const { givenACodeSampleToTokenize var("std::deque > intsets;"); - const Token* t = 0; - bool found = var.tokens()->findClosingBracket(t); - ASSERT(! found); - ASSERT(! t); + const Token* t = var.tokens()->findClosingBracket(); + ASSERT(t == NULL); } void canFindMatchingBracketsInnerPair() const { givenACodeSampleToTokenize var("std::deque > intsets;"); - Token* t = 0; - bool found = var.tokens()->tokAt(7)->findClosingBracket(t); - ASSERT(found); + Token* t = const_cast(var.tokens()->tokAt(7))->findClosingBracket(); ASSERT_EQUALS(">", t->str()); ASSERT(var.tokens()->tokAt(9) == t); } @@ -851,20 +847,15 @@ private: void canFindMatchingBracketsOuterPair() const { givenACodeSampleToTokenize var("std::deque > intsets;"); - const Token* t = 0; - bool found = var.tokens()->tokAt(3)->findClosingBracket(t); - ASSERT(found); + const Token* t = var.tokens()->tokAt(3)->findClosingBracket(); ASSERT_EQUALS(">", t->str()); ASSERT(var.tokens()->tokAt(10) == t); - } void canFindMatchingBracketsWithTooManyClosing() const { givenACodeSampleToTokenize var("X< 1>2 > x1;\n"); - const Token* t = 0; - bool found = var.tokens()->next()->findClosingBracket(t); - ASSERT(found); + const Token* t = var.tokens()->next()->findClosingBracket(); ASSERT_EQUALS(">", t->str()); ASSERT(var.tokens()->tokAt(3) == t); } @@ -872,14 +863,12 @@ private: void canFindMatchingBracketsWithTooManyOpening() const { givenACodeSampleToTokenize var("X < (2 < 1) > x1;\n"); - const Token* t = 0; - bool found = var.tokens()->next()->findClosingBracket(t); - ASSERT(found); + const Token* t = var.tokens()->next()->findClosingBracket(); + ASSERT(t != NULL && t->str() == ">"); - found = var.tokens()->tokAt(4)->findClosingBracket(t); - ASSERT(!found); + t = var.tokens()->tokAt(4)->findClosingBracket(); + ASSERT(t != NULL && t->str() == ")"); } - }; REGISTER_TEST(TestToken)