Skip to content
Open
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
14 changes: 7 additions & 7 deletions externals/simplecpp/simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2512,11 +2512,11 @@ static void simplifySizeof(simplecpp::TokenList &expr, const std::map<std::strin
for (simplecpp::Token *tok = expr.front(); tok; tok = tok->next) {
if (tok->str() != "sizeof")
continue;
simplecpp::Token *tok1 = tok->next;
const simplecpp::Token *tok1 = tok->next;
if (!tok1) {
throw std::runtime_error("missing sizeof argument");
}
simplecpp::Token *tok2 = tok1->next;
const simplecpp::Token *tok2 = tok1->next;
if (!tok2) {
throw std::runtime_error("missing sizeof argument");
}
Expand All @@ -2531,7 +2531,7 @@ static void simplifySizeof(simplecpp::TokenList &expr, const std::map<std::strin
}

std::string type;
for (simplecpp::Token *typeToken = tok1; typeToken != tok2; typeToken = typeToken->next) {
for (const simplecpp::Token *typeToken = tok1; typeToken != tok2; typeToken = typeToken->next) {
if ((typeToken->str() == "unsigned" || typeToken->str() == "signed") && typeToken->next->name)
continue;
if (typeToken->str() == "*" && type.find('*') != std::string::npos)
Expand Down Expand Up @@ -2582,11 +2582,11 @@ static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI
for (simplecpp::Token *tok = expr.front(); tok; tok = tok->next) {
if (tok->str() != HAS_INCLUDE)
continue;
simplecpp::Token *tok1 = tok->next;
const simplecpp::Token *tok1 = tok->next;
if (!tok1) {
throw std::runtime_error("missing __has_include argument");
}
simplecpp::Token *tok2 = tok1->next;
const simplecpp::Token *tok2 = tok1->next;
if (!tok2) {
throw std::runtime_error("missing __has_include argument");
}
Expand All @@ -2604,7 +2604,7 @@ static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI
const bool systemheader = (tok1 && tok1->op == '<');
std::string header;
if (systemheader) {
simplecpp::Token *tok3 = tok1->next;
const simplecpp::Token *tok3 = tok1->next;
if (!tok3) {
throw std::runtime_error("missing __has_include closing angular bracket");
}
Expand All @@ -2615,7 +2615,7 @@ static void simplifyHasInclude(simplecpp::TokenList &expr, const simplecpp::DUI
}
}

for (simplecpp::Token *headerToken = tok1->next; headerToken != tok3; headerToken = headerToken->next)
for (const simplecpp::Token *headerToken = tok1->next; headerToken != tok3; headerToken = headerToken->next)
header += headerToken->str();
} else {
header = tok1->str().substr(1U, tok1->str().size() - 2U);
Expand Down
6 changes: 3 additions & 3 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1910,7 +1910,7 @@ void CheckOther::checkConstPointer()
if (lhs && lhs->variable() && lhs->variable()->isReference() && lhs->variable()->nameToken() == lhs && !lhs->variable()->isConst())
takingRef = true;
if (lhs && lhs->valueType() && lhs->valueType()->pointer && (lhs->valueType()->constness & 1) == 0 &&
parent->valueType() && parent->valueType()->pointer)
parent->valueType() && parent->valueType()->pointer && lhs->variable() != var)
nonConstPtrAssignment = true;
if (!takingRef && !nonConstPtrAssignment)
continue;
Expand All @@ -1927,9 +1927,9 @@ void CheckOther::checkConstPointer()
}
} else {
int argn = -1;
if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<"))
if (Token::Match(parent, "%oror%|%comp%|&&|?|!|-|<<|;"))
continue;
if (hasIncDecPlus && !parent->astParent())
if (hasIncDecPlus && (!parent->astParent() || parent->astParent()->str() == ";"))
continue;
if (Token::simpleMatch(parent, "(") && Token::Match(parent->astOperand1(), "if|while"))
continue;
Expand Down
2 changes: 1 addition & 1 deletion lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2680,7 +2680,7 @@ void Token::Impl::setCppcheckAttribute(CppcheckAttributesType type, MathLib::big

bool Token::Impl::getCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint &value) const
{
CppcheckAttributes *attr = mCppcheckAttributes;
const CppcheckAttributes *attr = mCppcheckAttributes;
while (attr && attr->type != type)
attr = attr->next;
if (attr)
Expand Down
24 changes: 24 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4511,6 +4511,30 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("[test.cpp:4:8]: (style) Variable 'tok1' can be declared as pointer to const [constVariablePointer]\n",
errout_str());

check("struct S { S* next; };\n" // #14119
"void f(S* s) {\n"
" for (S* p = s->next; p != nullptr; p = p->next) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3:13]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n",
errout_str());

check("void f(int* p) {\n"
" for (int* q = p; q;)\n"
" break;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:15]: (style) Variable 'q' can be declared as pointer to const [constVariablePointer]\n",
errout_str());

check("void g(const int*);\n" // #14148
"void f() {\n"
" int a[] = {1, 2, 3};\n"
" for (int* p = a; *p != 3; p++) {\n"
" g(p);\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:15]: (style) Variable 'p' can be declared as pointer to const [constVariablePointer]\n",
errout_str());
}

void constArray() {
Expand Down