MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / setVarIdPass1

Method setVarIdPass1

lib/tokenize.cpp:4710–5111  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4708};
4709
4710void Tokenizer::setVarIdPass1()
4711{
4712 const bool cpp = isCPP();
4713 // Variable declarations can't start with "return" etc.
4714 const std::unordered_set<std::string>& notstart = (isC()) ? notstart_c : notstart_cpp;
4715
4716 VariableMap variableMap;
4717 std::map<nonneg int, std::map<std::string, nonneg int>> structMembers;
4718
4719 std::stack<VarIdScopeInfo> scopeStack;
4720
4721 scopeStack.emplace(/*VarIdScopeInfo()*/);
4722 std::stack<const Token *> functionDeclEndStack;
4723 const Token *functionDeclEndToken = nullptr;
4724 bool initlist = false;
4725 bool inlineFunction = false;
4726 for (Token *tok = list.front(); tok; tok = tok->next()) {
4727 if (tok->isOp())
4728 continue;
4729 if (cpp && Token::simpleMatch(tok, "template <")) {
4730 Token* closingBracket = tok->next()->findClosingBracket();
4731 if (closingBracket)
4732 tok = closingBracket;
4733 continue;
4734 }
4735
4736 if (tok == functionDeclEndToken) {
4737 functionDeclEndStack.pop();
4738 functionDeclEndToken = functionDeclEndStack.empty() ? nullptr : functionDeclEndStack.top();
4739 if (tok->str() == ":")
4740 initlist = true;
4741 else if (tok->str() == ";") {
4742 if (!variableMap.leaveScope())
4743 cppcheckError(tok);
4744 } else if (tok->str() == "{") {
4745 scopeStack.emplace(true, scopeStack.top().isStructInit || tok->strAt(-1) == "=", /*isEnum=*/ false, variableMap.getVarId());
4746
4747 // check if this '{' is a start of an "if" body
4748 const Token * ifToken = tok->previous();
4749 if (ifToken && ifToken->str() == ")")
4750 ifToken = ifToken->link();
4751 else
4752 ifToken = nullptr;
4753 if (ifToken)
4754 ifToken = ifToken->previous();
4755 if (ifToken && ifToken->str() == "if") {
4756 // open another scope to differentiate between variables declared in the "if" condition and in the "if" body
4757 variableMap.enterScope();
4758 }
4759 }
4760 } else if (!initlist && tok->str()=="(") {
4761 const Token * newFunctionDeclEnd = nullptr;
4762 if (!scopeStack.top().isExecutable)
4763 newFunctionDeclEnd = TokenList::isFunctionHead(tok, "{:;");
4764 else {
4765 const Token* tokenLinkNext = tok->link()->next();
4766 if (Token::simpleMatch(tokenLinkNext, ".")) { // skip trailing return type
4767 tokenLinkNext = tokenLinkNext->next();

Callers

nothing calls this directly

Calls 15

isCPPFunction · 0.85
isEnumStartFunction · 0.85
setVarIdClassDeclarationFunction · 0.85
terminatedFunction · 0.85
findsimplematchFunction · 0.85
setVarIdParseDeclarationFunction · 0.85
addTemplateVarIdUsageFunction · 0.85
setVarIdStructMembersFunction · 0.85
frontMethod · 0.80
nextMethod · 0.80
isOpMethod · 0.80

Tested by

no test coverage detected