Skip to content
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
9 changes: 6 additions & 3 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ bool isAliasOf(const Token *tok, nonneg int varid, bool* inconclusive)
return false;
}

bool isAliasOf(const Token* tok, const Token* expr, int* indirect)
bool isAliasOf(const Token* tok, const Token* expr, nonneg int* indirect)
{
const Token* r = nullptr;
if (indirect)
Expand Down Expand Up @@ -2906,7 +2906,7 @@ static bool isExpressionChangedAt(const F& getExprTok,
// TODO: Is global variable really changed by function call?
return true;
}
int i = 1;
nonneg int i = 1;
bool aliased = false;
// If we can't find the expression then assume it is an alias
auto expr = getExprTok();
Expand All @@ -2916,7 +2916,10 @@ static bool isExpressionChangedAt(const F& getExprTok,
aliased = isAliasOf(tok, expr, &i);
if (!aliased)
return false;
if (isVariableChanged(tok, indirect + i, settings, depth))
i += indirect;
if (tok->valueType() && tok->valueType()->pointer)
i = std::min(i, tok->valueType()->pointer);
if (isVariableChanged(tok, i, settings, depth))
return true;
// TODO: Try to traverse the lambda function
if (Token::Match(tok, "%var% ("))
Expand Down
2 changes: 1 addition & 1 deletion lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ bool isExpressionChangedAt(const Token* expr,
/// If token is an alias if another variable
bool isAliasOf(const Token *tok, nonneg int varid, bool* inconclusive = nullptr);

bool isAliasOf(const Token* tok, const Token* expr, int* indirect = nullptr);
bool isAliasOf(const Token* tok, const Token* expr, nonneg int* indirect = nullptr);

const Token* getArgumentStart(const Token* ftok);

Expand Down
9 changes: 7 additions & 2 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1916,8 +1916,13 @@ void CheckOther::checkConstPointer()
continue;
if (Token::simpleMatch(parent, "(") && Token::Match(parent->astOperand1(), "if|while"))
continue;
if (Token::simpleMatch(parent, "=") && parent->astOperand1() == tok)
continue;
if (Token::simpleMatch(parent, "=")) {
const Token* lhs = parent->astOperand1();
if (lhs == tok)
continue;
if (lhs && lhs->valueType() && lhs->valueType()->isConst(vt->pointer))
continue;
}
if (const Token* ftok = getTokenArgumentFunction(tok, argn)) {
if (ftok->function()) {
const bool isCastArg = parent->isCast() && !ftok->function()->getOverloadedFunctions().empty(); // assume that cast changes the called function
Expand Down
4 changes: 2 additions & 2 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2296,7 +2296,7 @@ Variable& Variable::operator=(const Variable &var) &
if (this == &var)
return *this;

ValueType* vt = nullptr;
const ValueType* vt = nullptr;
if (var.mValueType)
vt = new ValueType(*var.mValueType);

Expand Down Expand Up @@ -2462,7 +2462,7 @@ void Variable::setValueType(const ValueType &valueType)
if (declType && !declType->next()->valueType())
return;
}
auto* vt = new ValueType(valueType);
const auto* vt = new ValueType(valueType);
delete mValueType;
mValueType = vt;
if ((mValueType->pointer > 0) && (!isArray() || Token::Match(mNameToken->previous(), "( * %name% )")))
Expand Down
6 changes: 3 additions & 3 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ void Tokenizer::simplifyTypedefCpp()
bool refToArray = false;
bool ptrMember = false;
bool typeOf = false;
Token *namespaceStart = nullptr;
const Token *namespaceStart = nullptr;
Token *namespaceEnd = nullptr;

// check for invalid input
Expand Down Expand Up @@ -3025,7 +3025,7 @@ bool Tokenizer::simplifyUsing()
ScopeInfo3 scopeInfo1;
ScopeInfo3 *currentScope1 = &scopeInfo1;
Token *startToken = list.front();
Token *endToken = nullptr;
const Token *endToken = nullptr;
bool inMemberFunc = false;
const ScopeInfo3 * memberFuncScope = nullptr;
const Token * memberFuncEnd = nullptr;
Expand All @@ -3039,7 +3039,7 @@ bool Tokenizer::simplifyUsing()
if (!currentScope1)
return substitute; // something bad happened
startToken = usingEnd->next();
endToken = const_cast<Token*>(currentScope->bodyEnd->next());
endToken = currentScope->bodyEnd->next();
if (currentScope->type == ScopeInfo3::MemberFunction) {
const ScopeInfo3 * temp = currentScope->findScope(currentScope->fullName);
if (temp) {
Expand Down
10 changes: 10 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4497,6 +4497,16 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("[test.cpp:2:18]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n"
"[test.cpp:5:18]: (style) Parameter 's' can be declared as pointer to const [constParameterPointer]\n",
errout_str());

check("struct T;\n"
"void use(const T*);\n"
"void f(T* tok0) {\n"
" T *tok1 = tok0;\n"
" const T *tok2 = tok1;\n"
" use(tok2);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:8]: (style) Variable 'tok1' can be declared as pointer to const [constVariablePointer]\n",
errout_str());
}

void constArray() {
Expand Down