| 1550 | } |
| 1551 | |
| 1552 | void TemplateSimplifier::addNamespace(const TokenAndName &templateDeclaration, const Token *tok) |
| 1553 | { |
| 1554 | // find start of qualification |
| 1555 | const Token * tokStart = tok; |
| 1556 | int offset = 0; |
| 1557 | while (Token::Match(tokStart->tokAt(-2), "%name% ::")) { |
| 1558 | tokStart = tokStart->tokAt(-2); |
| 1559 | offset -= 2; |
| 1560 | } |
| 1561 | // decide if namespace needs to be inserted in or appended to token list |
| 1562 | const bool insert = tokStart != tok; |
| 1563 | |
| 1564 | std::string::size_type start = 0; |
| 1565 | std::string::size_type end = 0; |
| 1566 | bool inTemplate = false; |
| 1567 | int level = 0; |
| 1568 | while ((end = templateDeclaration.scope().find(' ', start)) != std::string::npos) { |
| 1569 | std::string token = templateDeclaration.scope().substr(start, end - start); |
| 1570 | // done if scopes overlap |
| 1571 | if (token == tokStart->str() && tok->strAt(-1) != "::") |
| 1572 | break; |
| 1573 | if (token == "<") { |
| 1574 | inTemplate = true; |
| 1575 | ++level; |
| 1576 | } |
| 1577 | if (inTemplate) { |
| 1578 | if (insert) |
| 1579 | mTokenList.back()->tokAt(offset)->str(mTokenList.back()->strAt(offset) + token); |
| 1580 | else |
| 1581 | mTokenList.back()->str(mTokenList.back()->str() + token); |
| 1582 | if (token == ">") { |
| 1583 | --level; |
| 1584 | if (level == 0) |
| 1585 | inTemplate = false; |
| 1586 | } |
| 1587 | } else { |
| 1588 | if (insert) |
| 1589 | mTokenList.back()->tokAt(offset)->insertToken(token); |
| 1590 | else |
| 1591 | mTokenList.addtoken(token, tok->linenr(), tok->column(), tok->fileIndex()); |
| 1592 | } |
| 1593 | start = end + 1; |
| 1594 | } |
| 1595 | // don't add if it already exists |
| 1596 | std::string token = templateDeclaration.scope().substr(start, end - start); |
| 1597 | if (token != tokStart->str() || tok->strAt(-1) != "::") { |
| 1598 | if (insert) { |
| 1599 | if (!inTemplate) |
| 1600 | mTokenList.back()->tokAt(offset)->insertToken(templateDeclaration.scope().substr(start)); |
| 1601 | else |
| 1602 | mTokenList.back()->tokAt(offset)->str(mTokenList.back()->strAt(offset) + templateDeclaration.scope().substr(start)); |
| 1603 | mTokenList.back()->tokAt(offset)->insertToken("::"); |
| 1604 | } else { |
| 1605 | if (!inTemplate) |
| 1606 | mTokenList.addtoken(templateDeclaration.scope().substr(start), tok->linenr(), tok->column(), tok->fileIndex()); |
| 1607 | else |
| 1608 | mTokenList.back()->str(mTokenList.back()->str() + templateDeclaration.scope().substr(start)); |
| 1609 | mTokenList.addtoken("::", tok->linenr(), tok->column(), tok->fileIndex()); |