Code simplification now that ">>" is turned into "> >" when it closes template argument lists.

This commit is contained in:
Simon Martin 2015-03-14 23:02:10 +01:00
parent 40769c9a59
commit 32a7a178e8
2 changed files with 11 additions and 43 deletions

View File

@ -86,7 +86,7 @@ void TemplateSimplifier::cleanupAfterSimplify(Token *tokens)
end = end->link()->next();
break;
}
if (!Token::Match(end, "%name%|::|<|>|>>|,")) {
if (!Token::Match(end, "%name%|::|<|>|,")) {
end = nullptr;
break;
}
@ -564,10 +564,10 @@ void TemplateSimplifier::useDefaultArgumentValues(const std::list<Token *> &temp
++templateParmDepth;
// end of template parameters?
if (Token::Match(tok, ">|>>")) {
if (Token::Match(tok, ">|>> class|struct %name%"))
if (tok->str() == ">") {
if (Token::Match(tok, "> class|struct %name%"))
classname = tok->strAt(2);
templateParmDepth -= (1 + (tok->str() == ">>"));
--templateParmDepth;
if (0 == templateParmDepth)
break;
}
@ -649,11 +649,7 @@ void TemplateSimplifier::useDefaultArgumentValues(const std::list<Token *> &temp
++indentlevel;
} else if (indentlevel > 0 && tok2->str() == ">")
--indentlevel;
else if (indentlevel > 0 && tok2->str() == ">>") {
indentlevel -= 2;
if (indentlevel < 0)
tok2->str(">");
} else if (indentlevel == 0 && Token::Match(tok2, ",|>|>>"))
else if (indentlevel == 0 && Token::Match(tok2, ",|>"))
break;
if (indentlevel < 0)
break;
@ -680,13 +676,11 @@ bool TemplateSimplifier::instantiateMatch(const Token *instance, const std::stri
if (patternAfter) {
const Token *tok = instance;
unsigned int indentlevel = 0;
for (tok = instance; tok && (tok->str() != ">" || indentlevel > 0) && (tok->str() != ">>" || indentlevel > 1); tok = tok->next()) {
for (tok = instance; tok && (tok->str() != ">" || indentlevel > 0); tok = tok->next()) {
if (Token::Match(tok, "[<,] %name% <") && templateParameters(tok->tokAt(2)) > 0)
++indentlevel;
if (indentlevel > 0 && tok->str() == ">")
--indentlevel;
if (indentlevel > 0 && tok->str() == ">>")
indentlevel -= (indentlevel > 1) ? 2 : 1;
}
if (!tok || !Token::Match(tok->next(), patternAfter))
return false;
@ -779,17 +773,12 @@ void TemplateSimplifier::expandTemplate(
if (itype < typeParametersInDeclaration.size()) {
unsigned int typeindentlevel = 0;
for (const Token *typetok = typesUsedInTemplateInstantiation[itype];
typetok && (typeindentlevel>0 || !Token::Match(typetok, ",|>|>>"));
typetok && (typeindentlevel>0 || !Token::Match(typetok, ",|>"));
typetok = typetok->next()) {
if (Token::Match(typetok, "%name% <") && templateParameters(typetok->next()) > 0)
++typeindentlevel;
else if (typeindentlevel > 0 && typetok->str() == ">")
--typeindentlevel;
else if (typeindentlevel > 0 && typetok->str() == ">>") {
if (typeindentlevel == 1)
break;
typeindentlevel -= 2;
}
tokenlist.addtoken(typetok, tok3->linenr(), tok3->fileIndex());
}
continue;
@ -1260,15 +1249,7 @@ bool TemplateSimplifier::simplifyTemplateInstantiations(
++indentlevel;
else if (indentlevel > 0 && Token::Match(tok3, "> [,>]"))
--indentlevel;
else if (indentlevel > 0 && tok3->str() == ">>") {
if (indentlevel == 1) {
templateMatchPattern += '>';
typeForNewNameStr += '>';
break;
}
indentlevel -= 2;
}
templateMatchPattern += (tok3->str() == ">>") ? std::string("> >") : tok3->str();
templateMatchPattern += tok3->str();
templateMatchPattern += ' ';
if (indentlevel == 0 && Token::Match(tok3->previous(), "[<,]"))
typesUsedInTemplateInstantiation.push_back(tok3);

View File

@ -2368,7 +2368,7 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map<std::stri
} else if ((TemplateSimplifier::templateParameters(tok2) > 0) ||
Token::simpleMatch(tok2, "< >") /* Ticket #4764 */) {
tok2 = tok2->findClosingBracket();
if (!Token::Match(tok2, ">|>>"))
if (tok2->str() != ">")
break;
singleNameCount = 1;
} else if (Token::Match(tok2, "&|&&")) {
@ -2964,14 +2964,10 @@ void Tokenizer::createLinks2()
type.pop();
else if (token->str() == "<" && token->previous() && token->previous()->isName() && !token->previous()->varId())
type.push(token);
else if (Token::Match(token, ">|>>")) {
else if (token->str() == ">") {
if (type.empty() || type.top()->str() != "<") // < and > don't match.
continue;
if (token->next() && !Token::Match(token->next(), "%name%|>|>>|&|*|::|,|(|)|{|;|["))
continue;
// Check type of open link
if (token->str() == ">>" && type.size() < 2)
if (token->next() && !Token::Match(token->next(), "%name%|>|&|*|::|,|(|)|{|;|["))
continue;
// if > is followed by [ .. "new a<b>[" is expected
@ -2987,15 +2983,6 @@ void Tokenizer::createLinks2()
Token* top = type.top();
type.pop();
if (token->str() == ">>" && type.top()->str() != "<") {
type.push(top);
continue;
}
if (token->str() == ">>") { // C++11 right angle bracket
token->str(">");
token->insertToken(">");
}
Token::createMutualLinks(top, token);
}