Mark members and arguments with 'nonneg'

This commit is contained in:
Daniel Marjamäki 2019-07-14 12:22:33 +02:00
parent 36d7e8a361
commit 0014fe880e
10 changed files with 80 additions and 73 deletions

View File

@ -208,6 +208,10 @@ script:
- echo $CXXFLAGS
- make -s test -j2 CPPFLAGS=-DTEST_MATHLIB_VALUE
- touch lib/mathlib.cpp test/testmathlib.cpp
# compile cppcheck with -DNONNEG
- echo $CXXFLAGS
- make -s CPPFLAGS=-DNONNEG -j2
- make clean
# compile cppcheck, default build
- echo $CXXFLAGS
- make -s test -j2

View File

@ -227,7 +227,7 @@ bool precedes(const Token * tok1, const Token * tok2)
return tok1->index() < tok2->index();
}
static bool isAliased(const Token * startTok, const Token * endTok, int varid)
static bool isAliased(const Token * startTok, const Token * endTok, nonneg int varid)
{
for (const Token *tok = startTok; tok != endTok; tok = tok->next()) {
if (Token::Match(tok, "= & %varid% ;", varid))
@ -248,7 +248,7 @@ static bool isAliased(const Token * startTok, const Token * endTok, int varid)
return false;
}
static bool exprDependsOnThis(const Token *expr, int depth)
static bool exprDependsOnThis(const Token *expr, nonneg int depth)
{
if (!expr)
return false;
@ -804,7 +804,7 @@ bool isReturnScope(const Token * const endToken)
return false;
}
bool isVariableChangedByFunctionCall(const Token *tok, int varid, const Settings *settings, bool *inconclusive)
bool isVariableChangedByFunctionCall(const Token *tok, nonneg int varid, const Settings *settings, bool *inconclusive)
{
if (!tok)
return false;
@ -930,7 +930,7 @@ bool isVariableChangedByFunctionCall(const Token *tok, const Settings *settings,
return arg && !arg->isConst() && arg->isReference();
}
bool isVariableChanged(const Token *start, const Token *end, const int varid, bool globalvar, const Settings *settings, bool cpp)
bool isVariableChanged(const Token *start, const Token *end, const nonneg int varid, bool globalvar, const Settings *settings, bool cpp)
{
for (const Token *tok = start; tok != end; tok = tok->next()) {
if (tok->varId() != varid) {
@ -1017,7 +1017,7 @@ int numberOfArguments(const Token *start)
return arguments;
}
static void getArgumentsRecursive(const Token *tok, std::vector<const Token *> *arguments, int depth)
static void getArgumentsRecursive(const Token *tok, std::vector<const Token *> *arguments, nonneg int depth)
{
++depth;
if (!tok || depth >= 100)

View File

@ -27,6 +27,7 @@
#include <vector>
#include "errorlogger.h"
#include "utils.h"
class Library;
class Settings;
@ -118,7 +119,7 @@ bool isReturnScope(const Token *endToken);
* @param settings program settings
* @param inconclusive pointer to output variable which indicates that the answer of the question is inconclusive
*/
bool isVariableChangedByFunctionCall(const Token *tok, int varid, const Settings *settings, bool *inconclusive);
bool isVariableChangedByFunctionCall(const Token *tok, nonneg int varid, const Settings *settings, bool *inconclusive);
/** Is variable changed by function call?
* In case the answer of the question is inconclusive, e.g. because the function declaration is not known
@ -131,7 +132,7 @@ bool isVariableChangedByFunctionCall(const Token *tok, int varid, const Settings
bool isVariableChangedByFunctionCall(const Token *tok, const Settings *settings, bool *inconclusive);
/** Is variable changed in block of code? */
bool isVariableChanged(const Token *start, const Token *end, const int varid, bool globalvar, const Settings *settings, bool cpp);
bool isVariableChanged(const Token *start, const Token *end, const nonneg int varid, bool globalvar, const Settings *settings, bool cpp);
bool isVariableChanged(const Variable * var, const Settings *settings, bool cpp);

View File

@ -879,7 +879,7 @@ bool CheckUninitVar::checkLoopBody(const Token *tok, const Variable& var, const
return false;
}
void CheckUninitVar::checkRhs(const Token *tok, const Variable &var, Alloc alloc, int number_of_if, const std::string &membervar)
void CheckUninitVar::checkRhs(const Token *tok, const Variable &var, Alloc alloc, nonneg int number_of_if, const std::string &membervar)
{
bool rhs = false;
int indent = 0;

View File

@ -76,7 +76,7 @@ public:
bool checkScopeForVariable(const Token *tok, const Variable& var, bool* const possibleInit, bool* const noreturn, Alloc* const alloc, const std::string &membervar, std::map<int, VariableValue> variableValue);
bool checkIfForWhileHead(const Token *startparentheses, const Variable& var, bool suppressErrors, bool isuninit, Alloc alloc, const std::string &membervar);
bool checkLoopBody(const Token *tok, const Variable& var, const Alloc alloc, const std::string &membervar, const bool suppressErrors);
void checkRhs(const Token *tok, const Variable &var, Alloc alloc, int number_of_if, const std::string &membervar);
void checkRhs(const Token *tok, const Variable &var, Alloc alloc, nonneg int number_of_if, const std::string &membervar);
bool isVariableUsage(const Token *vartok, bool pointer, Alloc alloc) const;
int isFunctionParUsage(const Token *vartok, bool pointer, Alloc alloc) const;
bool isMemberVariableAssignment(const Token *tok, const std::string &membervar) const;

View File

@ -457,7 +457,7 @@ std::list<CTU::FileInfo::UnsafeUsage> CTU::getUnsafeUsage(const Tokenizer *token
}
static bool findPath(const std::string &callId,
int callArgNr,
nonneg int callArgNr,
MathLib::bigint unsafeValue,
CTU::FileInfo::InvalidValueType invalidValue,
const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> &callsMap,

View File

@ -40,16 +40,16 @@ namespace CTU {
struct Location {
Location() = default;
Location(const Tokenizer *tokenizer, const Token *tok);
Location(const std::string &fileName, int linenr) : fileName(fileName), linenr(linenr) {}
Location(const std::string &fileName, nonneg int linenr) : fileName(fileName), linenr(linenr) {}
std::string fileName;
int linenr;
nonneg int linenr;
};
struct UnsafeUsage {
UnsafeUsage() = default;
UnsafeUsage(const std::string &myId, int myArgNr, const std::string &myArgumentName, const Location &location, MathLib::bigint value) : myId(myId), myArgNr(myArgNr), myArgumentName(myArgumentName), location(location), value(value) {}
UnsafeUsage(const std::string &myId, nonneg int myArgNr, const std::string &myArgumentName, const Location &location, MathLib::bigint value) : myId(myId), myArgNr(myArgNr), myArgumentName(myArgumentName), location(location), value(value) {}
std::string myId;
int myArgNr;
nonneg int myArgNr;
std::string myArgumentName;
Location location;
MathLib::bigint value;
@ -89,7 +89,7 @@ namespace CTU {
public:
NestedCall() = default;
NestedCall(const std::string &myId, int myArgNr, const std::string &callId, int callArgnr, const std::string &callFunctionName, const Location &location)
NestedCall(const std::string &myId, nonneg int myArgNr, const std::string &callId, nonneg int callArgnr, const std::string &callFunctionName, const Location &location)
: CallBase(callId, callArgnr, callFunctionName, location),
myId(myId),
myArgNr(myArgNr) {
@ -101,7 +101,7 @@ namespace CTU {
bool loadFromXml(const tinyxml2::XMLElement *xmlElement);
std::string myId;
int myArgNr;
nonneg int myArgNr;
};
std::list<FunctionCall> functionCalls;

View File

@ -22,6 +22,7 @@
//---------------------------------------------------------------------------
#include "config.h"
#include "utils.h"
#include <climits>
#include <string>
@ -63,24 +64,24 @@ namespace cppcheck {
return value >= min_value(long_bit) && value <= max_value(long_bit);
}
int char_bit; /// bits in char
int short_bit; /// bits in short
int int_bit; /// bits in int
int long_bit; /// bits in long
int long_long_bit; /// bits in long long
nonneg int char_bit; /// bits in char
nonneg int short_bit; /// bits in short
nonneg int int_bit; /// bits in int
nonneg int long_bit; /// bits in long
nonneg int long_long_bit; /// bits in long long
/** size of standard types */
int sizeof_bool;
int sizeof_short;
int sizeof_int;
int sizeof_long;
int sizeof_long_long;
int sizeof_float;
int sizeof_double;
int sizeof_long_double;
int sizeof_wchar_t;
int sizeof_size_t;
int sizeof_pointer;
nonneg int sizeof_bool;
nonneg int sizeof_short;
nonneg int sizeof_int;
nonneg int sizeof_long;
nonneg int sizeof_long_long;
nonneg int sizeof_float;
nonneg int sizeof_double;
nonneg int sizeof_long_double;
nonneg int sizeof_wchar_t;
nonneg int sizeof_size_t;
nonneg int sizeof_pointer;
char defaultSign; // unsigned:'u', signed:'s', unknown:'\0'

View File

@ -220,7 +220,7 @@ std::string Token::strValue() const
return ret;
}
void Token::deleteNext(int count)
void Token::deleteNext(nonneg int count)
{
while (mNext && count > 0) {
Token *n = mNext;
@ -240,7 +240,7 @@ void Token::deleteNext(int count)
mTokensFrontBack->back = this;
}
void Token::deletePrevious(int count)
void Token::deletePrevious(nonneg int count)
{
while (mPrevious && count > 0) {
Token *p = mPrevious;
@ -381,7 +381,7 @@ const std::string &Token::strAt(int index) const
return tok ? tok->mStr : emptyString;
}
static int multiComparePercent(const Token *tok, const char*& haystack, int varid)
static int multiComparePercent(const Token *tok, const char*& haystack, nonneg int varid)
{
++haystack;
// Compare only the first character of the string for optimization reasons
@ -521,7 +521,7 @@ static int multiComparePercent(const Token *tok, const char*& haystack, int vari
return 0xFFFF;
}
int Token::multiCompare(const Token *tok, const char *haystack, int varid)
nonneg int Token::multiCompare(const Token *tok, const char *haystack, nonneg int varid)
{
const char *needle = tok->str().c_str();
const char *needlePointer = needle;
@ -626,7 +626,7 @@ const char *Token::chrInFirstWord(const char *str, char c)
}
}
bool Token::Match(const Token *tok, const char pattern[], int varid)
bool Token::Match(const Token *tok, const char pattern[], nonneg int varid)
{
const char *p = pattern;
while (*p) {
@ -713,7 +713,7 @@ bool Token::Match(const Token *tok, const char pattern[], int varid)
return true;
}
int Token::getStrLength(const Token *tok)
nonneg int Token::getStrLength(const Token *tok)
{
assert(tok != nullptr);
assert(tok->mTokType == eString);
@ -741,7 +741,7 @@ int Token::getStrLength(const Token *tok)
return len;
}
int Token::getStrSize(const Token *tok)
nonneg int Token::getStrSize(const Token *tok)
{
assert(tok != nullptr);
assert(tok->tokType() == eString);
@ -937,7 +937,7 @@ const Token *Token::findsimplematch(const Token * const startTok, const char pat
return nullptr;
}
const Token *Token::findmatch(const Token * const startTok, const char pattern[], const int varId)
const Token *Token::findmatch(const Token * const startTok, const char pattern[], const nonneg int varId)
{
for (const Token* tok = startTok; tok; tok = tok->next()) {
if (Token::Match(tok, pattern, varId))
@ -946,7 +946,7 @@ const Token *Token::findmatch(const Token * const startTok, const char pattern[]
return nullptr;
}
const Token *Token::findmatch(const Token * const startTok, const char pattern[], const Token * const end, const int varId)
const Token *Token::findmatch(const Token * const startTok, const char pattern[], const Token * const end, const nonneg int varId)
{
for (const Token* tok = startTok; tok && tok != end; tok = tok->next()) {
if (Token::Match(tok, pattern, varId))
@ -1333,7 +1333,7 @@ std::string Token::expressionString() const
return stringFromTokenRange(tokens.first, tokens.second);
}
static void astStringXml(const Token *tok, int indent, std::ostream &out)
static void astStringXml(const Token *tok, nonneg int indent, std::ostream &out)
{
const std::string strindent(indent, ' ');
@ -1387,7 +1387,7 @@ void Token::printAst(bool verbose, bool xml, std::ostream &out) const
}
}
static void indent(std::string &str, const int indent1, const int indent2)
static void indent(std::string &str, const nonneg int indent1, const nonneg int indent2)
{
for (int i = 0; i < indent1; ++i)
str += ' ';
@ -1395,7 +1395,7 @@ static void indent(std::string &str, const int indent1, const int indent2)
str += "| ";
}
void Token::astStringVerboseRecursive(std::string& ret, const int indent1, const int indent2) const
void Token::astStringVerboseRecursive(std::string& ret, const nonneg int indent1, const nonneg int indent2) const
{
if (isExpandedMacro())
ret += '$';
@ -1582,7 +1582,7 @@ const ValueFlow::Value * Token::getValueGE(const MathLib::bigint val, const Sett
return ret;
}
const ValueFlow::Value * Token::getInvalidValue(const Token *ftok, int argnr, const Settings *settings) const
const ValueFlow::Value * Token::getInvalidValue(const Token *ftok, nonneg int argnr, const Settings *settings) const
{
if (!mImpl->mValues || !settings)
return nullptr;

View File

@ -25,6 +25,7 @@
#include "mathlib.h"
#include "valueflow.h"
#include "templatesimplifier.h"
#include "utils.h"
#include <algorithm>
#include <cstddef>
@ -51,10 +52,10 @@ struct TokensFrontBack {
};
struct TokenImpl {
int mVarId;
int mFileIndex;
int mLineNumber;
int mColumn;
nonneg int mVarId;
nonneg int mFileIndex;
nonneg int mLineNumber;
nonneg int mColumn;
// AST..
Token *mAstOperand1;
@ -74,12 +75,12 @@ struct TokenImpl {
* A value from 0-100 that provides a rough idea about where in the token
* list this token is located.
*/
int mProgressValue;
nonneg int mProgressValue;
/**
* Token index. Position in token list
*/
int mIndex;
nonneg int mIndex;
// original name like size_t
std::string* mOriginalName;
@ -187,12 +188,12 @@ public:
/**
* Unlink and delete the next 'count' tokens.
*/
void deleteNext(int count = 1);
void deleteNext(nonneg int count = 1);
/**
* Unlink and delete the previous 'count' tokens.
*/
void deletePrevious(int count = 1);
void deletePrevious(nonneg int count = 1);
/**
* Swap the contents of this token with the next token.
@ -288,7 +289,7 @@ public:
* @return true if given token matches with given pattern
* false if given token does not match with given pattern
*/
static bool Match(const Token *tok, const char pattern[], int varid = 0);
static bool Match(const Token *tok, const char pattern[], nonneg int varid = 0);
/**
* @return length of C-string.
@ -297,7 +298,7 @@ public:
*
* @param tok token with C-string
**/
static int getStrLength(const Token *tok);
static nonneg int getStrLength(const Token *tok);
/**
* @return sizeof of C-string.
@ -306,7 +307,7 @@ public:
*
* @param tok token with C-string
**/
static int getStrSize(const Token *tok);
static nonneg int getStrSize(const Token *tok);
/**
* @return char of C-string at index (possible escaped "\\n")
@ -586,18 +587,18 @@ public:
static const Token *findsimplematch(const Token * const startTok, const char pattern[]);
static const Token *findsimplematch(const Token * const startTok, const char pattern[], const Token * const end);
static const Token *findmatch(const Token * const startTok, const char pattern[], const int varId = 0);
static const Token *findmatch(const Token * const startTok, const char pattern[], const Token * const end, const int varId = 0);
static const Token *findmatch(const Token * const startTok, const char pattern[], const nonneg int varId = 0);
static const Token *findmatch(const Token * const startTok, const char pattern[], const Token * const end, const nonneg int varId = 0);
static Token *findsimplematch(Token * const startTok, const char pattern[]) {
return const_cast<Token *>(findsimplematch(const_cast<const Token *>(startTok), pattern));
}
static Token *findsimplematch(Token * const startTok, const char pattern[], const Token * const end) {
return const_cast<Token *>(findsimplematch(const_cast<const Token *>(startTok), pattern, end));
}
static Token *findmatch(Token * const startTok, const char pattern[], const int varId = 0) {
static Token *findmatch(Token * const startTok, const char pattern[], const nonneg int varId = 0) {
return const_cast<Token *>(findmatch(const_cast<const Token *>(startTok), pattern, varId));
}
static Token *findmatch(Token * const startTok, const char pattern[], const Token * const end, const int varId = 0) {
static Token *findmatch(Token * const startTok, const char pattern[], const Token * const end, const nonneg int varId = 0) {
return const_cast<Token *>(findmatch(const_cast<const Token *>(startTok), pattern, end, varId));
}
@ -615,26 +616,26 @@ public:
* 0 if needle was empty string
* -1 if needle was not found
*/
static int multiCompare(const Token *tok, const char *haystack, int varid);
static nonneg int multiCompare(const Token *tok, const char *haystack, nonneg int varid);
int fileIndex() const {
nonneg int fileIndex() const {
return mImpl->mFileIndex;
}
void fileIndex(int indexOfFile) {
void fileIndex(nonneg int indexOfFile) {
mImpl->mFileIndex = indexOfFile;
}
int linenr() const {
nonneg int linenr() const {
return mImpl->mLineNumber;
}
void linenr(int lineNumber) {
void linenr(nonneg int lineNumber) {
mImpl->mLineNumber = lineNumber;
}
int col() const {
nonneg int col() const {
return mImpl->mColumn;
}
void col(int c) {
void col(nonneg int c) {
mImpl->mColumn = c;
}
@ -667,10 +668,10 @@ public:
}
int varId() const {
nonneg int varId() const {
return mImpl->mVarId;
}
void varId(int id) {
void varId(nonneg int id) {
mImpl->mVarId = id;
if (id != 0) {
tokType(eVariable);
@ -873,7 +874,7 @@ public:
static void move(Token *srcStart, Token *srcEnd, Token *newLocation);
/** Get progressValue (0 - 100) */
int progressValue() const {
nonneg int progressValue() const {
return mImpl->mProgressValue;
}
@ -981,7 +982,7 @@ public:
const ValueFlow::Value * getValueLE(const MathLib::bigint val, const Settings *settings) const;
const ValueFlow::Value * getValueGE(const MathLib::bigint val, const Settings *settings) const;
const ValueFlow::Value * getInvalidValue(const Token *ftok, int argnr, const Settings *settings) const;
const ValueFlow::Value * getInvalidValue(const Token *ftok, nonneg int argnr, const Settings *settings) const;
const ValueFlow::Value * getContainerSizeValue(const MathLib::bigint val) const {
if (!mImpl->mValues)
@ -1006,7 +1007,7 @@ public:
mImpl->mValues->remove_if(pred);
}
int index() const {
nonneg int index() const {
return mImpl->mIndex;
}
@ -1107,7 +1108,7 @@ private:
void update_property_char_string_literal();
/** Internal helper function to avoid excessive string allocations */
void astStringVerboseRecursive(std::string& ret, const int indent1 = 0, const int indent2 = 0) const;
void astStringVerboseRecursive(std::string& ret, const nonneg int indent1 = 0, const nonneg int indent2 = 0) const;
public:
void astOperand1(Token *tok);