Refactoring Token::findClosingBracket.

This commit is contained in:
Daniel Marjamäki 2013-07-31 10:30:20 +02:00
parent 6c05f78a8f
commit 2a78db4c06
5 changed files with 33 additions and 43 deletions

View File

@ -448,7 +448,7 @@ std::list<Token *> TemplateSimplifier::getTemplateInstantiations(Token *tokens)
for (Token *tok = tokens; tok; tok = tok->next()) { for (Token *tok = tokens; tok; tok = tok->next()) {
// template definition.. skip it // template definition.. skip it
if (Token::simpleMatch(tok, "template <")) { if (Token::simpleMatch(tok, "template <")) {
tok->next()->findClosingBracket(tok); tok = tok->next()->findClosingBracket();
if (!tok) if (!tok)
break; break;
} else if (Token::Match(tok->previous(), "[({};=] %var% <") || } else if (Token::Match(tok->previous(), "[({};=] %var% <") ||
@ -456,8 +456,7 @@ std::list<Token *> TemplateSimplifier::getTemplateInstantiations(Token *tokens)
// Add inner template instantiations first => go to the ">" // Add inner template instantiations first => go to the ">"
// and then parse backwards, adding all seen instantiations // and then parse backwards, adding all seen instantiations
const Token *tok2; const Token *tok2 = tok->next()->findClosingBracket();
tok->next()->findClosingBracket(tok2);
// parse backwards and add template instantiations // parse backwards and add template instantiations
for (; tok2 && tok2 != tok; tok2 = tok2->previous()) { for (; tok2 && tok2 != tok; tok2 = tok2->previous()) {

View File

@ -836,30 +836,38 @@ Token* Token::nextArgument() const
return 0; return 0;
} }
bool Token::findClosingBracket(const Token*& closing) const const Token * Token::findClosingBracket() const
{ {
const Token *closing = 0;
if (_str == "<") { if (_str == "<") {
unsigned int depth = 0; unsigned int depth = 0;
for (closing = this; closing != NULL; closing = closing->next()) { for (closing = this; closing != NULL; closing = closing->next()) {
if (closing->str() == "{" || closing->str() == "[" || closing->str() == "(") if (closing->str() == "{" || closing->str() == "[" || closing->str() == "(")
closing = closing->link(); closing = closing->link();
else if (closing->str() == "}" || closing->str() == "]" || closing->str() == ")" || closing->str() == ";" || closing->str() == "=") else if (closing->str() == "}" || closing->str() == "]" || closing->str() == ")" || closing->str() == ";" || closing->str() == "=")
return false; break;
else if (closing->str() == "<") else if (closing->str() == "<")
++depth; ++depth;
else if (closing->str() == ">") { else if (closing->str() == ">") {
if (--depth == 0) if (--depth == 0)
return true; break;
} else if (closing->str() == ">>") { } else if (closing->str() == ">>") {
if (--depth == 0) if (--depth == 0)
return true; break;
if (--depth == 0) if (--depth == 0)
return true; break;
} }
} }
} }
return false; return closing;
}
Token * Token::findClosingBracket()
{
// return value of const function
return const_cast<Token*>(const_cast<const Token*>(this)->findClosingBracket());
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -546,16 +546,10 @@ public:
/** /**
* Returns the closing bracket of opening '<'. Should only be used if link() * Returns the closing bracket of opening '<'. Should only be used if link()
* is unavailable. * is unavailable.
* @param closing The closing token is stored in that parameter * @return closing '>', ')', ']' or '}'. if no closing bracket is found, NULL is returned
* @return success
*/ */
bool findClosingBracket(const Token*& closing) const; const Token* findClosingBracket() const;
bool findClosingBracket(Token*& closing) const { Token* findClosingBracket();
const Token* tok;
bool retVal = findClosingBracket(tok);
closing = const_cast<Token*>(tok);
return retVal;
}
private: private:
void next(Token *nextToken) { void next(Token *nextToken) {

View File

@ -661,7 +661,7 @@ void Tokenizer::simplifyTypedef()
// check for template // check for template
if (tokOffset->str() == "<") { if (tokOffset->str() == "<") {
tokOffset->findClosingBracket(typeEnd); typeEnd = tokOffset->findClosingBracket();
while (typeEnd && Token::Match(typeEnd->next(), ":: %type%")) while (typeEnd && Token::Match(typeEnd->next(), ":: %type%"))
typeEnd = typeEnd->tokAt(2); typeEnd = typeEnd->tokAt(2);
@ -2662,8 +2662,8 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::stri
++typeCount; ++typeCount;
} }
} else if (tok2->str() == "<" && TemplateSimplifier::templateParameters(tok2) > 0) { } else if (tok2->str() == "<" && TemplateSimplifier::templateParameters(tok2) > 0) {
bool ok = tok2->findClosingBracket(tok2); tok2 = tok2->findClosingBracket();
if (!ok || !tok2) if (!Token::Match(tok2, ">|>>"))
break; break;
} else if (tok2->str() == "&" || tok2->str() == "&&") { } else if (tok2->str() == "&" || tok2->str() == "&&") {
ref = true; ref = true;
@ -5251,7 +5251,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_
if (tok2->str() == "{" || tok2->str() == "(" || tok2->str() == "[") if (tok2->str() == "{" || tok2->str() == "(" || tok2->str() == "[")
tok2 = tok2->link(); tok2 = tok2->link();
if (tok2->str() == "<" && TemplateSimplifier::templateParameters(tok2) > 0) if (tok2->str() == "<" && TemplateSimplifier::templateParameters(tok2) > 0)
tok2->findClosingBracket(tok2); tok2 = tok2->findClosingBracket();
tok2 = tok2->next(); tok2 = tok2->next();
} }
if (tok2 && tok2->str() == ";") if (tok2 && tok2->str() == ";")
@ -5305,7 +5305,7 @@ void Tokenizer::simplifyVarDecl(Token * tokBegin, Token * tokEnd, bool only_k_r_
tok2 = tok2->link(); tok2 = tok2->link();
else if (tok2->str() == "<" && tok2->previous()->isName() && !tok2->previous()->varId()) else if (tok2->str() == "<" && tok2->previous()->isName() && !tok2->previous()->varId())
tok2->findClosingBracket(tok2); tok2 = tok2->findClosingBracket();
else if (std::strchr(";,", tok2->str()[0])) { else if (std::strchr(";,", tok2->str()[0])) {
// "type var =" => "type var; var =" // "type var =" => "type var; var ="

View File

@ -832,18 +832,14 @@ private:
void canFindMatchingBracketsNeedsOpen() const { void canFindMatchingBracketsNeedsOpen() const {
givenACodeSampleToTokenize var("std::deque<std::set<int> > intsets;"); givenACodeSampleToTokenize var("std::deque<std::set<int> > intsets;");
const Token* t = 0; const Token* t = var.tokens()->findClosingBracket();
bool found = var.tokens()->findClosingBracket(t); ASSERT(t == NULL);
ASSERT(! found);
ASSERT(! t);
} }
void canFindMatchingBracketsInnerPair() const { void canFindMatchingBracketsInnerPair() const {
givenACodeSampleToTokenize var("std::deque<std::set<int> > intsets;"); givenACodeSampleToTokenize var("std::deque<std::set<int> > intsets;");
Token* t = 0; Token* t = const_cast<Token*>(var.tokens()->tokAt(7))->findClosingBracket();
bool found = var.tokens()->tokAt(7)->findClosingBracket(t);
ASSERT(found);
ASSERT_EQUALS(">", t->str()); ASSERT_EQUALS(">", t->str());
ASSERT(var.tokens()->tokAt(9) == t); ASSERT(var.tokens()->tokAt(9) == t);
} }
@ -851,20 +847,15 @@ private:
void canFindMatchingBracketsOuterPair() const { void canFindMatchingBracketsOuterPair() const {
givenACodeSampleToTokenize var("std::deque<std::set<int> > intsets;"); givenACodeSampleToTokenize var("std::deque<std::set<int> > intsets;");
const Token* t = 0; const Token* t = var.tokens()->tokAt(3)->findClosingBracket();
bool found = var.tokens()->tokAt(3)->findClosingBracket(t);
ASSERT(found);
ASSERT_EQUALS(">", t->str()); ASSERT_EQUALS(">", t->str());
ASSERT(var.tokens()->tokAt(10) == t); ASSERT(var.tokens()->tokAt(10) == t);
} }
void canFindMatchingBracketsWithTooManyClosing() const { void canFindMatchingBracketsWithTooManyClosing() const {
givenACodeSampleToTokenize var("X< 1>2 > x1;\n"); givenACodeSampleToTokenize var("X< 1>2 > x1;\n");
const Token* t = 0; const Token* t = var.tokens()->next()->findClosingBracket();
bool found = var.tokens()->next()->findClosingBracket(t);
ASSERT(found);
ASSERT_EQUALS(">", t->str()); ASSERT_EQUALS(">", t->str());
ASSERT(var.tokens()->tokAt(3) == t); ASSERT(var.tokens()->tokAt(3) == t);
} }
@ -872,14 +863,12 @@ private:
void canFindMatchingBracketsWithTooManyOpening() const { void canFindMatchingBracketsWithTooManyOpening() const {
givenACodeSampleToTokenize var("X < (2 < 1) > x1;\n"); givenACodeSampleToTokenize var("X < (2 < 1) > x1;\n");
const Token* t = 0; const Token* t = var.tokens()->next()->findClosingBracket();
bool found = var.tokens()->next()->findClosingBracket(t); ASSERT(t != NULL && t->str() == ">");
ASSERT(found);
found = var.tokens()->tokAt(4)->findClosingBracket(t); t = var.tokens()->tokAt(4)->findClosingBracket();
ASSERT(!found); ASSERT(t != NULL && t->str() == ")");
} }
}; };
REGISTER_TEST(TestToken) REGISTER_TEST(TestToken)