Refactoring Token::findClosingBracket.
This commit is contained in:
parent
6c05f78a8f
commit
2a78db4c06
|
@ -448,7 +448,7 @@ std::list<Token *> 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<Token *> 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()) {
|
||||
|
|
|
@ -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<Token*>(const_cast<const Token*>(this)->findClosingBracket());
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
|
12
lib/token.h
12
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<Token*>(tok);
|
||||
return retVal;
|
||||
}
|
||||
const Token* findClosingBracket() const;
|
||||
Token* findClosingBracket();
|
||||
|
||||
private:
|
||||
void next(Token *nextToken) {
|
||||
|
|
|
@ -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::map<std::stri
|
|||
++typeCount;
|
||||
}
|
||||
} else if (tok2->str() == "<" && 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 ="
|
||||
|
|
|
@ -832,18 +832,14 @@ private:
|
|||
void canFindMatchingBracketsNeedsOpen() const {
|
||||
givenACodeSampleToTokenize var("std::deque<std::set<int> > 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<std::set<int> > intsets;");
|
||||
|
||||
Token* t = 0;
|
||||
bool found = var.tokens()->tokAt(7)->findClosingBracket(t);
|
||||
ASSERT(found);
|
||||
Token* t = const_cast<Token*>(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<std::set<int> > 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)
|
||||
|
|
Loading…
Reference in New Issue