Skip to content

Commit 5216f90

Browse files
committed
Refactoring Check64BitPortability. Use ValueType.
1 parent 7f1b43e commit 5216f90

2 files changed

Lines changed: 37 additions & 59 deletions

File tree

‎lib/check64bit.cpp‎

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,6 @@ namespace {
3030
Check64BitPortability instance;
3131
}
3232

33-
/** Is given variable a pointer or array? */
34-
static bool isaddr(const Variable *var)
35-
{
36-
return (var && (var->isPointer() || var->isArray()));
37-
}
38-
39-
/** Is given variable an integer variable */
40-
static bool isint(const Variable *var)
41-
{
42-
return (var && var->isIntegralType() && !var->isArrayOrPointer() && var->typeStartToken()->str() != "bool");
43-
}
44-
4533
void Check64BitPortability::pointerassignment()
4634
{
4735
if (!_settings->isEnabled("portability"))
@@ -65,59 +53,49 @@ void Check64BitPortability::pointerassignment()
6553
continue;
6654

6755
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
68-
if (Token::Match(tok, "return %name%|%num% [;+]") && !Token::simpleMatch(tok, "return 0 ;")) {
69-
enum { NO, INT, PTR, PTRDIFF } type = NO;
70-
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) {
71-
if ((type == NO || type == INT) && Token::Match(tok2, "%var% [+;]") && isaddr(tok2->variable()))
72-
type = PTR;
73-
else if (type == NO && (tok2->isNumber() || isint(tok2->variable())))
74-
type = INT;
75-
else if (type == PTR && Token::Match(tok2, "- %var%") && isaddr(tok2->next()->variable()))
76-
type = PTRDIFF;
77-
else if (tok2->str() == "(") {
78-
// TODO: handle parentheses
79-
type = NO;
80-
break;
81-
} else if (type == PTR && Token::simpleMatch(tok2, "."))
82-
type = NO; // Reset after pointer reference, see #4642
83-
else if (tok2->str() == ";")
84-
break;
85-
}
86-
87-
if (retPointer && (type == INT || type == PTRDIFF))
88-
returnIntegerError(tok);
89-
else if (!retPointer && type == PTR)
90-
returnPointerError(tok);
91-
}
56+
if (tok->str() != "return")
57+
continue;
58+
59+
if (!tok->astOperand1() || tok->astOperand1()->isNumber())
60+
continue;
61+
62+
const ValueType * const returnType = tok->astOperand1()->valueType();
63+
if (!returnType)
64+
continue;
65+
66+
if (retPointer && returnType->pointer == 0U)
67+
returnIntegerError(tok);
68+
69+
if (!retPointer && returnType->pointer >= 1U)
70+
returnPointerError(tok);
9271
}
9372
}
9473

9574
// Check assignments
9675
for (std::size_t i = 0; i < functions; ++i) {
9776
const Scope * scope = symbolDatabase->functionScopes[i];
9877
for (const Token *tok = scope->classStart; tok && tok != scope->classEnd; tok = tok->next()) {
99-
if (Token::Match(tok, "[;{}] %var% = %name%")) {
100-
const Token* tok2 = tok->tokAt(3);
101-
while (Token::Match(tok2->next(), ".|::"))
102-
tok2 = tok2->tokAt(2);
103-
if (!Token::Match(tok2, "%var% ;|+"))
104-
continue;
105-
106-
const Variable *var1(tok->next()->variable());
107-
const Variable *var2(tok2->variable());
108-
109-
if (isaddr(var1) && isint(var2) && tok2->strAt(1) != "+")
110-
assignmentIntegerToAddressError(tok->next());
111-
112-
else if (isint(var1) && isaddr(var2) && !tok2->isPointerCompare()) {
113-
// assigning address => warning
114-
// some trivial addition => warning
115-
if (Token::Match(tok2->next(), "+ %any% !!;"))
116-
continue;
117-
118-
assignmentAddressToIntegerError(tok->next());
119-
}
120-
}
78+
if (tok->str() != "=")
79+
continue;
80+
81+
const ValueType *lhstype = tok->astOperand1() ? tok->astOperand1()->valueType() : nullptr;
82+
const ValueType *rhstype = tok->astOperand2() ? tok->astOperand2()->valueType() : nullptr;
83+
if (!lhstype || !rhstype)
84+
continue;
85+
86+
// Assign integer to pointer..
87+
if (lhstype->pointer >= 1U &&
88+
rhstype->pointer == 0U &&
89+
rhstype->originalTypeName.empty() &&
90+
rhstype->type == ValueType::Type::INT)
91+
assignmentIntegerToAddressError(tok);
92+
93+
// Assign pointer to integer..
94+
if (rhstype->pointer >= 1U &&
95+
lhstype->pointer == 0U &&
96+
lhstype->originalTypeName.empty() &&
97+
lhstype->type == ValueType::Type::INT)
98+
assignmentAddressToIntegerError(tok);
12199
}
122100
}
123101
}

‎test/test64bit.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ class Test64BitPortability : public TestFixture {
138138
" int x = 10;\n"
139139
" int *a = x * x;\n"
140140
"}");
141-
TODO_ASSERT_EQUALS("error", "", errout.str());
141+
ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning an integer to a pointer is not portable.\n", errout.str());
142142

143143
check("void foo(int *start, int *end) {\n"
144144
" int len;\n"

0 commit comments

Comments
 (0)