changed `bool_to_string()` to return `const char*` instead and use it in more cases (#5385)
This commit is contained in:
parent
bbe45ff0eb
commit
639a4131c4
|
@ -18,6 +18,8 @@
|
||||||
|
|
||||||
#include "cppchecklibrarydata.h"
|
#include "cppchecklibrarydata.h"
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
@ -615,7 +617,7 @@ static void writeFunction(QXmlStreamWriter &xmlWriter, const CppcheckLibraryData
|
||||||
xmlWriter.writeEndElement();
|
xmlWriter.writeEndElement();
|
||||||
}
|
}
|
||||||
if (function.noreturn != CppcheckLibraryData::Function::Unknown)
|
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)
|
if (function.leakignore)
|
||||||
xmlWriter.writeEmptyElement("leak-ignore");
|
xmlWriter.writeEmptyElement("leak-ignore");
|
||||||
// Argument info..
|
// Argument info..
|
||||||
|
@ -719,7 +721,7 @@ static void writeMemoryResource(QXmlStreamWriter &xmlWriter, const CppcheckLibra
|
||||||
} else {
|
} else {
|
||||||
xmlWriter.writeStartElement("alloc");
|
xmlWriter.writeStartElement("alloc");
|
||||||
}
|
}
|
||||||
xmlWriter.writeAttribute("init", alloc.init ? "true" : "false");
|
xmlWriter.writeAttribute("init", bool_to_string(alloc.init));
|
||||||
if (alloc.arg != -1) {
|
if (alloc.arg != -1) {
|
||||||
xmlWriter.writeAttribute("arg", QString("%1").arg(alloc.arg));
|
xmlWriter.writeAttribute("arg", QString("%1").arg(alloc.arg));
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#include "libraryaddfunctiondialog.h"
|
#include "libraryaddfunctiondialog.h"
|
||||||
#include "libraryeditargdialog.h"
|
#include "libraryeditargdialog.h"
|
||||||
#include "path.h"
|
#include "path.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#include "ui_librarydialog.h"
|
#include "ui_librarydialog.h"
|
||||||
|
|
||||||
|
@ -343,11 +344,11 @@ QString LibraryDialog::getArgText(const CppcheckLibraryData::Function::Arg &arg)
|
||||||
if (arg.nr != CppcheckLibraryData::Function::Arg::ANY)
|
if (arg.nr != CppcheckLibraryData::Function::Arg::ANY)
|
||||||
s += QString::number(arg.nr);
|
s += QString::number(arg.nr);
|
||||||
|
|
||||||
s += "\n not bool: " + QString(arg.notbool ? "true" : "false");
|
s += "\n not bool: " + QString(bool_to_string(arg.notbool));
|
||||||
s += "\n not null: " + QString(arg.notnull ? "true" : "false");
|
s += "\n not null: " + QString(bool_to_string(arg.notnull));
|
||||||
s += "\n not uninit: " + QString(arg.notuninit ? "true" : "false");
|
s += "\n not uninit: " + QString(bool_to_string(arg.notuninit));
|
||||||
s += "\n format string: " + QString(arg.formatstr ? "true" : "false");
|
s += "\n format string: " + QString(bool_to_string(arg.formatstr));
|
||||||
s += "\n strz: " + QString(arg.strz ? "true" : "false");
|
s += "\n strz: " + QString(bool_to_string(arg.strz));
|
||||||
s += "\n valid: " + QString(arg.valid.isEmpty() ? "any" : arg.valid);
|
s += "\n valid: " + QString(arg.valid.isEmpty() ? "any" : arg.valid);
|
||||||
for (const CppcheckLibraryData::Function::Arg::MinSize &minsize : arg.minsizes) {
|
for (const CppcheckLibraryData::Function::Arg::MinSize &minsize : arg.minsizes) {
|
||||||
s += "\n minsize: " + minsize.type + " " + minsize.arg + " " + minsize.arg2;
|
s += "\n minsize: " + minsize.type + " " + minsize.arg + " " + minsize.arg2;
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "importproject.h"
|
#include "importproject.h"
|
||||||
#include "settings.h"
|
#include "settings.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
@ -847,7 +848,7 @@ bool ProjectFile::write(const QString &filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
xmlWriter.writeStartElement(CppcheckXml::AnalyzeAllVsConfigsElementName);
|
xmlWriter.writeStartElement(CppcheckXml::AnalyzeAllVsConfigsElementName);
|
||||||
xmlWriter.writeCharacters(mAnalyzeAllVsConfigs ? "true" : "false");
|
xmlWriter.writeCharacters(bool_to_string(mAnalyzeAllVsConfigs));
|
||||||
xmlWriter.writeEndElement();
|
xmlWriter.writeEndElement();
|
||||||
|
|
||||||
if (clangParser) {
|
if (clangParser) {
|
||||||
|
@ -857,11 +858,11 @@ bool ProjectFile::write(const QString &filename)
|
||||||
}
|
}
|
||||||
|
|
||||||
xmlWriter.writeStartElement(CppcheckXml::CheckHeadersElementName);
|
xmlWriter.writeStartElement(CppcheckXml::CheckHeadersElementName);
|
||||||
xmlWriter.writeCharacters(mCheckHeaders ? "true" : "false");
|
xmlWriter.writeCharacters(bool_to_string(mCheckHeaders));
|
||||||
xmlWriter.writeEndElement();
|
xmlWriter.writeEndElement();
|
||||||
|
|
||||||
xmlWriter.writeStartElement(CppcheckXml::CheckUnusedTemplatesElementName);
|
xmlWriter.writeStartElement(CppcheckXml::CheckUnusedTemplatesElementName);
|
||||||
xmlWriter.writeCharacters(mCheckUnusedTemplates ? "true" : "false");
|
xmlWriter.writeCharacters(bool_to_string(mCheckUnusedTemplates));
|
||||||
xmlWriter.writeEndElement();
|
xmlWriter.writeEndElement();
|
||||||
|
|
||||||
xmlWriter.writeStartElement(CppcheckXml::MaxCtuDepthElementName);
|
xmlWriter.writeStartElement(CppcheckXml::MaxCtuDepthElementName);
|
||||||
|
|
|
@ -8,7 +8,7 @@ add_executable(test-cppchecklibrarydata
|
||||||
testcppchecklibrarydata.cpp
|
testcppchecklibrarydata.cpp
|
||||||
${CMAKE_SOURCE_DIR}/gui/cppchecklibrarydata.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_compile_definitions(test-cppchecklibrarydata PRIVATE SRCDIR="${CMAKE_CURRENT_SOURCE_DIR}")
|
||||||
target_link_libraries(test-cppchecklibrarydata ${QT_CORE_LIB} ${QT_TEST_LIB})
|
target_link_libraries(test-cppchecklibrarydata ${QT_CORE_LIB} ${QT_TEST_LIB})
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include "symboldatabase.h"
|
#include "symboldatabase.h"
|
||||||
#include "token.h"
|
#include "token.h"
|
||||||
#include "tokenize.h"
|
#include "tokenize.h"
|
||||||
|
#include "utils.h"
|
||||||
#include "vfvalue.h"
|
#include "vfvalue.h"
|
||||||
|
|
||||||
#include "checkother.h" // comparisonNonZeroExpressionLessThanZero and testIfNonZeroExpressionIsPositive
|
#include "checkother.h" // comparisonNonZeroExpressionLessThanZero and testIfNonZeroExpressionIsPositive
|
||||||
|
@ -243,7 +244,7 @@ void CheckCondition::assignIfError(const Token *tok1, const Token *tok2, const s
|
||||||
reportError(locations,
|
reportError(locations,
|
||||||
Severity::style,
|
Severity::style,
|
||||||
"assignIfError",
|
"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;
|
std::ostringstream expression;
|
||||||
expression << std::hex << "(X " << bitop << " 0x" << value1 << ") " << op << " 0x" << value2;
|
expression << std::hex << "(X " << bitop << " 0x" << value1 << ") " << op << " 0x" << value2;
|
||||||
|
|
||||||
const std::string errmsg("Expression '" + expression.str() + "' is always " + (result?"true":"false") + ".\n"
|
const std::string errmsg("Expression '" + expression.str() + "' is always " + bool_to_string(result) + ".\n"
|
||||||
"The expression '" + expression.str() + "' is always " + (result?"true":"false") +
|
"The expression '" + expression.str() + "' is always " + bool_to_string(result) +
|
||||||
". Check carefully constants and operators used, these errors might be hard to "
|
". 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 "
|
"spot sometimes. In case of complex expression it might help to split it to "
|
||||||
"separate expressions.");
|
"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 bool alwaysTrue = value && (value->intvalue != 0 || value->isImpossible());
|
||||||
const std::string expr = tok ? tok->expressionString() : std::string("x");
|
const std::string expr = tok ? tok->expressionString() : std::string("x");
|
||||||
const std::string conditionStr = (Token::simpleMatch(condition, "return") ? "Return value" : "Condition");
|
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);
|
const ErrorPath errorPath = getErrorPath(tok, value, errmsg);
|
||||||
reportError(errorPath,
|
reportError(errorPath,
|
||||||
Severity::style,
|
Severity::style,
|
||||||
|
@ -1694,7 +1695,7 @@ void CheckCondition::checkInvalidTestForOverflow()
|
||||||
result = (cmp == ">" || cmp == ">=");
|
result = (cmp == ">" || cmp == ">=");
|
||||||
else
|
else
|
||||||
result = (cmp == "<" || cmp == "<=");
|
result = (cmp == "<" || cmp == "<=");
|
||||||
invalidTestForOverflow(tok, lhs->valueType(), result ? "true" : "false");
|
invalidTestForOverflow(tok, lhs->valueType(), bool_to_string(result));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2031,7 +2032,7 @@ void CheckCondition::compareValueOutOfTypeRangeError(const Token *comparison, co
|
||||||
comparison,
|
comparison,
|
||||||
Severity::style,
|
Severity::style,
|
||||||
"compareValueOutOfTypeRangeError",
|
"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,
|
CWE398,
|
||||||
Certainty::normal);
|
Certainty::normal);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2785,7 +2785,7 @@ void CheckOther::checkComparisonFunctionIsAlwaysTrueOrFalse()
|
||||||
}
|
}
|
||||||
void CheckOther::checkComparisonFunctionIsAlwaysTrueOrFalseError(const Token* tok, const std::string &functionName, const std::string &varName, const bool result)
|
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;
|
const struct CWE cweResult = result ? CWE571 : CWE570;
|
||||||
|
|
||||||
reportError(tok, Severity::warning, "comparisonFunctionIsAlwaysTrueOrFalse",
|
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.");
|
reportError(tok, Severity::style, "knownPointerToBool", "Pointer expression 'p' converted to bool is always true.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::string cond = value->intvalue ? "true" : "false";
|
std::string cond = bool_to_string(value->intvalue);
|
||||||
const std::string& expr = tok->expressionString();
|
const std::string& expr = tok->expressionString();
|
||||||
std::string errmsg = "Pointer expression '" + expr + "' converted to bool is always " + cond + ".";
|
std::string errmsg = "Pointer expression '" + expr + "' converted to bool is always " + cond + ".";
|
||||||
const ErrorPath errorPath = getErrorPath(tok, value, errmsg);
|
const ErrorPath errorPath = getErrorPath(tok, value, errmsg);
|
||||||
|
|
|
@ -328,7 +328,7 @@ void CheckString::incorrectStringBooleanError(const Token *tok, const std::strin
|
||||||
{
|
{
|
||||||
const bool charLiteral = isCharLiteral(string);
|
const bool charLiteral = isCharLiteral(string);
|
||||||
const std::string literalType = charLiteral ? "char" : "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,
|
reportError(tok,
|
||||||
Severity::warning,
|
Severity::warning,
|
||||||
charLiteral ? "incorrectCharBooleanError" : "incorrectStringBooleanError",
|
charLiteral ? "incorrectCharBooleanError" : "incorrectStringBooleanError",
|
||||||
|
|
|
@ -854,7 +854,7 @@ void Preprocessor::dump(std::ostream &out) const
|
||||||
<< " usefile=\"" << ErrorLogger::toxml(macroUsage.useLocation.file()) << "\""
|
<< " usefile=\"" << ErrorLogger::toxml(macroUsage.useLocation.file()) << "\""
|
||||||
<< " useline=\"" << macroUsage.useLocation.line << "\""
|
<< " useline=\"" << macroUsage.useLocation.line << "\""
|
||||||
<< " usecolumn=\"" << macroUsage.useLocation.col << "\""
|
<< " usecolumn=\"" << macroUsage.useLocation.col << "\""
|
||||||
<< " is-known-value=\"" << (macroUsage.macroValueKnown ? "true" : "false") << "\""
|
<< " is-known-value=\"" << bool_to_string(macroUsage.macroValueKnown) << "\""
|
||||||
<< "/>" << std::endl;
|
<< "/>" << std::endl;
|
||||||
}
|
}
|
||||||
out << " </macro-usage>" << std::endl;
|
out << " </macro-usage>" << std::endl;
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include "token.h"
|
#include "token.h"
|
||||||
#include "tokenize.h"
|
#include "tokenize.h"
|
||||||
#include "tokenlist.h"
|
#include "tokenlist.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
@ -2513,17 +2514,17 @@ void TemplateSimplifier::simplifyTemplateArgs(Token *start, const Token *end, st
|
||||||
std::string result;
|
std::string result;
|
||||||
|
|
||||||
if (cmp == "==")
|
if (cmp == "==")
|
||||||
result = (op1 == op2) ? "true" : "false";
|
result = bool_to_string(op1 == op2);
|
||||||
else if (cmp == "!=")
|
else if (cmp == "!=")
|
||||||
result = (op1 != op2) ? "true" : "false";
|
result = bool_to_string(op1 != op2);
|
||||||
else if (cmp == "<=")
|
else if (cmp == "<=")
|
||||||
result = (op1 <= op2) ? "true" : "false";
|
result = bool_to_string(op1 <= op2);
|
||||||
else if (cmp == ">=")
|
else if (cmp == ">=")
|
||||||
result = (op1 >= op2) ? "true" : "false";
|
result = bool_to_string(op1 >= op2);
|
||||||
else if (cmp == "<")
|
else if (cmp == "<")
|
||||||
result = (op1 < op2) ? "true" : "false";
|
result = bool_to_string(op1 < op2);
|
||||||
else
|
else
|
||||||
result = (op1 > op2) ? "true" : "false";
|
result = bool_to_string(op1 > op2);
|
||||||
|
|
||||||
tok->str(result);
|
tok->str(result);
|
||||||
tok->deleteNext(2);
|
tok->deleteNext(2);
|
||||||
|
|
|
@ -5965,10 +5965,10 @@ void Tokenizer::dump(std::ostream &out) const
|
||||||
outs += " <container id=\"";
|
outs += " <container id=\"";
|
||||||
outs += id_string(c);
|
outs += id_string(c);
|
||||||
outs += "\" array-like-index-op=\"";
|
outs += "\" array-like-index-op=\"";
|
||||||
outs += (c->arrayLike_indexOp ? "true" : "false");
|
outs += bool_to_string(c->arrayLike_indexOp);
|
||||||
outs += "\" ";
|
outs += "\" ";
|
||||||
outs += "std-string-like=\"";
|
outs += "std-string-like=\"";
|
||||||
outs +=(c->stdStringLike ? "true" : "false");
|
outs += bool_to_string(c->stdStringLike);
|
||||||
outs += "\"/>";
|
outs += "\"/>";
|
||||||
outs += '\n';
|
outs += '\n';
|
||||||
}
|
}
|
||||||
|
|
|
@ -325,7 +325,7 @@ static inline std::string id_string(const void* p)
|
||||||
return id_string_i(reinterpret_cast<uintptr_t>(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";
|
return b ? "true" : "false";
|
||||||
}
|
}
|
||||||
|
|
|
@ -2953,7 +2953,7 @@ struct ValueFlowAnalyzer : Analyzer {
|
||||||
std::string s = state ? "empty" : "not empty";
|
std::string s = state ? "empty" : "not empty";
|
||||||
addErrorPath(tok, "Assuming container is " + s);
|
addErrorPath(tok, "Assuming container is " + s);
|
||||||
} else {
|
} else {
|
||||||
std::string s = state ? "true" : "false";
|
std::string s = bool_to_string(state);
|
||||||
addErrorPath(tok, "Assuming condition is " + s);
|
addErrorPath(tok, "Assuming condition is " + s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue