Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions gui/cppchecklibrarydata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include "cppchecklibrarydata.h"

#include "utils.h"

#include <stdexcept>
#include <string>

Expand Down Expand Up @@ -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..
Expand Down Expand Up @@ -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));
}
Expand Down
11 changes: 6 additions & 5 deletions gui/librarydialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "libraryaddfunctiondialog.h"
#include "libraryeditargdialog.h"
#include "path.h"
#include "utils.h"

#include "ui_librarydialog.h"

Expand Down Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions gui/projectfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "config.h"
#include "importproject.h"
#include "settings.h"
#include "utils.h"

#include <utility>

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion gui/test/cppchecklibrarydata/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
13 changes: 7 additions & 6 deletions lib/checkcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -3854,7 +3854,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);
Expand Down
2 changes: 1 addition & 1 deletion lib/checkstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion lib/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "settings.h"
#include "standards.h"
#include "suppressions.h"
#include "utils.h"

#include <algorithm>
#include <array>
Expand Down Expand Up @@ -853,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;
Expand Down
13 changes: 7 additions & 6 deletions lib/templatesimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "token.h"
#include "tokenize.h"
#include "tokenlist.h"
#include "utils.h"

#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,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";
}
Expand Down
2 changes: 1 addition & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Loading