changed `bool_to_string()` to return `const char*` instead and use it… · cppchecksolutions/cppcheck@639a413 · GitHub
Skip to content

Commit 639a413

Browse files
authored
changed bool_to_string() to return const char* instead and use it in more cases (cppcheck-opensource#5385)
1 parent bbe45ff commit 639a413

12 files changed

Lines changed: 37 additions & 31 deletions

‎gui/cppchecklibrarydata.cpp‎

Lines changed: 4 additions & 2 deletions

‎gui/librarydialog.cpp‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "libraryaddfunctiondialog.h"
2323
#include "libraryeditargdialog.h"
2424
#include "path.h"
25+
#include "utils.h"
2526

2627
#include "ui_librarydialog.h"
2728

@@ -343,11 +344,11 @@ QString LibraryDialog::getArgText(const CppcheckLibraryData::Function::Arg &arg)
343344
if (arg.nr != CppcheckLibraryData::Function::Arg::ANY)
344345
s += QString::number(arg.nr);
345346

346-
s += "\n not bool: " + QString(arg.notbool ? "true" : "false");
347-
s += "\n not null: " + QString(arg.notnull ? "true" : "false");
348-
s += "\n not uninit: " + QString(arg.notuninit ? "true" : "false");
349-
s += "\n format string: " + QString(arg.formatstr ? "true" : "false");
350-
s += "\n strz: " + QString(arg.strz ? "true" : "false");
347+
s += "\n not bool: " + QString(bool_to_string(arg.notbool));
348+
s += "\n not null: " + QString(bool_to_string(arg.notnull));
349+
s += "\n not uninit: " + QString(bool_to_string(arg.notuninit));
350+
s += "\n format string: " + QString(bool_to_string(arg.formatstr));
351+
s += "\n strz: " + QString(bool_to_string(arg.strz));
351352
s += "\n valid: " + QString(arg.valid.isEmpty() ? "any" : arg.valid);
352353
for (const CppcheckLibraryData::Function::Arg::MinSize &minsize : arg.minsizes) {
353354
s += "\n minsize: " + minsize.type + " " + minsize.arg + " " + minsize.arg2;

‎gui/projectfile.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "config.h"
2323
#include "importproject.h"
2424
#include "settings.h"
25+
#include "utils.h"
2526

2627
#include <utility>
2728

@@ -847,7 +848,7 @@ bool ProjectFile::write(const QString &filename)
847848
}
848849

849850
xmlWriter.writeStartElement(CppcheckXml::AnalyzeAllVsConfigsElementName);
850-
xmlWriter.writeCharacters(mAnalyzeAllVsConfigs ? "true" : "false");
851+
xmlWriter.writeCharacters(bool_to_string(mAnalyzeAllVsConfigs));
851852
xmlWriter.writeEndElement();
852853

853854
if (clangParser) {
@@ -857,11 +858,11 @@ bool ProjectFile::write(const QString &filename)
857858
}
858859

859860
xmlWriter.writeStartElement(CppcheckXml::CheckHeadersElementName);
860-
xmlWriter.writeCharacters(mCheckHeaders ? "true" : "false");
861+
xmlWriter.writeCharacters(bool_to_string(mCheckHeaders));
861862
xmlWriter.writeEndElement();
862863

863864
xmlWriter.writeStartElement(CppcheckXml::CheckUnusedTemplatesElementName);
864-
xmlWriter.writeCharacters(mCheckUnusedTemplates ? "true" : "false");
865+
xmlWriter.writeCharacters(bool_to_string(mCheckUnusedTemplates));
865866
xmlWriter.writeEndElement();
866867

867868
xmlWriter.writeStartElement(CppcheckXml::MaxCtuDepthElementName);

‎gui/test/cppchecklibrarydata/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ add_executable(test-cppchecklibrarydata
88
testcppchecklibrarydata.cpp
99
${CMAKE_SOURCE_DIR}/gui/cppchecklibrarydata.cpp
1010
)
11-
target_include_directories(test-cppchecklibrarydata PRIVATE ${CMAKE_SOURCE_DIR}/gui)
11+
target_include_directories(test-cppchecklibrarydata PRIVATE ${CMAKE_SOURCE_DIR}/lib ${CMAKE_SOURCE_DIR}/gui)
1212
target_compile_definitions(test-cppchecklibrarydata PRIVATE SRCDIR="${CMAKE_CURRENT_SOURCE_DIR}")
1313
target_link_libraries(test-cppchecklibrarydata ${QT_CORE_LIB} ${QT_TEST_LIB})
1414

‎lib/checkcondition.cpp‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "symboldatabase.h"
3030
#include "token.h"
3131
#include "tokenize.h"
32+
#include "utils.h"
3233
#include "vfvalue.h"
3334

3435
#include "checkother.h" // comparisonNonZeroExpressionLessThanZero and testIfNonZeroExpressionIsPositive
@@ -243,7 +244,7 @@ void CheckCondition::assignIfError(const Token *tok1, const Token *tok2, const s
243244
reportError(locations,
244245
Severity::style,
245246
"assignIfError",
246-
"Mismatching assignment and comparison, comparison '" + condition + "' is always " + std::string(result ? "true" : "false") + ".", CWE398, Certainty::normal);
247+
"Mismatching assignment and comparison, comparison '" + condition + "' is always " + std::string(bool_to_string(result)) + ".", CWE398, Certainty::normal);
247248
}
248249

