Skip to content

Commit beea7fa

Browse files
committed
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.
1 parent d636a83 commit beea7fa

4 files changed

Lines changed: 22 additions & 3 deletions

File tree

lib/astutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ bool precedes(const Token * tok1, const Token * tok2)
224224
return false;
225225
if (!tok2)
226226
return false;
227-
return tok1->progressValue() < tok2->progressValue();
227+
return tok1->index() < tok2->index();
228228
}
229229

230230
static bool isAliased(const Token * startTok, const Token * endTok, unsigned int varid)

lib/token.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,6 +1742,13 @@ void Token::assignProgressValues(Token *tok)
17421742
tok2->mImpl->mProgressValue = count++ * 100 / total_count;
17431743
}
17441744

1745+
void Token::assignIndexes()
1746+
{
1747+
unsigned int index = (mPrevious ? mPrevious->mImpl->mIndex : 0) + 1;
1748+
for (Token *tok = this; tok; tok = tok->next())
1749+
tok->mImpl->mIndex = index++;
1750+
}
1751+
17451752
void Token::setValueType(ValueType *vt)
17461753
{
17471754
if (vt != mImpl->mValueType) {

lib/token.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ struct TokenImpl {
7676
*/
7777
unsigned int mProgressValue;
7878

79+
/**
80+
* Token index. Position in token list
81+
*/
82+
unsigned int mIndex;
83+
7984
// original name like size_t
8085
std::string* mOriginalName;
8186

@@ -103,6 +108,7 @@ struct TokenImpl {
103108
, mScope(nullptr)
104109
, mFunction(nullptr) // Initialize whole union
105110
, mProgressValue(0)
111+
, mIndex(0)
106112
, mOriginalName(nullptr)
107113
, mValueType(nullptr)
108114
, mValues(nullptr)
@@ -853,7 +859,7 @@ class CPPCHECKLIB Token {
853859
*/
854860
static void move(Token *srcStart, Token *srcEnd, Token *newLocation);
855861

856-
/** Get progressValue */
862+
/** Get progressValue (0 - 100) */
857863
unsigned int progressValue() const {
858864
return mImpl->mProgressValue;
859865
}
@@ -987,6 +993,12 @@ class CPPCHECKLIB Token {
987993
mImpl->mValues->remove_if(pred);
988994
}
989995

996+
unsigned int index() const {
997+
return mImpl->mIndex;
998+
}
999+
1000+
void assignIndexes();
1001+
9901002
private:
9911003

9921004
void next(Token *nextToken) {

lib/tokenize.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4522,7 +4522,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
45224522

45234523
validate();
45244524

4525-
Token::assignProgressValues(list.front());
4525+
list.front()->assignIndexes();
45264526

45274527
return true;
45284528
}

0 commit comments

Comments
 (0)