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

Method suspiciousFloatingPointCast

lib/checkother.cpp:481–529  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

479}
480
481void CheckOtherImpl::suspiciousFloatingPointCast()
482{
483 if (!mSettings.severity.isEnabled(Severity::style) && !mSettings.isPremiumEnabled("suspiciousFloatingPointCast"))
484 return;
485
486 logChecker("CheckOther::suspiciousFloatingPointCast"); // style
487
488 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
489 for (const Scope * scope : symbolDatabase->functionScopes) {
490 const Token* tok = scope->bodyStart;
491 if (scope->function && scope->function->isConstructor())
492 tok = scope->classDef;
493 for (; tok && tok != scope->bodyEnd; tok = tok->next()) {
494
495 if (!tok->isCast())
496 continue;
497
498 const ValueType* vt = tok->valueType();
499 if (!vt || vt->pointer || vt->reference != Reference::None || (vt->type != ValueType::FLOAT && vt->type != ValueType::DOUBLE))
500 continue;
501
502 using VTT = std::vector<ValueType::Type>;
503 const VTT sourceTypes = vt->type == ValueType::FLOAT ? VTT{ ValueType::DOUBLE, ValueType::LONGDOUBLE } : VTT{ ValueType::LONGDOUBLE };
504
505 const Token* source = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1();
506 if (!source || !source->valueType() || std::find(sourceTypes.begin(), sourceTypes.end(), source->valueType()->type) == sourceTypes.end())
507 continue;
508
509 const Token* parent = tok->astParent();
510 if (!parent)
511 continue;
512
513 const ValueType* parentVt = parent->valueType();
514 if (!parentVt || parent->str() == "(") {
515 int argn{};
516 if (const Token* ftok = getTokenArgumentFunction(tok, argn)) {
517 if (ftok->function()) {
518 if (const Variable* argVar = ftok->function()->getArgumentVar(argn))
519 parentVt = argVar->valueType();
520 }
521 }
522 }
523 if (!parentVt || std::find(sourceTypes.begin(), sourceTypes.end(), parentVt->type) == sourceTypes.end())
524 continue;
525
526 suspiciousFloatingPointCastError(tok);
527 }
528 }
529}
530
531void CheckOtherImpl::suspiciousFloatingPointCastError(const Token* tok)
532{

Callers 1

runChecksMethod · 0.45

Calls 13

getTokenArgumentFunctionFunction · 0.85
isPremiumEnabledMethod · 0.80
nextMethod · 0.80
isCastMethod · 0.80
astOperand2Method · 0.80
astOperand1Method · 0.80
astParentMethod · 0.80
getArgumentVarMethod · 0.80
isEnabledMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected