diff --git a/gui/cppchecklibrarydata.cpp b/gui/cppchecklibrarydata.cpp index 83fe76622..23291e5da 100644 --- a/gui/cppchecklibrarydata.cpp +++ b/gui/cppchecklibrarydata.cpp @@ -18,6 +18,8 @@ #include "cppchecklibrarydata.h" +#include "utils.h" + #include #include @@ -615,7 +617,7 @@ static void writeFunction(QXmlStreamWriter &xmlWriter, const CppcheckLibraryData xmlWriter.writeEndElement(); } if (function.noreturn != CppcheckLibraryData::Function::Unknown) - xmlWriter.writeTextElement("noreturn", (function.noreturn == CppcheckLibraryData::Function::True) ? "true" : "false"); + xmlWriter.writeTextElement("noreturn", bool_to_string(function.noreturn == CppcheckLibraryData::Function::True)); if (function.leakignore) xmlWriter.writeEmptyElement("leak-ignore"); // Argument info.. @@ -719,7 +721,7 @@ static void writeMemoryResource(QXmlStreamWriter &xmlWriter, const CppcheckLibra } else { xmlWriter.writeStartElement("alloc"); } - xmlWriter.writeAttribute("init", alloc.init ? "true" : "false"); + xmlWriter.writeAttribute("init", bool_to_string(alloc.init)); if (alloc.arg != -1) { xmlWriter.writeAttribute("arg", QString("%1").arg(alloc.arg)); } diff --git a/gui/librarydialog.cpp b/gui/librarydialog.cpp index 475456799..506cb6bc1 100644 --- a/gui/librarydialog.cpp +++ b/gui/librarydialog.cpp @@ -22,6 +22,7 @@ #include "libraryaddfunctiondialog.h" #include "libraryeditargdialog.h" #include "path.h" +#include "utils.h" #include "ui_librarydialog.h" @@ -343,11 +344,11 @@ QString LibraryDialog::getArgText(const CppcheckLibraryData::Function::Arg &arg) if (arg.nr != CppcheckLibraryData::Function::Arg::ANY) s += QString::number(arg.nr); - s += "\n not bool: " + QString(arg.notbool ? "true" : "false"); - s += "\n not null: " + QString(arg.notnull ? "true" : "false"); - s += "\n not uninit: " + QString(arg.notuninit ? "true" : "false"); - s += "\n format string: " + QString(arg.formatstr ? "true" : "false"); - s += "\n strz: " + QString(arg.strz ? "true" : "false"); + s += "\n not bool: " + QString(bool_to_string(arg.notbool)); + s += "\n not null: " + QString(bool_to_string(arg.notnull)); + s += "\n not uninit: " + QString(bool_to_string(arg.notuninit)); + s += "\n format string: " + QString(bool_to_string(arg.formatstr)); + s += "\n strz: " + QString(bool_to_string(arg.strz)); s += "\n valid: " + QString(arg.valid.isEmpty() ? "any" : arg.valid); for (const CppcheckLibraryData::Function::Arg::MinSize &minsize : arg.minsizes) { s += "\n minsize: " + minsize.type + " " + minsize.arg + " " + minsize.arg2; diff --git a/gui/projectfile.cpp b/gui/projectfile.cpp index 349c91ebe..49f7157f8 100644 --- a/gui/projectfile.cpp +++ b/gui/projectfile.cpp @@ -22,6 +22,7 @@ #include "config.h" #include "importproject.h" #include "settings.h" +#include "utils.h" #include @@ -847,7 +848,7 @@ bool ProjectFile::write(const QString &filename) } xmlWriter.writeStartElement(CppcheckXml::AnalyzeAllVsConfigsElementName); - xmlWriter.writeCharacters(mAnalyzeAllVsConfigs ? "true" : "false"); + xmlWriter.writeCharacters(bool_to_string(mAnalyzeAllVsConfigs)); xmlWriter.writeEndElement(); if (clangParser) { @@ -857,11 +858,11 @@ bool ProjectFile::write(const QString &filename) } xmlWriter.writeStartElement(CppcheckXml::CheckHeadersElementName); - xmlWriter.writeCharacters(mCheckHeaders ? "true" : "false"); + xmlWriter.writeCharacters(bool_to_string(mCheckHeaders)); xmlWriter.writeEndElement(); xmlWriter.writeStartElement(CppcheckXml::CheckUnusedTemplatesElementName); - xmlWriter.writeCharacters(mCheckUnusedTemplates ? "true" : "false"); + xmlWriter.writeCharacters(bool_to_string(mCheckUnusedTemplates)); xmlWriter.writeEndElement(); xmlWriter.writeStartElement(CppcheckXml::MaxCtuDepthElementName); diff --git a/gui/test/cppchecklibrarydata/CMakeLists.txt b/gui/test/cppchecklibrarydata/CMakeLists.txt index cf29fafdf..94e661195 100644 --- a/gui/test/cppchecklibrarydata/CMakeLists.txt +++ b/gui/test/cppchecklibrarydata/CMakeLists.txt @@ -8,7 +8,7 @@ add_executable(test-cppchecklibrarydata testcppchecklibrarydata.cpp ${CMAKE_SOURCE_DIR}/gui/cppchecklibrarydata.cpp ) -target_include_directories(test-cppchecklibrarydata PRIVATE ${CMAKE_SOURCE_DIR}/gui) +target_include_directories(test-cppchecklibrarydata PRIVATE ${CMAKE_SOURCE_DIR}/lib ${CMAKE_SOURCE_DIR}/gui) target_compile_definitions(test-cppchecklibrarydata PRIVATE SRCDIR="${CMAKE_CURRENT_SOURCE_DIR}") target_link_libraries(test-cppchecklibrarydata ${QT_CORE_LIB} ${QT_TEST_LIB}) diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index fa7700707..d0e2e6d92 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -29,6 +29,7 @@ #include "symboldatabase.h" #include "token.h" #include "tokenize.h" +#include "utils.h" #include "vfvalue.h" #include "checkother.h" // comparisonNonZeroExpressionLessThanZero and testIfNonZeroExpressionIsPositive @@ -243,7 +244,7 @@ void CheckCondition::assignIfError(const Token *tok1, const Token *tok2, const s reportError(locations, Severity::style, "assignIfError", - "Mismatching assignment and comparison, comparison '" + condition + "' is always " + std::string(result ? "true" : "false") + ".", CWE398, Certainty::normal); + "Mismatching assignment and comparison, comparison '" + condition + "' is always " + std::string(bool_to_string(result)) + ".", CWE398, Certainty::normal); } @@ -421,8 +422,8 @@ void CheckCondition::comparisonError(const Token *tok, const std::string &bitop, std::ostringstream expression; expression << std::hex << "(X " << bitop << " 0x" << value1 << ") " << op << " 0x" << value2; - const std::string errmsg("Expression '" + expression.str() + "' is always " + (result?"true":"false") + ".\n" - "The expression '" + expression.str() + "' is always " + (result?"true":"false") + + const std::string errmsg("Expression '" + expression.str() + "' is always " + bool_to_string(result) + ".\n" + "The expression '" + expression.str() + "' is always " + bool_to_string(result) + ". Check carefully constants and operators used, these errors might be hard to " "spot sometimes. In case of complex expression it might help to split it to " "separate expressions."); @@ -1620,7 +1621,7 @@ void CheckCondition::alwaysTrueFalseError(const Token* tok, const Token* conditi const bool alwaysTrue = value && (value->intvalue != 0 || value->isImpossible()); const std::string expr = tok ? tok->expressionString() : std::string("x"); const std::string conditionStr = (Token::simpleMatch(condition, "return") ? "Return value" : "Condition"); - const std::string errmsg = conditionStr + " '" + expr + "' is always " + (alwaysTrue ? "true" : "false"); + const std::string errmsg = conditionStr + " '" + expr + "' is always " + bool_to_string(alwaysTrue); const ErrorPath errorPath = getErrorPath(tok, value, errmsg); reportError(errorPath, Severity::style, @@ -1694,7 +1695,7 @@ void CheckCondition::checkInvalidTestForOverflow() result = (cmp == ">" || cmp == ">="); else result = (cmp == "<" || cmp == "<="); - invalidTestForOverflow(tok, lhs->valueType(), result ? "true" : "false"); + invalidTestForOverflow(tok, lhs->valueType(), bool_to_string(result)); continue; } @@ -2031,7 +2032,7 @@ void CheckCondition::compareValueOutOfTypeRangeError(const Token *comparison, co comparison, Severity::style, "compareValueOutOfTypeRangeError", - "Comparing expression of type '" + type + "' against value " + std::to_string(value) + ". Condition is always " + (result ? "true" : "false") + ".", + "Comparing expression of type '" + type + "' against value " + std::to_string(value) + ". Condition is always " + bool_to_string(result) + ".", CWE398, Certainty::normal); } diff --git a/lib/checkother.cpp b/lib/checkother.cpp index fef42ec09..f6f47c371 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2785,7 +2785,7 @@ void CheckOther::checkComparisonFunctionIsAlwaysTrueOrFalse() } void CheckOther::checkComparisonFunctionIsAlwaysTrueOrFalseError(const Token* tok, const std::string &functionName, const std::string &varName, const bool result) { - const std::string strResult = result ? "true" : "false"; + const std::string strResult = bool_to_string(result); const struct CWE cweResult = result ? CWE571 : CWE570; reportError(tok, Severity::warning, "comparisonFunctionIsAlwaysTrueOrFalse", @@ -3860,7 +3860,7 @@ void CheckOther::knownPointerToBoolError(const Token* tok, const ValueFlow::Valu reportError(tok, Severity::style, "knownPointerToBool", "Pointer expression 'p' converted to bool is always true."); return; } - std::string cond = value->intvalue ? "true" : "false"; + std::string cond = bool_to_string(value->intvalue); const std::string& expr = tok->expressionString(); std::string errmsg = "Pointer expression '" + expr + "' converted to bool is always " + cond + "."; const ErrorPath errorPath = getErrorPath(tok, value, errmsg); diff --git a/lib/checkstring.cpp b/lib/checkstring.cpp index 6b230b1d9..f4b324fcf 100644 --- a/lib/checkstring.cpp +++ b/lib/checkstring.cpp @@ -328,7 +328,7 @@ void CheckString::incorrectStringBooleanError(const Token *tok, const std::strin { const bool charLiteral = isCharLiteral(string); const std::string literalType = charLiteral ? "char" : "string"; - const std::string result = getCharLiteral(string) == "\\0" ? "false" : "true"; + const std::string result = bool_to_string(getCharLiteral(string) != "\\0"); reportError(tok, Severity::warning, charLiteral ? "incorrectCharBooleanError" : "incorrectStringBooleanError", diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index aa572cb5c..f2db1c8d0 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -854,7 +854,7 @@ void Preprocessor::dump(std::ostream &out) const << " usefile=\"" << ErrorLogger::toxml(macroUsage.useLocation.file()) << "\"" << " useline=\"" << macroUsage.useLocation.line << "\"" << " usecolumn=\"" << macroUsage.useLocation.col << "\"" - << " is-known-value=\"" << (macroUsage.macroValueKnown ? "true" : "false") << "\"" + << " is-known-value=\"" << bool_to_string(macroUsage.macroValueKnown) << "\"" << "/>" << std::endl; } out << " " << std::endl; diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 21a1489e2..d2a068d8f 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -26,6 +26,7 @@ #include "token.h" #include "tokenize.h" #include "tokenlist.h" +#include "utils.h" #include #include @@ -2513,17 +2514,17 @@ void TemplateSimplifier::simplifyTemplateArgs(Token *start, const Token *end, st std::string result; if (cmp == "==") - result = (op1 == op2) ? "true" : "false"; + result = bool_to_string(op1 == op2); else if (cmp == "!=") - result = (op1 != op2) ? "true" : "false"; + result = bool_to_string(op1 != op2); else if (cmp == "<=") - result = (op1 <= op2) ? "true" : "false"; + result = bool_to_string(op1 <= op2); else if (cmp == ">=") - result = (op1 >= op2) ? "true" : "false"; + result = bool_to_string(op1 >= op2); else if (cmp == "<") - result = (op1 < op2) ? "true" : "false"; + result = bool_to_string(op1 < op2); else - result = (op1 > op2) ? "true" : "false"; + result = bool_to_string(op1 > op2); tok->str(result); tok->deleteNext(2); diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index aaa7d599a..3e44a1cfd 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5965,10 +5965,10 @@ void Tokenizer::dump(std::ostream &out) const outs += " arrayLike_indexOp ? "true" : "false"); + outs += bool_to_string(c->arrayLike_indexOp); outs += "\" "; outs += "std-string-like=\""; - outs +=(c->stdStringLike ? "true" : "false"); + outs += bool_to_string(c->stdStringLike); outs += "\"/>"; outs += '\n'; } diff --git a/lib/utils.h b/lib/utils.h index 690a47c10..d49af4994 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -325,7 +325,7 @@ static inline std::string id_string(const void* p) return id_string_i(reinterpret_cast(p)); } -static inline std::string bool_to_string(bool b) +static inline const char* bool_to_string(bool b) { return b ? "true" : "false"; } diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index a6e5311dc..2ef1b5af8 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2953,7 +2953,7 @@ struct ValueFlowAnalyzer : Analyzer { std::string s = state ? "empty" : "not empty"; addErrorPath(tok, "Assuming container is " + s); } else { - std::string s = state ? "true" : "false"; + std::string s = bool_to_string(state); addErrorPath(tok, "Assuming condition is " + s); } }