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

Function setVarIdClassDeclaration

lib/tokenize.cpp:4569–4650  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4567}
4568
4569static bool setVarIdClassDeclaration(Token* const startToken,
4570 VariableMap& variableMap,
4571 const nonneg int scopeStartVarId,
4572 const std::map<std::string, std::set<std::string>>& templateVarUsage,
4573 std::map<nonneg int, std::map<std::string, nonneg int>>& structMembers,
4574 std::set<nonneg int>& templateVarIdUsage)
4575{
4576 // end of scope
4577 const Token* const endToken = startToken->link();
4578
4579 // determine class name
4580 std::string className;
4581 for (const Token *tok = startToken->previous(); tok; tok = tok->previous()) {
4582 if (!tok->isName() && tok->str() != ":")
4583 break;
4584 if (Token::Match(tok, "class|struct|enum %type% [:{]")) {
4585 className = tok->strAt(1);
4586 break;
4587 }
4588 }
4589
4590 // replace varids..
4591 int indentlevel = 0;
4592 bool initList = false;
4593 bool inEnum = false;
4594 const Token *initListArgLastToken = nullptr;
4595 for (Token *tok = startToken->next(); tok != endToken; tok = tok->next()) {
4596 if (!tok)
4597 return false;
4598 if (initList) {
4599 if (tok == initListArgLastToken)
4600 initListArgLastToken = nullptr;
4601 else if (!initListArgLastToken &&
4602 Token::Match(tok->previous(), "%name%|>|>> {|(") &&
4603 Token::Match(tok->link(), "}|) ,|{"))
4604 initListArgLastToken = tok->link();
4605 }
4606 if (tok->str() == "{") {
4607 inEnum = isEnumStart(tok);
4608 if (!inEnum && isClassStructUnionEnumStart(tok)) { // nested type
4609 tok = tok->link();
4610 continue;
4611 }
4612 if (initList && !initListArgLastToken)
4613 initList = false;
4614 ++indentlevel;
4615 } else if (tok->str() == "}") {
4616 --indentlevel;
4617 inEnum = false;
4618 } else if (initList && indentlevel == 0 && Token::Match(tok->previous(), "[,:] %name% [({]")) {
4619 const auto it = variableMap.map(false).find(tok->str());
4620 if (it != variableMap.map(false).end()) {
4621 tok->varId(it->second.id);
4622 }
4623 } else if (tok->isName() && tok->varId() <= scopeStartVarId) {
4624 if (indentlevel > 0 || initList) {
4625 if (Token::Match(tok->previous(), "::|.") && tok->strAt(-2) != "this" && !Token::simpleMatch(tok->tokAt(-5), "( * this ) ."))
4626 continue;

Callers 1

setVarIdPass1Method · 0.85

Calls 10

isEnumStartFunction · 0.85
setVarIdStructMembersFunction · 0.85
addTemplateVarIdUsageFunction · 0.85
nextMethod · 0.80
simpleMatchFunction · 0.70
strMethod · 0.45
findMethod · 0.45
endMethod · 0.45
tokAtMethod · 0.45

Tested by

no test coverage detected