changed `bool_to_string()` to return `const char*` instead and use it in more cases (#5385)

This commit is contained in:
Oliver Stöneberg 2023-09-11 11:34:22 +02:00 committed by GitHub
parent bbe45ff0eb
commit 639a4131c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 37 additions and 31 deletions

View File

@ -18,6 +18,8 @@
#include "cppchecklibrarydata.h"
#include "utils.h"
#include <stdexcept>
#include <string>
@ -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));
}

View File

@ -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;

View File

@ -22,6 +22,7 @@
#include "config.h"
#include "importproject.h"
#include "settings.h"
#include "utils.h"
#include <utility>
@ -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);

View File

@ -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})

View File

@ -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);
}

View File

@ -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);

View File

@ -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",

View File

@ -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 << " </macro-usage>" << std::endl;

View File

@ -26,6 +26,7 @@
#include "token.h"
#include "tokenize.h"
#include "tokenlist.h"
#include "utils.h"
#include <algorithm>
#include <cassert>
@ -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);

View File

@ -5965,10 +5965,10 @@ void Tokenizer::dump(std::ostream &out) const
outs += " <container id=\"";
outs += id_string(c);
outs += "\" array-like-index-op=\"";
outs += (c->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';
}

View File

@ -325,7 +325,7 @@ static inline std::string id_string(const void* p)
return id_string_i(reinterpret_cast<uintptr_t>(p));
}
static inline std::string bool_to_string(bool b)
static inline const char* bool_to_string(bool b)
{
return b ? "true" : "false";
}

View File

@ -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);
}
}