@@ -2869,38 +2869,37 @@ static bool isTernaryAssignment(const Token* assignTok, nonneg int loopVarId, no
28692869
28702870namespace {
28712871 struct LoopAnalyzer {
2872- const Token* bodyTok = nullptr ;
2873- const Token* loopVar = nullptr ;
2874- const Settings* settings = nullptr ;
2875- std::set<nonneg int > varsChanged;
2876-
2877- explicit LoopAnalyzer (const Token* tok, const Settings* psettings)
2878- : bodyTok(tok->linkAt (1 )->next()), settings(psettings)
2872+ private:
2873+ const Token* mBodyTok ;
2874+ const Token* mLoopVar {};
2875+ const Settings& mSettings ;
2876+ std::set<nonneg int > mVarsChanged ;
2877+
2878+ public:
2879+ explicit LoopAnalyzer (const Token* tok, const Settings& settings)
2880+ : mBodyTok(tok->linkAt (1 )->next()), mSettings(settings)
28792881 {
28802882 const Token* splitTok = tok->next ()->astOperand2 ();
28812883 if (Token::simpleMatch (splitTok, " :" ) && splitTok->previous ()->varId () != 0 ) {
2882- loopVar = splitTok->previous ();
2884+ mLoopVar = splitTok->previous ();
28832885 }
28842886 if (valid ()) {
28852887 findChangedVariables ();
28862888 }
28872889 }
2888- bool isLoopVarChanged () const {
2889- return varsChanged.count (loopVar->varId ()) > 0 ;
2890- }
2891-
2890+ private:
28922891 bool isModified (const Token* tok) const
28932892 {
28942893 if (tok->variable () && tok->variable ()->isConst ())
28952894 return false ;
28962895 int n = 1 + (astIsPointer (tok) ? 1 : 0 );
28972896 for (int i = 0 ; i < n; i++) {
28982897 bool inconclusive = false ;
2899- if (isVariableChangedByFunctionCall (tok, i, *settings , &inconclusive))
2898+ if (isVariableChangedByFunctionCall (tok, i, mSettings , &inconclusive))
29002899 return true ;
29012900 if (inconclusive)
29022901 return true ;
2903- if (isVariableChanged (tok, i, *settings ))
2902+ if (isVariableChanged (tok, i, mSettings ))
29042903 return true ;
29052904 }
29062905 return false ;
@@ -2909,7 +2908,7 @@ namespace {
29092908 template <class Predicate , class F >
29102909 void findTokens (Predicate pred, F f) const
29112910 {
2912- for (const Token* tok = bodyTok ; precedes (tok, bodyTok ->link ()); tok = tok->next ()) {
2911+ for (const Token* tok = mBodyTok ; precedes (tok, mBodyTok ->link ()); tok = tok->next ()) {
29132912 if (pred (tok))
29142913 f (tok);
29152914 }
@@ -2918,7 +2917,7 @@ namespace {
29182917 template <class Predicate >
29192918 const Token* findToken (Predicate pred) const
29202919 {
2921- for (const Token* tok = bodyTok ; precedes (tok, bodyTok ->link ()); tok = tok->next ()) {
2920+ for (const Token* tok = mBodyTok ; precedes (tok, mBodyTok ->link ()); tok = tok->next ()) {
29222921 if (pred (tok))
29232922 return tok;
29242923 }
@@ -2933,58 +2932,56 @@ namespace {
29332932 }
29342933
29352934 bool valid () const {
2936- return bodyTok && loopVar ;
2935+ return mBodyTok && mLoopVar ;
29372936 }
29382937
2938+ public:
29392939 std::string findAlgo () const
29402940 {
29412941 if (!valid ())
29422942 return " " ;
2943- bool loopVarChanged = isLoopVarChanged ();
2944- if (!loopVarChanged && varsChanged.empty ()) {
2945- if (hasGotoOrBreak ())
2946- return " " ;
2947- bool alwaysTrue = true ;
2948- bool alwaysFalse = true ;
2949- auto hasReturn = [](const Token* tok) {
2950- return Token::simpleMatch (tok, " return" );
2951- };
2952- findTokens (hasReturn, [&](const Token* tok) {
2953- const Token* returnTok = tok->astOperand1 ();
2954- if (!returnTok || !returnTok->hasKnownIntValue () || !astIsBool (returnTok)) {
2955- alwaysTrue = false ;
2956- alwaysFalse = false ;
2957- return ;
2958- }
2959- (returnTok->getKnownIntValue () ? alwaysTrue : alwaysFalse) &= true ;
2960- (returnTok->getKnownIntValue () ? alwaysFalse : alwaysTrue) &= false ;
2961- });
2962- if (alwaysTrue == alwaysFalse)
2963- return " " ;
2964- if (alwaysTrue)
2965- return " std::any_of" ;
2966- return " std::all_of or std::none_of" ;
2967- }
2968- return " " ;
2943+ if (!mVarsChanged .empty ())
2944+ return " " ;
2945+ if (hasGotoOrBreak ())
2946+ return " " ;
2947+ bool alwaysTrue = true ;
2948+ bool alwaysFalse = true ;
2949+ const auto hasReturn = [](const Token* tok) {
2950+ return Token::simpleMatch (tok, " return" );
2951+ };
2952+ findTokens (hasReturn, [&](const Token* tok) {
2953+ const Token* returnTok = tok->astOperand1 ();
2954+ if (!returnTok || !returnTok->hasKnownIntValue () || !astIsBool (returnTok)) {
2955+ alwaysTrue = false ;
2956+ alwaysFalse = false ;
2957+ return ;
2958+ }
2959+ (returnTok->getKnownIntValue () ? alwaysTrue : alwaysFalse) &= true ;
2960+ (returnTok->getKnownIntValue () ? alwaysFalse : alwaysTrue) &= false ;
2961+ });
2962+ if (alwaysTrue == alwaysFalse)
2963+ return " " ;
2964+ if (alwaysTrue)
2965+ return " std::any_of" ;
2966+ return " std::all_of or std::none_of" ;
29692967 }
2970-
2968+ private:
29712969 bool isLocalVar (const Variable* var) const
29722970 {
29732971 if (!var)
29742972 return false ;
29752973 if (var->isPointer () || var->isReference ())
29762974 return false ;
2977- if (var->declarationId () == loopVar ->varId ())
2975+ if (var->declarationId () == mLoopVar ->varId ())
29782976 return false ;
29792977 const Scope* scope = var->scope ();
2980- return scope && scope->isNestedIn (bodyTok ->scope ());
2978+ return scope && scope->isNestedIn (mBodyTok ->scope ());
29812979 }
29822980
2983- private:
29842981 void findChangedVariables ()
29852982 {
29862983 std::set<nonneg int > vars;
2987- for (const Token* tok = bodyTok ; precedes (tok, bodyTok ->link ()); tok = tok->next ()) {
2984+ for (const Token* tok = mBodyTok ; precedes (tok, mBodyTok ->link ()); tok = tok->next ()) {
29882985 if (tok->varId () == 0 )
29892986 continue ;
29902987 if (vars.count (tok->varId ()) > 0 )
@@ -2995,7 +2992,7 @@ namespace {
29952992 }
29962993 if (!isModified (tok))
29972994 continue ;
2998- varsChanged .insert (tok->varId ());
2995+ mVarsChanged .insert (tok->varId ());
29992996 vars.insert (tok->varId ());
30002997 }
30012998 }
@@ -3046,7 +3043,7 @@ void CheckStlImpl::useStlAlgorithm()
30463043 continue ;
30473044 if (!Token::simpleMatch (tok->linkAt (1 ), " ) {" ))
30483045 continue ;
3049- LoopAnalyzer a{tok, & mSettings };
3046+ LoopAnalyzer a{tok, mSettings };
30503047 std::string algoName = a.findAlgo ();
30513048 if (!algoName.empty ()) {
30523049 useStlAlgorithmError (tok, algoName);
0 commit comments