From c9eab77683391f3a39dc752f699da0edccb473f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 3 May 2009 14:31:54 +0200 Subject: [PATCH] varid: speedup of the algorithm for setting variable ids --- src/tokenize.cpp | 108 ++++++++++++++++++++++++++++++----------------- 1 file changed, 69 insertions(+), 39 deletions(-) diff --git a/src/tokenize.cpp b/src/tokenize.cpp index 94bd2fcc6..31c9158cd 100644 --- a/src/tokenize.cpp +++ b/src/tokenize.cpp @@ -738,53 +738,83 @@ void Tokenizer::setVarId() { const std::string &classname(tok->next()->str()); - unsigned int indentlevel = 0; // What member variables are there in this class? - for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) + std::list varlist; { - // Indentation.. - if (tok2->str() == "{") - ++indentlevel; - else if (tok2->str() == "}") + unsigned int indentlevel = 0; + for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) { - if (indentlevel <= 1) - break; - --indentlevel; - } - - // If a variable id is found in the class declaration, start - // updating the variable ids in member functions.. - else if (indentlevel == 1 && tok2->varId() > 0) - { - const unsigned int varid(tok2->varId()); - const std::string &varname(tok2->str()); - - for (Token *tok3 = tok; tok3; tok3 = tok3->next()) + // Indentation.. + if (tok2->str() == "{") + ++indentlevel; + else if (tok2->str() == "}") { - // Found a class function.. - if (Token::Match(tok3, (classname + " :: %var% (").c_str())) + if (indentlevel <= 1) + break; + --indentlevel; + } + + // Found a member variable.. + else if (indentlevel == 1 && tok2->varId() > 0) + varlist.push_back(tok2); + } + } + + // Are there any member variables in this class? + if (varlist.empty()) + continue; + + + // Member functions for this class.. + std::list funclist; + { + const std::string funcpattern(classname + " :: %var% ("); + for (Token *tok2 = tok; tok2; tok2 = tok2->next()) + { + // Found a class function.. + if (Token::Match(tok2, funcpattern.c_str())) + { + // Goto the end paranthesis.. + while (tok2 && tok2->str() != ")") + tok2 = tok2->next(); + if (!tok2) + break; + + // If this is a function implementation.. add it to funclist + if (Token::Match(tok2, ") const|volatile| {")) + funclist.push_back(tok2); + } + } + } + + // Are there any member functions for this class? + if (funclist.empty()) + continue; + + + // Update the variable ids.. + for (std::list::const_iterator var = varlist.begin(); var != varlist.end(); ++var) + { + const unsigned int varid((*var)->varId()); + const std::string &varname((*var)->str()); + + // Parse each function.. + for (std::list::iterator func = funclist.begin(); func != funclist.end(); ++func) + { + unsigned int indentlevel = 0; + for (Token *tok2 = *func; tok2; tok2 = tok2->next()) + { + if (tok2->str() == "{") + ++indentlevel; + else if (tok2->str() == "}") { - unsigned int indentlevel2 = 0; - while (tok3) - { - if (tok3->str() == "{") - ++indentlevel2; - else if (tok3->str() == "}") - { - if (indentlevel2 <= 1) - break; - --indentlevel2; - } - else if (indentlevel2 == 0 && tok3->str() == ";") - break; - else if (indentlevel2 > 0 && tok3->varId() == 0 && tok3->str() == varname) - tok3->varId(varid); - tok3 = tok3->next(); - } - if (!tok3) + if (indentlevel <= 1) break; + --indentlevel; } + else if (indentlevel > 0 && tok2->str() == varname && tok2->varId() == 0) + tok2->varId(varid); } } }