From beea7fa8d237c86026cde9446f36bab268476576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 21 Jun 2019 22:16:23 +0200 Subject: [PATCH] Token::index(): Created Token member that indicates the Token position in the token list. It can be used to quickly check if tok1 precedes tok2. --- lib/astutils.cpp | 2 +- lib/token.cpp | 7 +++++++ lib/token.h | 14 +++++++++++++- lib/tokenize.cpp | 2 +- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 3912819b0..2b6a6e246 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -224,7 +224,7 @@ bool precedes(const Token * tok1, const Token * tok2) return false; if (!tok2) return false; - return tok1->progressValue() < tok2->progressValue(); + return tok1->index() < tok2->index(); } static bool isAliased(const Token * startTok, const Token * endTok, unsigned int varid) diff --git a/lib/token.cpp b/lib/token.cpp index 18206b0b9..0cb86a52c 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1742,6 +1742,13 @@ void Token::assignProgressValues(Token *tok) tok2->mImpl->mProgressValue = count++ * 100 / total_count; } +void Token::assignIndexes() +{ + unsigned int index = (mPrevious ? mPrevious->mImpl->mIndex : 0) + 1; + for (Token *tok = this; tok; tok = tok->next()) + tok->mImpl->mIndex = index++; +} + void Token::setValueType(ValueType *vt) { if (vt != mImpl->mValueType) { diff --git a/lib/token.h b/lib/token.h index 822726a2c..ef290dcb8 100644 --- a/lib/token.h +++ b/lib/token.h @@ -76,6 +76,11 @@ struct TokenImpl { */ unsigned int mProgressValue; + /** + * Token index. Position in token list + */ + unsigned int mIndex; + // original name like size_t std::string* mOriginalName; @@ -103,6 +108,7 @@ struct TokenImpl { , mScope(nullptr) , mFunction(nullptr) // Initialize whole union , mProgressValue(0) + , mIndex(0) , mOriginalName(nullptr) , mValueType(nullptr) , mValues(nullptr) @@ -853,7 +859,7 @@ public: */ static void move(Token *srcStart, Token *srcEnd, Token *newLocation); - /** Get progressValue */ + /** Get progressValue (0 - 100) */ unsigned int progressValue() const { return mImpl->mProgressValue; } @@ -987,6 +993,12 @@ public: mImpl->mValues->remove_if(pred); } + unsigned int index() const { + return mImpl->mIndex; + } + + void assignIndexes(); + private: void next(Token *nextToken) { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 0ffb28d14..cf199aa89 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4522,7 +4522,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[]) validate(); - Token::assignProgressValues(list.front()); + list.front()->assignIndexes(); return true; }