Skip to content

Commit e5f0b8e

Browse files
authored
moved TokenImpl out of global scope (danmar#7831)
1 parent 39f3a75 commit e5f0b8e

File tree

7 files changed

+146
-143
lines changed

7 files changed

+146
-143
lines changed

lib/checkleakautovar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
396396
}
397397
}
398398

399-
if (tok->isCpp11init() == TokenImpl::Cpp11init::CPP11INIT) {
399+
if (tok->isCpp11init() == Token::Cpp11init::CPP11INIT) {
400400
const Token *newTok = tok->astOperand1();
401401
const Token *oldTok = tok->astOperand2();
402402
if (newTok && newTok->varId() && oldTok && oldTok->varId()) {

lib/token.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,16 @@ namespace {
5858
};
5959
}
6060

61-
const std::list<ValueFlow::Value> TokenImpl::mEmptyValueList;
61+
const std::list<ValueFlow::Value> Token::mEmptyValueList;
6262
const std::string Token::mEmptyString;
6363

6464
Token::Token(const TokenList& tokenlist, std::shared_ptr<TokensFrontBack> tokensFrontBack)
6565
: mList(tokenlist)
6666
, mTokensFrontBack(std::move(tokensFrontBack))
67+
, mImpl(new Impl)
6768
, mIsC(mList.isC())
6869
, mIsCpp(mList.isCPP())
69-
{
70-
mImpl = new TokenImpl();
71-
}
70+
{}
7271

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

2645-
TokenImpl::~TokenImpl()
2644+
Token::Impl::~Impl()
26462645
{
26472646
delete mMacroName;
26482647
delete mOriginalName;
26492648
delete mValueType;
26502649
delete mValues;
26512650

26522651
if (mTemplateSimplifierPointers) {
2653-
for (auto *templateSimplifierPointer : *mTemplateSimplifierPointers) {
2654-
templateSimplifierPointer->token(nullptr);
2652+
for (auto *p : *mTemplateSimplifierPointers) {
2653+
p->token(nullptr);
26552654
}
26562655
}
26572656
delete mTemplateSimplifierPointers;
@@ -2663,7 +2662,7 @@ TokenImpl::~TokenImpl()
26632662
}
26642663
}
26652664

2666-
void TokenImpl::setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint value)
2665+
void Token::Impl::setCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint value)
26672666
{
26682667
CppcheckAttributes *attr = mCppcheckAttributes;
26692668
while (attr && attr->type != type)
@@ -2679,7 +2678,7 @@ void TokenImpl::setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, M
26792678
}
26802679
}
26812680

