Skip to content

fixed #13743 - FP AssignmentAddressToInteger for double cast #7417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ static const std::unordered_set<std::string> controlFlowKeywords = {

void Token::update_property_info()
{
assert(mImpl);

setFlag(fIsControlFlowKeyword, false);
// TODO: clear fIsLong
isStandardType(false);
Expand Down Expand Up @@ -134,8 +136,7 @@ void Token::update_property_info()
else if (mStr == "asm") { // TODO: not a keyword
tokType(eKeyword);
}
// TODO: remove condition? appears to be (no longer necessary) protection for reset of varids in Tokenizer::setVarId()
else if (mTokType != eVariable && mTokType != eFunction && mTokType != eType && mTokType != eKeyword) {
else {
tokType(eName);
// some types are not being treated as keywords
update_property_isStandardType();
Expand Down Expand Up @@ -183,8 +184,7 @@ void Token::update_property_info()
} else {
tokType(eNone);
}
// TODO: make sure varid is only set for eVariable
//assert(!mImpl->mVarId || mTokType == eVariable);
assert(!mImpl->mVarId || mTokType == eVariable);
// TODO: validate type for linked token?
}

Expand Down
13 changes: 4 additions & 9 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -956,15 +956,7 @@ class CPPCHECKLIB Token {
return;

mImpl->mVarId = id;
// TODO: remove special handling?
if (id != 0) {
tokType(eVariable);
isStandardType(false);
// TODO: clear fIsLong
// TODO: clear fIsControlFlowKeyword
} else {
update_property_info();
}
update_property_info();
}

nonneg int exprId() const {
Expand Down Expand Up @@ -1104,6 +1096,9 @@ class CPPCHECKLIB Token {
* to.
*/
void link(Token *linkToToken) {
if (mLink == linkToToken)
return;

mLink = linkToToken;
if (mStr == "<" || mStr == ">")
update_property_info();
Expand Down
11 changes: 10 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3543,6 +3543,7 @@ void Tokenizer::combineOperators()
}
if (simplify) {
tok->str(tok->str() + ":");
tok->tokType(Token::Type::eKeyword); // we need to preserve the keyword type after setting a non-keyword string
tok->deleteNext();
}
} else if (tok->str() == "->") {
Expand Down Expand Up @@ -10278,6 +10279,8 @@ void Tokenizer::simplifyOperatorName()
for (Token *tok = list.front(); tok; tok = tok->next()) {
if (Token::Match(tok, "using|:: operator %op%|%name% ;")) {
tok->next()->str("operator" + tok->strAt(2));
tok->next()->tokType(Token::Type::eKeyword); // we need to preserve the keyword type after setting a non-keyword string
// TODO: tok->next()->isOperatorKeyword(true);
tok->next()->deleteNext();
continue;
}
Expand All @@ -10287,6 +10290,8 @@ void Tokenizer::simplifyOperatorName()
// operator op
if (Token::Match(tok, "operator %op% (") && !operatorEnd(tok->linkAt(2))) {
tok->str(tok->str() + tok->strAt(1));
tok->tokType(Token::Type::eKeyword); // we need to preserve the keyword type after setting a non-keyword string
// TODO: tok->isOperatorKeyword(true);
tok->deleteNext();
continue;
}
Expand Down Expand Up @@ -10359,11 +10364,15 @@ void Tokenizer::simplifyOperatorName()
const bool returnsRef = Token::simpleMatch(par, "( & (") && tok->next()->isName();
if (par && !op.empty()) {
if (returnsRef) {
par->next()->insertToken("operator" + op)->isOperatorKeyword(true);
Token* tok_op = par->next()->insertToken("operator" + op);
// TODO: tok_op->tokType(Token::Type::eKeyword); // the given token is not a keyword but should be treated as such
tok_op->isOperatorKeyword(true);
tok->deleteThis();
}
else {
tok->str("operator" + op);
tok->tokType(Token::Type::eKeyword); // we need to preserve the keyword type after setting a non-keyword string
// TODO: tok->isOperatorKeyword(true);
Token::eraseTokens(tok, par);
}
}
Expand Down
10 changes: 8 additions & 2 deletions test/test64bit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class Test64BitPortability : public TestFixture {

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

// Check char variable usage..
Check64BitPortability check64BitPortability(&tokenizer, &settings, this);
Expand Down Expand Up @@ -156,6 +156,12 @@ class Test64BitPortability : public TestFixture {
" return p ? p : 0;\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("int f(int* p) {\n"
" int n = (int)(size_t)*p;\n"
" return n;\n"
"}\n", false);
ASSERT_EQUALS("", errout_str());
}

void structmember() {
Expand Down
19 changes: 12 additions & 7 deletions test/testtoken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class TestToken : public TestFixture {
TEST_CASE(update_property_info_ecomparisonop_link);
TEST_CASE(update_property_info_etype_c);
TEST_CASE(update_property_info_etype_cpp);
TEST_CASE(update_property_info_replace); // #13743

TEST_CASE(varid_reset);
}

Expand Down Expand Up @@ -1341,13 +1343,6 @@ class TestToken : public TestFixture {
tok.varId(17);
assert_tok(&tok, Token::Type::eVariable);
}
{
TokensFrontBack tokensFrontBack(list);
Token tok(tokensFrontBack);
tok.varId(17);
tok.str("var1");
Comment on lines -1347 to -1348
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str() clears the varid so this test was pointless.

assert_tok(&tok, Token::Type::eVariable);
}
}

void update_property_info_ekeyword_c() const
Expand Down Expand Up @@ -1496,6 +1491,16 @@ class TestToken : public TestFixture {
}
}

void update_property_info_replace() const // #13743
{
TokensFrontBack tokensFrontBack(list);
Token tok(tokensFrontBack);
tok.str("size_t");
assert_tok(&tok, Token::Type::eType, false, true);
tok.str("long");
assert_tok(&tok, Token::Type::eType, false, true);
}

void varid_reset() const
{
TokenList list_c{&settingsDefault};
Expand Down
20 changes: 20 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,8 @@ class TestTokenizer : public TestFixture {

TEST_CASE(atomicCast); // #12605
TEST_CASE(constFunctionPtrTypedef); // #12135

TEST_CASE(simplifyPlatformTypes);
}

#define tokenizeAndStringify(...) tokenizeAndStringify_(__FILE__, __LINE__, __VA_ARGS__)
Expand Down Expand Up @@ -8495,6 +8497,24 @@ class TestTokenizer : public TestFixture {
ASSERT_NO_THROW(tokenizeAndStringify(code));
ASSERT_EQUALS("void ( * const f ) ( ) ;", tokenizeAndStringify("typedef void (*fp_t)(); fp_t const f;"));
}

void simplifyPlatformTypes() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these tests go here? #7416

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They used to be there but I included them here to have more coverage of the various parts of the code which triggered the issue.

{
const char code[] = "size_t f();";
ASSERT_EQUALS("unsigned long f ( ) ;", tokenizeAndStringify(code, true, Platform::Type::Unix32));
ASSERT_EQUALS("unsigned long f ( ) ;", tokenizeAndStringify(code, true, Platform::Type::Unix64));
}
{
const char code[] = "ssize_t f();";
ASSERT_EQUALS("long f ( ) ;", tokenizeAndStringify(code, true, Platform::Type::Unix32));
ASSERT_EQUALS("long f ( ) ;", tokenizeAndStringify(code, true, Platform::Type::Unix64));
}
{
const char code[] = "std::ptrdiff_t f();";
ASSERT_EQUALS("long f ( ) ;", tokenizeAndStringify(code, true, Platform::Type::Unix32));
ASSERT_EQUALS("long f ( ) ;", tokenizeAndStringify(code, true, Platform::Type::Unix64));
}
}
};

REGISTER_TEST(TestTokenizer)
Expand Down
Loading