Skip to content

Commit 07f90be

Browse files
committed
Token: only look up standard types in update_property_info() if necessary
1 parent 3cee371 commit 07f90be

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

lib/token.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,18 @@ void Token::update_property_info()
127127
tokType(eVariable);
128128
else if (mTokensFrontBack.list.isKeyword(mStr)) {
129129
tokType(eKeyword);
130+
update_property_isStandardType();
130131
setFlag(fIsControlFlowKeyword, controlFlowKeywords.find(mStr) != controlFlowKeywords.end());
131132
}
132133
else if (mStr == "asm") { // TODO: not a keyword
133134
tokType(eKeyword);
134135
}
135-
else if (mTokType != eVariable && mTokType != eFunction && mTokType != eType && mTokType != eKeyword)
136+
// TODO: remove condition? appears to be (no longer necessary) protection for reset of varids in Tokenizer::setVarId()
137+
else if (mTokType != eVariable && mTokType != eFunction && mTokType != eType && mTokType != eKeyword) {
136138
tokType(eName);
139+
// some types are not being treated as keywords
140+
update_property_isStandardType();
141+
}
137142
} else if (simplecpp::Token::isNumberLike(mStr)) {
138143
if ((MathLib::isInt(mStr) || MathLib::isFloat(mStr)) && mStr.find('_') == std::string::npos)
139144
tokType(eNumber);
@@ -174,8 +179,6 @@ void Token::update_property_info()
174179
tokType(eEllipsis);
175180
else
176181
tokType(eOther);
177-
178-
update_property_isStandardType();
179182
} else {
180183
tokType(eNone);
181184
}

lib/token.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,9 @@ class CPPCHECKLIB Token {
952952
return mImpl->mVarId;
953953
}
954954
void varId(nonneg int id) {
955+
if (mImpl->mVarId == id)
956+
return;
957+
955958
mImpl->mVarId = id;
956959
// TODO: remove special handling?
957960
if (id != 0) {

test/testtoken.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class TestToken : public TestFixture {
123123
TEST_CASE(update_property_info_ecomparisonop_link);
124124
TEST_CASE(update_property_info_etype_c);
125125
TEST_CASE(update_property_info_etype_cpp);
126+
TEST_CASE(varid0);
126127
}
127128

128129
void nextprevious() const {
@@ -1494,6 +1495,18 @@ class TestToken : public TestFixture {
14941495
assert_tok(&tok, Token::Type::eType, /*l=*/ false, /*std=*/ true);
14951496
}
14961497
}
1498+
1499+
void varid0() const
1500+
{
1501+
TokenList list_c{&settingsDefault};
1502+
list_c.setLang(Standards::Language::C);
1503+
TokensFrontBack tokensFrontBack(list_c);
1504+
Token tok(tokensFrontBack);
1505+
tok.str("int"); // not treated as keyword in TokenList::isKeyword()
1506+
assert_tok(&tok, Token::Type::eType, /*l=*/ false, /*std=*/ true);
1507+
tok.varId(0);
1508+
assert_tok(&tok, Token::Type::eType, /*l=*/ false, /*std=*/ true);
1509+
}
14971510
#undef assert_tok
14981511
};
14991512

0 commit comments

Comments
 (0)