From 5bd49feef4f5593bb9be339140f093d85b7d367e Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Mon, 30 Oct 2023 23:40:19 +0100 Subject: [PATCH 1/3] Fix #12129 FN (regression): constParameterPointer --- lib/checkother.cpp | 2 +- test/testother.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 418c9f06bc2..b1f7d75ae97 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1705,7 +1705,7 @@ void CheckOther::checkConstPointer() continue; if (p->isArgument() && p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef())) continue; - if (p->typeStartToken() && !p->typeStartToken()->originalName().empty()) + if (p->typeStartToken() && !p->typeStartToken()->originalName().empty() && p->typeStartToken()->originalName().back() == '*') continue; constVariableError(p, p->isArgument() ? p->scope()->function : nullptr); } diff --git a/test/testother.cpp b/test/testother.cpp index 4440cf51781..1a475572005 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3828,6 +3828,13 @@ class TestOther : public TestFixture { " qsort(p, nmemb, size, cmp);\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void g(bool *r, std::size_t *b) {\n" // #12129 + " if (*r && *b >= 5) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'r' can be declared as pointer to const\n" + "[test.cpp:1]: (style)Parameter 'b' can be declared as pointer to const\n", + errout.str()); } void switchRedundantAssignmentTest() { From 6f6ada68b623515bced90f5bf4358186fbda794c Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 31 Oct 2023 12:26:45 +0100 Subject: [PATCH 2/3] Fix --- lib/checkother.cpp | 10 ++++++++-- test/testother.cpp | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index b1f7d75ae97..2ed2b91a3e7 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1705,12 +1705,18 @@ void CheckOther::checkConstPointer() continue; if (p->isArgument() && p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef())) continue; - if (p->typeStartToken() && !p->typeStartToken()->originalName().empty() && p->typeStartToken()->originalName().back() == '*') - continue; + if (p->typeStartToken() && !p->typeStartToken()->originalName().empty()) { + const Token* typeTok = p->typeEndToken(); + while (typeTok->originalName().empty() && typeTok != p->typeStartToken()) + typeTok = typeTok->previous(); + if (!typeTok->originalName().empty() && typeTok->originalName().back() == '*') + continue; + } constVariableError(p, p->isArgument() ? p->scope()->function : nullptr); } } } + void CheckOther::constVariableError(const Variable *var, const Function *function) { if (!var) { diff --git a/test/testother.cpp b/test/testother.cpp index 1a475572005..74f319e09d6 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3833,7 +3833,7 @@ class TestOther : public TestFixture { " if (*r && *b >= 5) {}\n" "}\n"); ASSERT_EQUALS("[test.cpp:1]: (style) Parameter 'r' can be declared as pointer to const\n" - "[test.cpp:1]: (style)Parameter 'b' can be declared as pointer to const\n", + "[test.cpp:1]: (style) Parameter 'b' can be declared as pointer to const\n", errout.str()); } From 35ffcfa1dadd5face479259c029e1b7fedebaabc Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 31 Oct 2023 14:23:08 +0100 Subject: [PATCH 3/3] Use isSimplifiedTypedef() --- lib/checkother.cpp | 9 +-------- lib/tokenlist.cpp | 14 +++++++++----- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 2ed2b91a3e7..c358415f12b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1703,15 +1703,8 @@ void CheckOther::checkConstPointer() const int indirect = p->isArray() ? p->dimensions().size() : 1; if (isVariableChanged(start, p->scope()->bodyEnd, indirect, p->declarationId(), false, mSettings, mTokenizer->isCPP())) continue; - if (p->isArgument() && p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef())) + if (p->typeStartToken() && p->typeStartToken()->isSimplifiedTypedef() && !(Token::simpleMatch(p->typeEndToken(), "*") && !p->typeEndToken()->isSimplifiedTypedef())) continue; - if (p->typeStartToken() && !p->typeStartToken()->originalName().empty()) { - const Token* typeTok = p->typeEndToken(); - while (typeTok->originalName().empty() && typeTok != p->typeStartToken()) - typeTok = typeTok->previous(); - if (!typeTok->originalName().empty() && typeTok->originalName().back() == '*') - continue; - } constVariableError(p, p->isArgument() ? p->scope()->function : nullptr); } } diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 0b892cc98e1..96db342b3e1 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -1918,20 +1918,24 @@ void TokenList::simplifyPlatformTypes() Token *typeToken; if (platformtype->mConstPtr) { tok->str("const"); - tok->insertToken("*"); - tok->insertToken(platformtype->mType); + tok->isSimplifiedTypedef(true); + tok->insertToken("*")->isSimplifiedTypedef(true); + tok->insertToken(platformtype->mType)->isSimplifiedTypedef(true); typeToken = tok; } else if (platformtype->mPointer) { tok->str(platformtype->mType); + tok->isSimplifiedTypedef(true); typeToken = tok; - tok->insertToken("*"); + tok->insertToken("*")->isSimplifiedTypedef(true); } else if (platformtype->mPtrPtr) { tok->str(platformtype->mType); + tok->isSimplifiedTypedef(true); typeToken = tok; - tok->insertToken("*"); - tok->insertToken("*"); + tok->insertToken("*")->isSimplifiedTypedef(true); + tok->insertToken("*")->isSimplifiedTypedef(true); } else { tok->str(platformtype->mType); + tok->isSimplifiedTypedef(true); typeToken = tok; } if (platformtype->mSigned)