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

Method size

lib/checkstl.cpp:1764–1811  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1762}
1763
1764void CheckStlImpl::size()
1765{
1766 if (!mSettings.severity.isEnabled(Severity::performance))
1767 return;
1768
1769 if (mSettings.standards.cpp >= Standards::CPP11)
1770 return;
1771
1772 logChecker("CheckStl::size"); // performance,c++03
1773
1774 const SymbolDatabase* const symbolDatabase = mTokenizer->getSymbolDatabase();
1775 for (const Scope * scope : symbolDatabase->functionScopes) {
1776 for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
1777 if (Token::Match(tok, "%var% . size ( )") ||
1778 Token::Match(tok, "%name% . %var% . size ( )")) {
1779 // get the variable
1780 const Token *varTok = tok;
1781 if (tok->strAt(2) != "size")
1782 varTok = varTok->tokAt(2);
1783
1784 const Token* const end = varTok->tokAt(5);
1785
1786 // check for comparison to zero
1787 if ((!tok->previous()->isArithmeticalOp() && Token::Match(end, "==|<=|!=|> 0")) ||
1788 (end->next() && !end->next()->isArithmeticalOp() && Token::Match(tok->tokAt(-2), "0 ==|>=|!=|<"))) {
1789 if (isCpp03ContainerSizeSlow(varTok)) {
1790 sizeError(varTok);
1791 continue;
1792 }
1793 }
1794
1795 // check for comparison to one
1796 if ((!tok->previous()->isArithmeticalOp() && Token::Match(end, ">=|< 1") && !end->tokAt(2)->isArithmeticalOp()) ||
1797 (end->next() && !end->next()->isArithmeticalOp() && Token::Match(tok->tokAt(-2), "1 <=|>") && !tok->tokAt(-3)->isArithmeticalOp())) {
1798 if (isCpp03ContainerSizeSlow(varTok))
1799 sizeError(varTok);
1800 }
1801
1802 // check for using as boolean expression
1803 else if ((Token::Match(tok->tokAt(-2), "if|while (") && end->str() == ")") ||
1804 (tok->previous()->tokType() == Token::eLogicalOp && Token::Match(end, "&&|)|,|;|%oror%"))) {
1805 if (isCpp03ContainerSizeSlow(varTok))
1806 sizeError(varTok);
1807 }
1808 }
1809 }
1810 }
1811}
1812
1813void CheckStlImpl::sizeError(const Token *tok)
1814{

Callers 15

checkRecursiveMethod · 0.45
possiblyAliasedMethod · 0.45
isRelativePatternFunction · 0.45
parseFunctionCallMethod · 0.45
getSummaryFilesFunction · 0.45
getSummaryDataFunction · 0.45
getOverrunIndexValuesFunction · 0.45
stringifyIndexesFunction · 0.45
checkBufferSizeFunction · 0.45
bufferOverflowMethod · 0.45

Calls 6

isCpp03ContainerSizeSlowFunction · 0.85
nextMethod · 0.80
isArithmeticalOpMethod · 0.80
isEnabledMethod · 0.45
tokAtMethod · 0.45
strMethod · 0.45

Tested by 15

podtypeValidMethod · 0.36
typechecksValidMethod · 0.36
smartPointerValidMethod · 0.36
platformTypeValidMethod · 0.36
memoryResourceValidMethod · 0.36
defineValidMethod · 0.36
undefineValidMethod · 0.36
reflectionValidMethod · 0.36
markupValidMethod · 0.36
containerValidMethod · 0.36
loadSimpleMethod · 0.36
loadSimpleWithIgnoreMethod · 0.36