From e9071a8bb6453aff859426a45da50d7d63039ff1 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Mon, 2 Jan 2012 22:43:38 +0200 Subject: [PATCH] Refactoring: Move code into templatesimplifier: templateParameters() --- lib/templatesimplifier.cpp | 52 +++++++++++++++++++++++++++++ lib/templatesimplifier.h | 8 +++++ lib/tokenize.cpp | 68 +++----------------------------------- 3 files changed, 64 insertions(+), 64 deletions(-) diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index d505ae177..007f2152a 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -170,3 +170,55 @@ const Token* TemplateSimplifier::hasComplicatedSyntaxErrorsInTemplates(Token *to return 0; } + +unsigned int TemplateSimplifier::templateParameters(const Token *tok) +{ + unsigned int numberOfParameters = 0; + + if (!tok) + return 0; + if (tok->str() != "<") + return 0; + tok = tok->next(); + + unsigned int level = 0; + + while (tok) { + if (level == 0) + ++numberOfParameters; + + // skip std:: + while (Token::Match(tok, "%var% ::")) + tok = tok->tokAt(2); + if (!tok) + return 0; + + // num/type .. + if (!tok->isNumber() && !tok->isName()) + return 0; + tok = tok->next(); + + // optional "*" + if (tok->str() == "*") + tok = tok->next(); + + // inner template + if (tok->str() == "<") { + ++level; + tok = tok->next(); + continue; + } + + // ,/> + while (tok->str() == ">") { + if (level == 0) + return numberOfParameters; + --level; + tok = tok->next(); + } + if (tok->str() != ",") + break; + tok = tok->next(); + } + return 0; +} diff --git a/lib/templatesimplifier.h b/lib/templatesimplifier.h index 63a35cb10..fd0af0ca5 100644 --- a/lib/templatesimplifier.h +++ b/lib/templatesimplifier.h @@ -48,6 +48,14 @@ public: * the location of syntax error. */ static const Token* hasComplicatedSyntaxErrorsInTemplates(Token *tokens); + + /** + * is the token pointing at a template parameters block + * < int , 3 > => yes + * \param tok start token that must point at "<" + * \return number of parameters (invalid parameters => 0) + */ + static unsigned int templateParameters(const Token *tok); }; /// @} diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 80812bc7c..bad3dbe18 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2681,66 +2681,6 @@ void Tokenizer::simplifyLabelsCaseDefault() } } - -/** - * is the token pointing at a template parameters block - * < int , 3 > => yes - * \param tok start token that must point at "<" - * \return number of parameters (invalid parameters => 0) - */ -static unsigned int templateParameters(const Token *tok) -{ - unsigned int numberOfParameters = 0; - - if (!tok) - return 0; - if (tok->str() != "<") - return 0; - tok = tok->next(); - - unsigned int level = 0; - - while (tok) { - if (level == 0) - ++numberOfParameters; - - // skip std:: - while (Token::Match(tok, "%var% ::")) - tok = tok->tokAt(2); - if (!tok) - return 0; - - // num/type .. - if (!tok->isNumber() && !tok->isName()) - return 0; - tok = tok->next(); - - // optional "*" - if (tok->str() == "*") - tok = tok->next(); - - // inner template - if (tok->str() == "<") { - ++level; - tok = tok->next(); - continue; - } - - // ,/> - while (tok->str() == ">") { - if (level == 0) - return numberOfParameters; - --level; - tok = tok->next(); - } - if (tok->str() != ",") - break; - tok = tok->next(); - } - return 0; -} - - /** * Remove "template < ..." they can cause false positives because they are not expanded */ @@ -2817,7 +2757,7 @@ std::set Tokenizer::simplifyTemplatesExpandSpecialized() while (tok2 && (tok2->isName() || tok2->str() == "*")) tok2 = tok2->next(); - if (!templateParameters(tok2)) + if (!TemplateSimplifier::templateParameters(tok2)) continue; // unknown template.. bail out @@ -2954,13 +2894,13 @@ std::list Tokenizer::simplifyTemplatesGetTemplateInstantiations() // parse backwards and add template instantiations for (; tok2 && tok2 != tok; tok2 = tok2->previous()) { if (Token::Match(tok2, ", %var% <") && - templateParameters(tok2->tokAt(2))) { + TemplateSimplifier::templateParameters(tok2->tokAt(2))) { used.push_back(tok2->next()); } } // Add outer template.. - if (templateParameters(tok->next())) + if (TemplateSimplifier::templateParameters(tok->next())) used.push_back(tok); } } @@ -3069,7 +3009,7 @@ static bool simplifyTemplatesInstantiateMatch(const Token *instance, const std:: if (!Token::simpleMatch(instance, (name + " <").c_str())) return false; - if (numberOfArguments != templateParameters(instance->next())) + if (numberOfArguments != TemplateSimplifier::templateParameters(instance->next())) return false; if (patternAfter) {