Skip to content

Commit 77b76aa

Browse files
authored
fixed #13743 - FP AssignmentAddressToInteger for double cast (#7417)
1 parent 43419fe commit 77b76aa

File tree

6 files changed

+58
-23
lines changed

6 files changed

+58
-23
lines changed

lib/token.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ static const std::unordered_set<std::string> controlFlowKeywords = {
107107

108108
void Token::update_property_info()
109109
{
110+
assert(mImpl);
111+
110112
setFlag(fIsControlFlowKeyword, false);
111113
// TODO: clear fIsLong
112114
isStandardType(false);
@@ -134,8 +136,7 @@ void Token::update_property_info()
134136
else if (mStr == "asm") { // TODO: not a keyword
135137
tokType(eKeyword);
136138
}
137-
// TODO: remove condition? appears to be (no longer necessary) protection for reset of varids in Tokenizer::setVarId()
138-
else if (mTokType != eVariable && mTokType != eFunction && mTokType != eType && mTokType != eKeyword) {
139+
else {
139140
tokType(eName);
140141
// some types are not being treated as keywords
141142
update_property_isStandardType();
@@ -183,8 +184,7 @@ void Token::update_property_info()
183184
} else {
184185
tokType(eNone);
185186
}
186-
// TODO: make sure varid is only set for eVariable
187-
//assert(!mImpl->mVarId || mTokType == eVariable);
187+
assert(!mImpl->mVarId || mTokType == eVariable);
188188
// TODO: validate type for linked token?
189189
}
190190

lib/token.h

+4-9
Original file line numberDiff line numberDiff line change
@@ -956,15 +956,7 @@ class CPPCHECKLIB Token {
956956
return;
957957

958958
mImpl->mVarId = id;
959-
// TODO: remove special handling?
960-
if (id != 0) {
961-
tokType(eVariable);
962-
isStandardType(false);
963-
// TODO: clear fIsLong
964-
// TODO: clear fIsControlFlowKeyword
965-
} else {
966-
update_property_info();
967-
}
959+
update_property_info();
968960
}
969961

970962
nonneg int exprId() const {
@@ -1104,6 +1096,9 @@ class CPPCHECKLIB Token {
11041096
* to.
11051097
*/
11061098
void link(Token *linkToToken) {
1099+
if (mLink == linkToToken)
1100+
return;
1101+
11071102
mLink = linkToToken;
11081103
if (mStr == "<" || mStr == ">")
11091104
update_property_info();

lib/tokenize.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -3543,6 +3543,7 @@ void Tokenizer::combineOperators()
35433543
}
35443544
if (simplify) {
35453545
tok->str(tok->str() + ":");
3546+
tok->tokType(Token::Type::eKeyword); // we need to preserve the keyword type after setting a non-keyword string
35463547
tok->deleteNext();
35473548
}
35483549
} else if (tok->str() == "->") {
@@ -10278,6 +10279,8 @@ void Tokenizer::simplifyOperatorName()
1027810279
for (Token *tok = list.front(); tok; tok = tok->next()) {
1027910280
if (Token::Match(tok, "using|:: operator %op%|%name% ;")) {
1028010281
tok->next()->str("operator" + tok->strAt(2));
10282+
tok->next()->tokType(Token::Type::eKeyword); // we need to preserve the keyword type after setting a non-keyword string
10283+
// TODO: tok->next()->isOperatorKeyword(true);
1028110284
tok->next()->deleteNext();
1028210285
continue;
1028310286
}
@@ -10287,6 +10290,8 @@ void Tokenizer::simplifyOperatorName()
1028710290
// operator op
1028810291
if (Token::Match(tok, "operator %op% (") && !operatorEnd(tok->linkAt(2))) {
1028910292
tok->str(tok->str() + tok->strAt(1));
10293+
tok->tokType(Token::Type::eKeyword); // we need to preserve the keyword type after setting a non-keyword string
10294+
// TODO: tok->isOperatorKeyword(true);
1029010295
tok->deleteNext();
1029110296
continue;
1029210297
}
@@ -10359,11 +10364,15 @@ void Tokenizer::simplifyOperatorName()
1035910364
const bool returnsRef = Token::simpleMatch(par, "( & (") && tok->next()->isName();
1036010365
if (par && !op.empty()) {
1036110366
if (returnsRef) {
10362-
par->next()->insertToken("operator" + op)->isOperatorKeyword(true);
10367+
Token* tok_op = par->next()->insertToken("operator" + op);
10368+
// TODO: tok_op->tokType(Token::Type::eKeyword); // the given token is not a keyword but should be treated as such
10369+
tok_op->isOperatorKeyword(true);
1036310370
tok->deleteThis();
1036410371
}
1036510372
else {
1036610373
tok->str("operator" + op);
10374+
tok->tokType(Token::Type::eKeyword); // we need to preserve the keyword type after setting a non-keyword string
10375+
// TODO: tok->isOperatorKeyword(true);
1036710376
Token::eraseTokens(tok, par);
1036810377
}
1036910378
}

test/test64bit.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ class Test64BitPortability : public TestFixture {
4343

4444
#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
4545
template<size_t size>
46-
void check_(const char* file, int line, const char (&code)[size]) {
46+
void check_(const char* file, int line, const char (&code)[size], bool cpp = true) {
4747
// Tokenize..
4848
SimpleTokenizer tokenizer(settings, *this);
49-
ASSERT_LOC(tokenizer.tokenize(code), file, line);
49+
ASSERT_LOC(tokenizer.tokenize(code, cpp), file, line);
5050

5151
// Check char variable usage..
5252
Check64BitPortability check64BitPortability(&tokenizer, &settings, this);
@@ -156,6 +156,12 @@ class Test64BitPortability : public TestFixture {
156156
" return p ? p : 0;\n"
157157
"}\n");
158158
ASSERT_EQUALS("", errout_str());
159+
160+
check("int f(int* p) {\n"
161+
" int n = (int)(size_t)*p;\n"
162+
" return n;\n"
163+
"}\n", false);
164+
ASSERT_EQUALS("", errout_str());
159165
}
160166

161167
void structmember() {

test/testtoken.cpp

+12-7
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ 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(update_property_info_replace); // #13743
127+
126128
TEST_CASE(varid_reset);
127129
}
128130

@@ -1341,13 +1343,6 @@ class TestToken : public TestFixture {
13411343
tok.varId(17);
13421344
assert_tok(&tok, Token::Type::eVariable);
13431345
}
1344-
{
1345-
TokensFrontBack tokensFrontBack(list);
1346-
Token tok(tokensFrontBack);
1347-
tok.varId(17);
1348-
tok.str("var1");
1349-
assert_tok(&tok, Token::Type::eVariable);
1350-
}
13511346
}
13521347

13531348
void update_property_info_ekeyword_c() const
@@ -1496,6 +1491,16 @@ class TestToken : public TestFixture {
14961491
}
14971492
}
14981493

1494+
void update_property_info_replace() const // #13743
1495+
{
1496+
TokensFrontBack tokensFrontBack(list);
1497+
Token tok(tokensFrontBack);
1498+
tok.str("size_t");
1499+
assert_tok(&tok, Token::Type::eType, false, true);
1500+
tok.str("long");
1501+
assert_tok(&tok, Token::Type::eType, false, true);
1502+
}
1503+
14991504
void varid_reset() const
15001505
{
15011506
TokenList list_c{&settingsDefault};

test/testtokenize.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ class TestTokenizer : public TestFixture {
475475

476476
TEST_CASE(atomicCast); // #12605
477477
TEST_CASE(constFunctionPtrTypedef); // #12135
478+
479+
TEST_CASE(simplifyPlatformTypes);
478480
}
479481

480482
#define tokenizeAndStringify(...) tokenizeAndStringify_(__FILE__, __LINE__, __VA_ARGS__)
@@ -8495,6 +8497,24 @@ class TestTokenizer : public TestFixture {
84958497
ASSERT_NO_THROW(tokenizeAndStringify(code));
84968498
ASSERT_EQUALS("void ( * const f ) ( ) ;", tokenizeAndStringify("typedef void (*fp_t)(); fp_t const f;"));
84978499
}
8500+
8501+
void simplifyPlatformTypes() {
8502+
{
8503+
const char code[] = "size_t f();";
8504+
ASSERT_EQUALS("unsigned long f ( ) ;", tokenizeAndStringify(code, true, Platform::Type::Unix32));
8505+
ASSERT_EQUALS("unsigned long f ( ) ;", tokenizeAndStringify(code, true, Platform::Type::Unix64));
8506+
}
8507+
{
8508+
const char code[] = "ssize_t f();";
8509+
ASSERT_EQUALS("long f ( ) ;", tokenizeAndStringify(code, true, Platform::Type::Unix32));
8510+
ASSERT_EQUALS("long f ( ) ;", tokenizeAndStringify(code, true, Platform::Type::Unix64));
8511+
}
8512+
{
8513+
const char code[] = "std::ptrdiff_t f();";
8514+
ASSERT_EQUALS("long f ( ) ;", tokenizeAndStringify(code, true, Platform::Type::Unix32));
8515+
ASSERT_EQUALS("long f ( ) ;", tokenizeAndStringify(code, true, Platform::Type::Unix64));
8516+
}
8517+
}
84988518
};
84998519

85008520
REGISTER_TEST(TestTokenizer)

0 commit comments

Comments
 (0)