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

Method checkLongCast

lib/checktype.cpp:339–408  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

337}
338
339void CheckTypeImpl::checkLongCast()
340{
341 if (!mSettings.severity.isEnabled(Severity::style) && !mSettings.isPremiumEnabled("truncLongCastAssignment"))
342 return;
343
344 logChecker("CheckType::checkLongCast"); // style
345
346 // Assignments..
347 for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
348 if (tok->str() != "=" || !Token::Match(tok->astOperand2(), "*|<<") || tok->astOperand2()->isUnaryOp("*"))
349 continue;
350
351 if (const ValueFlow::Value* v = tok->astOperand2()->getKnownValue(ValueFlow::Value::ValueType::INT)) {
352 if (mSettings.platform.isIntValue(v->intvalue))
353 continue;
354 }
355
356 const ValueType *lhstype = tok->astOperand1() ? tok->astOperand1()->valueType() : nullptr;
357 const ValueType *rhstype = tok->astOperand2()->valueType();
358
359 if (!lhstype || !rhstype)
360 continue;
361 if (!checkTypeCombination(*rhstype, *lhstype, mSettings))
362 continue;
363
364 // assign int result to long/longlong const nonpointer?
365 if (rhstype->pointer == 0U &&
366 rhstype->originalTypeName.empty() &&
367 lhstype->pointer == 0U &&
368 lhstype->originalTypeName.empty())
369 longCastAssignError(tok, rhstype, lhstype);
370 }
371
372 // Return..
373 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
374 for (const Scope * scope : symbolDatabase->functionScopes) {
375
376 // function must return long data
377 const Token * def = scope->classDef;
378 if (!Token::Match(def, "%name% (") || !def->next()->valueType())
379 continue;
380 const ValueType* retVt = def->next()->valueType();
381
382 // return statements
383 const Token *ret = nullptr;
384 for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
385 if (tok->str() == "return") {
386 if (Token::Match(tok->astOperand1(), "<<|*")) {
387 const ValueType *type = tok->astOperand1()->valueType();
388 if (type && checkTypeCombination(*type, *retVt, mSettings) &&
389 type->pointer == 0U &&
390 type->originalTypeName.empty()) {
391 if (!tok->astOperand1()->hasKnownIntValue()) {
392 ret = tok;
393 } else if (!mSettings.platform.isIntValue(tok->astOperand1()->getKnownIntValue()))
394 ret = tok;
395 }
396 }

Callers 1

runChecksMethod · 0.80

Calls 12

checkTypeCombinationFunction · 0.85
isPremiumEnabledMethod · 0.80
nextMethod · 0.80
astOperand2Method · 0.80
isUnaryOpMethod · 0.80
getKnownValueMethod · 0.80
astOperand1Method · 0.80
getKnownIntValueMethod · 0.80
isEnabledMethod · 0.45
strMethod · 0.45
emptyMethod · 0.45
hasKnownIntValueMethod · 0.45

Tested by

no test coverage detected