@@ -1206,55 +1206,6 @@ void CheckOther::commaSeparatedReturnError(const Token *tok)
12061206 " macro is then used in a return statement, it is less likely such code is misunderstood." , CWE398, Certainty::normal);
12071207}
12081208
1209- // ---------------------------------------------------------------------------
1210- // Check for function parameters that should be passed by const reference
1211- // ---------------------------------------------------------------------------
1212- static int estimateSize (const Type* type, const Settings* settings, const SymbolDatabase* symbolDatabase, int recursionDepth = 0 )
1213- {
1214- if (recursionDepth > 20 )
1215- return 0 ;
1216-
1217- int cumulatedSize = 0 ;
1218- const bool isUnion = type->classScope ->type == Scope::ScopeType::eUnion;
1219- const auto accumulateSize = [](int & cumulatedSize, int size, bool isUnion) -> void {
1220- if (isUnion)
1221- cumulatedSize = std::max (cumulatedSize, size);
1222- else
1223- cumulatedSize += size;
1224- };
1225- std::set<const Scope*> anonScopes;
1226- for (const Variable& var : type->classScope ->varlist ) {
1227- int size = 0 ;
1228- if (var.isStatic ())
1229- continue ;
1230- if (var.isPointer () || var.isReference ())
1231- size = settings->platform .sizeof_pointer ;
1232- else if (var.type () && var.type ()->classScope )
1233- size = estimateSize (var.type (), settings, symbolDatabase, recursionDepth + 1 );
1234- else if (var.valueType () && var.valueType ()->type == ValueType::Type::CONTAINER)
1235- size = 3 * settings->platform .sizeof_pointer ; // Just guess
1236- else if (var.nameToken ()->scope () != type->classScope && var.nameToken ()->scope ()->definedType ) { // anonymous union
1237- const auto ret = anonScopes.insert (var.nameToken ()->scope ());
1238- if (ret.second )
1239- size = estimateSize (var.nameToken ()->scope ()->definedType , settings, symbolDatabase, recursionDepth + 1 );
1240- }
1241- else
1242- size = symbolDatabase->sizeOfType (var.typeStartToken ());
1243-
1244- if (var.isArray ())
1245- size *= std::accumulate (var.dimensions ().cbegin (), var.dimensions ().cend (), 1 , [](int v, const Dimension& d) {
1246- return v *= d.num ;
1247- });
1248-
1249- accumulateSize (cumulatedSize, size, isUnion);
1250- }
1251- return std::accumulate (type->derivedFrom .cbegin (), type->derivedFrom .cend (), cumulatedSize, [&](int v, const Type::BaseInfo& baseInfo) {
1252- if (baseInfo.type && baseInfo.type ->classScope )
1253- v += estimateSize (baseInfo.type , settings, symbolDatabase, recursionDepth + 1 );
1254- return v;
1255- });
1256- }
1257-
12581209void CheckOther::checkPassByReference ()
12591210{
12601211 if (!mSettings ->severity .isEnabled (Severity::performance) || mTokenizer ->isC ())
@@ -1288,7 +1239,7 @@ void CheckOther::checkPassByReference()
12881239 // Ensure that it is a large object.
12891240 if (!var->type ()->classScope )
12901241 inconclusive = true ;
1291- else if (estimateSize ( var->type (), mSettings , symbolDatabase ) <= 2 * mSettings ->platform .sizeof_pointer )
1242+ else if (!var-> valueType () || ValueFlow::getSizeOf (* var->valueType (), * mSettings ) <= 2 * mSettings ->platform .sizeof_pointer )
12921243 continue ;
12931244 }
12941245 else
@@ -2928,7 +2879,7 @@ void CheckOther::checkRedundantCopy()
29282879 const Token* varTok = fScope ->bodyEnd ->tokAt (-2 );
29292880 if (varTok->variable () && !varTok->variable ()->isGlobal () &&
29302881 (!varTok->variable ()->type () || !varTok->variable ()->type ()->classScope ||
2931- estimateSize (varTok->variable ()->type (), mSettings , symbolDatabase ) > 2 * mSettings ->platform .sizeof_pointer ))
2882+ (varTok->variable ()->valueType () && ValueFlow::getSizeOf (*varTok-> variable ()-> valueType (), * mSettings ) > 2 * mSettings ->platform .sizeof_pointer ) ))
29322883 redundantCopyError (startTok, startTok->str ());
29332884 }
29342885 }
@@ -3041,8 +2992,8 @@ void CheckOther::checkIncompleteArrayFill()
30412992 int size = mTokenizer ->sizeOfType (var->typeStartToken ());
30422993 if (size == 0 && var->valueType ()->pointer )
30432994 size = mSettings ->platform .sizeof_pointer ;
3044- else if (size == 0 && var->type ())
3045- size = estimateSize ( var->type (), mSettings , symbolDatabase );
2995+ else if (size == 0 && var->valueType ())
2996+ size = ValueFlow::getSizeOf (* var->valueType (), * mSettings );
30462997 const Token* tok3 = tok->next ()->astOperand2 ()->astOperand1 ()->astOperand1 ();
30472998 if ((size != 1 && size != 100 && size != 0 ) || var->isPointer ()) {
30482999 if (printWarning)
0 commit comments