Skip to content

Commit 3cf9100

Browse files
firewaveIOBYTE
andauthored
fixed #6316 (Use std::to_string() in place of MathLib::toString() ...) - deleted default implementation of Mathlib::toString() (#5341)
It was also used inconsistently and seemed to imply there is some special handling which wasn't the case. It was just an alias for `std::to_string()` for non-`double` types. So there was no need for it. --------- Co-authored-by: Robert Reif <reif@earthlink.net>
1 parent d8b44df commit 3cf9100

22 files changed

Lines changed: 81 additions & 94 deletions

lib/astutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ static bool match(const Token *tok, const std::string &rhs)
341341
{
342342
if (tok->str() == rhs)
343343
return true;
344-
if (!tok->varId() && tok->hasKnownIntValue() && MathLib::toString(tok->values().front().intvalue) == rhs)
344+
if (!tok->varId() && tok->hasKnownIntValue() && std::to_string(tok->values().front().intvalue) == rhs)
345345
return true;
346346
return false;
347347
}

lib/checkbufferoverrun.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static int getMinFormatStringOutputLength(const std::vector<const Token*> &param
119119
i_d_x_f_found = true;
120120
parameterLength = 1;
121121
if (inputArgNr < parameters.size() && parameters[inputArgNr]->hasKnownIntValue())
122-
parameterLength = MathLib::toString(parameters[inputArgNr]->getKnownIntValue()).length();
122+
parameterLength = std::to_string(parameters[inputArgNr]->getKnownIntValue()).length();
123123

124124
handleNextParameter = true;
125125
break;
@@ -362,7 +362,7 @@ void CheckBufferOverrun::arrayIndex()
362362
static std::string stringifyIndexes(const std::string& array, const std::vector<ValueFlow::Value>& indexValues)
363363
{
364364
if (indexValues.size() == 1)
365-
return MathLib::toString(indexValues[0].intvalue);
365+
return std::to_string(indexValues[0].intvalue);
366366

367367
std::ostringstream ret;
368368
ret << array;
@@ -383,7 +383,7 @@ static std::string arrayIndexMessage(const Token* tok,
383383
const Token* condition)
384384
{
385385
auto add_dim = [](const std::string &s, const Dimension &dim) {
386-
return s + "[" + MathLib::toString(dim.num) + "]";
386+
return s + "[" + std::to_string(dim.num) + "]";
387387
};
388388
const std::string array = std::accumulate(dimensions.cbegin(), dimensions.cend(), tok->astOperand1()->expressionString(), add_dim);
389389

@@ -528,7 +528,7 @@ void CheckBufferOverrun::pointerArithmeticError(const Token *tok, const Token *i
528528

529529
std::string errmsg;
530530
if (indexValue->condition)
531-
errmsg = "Undefined behaviour, when '" + indexToken->expressionString() + "' is " + MathLib::toString(indexValue->intvalue) + " the pointer arithmetic '" + tok->expressionString() + "' is out of bounds.";
531+
errmsg = "Undefined behaviour, when '" + indexToken->expressionString() + "' is " + std::to_string(indexValue->intvalue) + " the pointer arithmetic '" + tok->expressionString() + "' is out of bounds.";
532532
else
533533
errmsg = "Undefined behaviour, pointer arithmetic '" + tok->expressionString() + "' is out of bounds.";
534534

@@ -992,13 +992,13 @@ bool CheckBufferOverrun::analyseWholeProgram1(const std::map<std::string, std::l
992992
if (type == 1) {
993993
errorId = "ctuArrayIndex";
994994
if (unsafeUsage.value > 0)
995-
errmsg = "Array index out of bounds; '" + unsafeUsage.myArgumentName + "' buffer size is " + MathLib::toString(functionCall->callArgValue) + " and it is accessed at offset " + MathLib::toString(unsafeUsage.value) + ".";
995+
errmsg = "Array index out of bounds; '" + unsafeUsage.myArgumentName + "' buffer size is " + std::to_string(functionCall->callArgValue) + " and it is accessed at offset " + std::to_string(unsafeUsage.value) + ".";
996996
else
997-
errmsg = "Array index out of bounds; buffer '" + unsafeUsage.myArgumentName + "' is accessed at offset " + MathLib::toString(unsafeUsage.value) + ".";
997+
errmsg = "Array index out of bounds; buffer '" + unsafeUsage.myArgumentName + "' is accessed at offset " + std::to_string(unsafeUsage.value) + ".";
998998
cwe = (unsafeUsage.value > 0) ? CWE_BUFFER_OVERRUN : CWE_BUFFER_UNDERRUN;
999999
} else {
10001000
errorId = "ctuPointerArith";
1001-
errmsg = "Pointer arithmetic overflow; '" + unsafeUsage.myArgumentName + "' buffer size is " + MathLib::toString(functionCall->callArgValue);
1001+
errmsg = "Pointer arithmetic overflow; '" + unsafeUsage.myArgumentName + "' buffer size is " + std::to_string(functionCall->callArgValue);
10021002
cwe = CWE_POINTER_ARITHMETIC_OVERFLOW;
10031003
}
10041004

lib/checkclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2845,7 +2845,7 @@ void CheckClass::virtualFunctionCallInConstructorError(
28452845
}
28462846

28472847
reportError(errorPath, Severity::style, "virtualCallInConstructor",
2848-
"Virtual function '" + funcname + "' is called from " + scopeFunctionTypeName + " '" + constructorName + "' at line " + MathLib::toString(lineNumber) + ". Dynamic binding is not used.", CWE(0U), Certainty::normal);
2848+
"Virtual function '" + funcname + "' is called from " + scopeFunctionTypeName + " '" + constructorName + "' at line " + std::to_string(lineNumber) + ". Dynamic binding is not used.", CWE(0U), Certainty::normal);
28492849
}
28502850

28512851
void CheckClass::pureVirtualFunctionCallInConstructorError(

lib/checkcondition.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ static bool parseComparison(const Token *comp, bool &not1, std::string &op, std:
10391039
return false;
10401040
op = invertOperatorForOperandSwap(comp->str());
10411041
if (op1->enumerator() && op1->enumerator()->value_known)
1042-
value = MathLib::toString(op1->enumerator()->value);
1042+
value = std::to_string(op1->enumerator()->value);
10431043
else
10441044
value = op1->str();
10451045
expr = op2;
@@ -1048,7 +1048,7 @@ static bool parseComparison(const Token *comp, bool &not1, std::string &op, std:
10481048
return false;
10491049
op = comp->str();
10501050
if (op2->enumerator() && op2->enumerator()->value_known)
1051-
value = MathLib::toString(op2->enumerator()->value);
1051+
value = std::to_string(op2->enumerator()->value);
10521052
else
10531053
value = op2->str();
10541054
expr = op1;

lib/checkother.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3518,7 +3518,7 @@ void CheckOther::funcArgNamesDifferent(const std::string & functionName, nonneg
35183518
std::list<const Token *> tokens = { declaration,definition };
35193519
reportError(tokens, Severity::style, "funcArgNamesDifferent",
35203520
"$symbol:" + functionName + "\n"
3521-
"Function '$symbol' argument " + MathLib::toString(index + 1) + " names different: declaration '" +
3521+
"Function '$symbol' argument " + std::to_string(index + 1) + " names different: declaration '" +
35223522
(declaration ? declaration->str() : std::string("A")) + "' definition '" +
35233523
(definition ? definition->str() : std::string("B")) + "'.", CWE628, Certainty::inconclusive);
35243524
}

lib/checkstl.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,14 @@ void CheckStl::outOfBounds()
203203
static std::string indexValueString(const ValueFlow::Value& indexValue, const std::string& containerName = emptyString)
204204
{
205205
if (indexValue.isIteratorStartValue())
206-
return "at position " + MathLib::toString(indexValue.intvalue) + " from the beginning";
206+
return "at position " + std::to_string(indexValue.intvalue) + " from the beginning";
207207
if (indexValue.isIteratorEndValue())
208-
return "at position " + MathLib::toString(-indexValue.intvalue) + " from the end";
209-
std::string indexString = MathLib::toString(indexValue.intvalue);
208+
return "at position " + std::to_string(-indexValue.intvalue) + " from the end";
209+
std::string indexString = std::to_string(indexValue.intvalue);
210210
if (indexValue.isSymbolicValue()) {
211211
indexString = containerName + ".size()";
212212
if (indexValue.intvalue != 0)
213-
indexString += "+" + MathLib::toString(indexValue.intvalue);
213+
indexString += "+" + std::to_string(indexValue.intvalue);
214214
}
215215
if (indexValue.bound == ValueFlow::Value::Bound::Lower)
216216
return "greater or equal to " + indexString;
@@ -242,11 +242,11 @@ void CheckStl::outOfBoundsError(const Token *tok, const std::string &containerNa
242242
errmsg = "Out of bounds access in expression '" + expression + "' because '$symbol' is empty.";
243243
} else if (indexValue) {
244244
if (containerSize->condition)
245-
errmsg = ValueFlow::eitherTheConditionIsRedundant(containerSize->condition) + " or $symbol size can be " + MathLib::toString(containerSize->intvalue) + ". Expression '" + expression + "' cause access out of bounds.";
245+
errmsg = ValueFlow::eitherTheConditionIsRedundant(containerSize->condition) + " or $symbol size can be " + std::to_string(containerSize->intvalue) + ". Expression '" + expression + "' cause access out of bounds.";
246246
else if (indexValue->condition)
247247
errmsg = ValueFlow::eitherTheConditionIsRedundant(indexValue->condition) + " or '" + index + "' can have the value " + indexValueString(*indexValue) + ". Expression '" + expression + "' cause access out of bounds.";
248248
else
249-
errmsg = "Out of bounds access in '" + expression + "', if '$symbol' size is " + MathLib::toString(containerSize->intvalue) + " and '" + index + "' is " + indexValueString(*indexValue);
249+
errmsg = "Out of bounds access in '" + expression + "', if '$symbol' size is " + std::to_string(containerSize->intvalue) + " and '" + index + "' is " + indexValueString(*indexValue);
250250
} else {
251251
// should not happen
252252
return;

lib/cppcheck.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
807807
const std::list<Directive> &directives = preprocessor.getDirectives();
808808
for (const Directive &dir : directives) {
809809
if (dir.str.compare(0,8,"#define ") == 0 || dir.str.compare(0,9,"#include ") == 0)
810-
code += "#line " + MathLib::toString(dir.linenr) + " \"" + dir.file + "\"\n" + dir.str + '\n';
810+
code += "#line " + std::to_string(dir.linenr) + " \"" + dir.file + "\"\n" + dir.str + '\n';
811811
}
812812
Tokenizer tokenizer2(&mSettings, this);
813813
std::istringstream istr2(code);
@@ -945,7 +945,7 @@ unsigned int CppCheck::checkFile(const std::string& filename, const std::string
945945

946946
} catch (const simplecpp::Output &o) {
947947
// #error etc during preprocessing
948-
configurationError.push_back((mCurrentConfig.empty() ? "\'\'" : mCurrentConfig) + " : [" + o.location.file() + ':' + MathLib::toString(o.location.line) + "] " + o.msg);
948+
configurationError.push_back((mCurrentConfig.empty() ? "\'\'" : mCurrentConfig) + " : [" + o.location.file() + ':' + std::to_string(o.location.line) + "] " + o.msg);
949949
--checkCount; // don't count invalid configurations
950950
continue;
951951

lib/ctu.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int CTU::maxCtuDepth = 2;
5959

6060
std::string CTU::getFunctionId(const Tokenizer *tokenizer, const Function *function)
6161
{
62-
return tokenizer->list.file(function->tokenDef) + ':' + MathLib::toString(function->tokenDef->linenr()) + ':' + MathLib::toString(function->tokenDef->column());
62+
return tokenizer->list.file(function->tokenDef) + ':' + std::to_string(function->tokenDef->linenr()) + ':' + std::to_string(function->tokenDef->column());
6363
}
6464

6565
CTU::FileInfo::Location::Location(const Tokenizer *tokenizer, const Token *tok)
@@ -579,7 +579,7 @@ std::list<ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(InvalidValueTy
579579
}
580580

581581
ErrorMessage::FileLocation fileLoc(path[index]->location.fileName, path[index]->location.lineNumber, path[index]->location.column);
582-
fileLoc.setinfo("Calling function " + path[index]->callFunctionName + ", " + MathLib::toString(path[index]->callArgNr) + getOrdinalText(path[index]->callArgNr) + " argument is " + value1);
582+
fileLoc.setinfo("Calling function " + path[index]->callFunctionName + ", " + std::to_string(path[index]->callArgNr) + getOrdinalText(path[index]->callArgNr) + " argument is " + value1);
583583
locationList.push_back(std::move(fileLoc));
584584
}
585585

lib/errorlogger.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ std::string ErrorMessage::serialize() const
240240
std::string oss;
241241
serializeString(oss, id);
242242
serializeString(oss, Severity::toString(severity));
243-
serializeString(oss, MathLib::toString(cwe.id));
244-
serializeString(oss, MathLib::toString(hash));
243+
serializeString(oss, std::to_string(cwe.id));
244+
serializeString(oss, std::to_string(hash));
245245
serializeString(oss, file0);
246246
if (certainty == Certainty::inconclusive) {
247247
const std::string text("inconclusive");
@@ -461,7 +461,7 @@ std::string ErrorMessage::toXML() const
461461
if (cwe.id)
462462
printer.PushAttribute("cwe", cwe.id);
463463
if (hash)
464-
printer.PushAttribute("hash", MathLib::toString(hash).c_str());
464+
printer.PushAttribute("hash", std::to_string(hash).c_str());
465465
if (certainty == Certainty::inconclusive)
466466
printer.PushAttribute("inconclusive", "true");
467467

@@ -624,14 +624,14 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
624624
pos1 = result.find("{inconclusive:", pos1);
625625
}
626626
findAndReplace(result, "{severity}", Severity::toString(severity));
627-
findAndReplace(result, "{cwe}", MathLib::toString(cwe.id));
627+
findAndReplace(result, "{cwe}", std::to_string(cwe.id));
628628
findAndReplace(result, "{message}", verbose ? mVerboseMessage : mShortMessage);
629629
if (!callStack.empty()) {
630630
if (result.find("{callstack}") != std::string::npos)
631631
findAndReplace(result, "{callstack}", ErrorLogger::callStackToString(callStack));
632632
findAndReplace(result, "{file}", callStack.back().getfile());
633-
findAndReplace(result, "{line}", MathLib::toString(callStack.back().line));
634-
findAndReplace(result, "{column}", MathLib::toString(callStack.back().column));
633+
findAndReplace(result, "{line}", std::to_string(callStack.back().line));
634+
findAndReplace(result, "{column}", std::to_string(callStack.back().column));
635635
if (result.find("{code}") != std::string::npos) {
636636
const std::string::size_type pos = result.find('\r');
637637
const char *endl;
@@ -660,8 +660,8 @@ std::string ErrorMessage::toString(bool verbose, const std::string &templateForm
660660
std::string text = templateLocation;
661661

662662
findAndReplace(text, "{file}", fileLocation.getfile());
663-
findAndReplace(text, "{line}", MathLib::toString(fileLocation.line));
664-
findAndReplace(text, "{column}", MathLib::toString(fileLocation.column));
663+
findAndReplace(text, "{line}", std::to_string(fileLocation.line));
664+
findAndReplace(text, "{column}", std::to_string(fileLocation.column));
665665
findAndReplace(text, "{info}", fileLocation.getinfo().empty() ? mShortMessage : fileLocation.getinfo());
666666
if (text.find("{code}") != std::string::npos) {
667667
const std::string::size_type pos = text.find('\r');

lib/mathlib.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ std::string MathLib::add(const std::string & first, const std::string & second)
10591059
return (value(first) + value(second)).str();
10601060
#else
10611061
if (MathLib::isInt(first) && MathLib::isInt(second)) {
1062-
return toString(toLongNumber(first) + toLongNumber(second)) + intsuffix(first, second);
1062+
return std::to_string(toLongNumber(first) + toLongNumber(second)) + intsuffix(first, second);
10631063
}
10641064

10651065
double d1 = toDoubleNumber(first);
@@ -1081,7 +1081,7 @@ std::string MathLib::subtract(const std::string &first, const std::string &secon
10811081
return (value(first) - value(second)).str();
10821082
#else
10831083
if (MathLib::isInt(first) && MathLib::isInt(second)) {
1084-
return toString(toLongNumber(first) - toLongNumber(second)) + intsuffix(first, second);
1084+
return std::to_string(toLongNumber(first) - toLongNumber(second)) + intsuffix(first, second);
10851085
}
10861086

10871087
if (first == second)
@@ -1112,7 +1112,7 @@ std::string MathLib::divide(const std::string &first, const std::string &second)
11121112
throw InternalError(nullptr, "Internal Error: Division by zero");
11131113
if (a == std::numeric_limits<bigint>::min() && std::abs(b)<=1)
11141114
throw InternalError(nullptr, "Internal Error: Division overflow");
1115-
return toString(toLongNumber(first) / b) + intsuffix(first, second);
1115+
return std::to_string(toLongNumber(first) / b) + intsuffix(first, second);
11161116
}
11171117
if (isNullValue(second)) {
11181118
if (isNullValue(first))
@@ -1129,7 +1129,7 @@ std::string MathLib::multiply(const std::string &first, const std::string &secon
11291129
return (value(first) * value(second)).str();
11301130
#else
11311131
if (MathLib::isInt(first) && MathLib::isInt(second)) {
1132-
return toString(toLongNumber(first) * toLongNumber(second)) + intsuffix(first, second);
1132+
return std::to_string(toLongNumber(first) * toLongNumber(second)) + intsuffix(first, second);
11331133
}
11341134
return toString(toDoubleNumber(first) * toDoubleNumber(second));
11351135
#endif
@@ -1144,7 +1144,7 @@ std::string MathLib::mod(const std::string &first, const std::string &second)
11441144
const bigint b = toLongNumber(second);
11451145
if (b == 0)
11461146
throw InternalError(nullptr, "Internal Error: Division by zero");
1147-
return toString(toLongNumber(first) % b) + intsuffix(first, second);
1147+
return std::to_string(toLongNumber(first) % b) + intsuffix(first, second);
11481148
}
11491149
return toString(std::fmod(toDoubleNumber(first),toDoubleNumber(second)));
11501150
#endif
@@ -1169,13 +1169,13 @@ std::string MathLib::calculate(const std::string &first, const std::string &seco
11691169
return MathLib::mod(first, second);
11701170

11711171
case '&':
1172-
return MathLib::toString(MathLib::toLongNumber(first) & MathLib::toLongNumber(second)) + intsuffix(first,second);
1172+
return std::to_string(MathLib::toLongNumber(first) & MathLib::toLongNumber(second)) + intsuffix(first,second);
11731173

11741174
case '|':
1175-
return MathLib::toString(MathLib::toLongNumber(first) | MathLib::toLongNumber(second)) + intsuffix(first,second);
1175+
return std::to_string(MathLib::toLongNumber(first) | MathLib::toLongNumber(second)) + intsuffix(first,second);
11761176

11771177
case '^':
1178-
return MathLib::toString(MathLib::toLongNumber(first) ^ MathLib::toLongNumber(second)) + intsuffix(first,second);
1178+
return std::to_string(MathLib::toLongNumber(first) ^ MathLib::toLongNumber(second)) + intsuffix(first,second);
11791179

11801180
default:
11811181
throw InternalError(nullptr, std::string("Unexpected action '") + action + "' in MathLib::calculate(). Please report this to Cppcheck developers.");

0 commit comments

Comments
 (0)