249250

@@ -421,8 +422,8 @@ void CheckCondition::comparisonError(const Token *tok, const std::string &bitop,
421422
std::ostringstream expression;
422423
expression << std::hex << "(X " << bitop << " 0x" << value1 << ") " << op << " 0x" << value2;
423424

424-
const std::string errmsg("Expression '" + expression.str() + "' is always " + (result?"true":"false") + ".\n"
425-
"The expression '" + expression.str() + "' is always " + (result?"true":"false") +
425+
const std::string errmsg("Expression '" + expression.str() + "' is always " + bool_to_string(result) + ".\n"
426+
"The expression '" + expression.str() + "' is always " + bool_to_string(result) +
426427
". Check carefully constants and operators used, these errors might be hard to "
427428
"spot sometimes. In case of complex expression it might help to split it to "
428429
"separate expressions.");
@@ -1620,7 +1621,7 @@ void CheckCondition::alwaysTrueFalseError(const Token* tok, const Token* conditi
16201621
const bool alwaysTrue = value && (value->intvalue != 0 || value->isImpossible());
16211622
const std::string expr = tok ? tok->expressionString() : std::string("x");
16221623
const std::string conditionStr = (Token::simpleMatch(condition, "return") ? "Return value" : "Condition");
1623-
const std::string errmsg = conditionStr + " '" + expr + "' is always " + (alwaysTrue ? "true" : "false");
1624+
const std::string errmsg = conditionStr + " '" + expr + "' is always " + bool_to_string(alwaysTrue);
16241625
const ErrorPath errorPath = getErrorPath(tok, value, errmsg);
16251626
reportError(errorPath,
16261627
Severity::style,
@@ -1694,7 +1695,7 @@ void CheckCondition::checkInvalidTestForOverflow()
16941695
result = (cmp == ">" || cmp == ">=");
16951696
else
16961697
result = (cmp == "<" || cmp == "<=");
1697-
invalidTestForOverflow(tok, lhs->valueType(), result ? "true" : "false");
1698+
invalidTestForOverflow(tok, lhs->valueType(), bool_to_string(result));
16981699
continue;
16991700
}
17001701

@@ -2031,7 +2032,7 @@ void CheckCondition::compareValueOutOfTypeRangeError(const Token *comparison, co
20312032
comparison,
20322033
Severity::style,
20332034
"compareValueOutOfTypeRangeError",
2034-
"Comparing expression of type '" + type + "' against value " + std::to_string(value) + ". Condition is always " + (result ? "true" : "false") + ".",
2035+
"Comparing expression of type '" + type + "' against value " + std::to_string(value) + ". Condition is always " + bool_to_string(result) + ".",
20352036
CWE398,
20362037
Certainty::normal);
20372038
}

‎lib/checkother.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2785,7 +2785,7 @@ void CheckOther::checkComparisonFunctionIsAlwaysTrueOrFalse()
27852785
}
27862786
void CheckOther::checkComparisonFunctionIsAlwaysTrueOrFalseError(const Token* tok, const std::string &functionName, const std::string &varName, const bool result)
27872787
{
2788-
const std::string strResult = result ? "true" : "false";
2788+
const std::string strResult = bool_to_string(result);
27892789
const struct CWE cweResult = result ? CWE571 : CWE570;
27902790

27912791
reportError(tok, Severity::warning, "comparisonFunctionIsAlwaysTrueOrFalse",
@@ -3860,7 +3860,7 @@ void CheckOther::knownPointerToBoolError(const Token* tok, const ValueFlow::Valu
38603860
reportError(tok, Severity::style, "knownPointerToBool", "Pointer expression 'p' converted to bool is always true.");
38613861
return;
38623862
}
3863-
std::string cond = value->intvalue ? "true" : "false";
3863+
std::string cond = bool_to_string(value->intvalue);
38643864
const std::string& expr = tok->expressionString();
38653865
std::string errmsg = "Pointer expression '" + expr + "' converted to bool is always " + cond + ".";
38663866
const ErrorPath errorPath = getErrorPath(tok, value, errmsg);

‎lib/checkstring.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ void CheckString::incorrectStringBooleanError(const Token *tok, const std::strin
328328
{
329329
const bool charLiteral = isCharLiteral(string);
330330
const std::string literalType = charLiteral ? "char" : "string";
331-
const std::string result = getCharLiteral(string) == "\\0" ? "false" : "true";
331+
const std::string result = bool_to_string(getCharLiteral(string) != "\\0");
332332
reportError(tok,
333333
Severity::warning,
334334
charLiteral ? "incorrectCharBooleanError" : "incorrectStringBooleanError",

‎lib/preprocessor.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ void Preprocessor::dump(std::ostream &out) const
854854
<< " usefile=\"" << ErrorLogger::toxml(macroUsage.useLocation.file()) << "\""
855855
<< " useline=\"" << macroUsage.useLocation.line << "\""
856856
<< " usecolumn=\"" << macroUsage.useLocation.col << "\""
857-
<< " is-known-value=\"" << (macroUsage.macroValueKnown ? "true" : "false") << "\""
857+
<< " is-known-value=\"" << bool_to_string(macroUsage.macroValueKnown) << "\""
858858
<< "/>" << std::endl;
859859
}
860860
out << " </macro-usage>" << std::endl;

‎lib/templatesimplifier.cpp‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "token.h"
2727
#include "tokenize.h"
2828
#include "tokenlist.h"
29+
#include "utils.h"
2930

3031
#include <algorithm>
3132
#include <cassert>
@@ -2513,17 +2514,17 @@ void TemplateSimplifier::simplifyTemplateArgs(Token *start, const Token *end, st
25132514
std::string result;
25142515

25152516
if (cmp == "==")
2516-
result = (op1 == op2) ? "true" : "false";
2517+
result = bool_to_string(op1 == op2);
25172518
else if (cmp == "!=")
2518-
result = (op1 != op2) ? "true" : "false";
2519+
result = bool_to_string(op1 != op2);
25192520
else if (cmp == "<=")
2520-
result = (op1 <= op2) ? "true" : "false";
2521+
result = bool_to_string(op1 <= op2);
25212522
else if (cmp == ">=")
2522-
result = (op1 >= op2) ? "true" : "false";
2523+
result = bool_to_string(op1 >= op2);
25232524
else if (cmp == "<")
2524-
result = (op1 < op2) ? "true" : "false";
2525+
result = bool_to_string(op1 < op2);
25252526
else
2526-
result = (op1 > op2) ? "true" : "false";
2527+
result = bool_to_string(op1 > op2);
25272528

25282529
tok->str(result);
25292530
tok->deleteNext(2);

‎lib/tokenize.cpp‎

Lines changed: 2 additions & 2 deletions

0 commit comments

Comments
 (0)