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

Method useStandardLibrary

lib/checkfunctions.cpp:745–847  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

743}
744
745void CheckFunctionsImpl::useStandardLibrary()
746{
747 if (!mSettings.severity.isEnabled(Severity::style))
748 return;
749
750 logChecker("CheckFunctions::useStandardLibrary"); // style
751
752 for (const Scope& scope: mTokenizer->getSymbolDatabase()->scopeList) {
753 if (scope.type != ScopeType::eFor)
754 continue;
755
756 const Token *forToken = scope.classDef;
757 // for ( initToken ; condToken ; stepToken )
758 const Token* initToken = getInitTok(forToken);
759 if (!initToken)
760 continue;
761 const Token* condToken = getCondTok(forToken);
762 if (!condToken)
763 continue;
764 const Token* stepToken = getStepTok(forToken);
765 if (!stepToken)
766 continue;
767
768 // 1. we expect that idx variable will be initialized with 0
769 const Token* idxToken = initToken->astOperand1();
770 const Token* initVal = initToken->astOperand2();
771 if (!idxToken || !initVal || !initVal->hasKnownIntValue() || initVal->getKnownIntValue() != 0)
772 continue;
773 const auto idxVarId = idxToken->varId();
774 if (0 == idxVarId)
775 continue;
776
777 // 2. we expect that idx will be less of some variable
778 if (!condToken->isComparisonOp())
779 continue;
780
781 const auto& secondOp = condToken->str();
782 const bool isLess = "<" == secondOp &&
783 isConstExpression(condToken->astOperand2(), mSettings.library) &&
784 condToken->astOperand1()->varId() == idxVarId;
785 const bool isMore = ">" == secondOp &&
786 isConstExpression(condToken->astOperand1(), mSettings.library) &&
787 condToken->astOperand2()->varId() == idxVarId;
788
789 if (!(isLess || isMore))
790 continue;
791
792 // 3. we expect idx incrementing by 1
793 const bool inc = stepToken->str() == "++" && stepToken->astOperand1() && stepToken->astOperand1()->varId() == idxVarId;
794 const bool plusOne = stepToken->isBinaryOp() && stepToken->str() == "+=" &&
795 stepToken->astOperand1()->varId() == idxVarId &&
796 stepToken->astOperand2()->str() == "1";
797 if (!inc && !plusOne)
798 continue;
799
800 // technically using void* here is not correct but some compilers could allow it
801
802 const Token *tok = scope.bodyStart;

Callers 1

runChecksMethod · 0.80

Calls 12

getInitTokFunction · 0.85
getCondTokFunction · 0.85
getStepTokFunction · 0.85
isConstExpressionFunction · 0.85
astOperand1Method · 0.80
astOperand2Method · 0.80
getKnownIntValueMethod · 0.80
isBinaryOpMethod · 0.80
nextMethod · 0.80
isEnabledMethod · 0.45
hasKnownIntValueMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected