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
15 changes: 12 additions & 3 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,12 @@ bool isStlStringType(const Token* tok)
(Token::simpleMatch(tok, "std :: basic_string <") && !Token::simpleMatch(tok->linkAt(3), "> ::"));
}

bool isVoidCast(const Token* tok)
{
return Token::simpleMatch(tok, "(") && tok->isCast() && tok->valueType() &&
tok->valueType()->type == ValueType::Type::VOID && tok->valueType()->pointer == 0;
}

bool isTemporary(const Token* tok, const Library* library, bool unknown)
{
if (!tok)
Expand Down Expand Up @@ -1062,7 +1068,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 +2912,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 +2922,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
4 changes: 3 additions & 1 deletion lib/astutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ const Token * astIsVariableComparison(const Token *tok, const std::string &comp,
bool isVariableDecl(const Token* tok);
bool isStlStringType(const Token* tok);

bool isVoidCast(const Token* tok);

bool isTemporary(const Token* tok, const Library* library, bool unknown = false);

const Token* previousBeforeAstLeftmostLeaf(const Token* tok);
Expand Down Expand Up @@ -379,7 +381,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
26 changes: 24 additions & 2 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,21 @@ static bool isVariableMutableInInitializer(const Token* start, const Token * end
return false;
}

static bool isCastToVoid(const Variable* var)
{
if (!var)
return false;
if (!var->scope())
return false;
for (const Token* tok = var->scope()->bodyStart; tok != var->scope()->bodyEnd; tok = tok->next()) {
if (tok->varId() != var->declarationId())
continue;
if (isVoidCast(tok->astParent()))
return true;
}
return false;
}

void CheckOther::checkConstVariable()
{
if ((!mSettings->severity.isEnabled(Severity::style) || mTokenizer->isC()) && !mSettings->isPremiumEnabled("constVariable"))
Expand Down Expand Up @@ -1689,6 +1704,8 @@ void CheckOther::checkConstVariable()
continue;
if (isStructuredBindingVariable(var)) // TODO: check all bound variables
continue;
if (isCastToVoid(var))
continue;
if (isVariableChanged(var, *mSettings))
continue;
const bool hasFunction = function != nullptr;
Expand Down Expand Up @@ -1916,8 +1933,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
5 changes: 0 additions & 5 deletions lib/checkuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,11 +1150,6 @@ static bool astIsRhs(const Token *tok)
return tok && tok->astParent() && tok == tok->astParent()->astOperand2();
}

static bool isVoidCast(const Token *tok)
{
return Token::simpleMatch(tok, "(") && tok->isCast() && tok->valueType() && tok->valueType()->type == ValueType::Type::VOID && tok->valueType()->pointer == 0;
}

const Token* CheckUninitVar::isVariableUsage(const Token *vartok, const Library& library, bool pointer, Alloc alloc, int indirect)
{
const bool cpp = vartok->isCpp();
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
8 changes: 5 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 Expand Up @@ -9543,6 +9543,8 @@ void Tokenizer::simplifyCPPAttribute()
Token* head = skipCPPOrAlignAttribute(tok)->next();
while (isCPPAttribute(head) || isAlignAttribute(head))
head = skipCPPOrAlignAttribute(head)->next();
if (!head)
syntaxError(tok);
head->isAttributeMaybeUnused(true);
} else if (Token::findsimplematch(tok->tokAt(2), "unused", tok->link())) {
Token* head = skipCPPOrAlignAttribute(tok)->next();
Expand Down
5 changes: 5 additions & 0 deletions test/testgarbage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ class TestGarbage : public TestFixture {
TEST_CASE(garbageCode226);
TEST_CASE(garbageCode227);
TEST_CASE(garbageCode228);
TEST_CASE(garbageCode229);

TEST_CASE(garbageCodeFuzzerClientMode1); // test cases created with the fuzzer client, mode 1

Expand Down Expand Up @@ -1768,6 +1769,10 @@ class TestGarbage : public TestFixture {
ASSERT_NO_THROW(checkCode("void f() { enum { A = [=]() mutable { return 0; }() }; }"));
ASSERT_NO_THROW(checkCode("enum { A = [=](void) mutable -> int { return 0; }() };"));
}
void garbageCode229() { // #14126
ASSERT_THROW_INTERNAL(checkCode("void f() {} [[maybe_unused]]"), SYNTAX);
}


void syntaxErrorFirstToken() {
ASSERT_THROW_INTERNAL(checkCode("&operator(){[]};"), SYNTAX); // #7818
Expand Down
18 changes: 15 additions & 3 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3300,6 +3300,10 @@ class TestOther : public TestFixture {
"};\n");
ASSERT_EQUALS("", errout_str());

// #14136
check("void f(int& x) { (void)x; }\n");
ASSERT_EQUALS("", errout_str());

check("void e();\n"
"void g(void);\n"
"void h(void);\n"
Expand Down Expand Up @@ -4497,6 +4501,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 Expand Up @@ -12163,9 +12177,7 @@ class TestOther : public TestFixture {
" for (auto &j : g(std::move(l))) { (void)j; }\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:20]: (style) Variable 'j' can be declared as reference to const [constVariableReference]\n"
"[test.cpp:4:36]: (warning) Access of moved variable 'l'. [accessMoved]\n",
errout_str());
ASSERT_EQUALS("[test.cpp:4:36]: (warning) Access of moved variable 'l'. [accessMoved]\n", errout_str());
}

void moveCallback()
Expand Down