Skip to content

Commit e4fbae2

Browse files
authored
TokenList: made TokensFrontBack instance a shared pointer (#7470)
1 parent 3b93a89 commit e4fbae2

File tree

6 files changed

+223
-217
lines changed

6 files changed

+223
-217
lines changed

lib/token.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ namespace {
6060
const std::list<ValueFlow::Value> TokenImpl::mEmptyValueList;
6161
const std::string Token::mEmptyString;
6262

63-
Token::Token(const TokenList& tokenlist, TokensFrontBack &tokensFrontBack)
63+
Token::Token(const TokenList& tokenlist, std::shared_ptr<TokensFrontBack> tokensFrontBack)
6464
: mList(tokenlist)
65-
, mTokensFrontBack(tokensFrontBack)
65+
, mTokensFrontBack(std::move(tokensFrontBack))
6666
, mIsC(mList.isC())
6767
, mIsCpp(mList.isCPP())
6868
{
@@ -286,7 +286,7 @@ void Token::deleteNext(nonneg int count)
286286
if (mNext)
287287
mNext->previous(this);
288288
else
289-
mTokensFrontBack.back = this;
289+
mTokensFrontBack->back = this;
290290
}
291291

292292
void Token::deletePrevious(nonneg int count)
@@ -306,7 +306,7 @@ void Token::deletePrevious(nonneg int count)
306306
if (mPrevious)
307307
mPrevious->next(this);
308308
else
309-
mTokensFrontBack.front = this;
309+
mTokensFrontBack->front = this;
310310
}
311311

312312
void Token::swapWithNext()
@@ -389,10 +389,10 @@ void Token::replace(Token *replaceThis, Token *start, Token *end)
389389
start->previous(replaceThis->previous());
390390
end->next(replaceThis->next());
391391

392-
if (end->mTokensFrontBack.back == end) {
392+
if (end->mTokensFrontBack->back == end) {
393393
while (end->next())
394394
end = end->next();
395-
end->mTokensFrontBack.back = end;
395+
end->mTokensFrontBack->back = end;
396396
}
397397

398398
// Update mProgressValue, fileIndex and linenr
@@ -1083,7 +1083,7 @@ Token* Token::insertToken(const std::string& tokenStr, const std::string& origin
10831083
newToken->previous(this->previous());
10841084
newToken->previous()->next(newToken);
10851085
} else {
1086-
mTokensFrontBack.front = newToken;
1086+
mTokensFrontBack->front = newToken;
10871087
}
10881088
this->previous(newToken);
10891089
newToken->next(this);
@@ -1092,7 +1092,7 @@ Token* Token::insertToken(const std::string& tokenStr, const std::string& origin
10921092
newToken->next(this->next());
10931093
newToken->next()->previous(newToken);
10941094
} else {
1095-
mTokensFrontBack.back = newToken;
1095+
mTokensFrontBack->back = newToken;
10961096
}
10971097
this->next(newToken);
10981098
newToken->previous(this);

lib/token.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class CPPCHECKLIB Token {
167167

168168
private:
169169
const TokenList& mList;
170-
TokensFrontBack& mTokensFrontBack;
170+
std::shared_ptr<TokensFrontBack> mTokensFrontBack;
171171

172172
static const std::string mEmptyString;
173173

@@ -186,7 +186,7 @@ class CPPCHECKLIB Token {
186186
eNone
187187
};
188188

189-
Token(const TokenList& tokenlist, TokensFrontBack &tokensFrontBack);
189+
Token(const TokenList& tokenlist, std::shared_ptr<TokensFrontBack> tokensFrontBack);
190190
// for usage in CheckIO::ArgumentInfo only
191191
explicit Token(const Token *tok);
192192
~Token();

lib/tokenlist.cpp

+67-65
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static constexpr int AST_MAX_DEPTH = 150;
6161

6262

6363
TokenList::TokenList(const Settings* settings)
64-
: mTokensFrontBack()
64+
: mTokensFrontBack(new TokensFrontBack)
6565
, mSettings(settings)
6666
{
6767
if (mSettings && (mSettings->enforcedLang != Standards::Language::None)) {
@@ -90,9 +90,11 @@ const std::string& TokenList::getSourceFilePath() const
9090
// Deallocate lists..
9191
void TokenList::deallocateTokens()
9292
{
93-
deleteTokens(mTokensFrontBack.front);
94-
mTokensFrontBack.front = nullptr;
95-
mTokensFrontBack.back = nullptr;
93+
if (mTokensFrontBack) {
94+
deleteTokens(mTokensFrontBack->front);
95+
mTokensFrontBack->front = nullptr;
96+
mTokensFrontBack->back = nullptr;
97+
}
9698
mFiles.clear();
9799
}
98100

@@ -171,100 +173,100 @@ void TokenList::addtoken(const std::string& str, const nonneg int lineno, const
171173
}
172174
}
173175

174-
if (mTokensFrontBack.back) {
175-
mTokensFrontBack.back->insertToken(str);
176+
if (mTokensFrontBack->back) {
177+
mTokensFrontBack->back->insertToken(str);
176178
} else {
177-
mTokensFrontBack.front = new Token(*this, mTokensFrontBack);
178-
mTokensFrontBack.back = mTokensFrontBack.front;
179-
mTokensFrontBack.back->str(str);
179+
mTokensFrontBack->front = new Token(*this, mTokensFrontBack);
180+
mTokensFrontBack->back = mTokensFrontBack->front;
181+
mTokensFrontBack->back->str(str);
180182
}
181183

182-
mTokensFrontBack.back->linenr(lineno);
183-
mTokensFrontBack.back->column(column);
184-
mTokensFrontBack.back->fileIndex(fileno);
184+
mTokensFrontBack->back->linenr(lineno);
185+
mTokensFrontBack->back->column(column);
186+
mTokensFrontBack->back->fileIndex(fileno);
185187
}
186188

187189
void TokenList::addtoken(const std::string& str, const Token *locationTok)
188190
{
189191
if (str.empty())
190192
return;
191193

192-
if (mTokensFrontBack.back) {
193-
mTokensFrontBack.back->insertToken(str);
194+
if (mTokensFrontBack->back) {
195+
mTokensFrontBack->back->insertToken(str);
194196
} else {
195-
mTokensFrontBack.front = new Token(*this, mTokensFrontBack);
196-
mTokensFrontBack.back = mTokensFrontBack.front;
197-
mTokensFrontBack.back->str(str);
197+
mTokensFrontBack->front = new Token(*this, mTokensFrontBack);
198+
mTokensFrontBack->back = mTokensFrontBack->front;
199+
mTokensFrontBack->back->str(str);
198200
}
199201

200-
mTokensFrontBack.back->linenr(locationTok->linenr());
201-
mTokensFrontBack.back->column(locationTok->column());
202-
mTokensFrontBack.back->fileIndex(locationTok->fileIndex());
202+
mTokensFrontBack->back->linenr(locationTok->linenr());
203+
mTokensFrontBack->back->column(locationTok->column());
204+
mTokensFrontBack->back->fileIndex(locationTok->fileIndex());
203205
}
204206

205207
void TokenList::addtoken(const Token * tok, const nonneg int lineno, const nonneg int column, const nonneg int fileno)
206208
{
207209
if (tok == nullptr)
208210
return;
209211

210-
if (mTokensFrontBack.back) {
211-
mTokensFrontBack.back->insertToken(tok->str(), tok->originalName());
212+
if (mTokensFrontBack->back) {
213+
mTokensFrontBack->back->insertToken(tok->str(), tok->originalName());
212214
} else {
213-
mTokensFrontBack.front = new Token(*this, mTokensFrontBack);
214-
mTokensFrontBack.back = mTokensFrontBack.front;
215-
mTokensFrontBack.back->str(tok->str());
215+
mTokensFrontBack->front = new Token(*this, mTokensFrontBack);
216+
mTokensFrontBack->back = mTokensFrontBack->front;
217+
mTokensFrontBack->back->str(tok->str());
216218
if (!tok->originalName().empty())
217-
mTokensFrontBack.back->originalName(tok->originalName());
219+
mTokensFrontBack->back->originalName(tok->originalName());
218220
}
219221

220-
mTokensFrontBack.back->linenr(lineno);
221-
mTokensFrontBack.back->column(column);
222-
mTokensFrontBack.back->fileIndex(fileno);
223-
mTokensFrontBack.back->flags(tok->flags());
222+
mTokensFrontBack->back->linenr(lineno);
223+
mTokensFrontBack->back->column(column);
224+
mTokensFrontBack->back->fileIndex(fileno);
225+
mTokensFrontBack->back->flags(tok->flags());
224226
}
225227

226228
void TokenList::addtoken(const Token *tok, const Token *locationTok)
227229
{
228230
if (tok == nullptr || locationTok == nullptr)
229231
return;
230232

231-
if (mTokensFrontBack.back) {
232-
mTokensFrontBack.back->insertToken(tok->str(), tok->originalName());
233+
if (mTokensFrontBack->back) {
234+
mTokensFrontBack->back->insertToken(tok->str(), tok->originalName());
233235
} else {
234-
mTokensFrontBack.front = new Token(*this, mTokensFrontBack);
235-
mTokensFrontBack.back = mTokensFrontBack.front;
236-
mTokensFrontBack.back->str(tok->str());
236+
mTokensFrontBack->front = new Token(*this, mTokensFrontBack);
237+
mTokensFrontBack->back = mTokensFrontBack->front;
238+
mTokensFrontBack->back->str(tok->str());
237239
if (!tok->originalName().empty())
238-
mTokensFrontBack.back->originalName(tok->originalName());
240+
mTokensFrontBack->back->originalName(tok->originalName());
239241
}
240242

241-
mTokensFrontBack.back->flags(tok->flags());
242-
mTokensFrontBack.back->linenr(locationTok->linenr());
243-
mTokensFrontBack.back->column(locationTok->column());
244-
mTokensFrontBack.back->fileIndex(locationTok->fileIndex());
243+
mTokensFrontBack->back->flags(tok->flags());
244+
mTokensFrontBack->back->linenr(locationTok->linenr());
245+
mTokensFrontBack->back->column(locationTok->column());
246+
mTokensFrontBack->back->fileIndex(locationTok->fileIndex());
245247
}
246248

247249
void TokenList::addtoken(const Token *tok)
248250
{
249251
if (tok == nullptr)
250252
return;
251253

252-
if (mTokensFrontBack.back) {
253-
mTokensFrontBack.back->insertToken(tok->str(), tok->originalName(), tok->getMacroName());
254+
if (mTokensFrontBack->back) {
255+
mTokensFrontBack->back->insertToken(tok->str(), tok->originalName(), tok->getMacroName());
254256
} else {
255-
mTokensFrontBack.front = new Token(*this, mTokensFrontBack);
256-
mTokensFrontBack.back = mTokensFrontBack.front;
257-
mTokensFrontBack.back->str(tok->str());
257+
mTokensFrontBack->front = new Token(*this, mTokensFrontBack);
258+
mTokensFrontBack->back = mTokensFrontBack->front;
259+
mTokensFrontBack->back->str(tok->str());
258260
if (!tok->originalName().empty())
259-
mTokensFrontBack.back->originalName(tok->originalName());
261+
mTokensFrontBack->back->originalName(tok->originalName());
260262
if (!tok->getMacroName().empty())
261-
mTokensFrontBack.back->setMacroName(tok->getMacroName());
263+
mTokensFrontBack->back->setMacroName(tok->getMacroName());
262264
}
263265

264-
mTokensFrontBack.back->flags(tok->flags());
265-
mTokensFrontBack.back->linenr(tok->linenr());
266-
mTokensFrontBack.back->column(tok->column());
267-
mTokensFrontBack.back->fileIndex(tok->fileIndex());
266+
mTokensFrontBack->back->flags(tok->flags());
267+
mTokensFrontBack->back->linenr(tok->linenr());
268+
mTokensFrontBack->back->column(tok->column());
269+
mTokensFrontBack->back->fileIndex(tok->fileIndex());
268270
}
269271

270272

@@ -395,19 +397,19 @@ void TokenList::createTokens(simplecpp::TokenList&& tokenList)
395397
if (str.size() > 1 && str[0] == '.' && std::isdigit(str[1]))
396398
str = '0' + str;
397399

398-
if (mTokensFrontBack.back) {
399-
mTokensFrontBack.back->insertToken(str);
400+
if (mTokensFrontBack->back) {
401+
mTokensFrontBack->back->insertToken(str);
400402
} else {
401-
mTokensFrontBack.front = new Token(*this, mTokensFrontBack);
402-
mTokensFrontBack.back = mTokensFrontBack.front;
403-
mTokensFrontBack.back->str(str);
403+
mTokensFrontBack->front = new Token(*this, mTokensFrontBack);
404+
mTokensFrontBack->back = mTokensFrontBack->front;
405+
mTokensFrontBack->back->str(str);
404406
}
405407

406-
mTokensFrontBack.back->fileIndex(tok->location.fileIndex);
407-
mTokensFrontBack.back->linenr(tok->location.line);
408-
mTokensFrontBack.back->column(tok->location.col);
408+
mTokensFrontBack->back->fileIndex(tok->location.fileIndex);
409+
mTokensFrontBack->back->linenr(tok->location.line);
410+
mTokensFrontBack->back->column(tok->location.col);
409411
if (!tok->macro.empty())
410-
mTokensFrontBack.back->setMacroName(tok->macro);
412+
mTokensFrontBack->back->setMacroName(tok->macro);
411413

412414
tok = tok->next;
413415
if (tok)
@@ -419,7 +421,7 @@ void TokenList::createTokens(simplecpp::TokenList&& tokenList)
419421
mFile = Path::getRelativePath(mFile, mSettings->basePaths);
420422
}
421423

422-
Token::assignProgressValues(mTokensFrontBack.front);
424+
Token::assignProgressValues(mTokensFrontBack->front);
423425
}
424426

425427
//---------------------------------------------------------------------------
@@ -1849,7 +1851,7 @@ static Token * createAstAtToken(Token *tok)
18491851

18501852
void TokenList::createAst() const
18511853
{
1852-
for (Token *tok = mTokensFrontBack.front; tok; tok = tok ? tok->next() : nullptr) {
1854+
for (Token *tok = mTokensFrontBack->front; tok; tok = tok ? tok->next() : nullptr) {
18531855
Token* const nextTok = createAstAtToken(tok);
18541856
if (precedes(nextTok, tok))
18551857
throw InternalError(tok, "Syntax Error: Infinite loop when creating AST.", InternalError::AST);
@@ -1874,11 +1876,11 @@ void TokenList::validateAst(bool print) const
18741876
{
18751877
OnException oe{[&] {
18761878
if (print)
1877-
mTokensFrontBack.front->printOut(std::cout);
1879+
mTokensFrontBack->front->printOut(std::cout);
18781880
}};
18791881
// Check for some known issues in AST to avoid crash/hang later on
18801882
std::set<const Token*> safeAstTokens; // list of "safe" AST tokens without endless recursion
1881-
for (const Token *tok = mTokensFrontBack.front; tok; tok = tok->next()) {
1883+
for (const Token *tok = mTokensFrontBack->front; tok; tok = tok->next()) {
18821884
// Syntax error if binary operator only has 1 operand
18831885
if ((tok->isAssignmentOp() || tok->isComparisonOp() || Token::Match(tok,"[|^/%]")) && tok->astOperand1() && !tok->astOperand2())
18841886
throw InternalError(tok, "Syntax Error: AST broken, binary operator has only one operand.", InternalError::AST);
@@ -2003,7 +2005,7 @@ bool TokenList::validateToken(const Token* tok) const
20032005
{
20042006
if (!tok)
20052007
return true;
2006-
for (const Token *t = mTokensFrontBack.front; t; t = t->next()) {
2008+
for (const Token *t = mTokensFrontBack->front; t; t = t->next()) {
20072009
if (tok==t)
20082010
return true;
20092011
}

lib/tokenlist.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626

2727
#include <cstddef>
2828
#include <iosfwd>
29+
#include <memory>
2930
#include <string>
3031
#include <vector>
3132

3233
class Token;
33-
class TokenList;
3434
class Settings;
3535

3636
namespace simplecpp {
@@ -115,20 +115,20 @@ class CPPCHECKLIB TokenList {
115115

116116
/** get first token of list */
117117
const Token *front() const {
118-
return mTokensFrontBack.front;
118+
return mTokensFrontBack->front;
119119
}
120120
// NOLINTNEXTLINE(readability-make-member-function-const) - do not allow usage of mutable pointer from const object
121121
Token *front() {
122-
return mTokensFrontBack.front;
122+
return mTokensFrontBack->front;
123123
}
124124

125125
/** get last token of list */
126126
const Token *back() const {
127-
return mTokensFrontBack.back;
127+
return mTokensFrontBack->back;
128128
}
129129
// NOLINTNEXTLINE(readability-make-member-function-const) - do not allow usage of mutable pointer from const object
130130
Token *back() {
131-
return mTokensFrontBack.back;
131+
return mTokensFrontBack->back;
132132
}
133133

134134
/**
@@ -212,7 +212,7 @@ class CPPCHECKLIB TokenList {
212212
bool createTokensInternal(std::istream &code, const std::string& file0);
213213

214214
/** Token list */
215-
TokensFrontBack mTokensFrontBack;
215+
std::shared_ptr<TokensFrontBack> mTokensFrontBack;
216216

217217
/** filenames for the tokenized source code (source + included) */
218218
std::vector<std::string> mFiles;

0 commit comments

Comments
 (0)