@@ -3793,6 +3793,14 @@ static std::set<MathLib::bigint> getSwitchValues(const Token *startbrace, bool &
37933793 return values;
37943794}
37953795
3796+ static bool coversAllEnumerators (const Scope *enumScope, const std::set<MathLib::bigint> &values)
3797+ {
3798+ const std::vector<Enumerator> &list = enumScope->enumeratorList ;
3799+ return std::all_of (list.cbegin (), list.cend (), [&](const Enumerator &e) {
3800+ return !e.value_known || values.count (e.value );
3801+ });
3802+ }
3803+
37963804bool isExhaustiveSwitch (const Token *startbrace)
37973805{
37983806 if (!startbrace || !Token::simpleMatch (startbrace->previous (), " ) {" ) || startbrace->scope ()->type != ScopeType::eSwitch)
@@ -3813,16 +3821,80 @@ bool isExhaustiveSwitch(const Token *startbrace)
38133821 if (condition->valueType ()->type == ValueType::Type::BOOL )
38143822 return switchValues.count (0 ) && switchValues.count (1 );
38153823
3816- if (condition->valueType ()->isEnum ()) {
3817- const std::vector<Enumerator> &enumList = condition->valueType ()->typeScope ->enumeratorList ;
3818- return std::all_of (enumList.cbegin (), enumList.cend (), [&](const Enumerator &e) {
3819- return !e.value_known || switchValues.count (e.value );
3820- });
3821- }
3824+ if (condition->valueType ()->isEnum ())
3825+ return coversAllEnumerators (condition->valueType ()->typeScope , switchValues);
38223826
38233827 return false ;
38243828}
38253829
3830+ static const Token *getEnumeratorTok (const Token *tok)
3831+ {
3832+ while (Token::simpleMatch (tok, " ::" ))
3833+ tok = tok->astOperand2 ();
3834+ return (tok && tok->enumerator ()) ? tok : nullptr ;
3835+ }
3836+
3837+ static const Token *getNextElseIf (const Token *ifTok)
3838+ {
3839+ const Token *endBrace = ifTok->linkAt (1 )->linkAt (1 );
3840+ if (!Token::simpleMatch (endBrace, " } else { if (" ) || !endBrace->tokAt (2 )->isInsertedBrace ())
3841+ return nullptr ;
3842+ return endBrace->tokAt (3 );
3843+ }
3844+
3845+ static std::vector<const Token *> getIfChainConditions (const Token *ifTok)
3846+ {
3847+ std::vector<const Token *> conditions;
3848+ for (; ifTok; ifTok = getNextElseIf (ifTok))
3849+ conditions.push_back (ifTok->next ()->astOperand2 ());
3850+ return conditions;
3851+ }
3852+
3853+ static const Token *getIfChainFinalElse (const Token *ifTok)
3854+ {
3855+ while (const Token *next = getNextElseIf (ifTok))
3856+ ifTok = next;
3857+ const Token *endBrace = ifTok->linkAt (1 )->linkAt (1 );
3858+ if (!Token::simpleMatch (endBrace, " } else {" ))
3859+ return nullptr ;
3860+ return endBrace->tokAt (2 );
3861+ }
3862+
3863+ bool isExhaustiveIfChain (const Token *ifTok)
3864+ {
3865+ if (getIfChainFinalElse (ifTok))
3866+ return true ;
3867+
3868+ const Token *expr = nullptr ;
3869+ const Scope *enumScope = nullptr ;
3870+ std::set<MathLib::bigint> values;
3871+
3872+ for (const Token *cond : getIfChainConditions (ifTok)) {
3873+ if (!Token::simpleMatch (cond, " ==" ))
3874+ return false ;
3875+
3876+ const Token *exprTok = cond->astOperand1 ();
3877+ const Token *enumTok = getEnumeratorTok (cond->astOperand2 ());
3878+ if (!enumTok || !enumTok->enumerator ()->value_known )
3879+ return false ;
3880+
3881+ if (enumScope && enumScope != enumTok->enumerator ()->scope )
3882+ return false ;
3883+
3884+ if (exprTok->exprId () == 0 || (expr && expr->exprId () != exprTok->exprId ()))
3885+ return false ;
3886+
3887+ expr = exprTok;
3888+ enumScope = enumTok->enumerator ()->scope ;
3889+ values.insert (enumTok->enumerator ()->value );
3890+ }
3891+
3892+ if (expr->valueType () && expr->valueType ()->typeScope != enumScope)
3893+ return false ;
3894+
3895+ return coversAllEnumerators (enumScope, values);
3896+ }
3897+
38263898bool isUnreachableOperand (const Token *tok)
38273899{
38283900 for (;;)
0 commit comments