Skip to content
Open
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
2 changes: 1 addition & 1 deletion lib/checkleakautovar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
}
}

if (tok->isCpp11init() == TokenImpl::Cpp11init::CPP11INIT) {
if (tok->isCpp11init() == Token::Cpp11init::CPP11INIT) {
const Token *newTok = tok->astOperand1();
const Token *oldTok = tok->astOperand2();
if (newTok && newTok->varId() && oldTok && oldTok->varId()) {
Expand Down
17 changes: 8 additions & 9 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,16 @@ namespace {
};
}

const std::list<ValueFlow::Value> TokenImpl::mEmptyValueList;
const std::list<ValueFlow::Value> Token::mEmptyValueList;
const std::string Token::mEmptyString;

Token::Token(const TokenList& tokenlist, std::shared_ptr<TokensFrontBack> tokensFrontBack)
: mList(tokenlist)
, mTokensFrontBack(std::move(tokensFrontBack))
, mImpl(new Impl)
, mIsC(mList.isC())
, mIsCpp(mList.isCPP())
{
mImpl = new TokenImpl();
}
{}

Token::Token(const Token* tok)
: Token(tok->mList, const_cast<Token*>(tok)->mTokensFrontBack)
Expand Down Expand Up @@ -2642,16 +2641,16 @@ const ValueFlow::Value* Token::getContainerSizeValue(const MathLib::bigint val)
return it == mImpl->mValues->end() ? nullptr : &*it;
}

TokenImpl::~TokenImpl()
Token::Impl::~Impl()
{
delete mMacroName;
delete mOriginalName;
delete mValueType;
delete mValues;

if (mTemplateSimplifierPointers) {
for (auto *templateSimplifierPointer : *mTemplateSimplifierPointers) {
templateSimplifierPointer->token(nullptr);
for (auto *p : *mTemplateSimplifierPointers) {
p->token(nullptr);
}
}
delete mTemplateSimplifierPointers;
Expand All @@ -2663,7 +2662,7 @@ TokenImpl::~TokenImpl()
}
}

void TokenImpl::setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint value)
void Token::Impl::setCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint value)
{
CppcheckAttributes *attr = mCppcheckAttributes;
while (attr && attr->type != type)
Expand All @@ -2679,7 +2678,7 @@ void TokenImpl::setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, M
}
}

