From bb4184ca034b105fd5fa76bf2d02f4789a7023a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 17 Apr 2012 19:06:00 +0200 Subject: [PATCH] Tokenizer::setVarIdNew: better handling of class variables --- lib/tokenize.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 70cf1db59..38e04dbbe 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2874,6 +2874,23 @@ static bool setVarIdParseDeclaration(const Token **tok, const std::map= 2 && tok2 && Token::Match(tok2->tokAt(-2), "!!:: %type%")); } +static void setVarIdClassDeclaration(Token * const startToken, const std::map &variableId, const unsigned int scopeStartVarId) +{ + // end of scope + const Token * const endToken = startToken->link(); + + // replace varids.. + for (Token *tok = startToken; tok != endToken; tok = tok->next()) { + if (tok->isName() && tok->varId() <= scopeStartVarId) { + const std::map::const_iterator it = variableId.find(tok->str()); + if (it != variableId.end()) { + tok->varId(it->second); + } + } + } +} + + void Tokenizer::setVarIdNew() { // Clear all variable ids @@ -2887,12 +2904,15 @@ void Tokenizer::setVarIdNew() std::stack< std::map > scopeInfo; std::stack executableScope; executableScope.push(false); + std::stack scopestartvarid; // varid when scope starts + scopestartvarid.push(0); for (Token *tok = _tokens; tok; tok = tok->next()) { // scope info to handle shadow variables.. if (tok->str() == "(" && Token::simpleMatch(tok->link(), ") {")) { scopeInfo.push(variableId); } else if (tok->str() == "{") { + scopestartvarid.push(_varId); if (Token::simpleMatch(tok->previous(), ")")) { executableScope.push(true); } else { @@ -2900,6 +2920,16 @@ void Tokenizer::setVarIdNew() scopeInfo.push(variableId); } } else if (tok->str() == "}") { + // Set variable ids in class declaration.. + if (!isC() && !executableScope.top()) { + setVarIdClassDeclaration(tok->link(), variableId, scopestartvarid.top()); + } + + scopestartvarid.pop(); + if (scopestartvarid.empty()) { // should be impossible + scopestartvarid.push(0); + } + if (scopeInfo.empty()) { variableId.clear(); } else {