2682-
bool TokenImpl::getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint &value) const
2681+
bool Token::Impl::getCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint &value) const
26832682
{
26842683
CppcheckAttributes *attr = mCppcheckAttributes;
26852684
while (attr && attr->type != type)

lib/token.h

Lines changed: 104 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -63,100 +63,6 @@ struct ScopeInfo2 {
6363

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

66-
struct TokenImpl {
67-
nonneg int mVarId{};
68-
nonneg int mFileIndex{};
69-
nonneg int mLineNumber{};
70-
nonneg int mColumn{};
71-
nonneg int mExprId{};
72-
73-
// original template argument location
74-
int mTemplateArgFileIndex{-1};
75-
int mTemplateArgLineNumber{-1};
76-
int mTemplateArgColumn{-1};
77-
78-
/**
79-
* A value from 0-100 that provides a rough idea about where in the token
80-
* list this token is located.
81-
*/
82-
nonneg int mProgressValue{};
83-
84-
/**
85-
* Token index. Position in token list
86-
*/
87-
nonneg int mIndex{};
88-
89-
/** Bitfield bit count. */
90-
short mBits = -1;
91-
92-
// AST..
93-
Token* mAstOperand1{};
94-
Token* mAstOperand2{};
95-
Token* mAstParent{};
96-
97-
// symbol database information
98-
const Scope* mScope{};
99-
union {
100-
const Function *mFunction;
101-
const Variable *mVariable;
102-
const ::Type* mType;
103-
const Enumerator *mEnumerator;
104-
};
105-
106-
// original name like size_t
107-
std::string* mOriginalName{};
108-
109-
// If this token came from a macro replacement list, this is the name of that macro
110-
std::string* mMacroName{};
111-
112-
// ValueType
113-
ValueType* mValueType{};
114-
115-
// ValueFlow
116-
std::list<ValueFlow::Value>* mValues{};
117-
static const std::list<ValueFlow::Value> mEmptyValueList;
118-
119-
// Pointer to a template in the template simplifier
120-
std::set<TemplateSimplifier::TokenAndName*>* mTemplateSimplifierPointers{};
121-
122-
// Pointer to the object representing this token's scope
123-
std::shared_ptr<ScopeInfo2> mScopeInfo;
124-
125-
// __cppcheck_in_range__
126-
struct CppcheckAttributes {
127-
enum Type : std::uint8_t { LOW, HIGH } type = LOW;
128-
MathLib::bigint value{};
129-
CppcheckAttributes* next{};
130-
};
131-
CppcheckAttributes* mCppcheckAttributes{};
132-
133-
// alignas expressions
134-
std::unique_ptr<std::vector<std::string>> mAttributeAlignas;
135-
void addAttributeAlignas(const std::string& a) {
136-
if (!mAttributeAlignas)
137-
mAttributeAlignas = std::unique_ptr<std::vector<std::string>>(new std::vector<std::string>());
138-
if (std::find(mAttributeAlignas->cbegin(), mAttributeAlignas->cend(), a) == mAttributeAlignas->cend())
139-
mAttributeAlignas->push_back(a);
140-
}
141-
142-
std::string mAttributeCleanup;
143-
144-
// For memoization, to speed up parsing of huge arrays #8897
145-
enum class Cpp11init : std::uint8_t { UNKNOWN, CPP11INIT, NOINIT } mCpp11init = Cpp11init::UNKNOWN;
146-
147-
TokenDebug mDebug{};
148-
149-
void setCppcheckAttribute(CppcheckAttributes::Type type, MathLib::bigint value);
150-
bool getCppcheckAttribute(CppcheckAttributes::Type type, MathLib::bigint &value) const;
151-
152-
TokenImpl() : mFunction(nullptr) {}
153-
154-
~TokenImpl();
155-
156-
TokenImpl(const TokenImpl &) = delete;
157-
TokenImpl operator=(const TokenImpl &) = delete;
158-
};
159-
16066
/// @addtogroup Core
16167
/// @{
16268

@@ -173,7 +79,104 @@ struct TokenImpl {
17379
class CPPCHECKLIB Token {
17480
friend class TestToken;
17581

82+
public:
83+
enum CppcheckAttributesType : std::uint8_t { LOW, HIGH };
84+
enum class Cpp11init : std::uint8_t { UNKNOWN, CPP11INIT, NOINIT };
85+
17686
private:
87+
struct Impl {
88+
nonneg int mVarId{};
89+
nonneg int mFileIndex{};
90+
nonneg int mLineNumber{};
91+
nonneg int mColumn{};
92+
nonneg int mExprId{};
93+
94+
// original template argument location
95+
int mTemplateArgFileIndex{-1};
96+
int mTemplateArgLineNumber{-1};
97+
int mTemplateArgColumn{-1};
98+
99+
/**
100+
* A value from 0-100 that provides a rough idea about where in the token
101+
* list this token is located.
102+
*/
103+
nonneg int mProgressValue{};
104+
105+
/**
106+
* Token index. Position in token list
107+
*/
108+
nonneg int mIndex{};
109+
110+
/** Bitfield bit count. */
111+
short mBits = -1;
112+
113+
// AST..
114+
Token* mAstOperand1{};
115+
Token* mAstOperand2{};
116+
Token* mAstParent{};
117+
118+
// symbol database information
119+
const Scope* mScope{};
120+
union {
121+
const Function *mFunction;
122+
const Variable *mVariable;
123+
const ::Type* mType;
124+
const Enumerator *mEnumerator;
125+
};
126+
127+
// original name like size_t
128+
std::string* mOriginalName{};
129+
130+
// If this token came from a macro replacement list, this is the name of that macro
131+
std::string* mMacroName{};
132+
133+
// ValueType
134+
ValueType* mValueType{};
135+
136+
// ValueFlow
137+
std::list<ValueFlow::Value>* mValues{};
138+
139+
// Pointer to a template in the template simplifier
140+
std::set<TemplateSimplifier::TokenAndName*>* mTemplateSimplifierPointers{};
141+
142+
// Pointer to the object representing this token's scope
143+
std::shared_ptr<ScopeInfo2> mScopeInfo;
144+
145+
// __cppcheck_in_range__
146+
struct CppcheckAttributes {
147+
CppcheckAttributesType type{LOW};
148+
MathLib::bigint value{};
149+
CppcheckAttributes* next{};
150+
};
151+
CppcheckAttributes* mCppcheckAttributes{};
152+
153+
// alignas expressions
154+
std::unique_ptr<std::vector<std::string>> mAttributeAlignas;
155+
void addAttributeAlignas(const std::string& a) {
156+
if (!mAttributeAlignas)
157+
mAttributeAlignas = std::unique_ptr<std::vector<std::string>>(new std::vector<std::string>());
158+
if (std::find(mAttributeAlignas->cbegin(), mAttributeAlignas->cend(), a) == mAttributeAlignas->cend())
159+
mAttributeAlignas->push_back(a);
160+
}
161+
162+
std::string mAttributeCleanup;
163+
164+
// For memoization, to speed up parsing of huge arrays #8897
165+
Cpp11init mCpp11init{Cpp11init::UNKNOWN};
166+
167+
TokenDebug mDebug{};
168+
169+
void setCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint value);
170+
bool getCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint &value) const;
171+
172+
Impl() : mFunction(nullptr) {}
173+
174+
~Impl();
175+
176+
Impl(const Impl &) = delete;
177+
Impl operator=(const Impl &) = delete;
178+
};
179+
177180
const TokenList& mList;
178181
std::shared_ptr<TokensFrontBack> mTokensFrontBack;
179182

@@ -594,10 +597,10 @@ class CPPCHECKLIB Token {
594597
bool hasAttributeCleanup() const {
595598
return !mImpl->mAttributeCleanup.empty();
596599
}
597-
void setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint value) {
600+
void setCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint value) {
598601
mImpl->setCppcheckAttribute(type, value);
599602
}
600-
bool getCppcheckAttribute(TokenImpl::CppcheckAttributes::Type type, MathLib::bigint &value) const {
603+
bool getCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint &value) const {
601604
return mImpl->getCppcheckAttribute(type, value);
602605
}
603606
// cppcheck-suppress unusedFunction
@@ -1346,7 +1349,7 @@ class CPPCHECKLIB Token {
13461349
}
13471350

13481351
const std::list<ValueFlow::Value>& values() const {
1349-
return mImpl->mValues ? *mImpl->mValues : TokenImpl::mEmptyValueList;
1352+
return mImpl->mValues ? *mImpl->mValues : mEmptyValueList;
13501353
}
13511354

13521355
/**
@@ -1406,6 +1409,7 @@ class CPPCHECKLIB Token {
14061409
void assignIndexes();
14071410

14081411
private:
1412+
static const std::list<ValueFlow::Value> mEmptyValueList;
14091413

14101414
void next(Token *nextToken) {
14111415
mNext = nextToken;
@@ -1499,7 +1503,7 @@ class CPPCHECKLIB Token {
14991503

15001504
uint64_t mFlags{};
15011505

1502-
TokenImpl* mImpl{};
1506+
Impl* mImpl{};
15031507

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

16321636
void setCpp11init(bool cpp11init) const {
1633-
mImpl->mCpp11init=cpp11init ? TokenImpl::Cpp11init::CPP11INIT : TokenImpl::Cpp11init::NOINIT;
1637+
mImpl->mCpp11init=cpp11init ? Cpp11init::CPP11INIT : Cpp11init::NOINIT;
16341638
}
1635-
TokenImpl::Cpp11init isCpp11init() const {
1639+
Cpp11init isCpp11init() const {
16361640
return mImpl->mCpp11init;
16371641
}
16381642

lib/tokenize.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9471,10 +9471,10 @@ void Tokenizer::simplifyCppcheckAttribute()
94719471

94729472
if (vartok->isName()) {
94739473
if (Token::Match(tok->previous(), "__cppcheck_low__ ( %num% )"))
9474-
vartok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW,
9474+
vartok->setCppcheckAttribute(Token::CppcheckAttributesType::LOW,
94759475
MathLib::toBigNumber(tok->tokAt(1)));
94769476
else if (Token::Match(tok->previous(), "__cppcheck_high__ ( %num% )"))
9477-
vartok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH,
9477+
vartok->setCppcheckAttribute(Token::CppcheckAttributesType::HIGH,
94789478
MathLib::toBigNumber(tok->tokAt(1)));
94799479
}
94809480

@@ -9562,16 +9562,16 @@ void Tokenizer::simplifyCPPAttribute()
95629562
}
95639563
if (argtok && argtok->str() == vartok->str()) {
95649564
if (vartok->strAt(1) == ">=")
9565-
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW,
9565+
argtok->setCppcheckAttribute(Token::CppcheckAttributesType::LOW,
95669566
MathLib::toBigNumber(vartok->tokAt(2)));
95679567
else if (vartok->strAt(1) == ">")
9568-
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::LOW,
9568+
argtok->setCppcheckAttribute(Token::CppcheckAttributesType::LOW,
95699569
MathLib::toBigNumber(vartok->tokAt(2)) + 1);
95709570
else if (vartok->strAt(1) == "<=")
9571-
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH,
9571+
argtok->setCppcheckAttribute(Token::CppcheckAttributesType::HIGH,
95729572
MathLib::toBigNumber(vartok->tokAt(2)));
95739573
else if (vartok->strAt(1) == "<")
9574-
argtok->setCppcheckAttribute(TokenImpl::CppcheckAttributes::Type::HIGH,
9574+
argtok->setCppcheckAttribute(Token::CppcheckAttributesType::HIGH,
95759575
MathLib::toBigNumber(vartok->tokAt(2)) - 1);
95769576
}
95779577
}

lib/tokenlist.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,9 @@ static Token * findCppTypeInitPar(Token *tok)
559559
static bool iscpp11init_impl(const Token * tok);
560560
static bool iscpp11init(const Token * const tok)
561561
{
562-
if (tok->isCpp11init() == TokenImpl::Cpp11init::UNKNOWN)
562+
if (tok->isCpp11init() == Token::Cpp11init::UNKNOWN)
563563
tok->setCpp11init(iscpp11init_impl(tok));
564-
return tok->isCpp11init() == TokenImpl::Cpp11init::CPP11INIT;
564+
return tok->isCpp11init() == Token::Cpp11init::CPP11INIT;
565565
}
566566

567567
static bool iscpp11init_impl(const Token * const tok)
@@ -570,8 +570,8 @@ static bool iscpp11init_impl(const Token * const tok)
570570
return false;
571571
const Token *nameToken = tok;
572572
while (nameToken && nameToken->str() == "{") {
573-
if (nameToken->isCpp11init() != TokenImpl::Cpp11init::UNKNOWN)
574-
return nameToken->isCpp11init() == TokenImpl::Cpp11init::CPP11INIT;
573+
if (nameToken->isCpp11init() != Token::Cpp11init::UNKNOWN)
574+
return nameToken->isCpp11init() == Token::Cpp11init::CPP11INIT;
575575
nameToken = nameToken->previous();
576576
if (nameToken && nameToken->str() == "," && Token::simpleMatch(nameToken->previous(), "} ,"))
577577
nameToken = nameToken->linkAt(-1);

lib/valueflow.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7158,8 +7158,8 @@ static void valueFlowSafeFunctions(const TokenList& tokenlist, const SymbolDatab
71587158
}
71597159

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

71647164
if (!isLow && !isHigh && !all)
71657165
continue;

0 commit comments

Comments
 (0)