| 743 | } |
| 744 | |
| 745 | void 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; |
no test coverage detected