bool TokenImpl::getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint &value) const
bool Token::Impl::getCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint &value) const
{
CppcheckAttributes *attr = mCppcheckAttributes;
while (attr && attr->type != type)
Expand Down
204 changes: 104 additions & 100 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,100 +63,6 @@ struct ScopeInfo2 {

enum class TokenDebug : std::uint8_t { None, ValueFlow, ValueType };

struct TokenImpl {
nonneg int mVarId{};
nonneg int mFileIndex{};
nonneg int mLineNumber{};
nonneg int mColumn{};
nonneg int mExprId{};

// original template argument location
int mTemplateArgFileIndex{-1};
int mTemplateArgLineNumber{-1};
int mTemplateArgColumn{-1};

/**
* A value from 0-100 that provides a rough idea about where in the token
* list this token is located.
*/
nonneg int mProgressValue{};

/**
* Token index. Position in token list
*/
nonneg int mIndex{};

/** Bitfield bit count. */
short mBits = -1;

// AST..
Token* mAstOperand1{};
Token* mAstOperand2{};
Token* mAstParent{};

// symbol database information
const Scope* mScope{};
union {
const Function *mFunction;
const Variable *mVariable;
const ::Type* mType;
const Enumerator *mEnumerator;
};

// original name like size_t
std::string* mOriginalName{};

// If this token came from a macro replacement list, this is the name of that macro
std::string* mMacroName{};

// ValueType
ValueType* mValueType{};

// ValueFlow
std::list<ValueFlow::Value>* mValues{};
static const std::list<ValueFlow::Value> mEmptyValueList;

// Pointer to a template in the template simplifier
std::set<TemplateSimplifier::TokenAndName*>* mTemplateSimplifierPointers{};

// Pointer to the object representing this token's scope
std::shared_ptr<ScopeInfo2> mScopeInfo;

// __cppcheck_in_range__
struct CppcheckAttributes {
enum Type : std::uint8_t { LOW, HIGH } type = LOW;
MathLib::bigint value{};
CppcheckAttributes* next{};
};
CppcheckAttributes* mCppcheckAttributes{};

// alignas expressions
std::unique_ptr<std::vector<std::string>> mAttributeAlignas;
void addAttributeAlignas(const std::string& a) {
if (!mAttributeAlignas)
mAttributeAlignas = std::unique_ptr<std::vector<std::string>>(new std::vector<std::string>());
if (std::find(mAttributeAlignas->cbegin(), mAttributeAlignas->cend(), a) == mAttributeAlignas->cend())
mAttributeAlignas->push_back(a);
}

std::string mAttributeCleanup;

// For memoization, to speed up parsing of huge arrays #8897
enum class Cpp11init : std::uint8_t { UNKNOWN, CPP11INIT, NOINIT } mCpp11init = Cpp11init::UNKNOWN;

TokenDebug mDebug{};

void setCppcheckAttribute(CppcheckAttributes::Type type, MathLib::bigint value);
bool getCppcheckAttribute(CppcheckAttributes::Type type, MathLib::bigint &value) const;

TokenImpl() : mFunction(nullptr) {}

~TokenImpl();

TokenImpl(const TokenImpl &) = delete;
TokenImpl operator=(const TokenImpl &) = delete;
};

/// @addtogroup Core
/// @{

Expand All @@ -173,7 +79,104 @@ struct TokenImpl {
class CPPCHECKLIB Token {
friend class TestToken;

public:
enum CppcheckAttributesType : std::uint8_t { LOW, HIGH };
enum class Cpp11init : std::uint8_t { UNKNOWN, CPP11INIT, NOINIT };

private:
struct Impl {
nonneg int mVarId{};
nonneg int mFileIndex{};
nonneg int mLineNumber{};
nonneg int mColumn{};
nonneg int mExprId{};

// original template argument location
int mTemplateArgFileIndex{-1};
int mTemplateArgLineNumber{-1};
int mTemplateArgColumn{-1};

/**
* A value from 0-100 that provides a rough idea about where in the token
* list this token is located.
*/
nonneg int mProgressValue{};

/**
* Token index. Position in token list
*/
nonneg int mIndex{};

/** Bitfield bit count. */
short mBits = -1;

// AST..
Token* mAstOperand1{};
Token* mAstOperand2{};
Token* mAstParent{};

// symbol database information
const Scope* mScope{};
union {
const Function *mFunction;
const Variable *mVariable;
const ::Type* mType;
const Enumerator *mEnumerator;
};

// original name like size_t
std::string* mOriginalName{};

// If this token came from a macro replacement list, this is the name of that macro
std::string* mMacroName{};

// ValueType
ValueType* mValueType{};

// ValueFlow
std::list<ValueFlow::Value>* mValues{};

// Pointer to a template in the template simplifier
std::set<TemplateSimplifier::TokenAndName*>* mTemplateSimplifierPointers{};

// Pointer to the object representing this token's scope
std::shared_ptr<ScopeInfo2> mScopeInfo;

// __cppcheck_in_range__
struct CppcheckAttributes {
CppcheckAttributesType type{LOW};
MathLib::bigint value{};
CppcheckAttributes* next{};
};
CppcheckAttributes* mCppcheckAttributes{};

// alignas expressions
std::unique_ptr<std::vector<std::string>> mAttributeAlignas;
void addAttributeAlignas(const std::string& a) {
if (!mAttributeAlignas)
mAttributeAlignas = std::unique_ptr<std::vector<std::string>>(new std::vector<std::string>());
if (std::find(mAttributeAlignas->cbegin(), mAttributeAlignas->cend(), a) == mAttributeAlignas->cend())
mAttributeAlignas->push_back(a);
}

std::string mAttributeCleanup;

// For memoization, to speed up parsing of huge arrays #8897
Cpp11init mCpp11init{Cpp11init::UNKNOWN};

TokenDebug mDebug{};

void setCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint value);
bool getCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint &value) const;

Impl() : mFunction(nullptr) {}

~Impl();

Impl(const Impl &) = delete;
Impl operator=(const Impl &) = delete;
};

const TokenList& mList;
std::shared_ptr<TokensFrontBack> mTokensFrontBack;

Expand Down Expand Up @@ -594,10 +597,10 @@ class CPPCHECKLIB Token {
bool hasAttributeCleanup() const {
return !mImpl->mAttributeCleanup.empty();
}
void setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint value) {
void setCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint value) {
mImpl->setCppcheckAttribute(type, value);
}
bool getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint &value) const {
bool getCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint &value) const {
return mImpl->getCppcheckAttribute(type, value);
}
// cppcheck-suppress unusedFunction
Expand Down Expand Up @@ -1346,7 +1349,7 @@ class CPPCHECKLIB Token {
}

const std::list<ValueFlow::Value>& values() const {
return mImpl->mValues ? *mImpl->mValues : TokenImpl::mEmptyValueList;
return mImpl->mValues ? *mImpl->mValues : mEmptyValueList;
}

/**
Expand Down Expand Up @@ -1406,6 +1409,7 @@ class CPPCHECKLIB Token {
void assignIndexes();

private:
static const std::list<ValueFlow::Value> mEmptyValueList;

void next(Token *nextToken) {
mNext = nextToken;
Expand Down Expand Up @@ -1499,7 +1503,7 @@ class CPPCHECKLIB Token {

uint64_t mFlags{};

TokenImpl* mImpl{};
Impl* mImpl{};

/**
* Get specified flag state.
Expand Down Expand Up @@ -1630,9 +1634,9 @@ class CPPCHECKLIB Token {
std::shared_ptr<ScopeInfo2> scopeInfo() const;

void setCpp11init(bool cpp11init) const {
mImpl->mCpp11init=cpp11init ? TokenImpl::Cpp11init::CPP11INIT : TokenImpl::Cpp11init::NOINIT;
mImpl->mCpp11init=cpp11init ? Cpp11init::CPP11INIT : Cpp11init::NOINIT;
}
TokenImpl::Cpp11init isCpp11init() const {
Cpp11init isCpp11init() const {
return mImpl->mCpp11init;
}

Expand Down
12 changes: 6 additions & 6 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9471,10 +9471,10 @@ void Tokenizer::simplifyCppcheckAttribute()

if (vartok->isName()) {
if (Token::Match(tok->previous(), "__cppcheck_low__ ( %num% )"))
vartok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW,
vartok->setCppcheckAttribute(Token::CppcheckAttributesType::LOW,
MathLib::toBigNumber(tok->tokAt(1)));
else if (Token::Match(tok->previous(), "__cppcheck_high__ ( %num% )"))
vartok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH,
vartok->setCppcheckAttribute(Token::CppcheckAttributesType::HIGH,
MathLib::toBigNumber(tok->tokAt(1)));
}

Expand Down Expand Up @@ -9562,16 +9562,16 @@ void Tokenizer::simplifyCPPAttribute()
}
if (argtok && argtok->str() == vartok->str()) {
if (vartok->strAt(1) == ">=")
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW,
argtok->setCppcheckAttribute(Token::CppcheckAttributesType::LOW,
MathLib::toBigNumber(vartok->tokAt(2)));
else if (vartok->strAt(1) == ">")
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW,
argtok->setCppcheckAttribute(Token::CppcheckAttributesType::LOW,
MathLib::toBigNumber(vartok->tokAt(2)) + 1);
else if (vartok->strAt(1) == "<=")
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH,
argtok->setCppcheckAttribute(Token::CppcheckAttributesType::HIGH,
MathLib::toBigNumber(vartok->tokAt(2)));
else if (vartok->strAt(1) == "<")
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH,
argtok->setCppcheckAttribute(Token::CppcheckAttributesType::HIGH,
MathLib::toBigNumber(vartok->tokAt(2)) - 1);
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/tokenlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,9 @@ static Token * findCppTypeInitPar(Token *tok)
static bool iscpp11init_impl(const Token * tok);
static bool iscpp11init(const Token * const tok)
{
if (tok->isCpp11init() == TokenImpl::Cpp11init::UNKNOWN)
if (tok->isCpp11init() == Token::Cpp11init::UNKNOWN)
tok->setCpp11init(iscpp11init_impl(tok));
return tok->isCpp11init() == TokenImpl::Cpp11init::CPP11INIT;
return tok->isCpp11init() == Token::Cpp11init::CPP11INIT;
}

static bool iscpp11init_impl(const Token * const tok)
Expand All @@ -570,8 +570,8 @@ static bool iscpp11init_impl(const Token * const tok)
return false;
const Token *nameToken = tok;
while (nameToken && nameToken->str() == "{") {
if (nameToken->isCpp11init() != TokenImpl::Cpp11init::UNKNOWN)
return nameToken->isCpp11init() == TokenImpl::Cpp11init::CPP11INIT;
if (nameToken->isCpp11init() != Token::Cpp11init::UNKNOWN)
return nameToken->isCpp11init() == Token::Cpp11init::CPP11INIT;
nameToken = nameToken->previous();
if (nameToken && nameToken->str() == "," && Token::simpleMatch(nameToken->previous(), "} ,"))
nameToken = nameToken->linkAt(-1);
Expand Down
4 changes: 2 additions & 2 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7160,8 +7160,8 @@ static void valueFlowSafeFunctions(const TokenList& tokenlist, const SymbolDatab
}

MathLib::bigint low, high;
bool isLow = arg.nameToken()->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW, low);
bool isHigh = arg.nameToken()->getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH, high);
bool isLow = arg.nameToken()->getCppcheckAttribute(Token::CppcheckAttributesType::LOW, low);
bool isHigh = arg.nameToken()->getCppcheckAttribute(Token::CppcheckAttributesType::HIGH, high);

if (!isLow && !isHigh && !all)
continue;
Expand Down
Loading