Skip to content

Commit 4e3063e

Browse files
authored
Fix #14791: FN shadowFunction depending on order of declaration of member functions (#8609)
1 parent ef5c1f0 commit 4e3063e

6 files changed

Lines changed: 41 additions & 37 deletions

File tree

lib/checkother.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4153,7 +4153,8 @@ static const Token *findShadowed(const Scope *scope, const Variable& var, int li
41534153
return v.nameToken();
41544154
}
41554155
auto it = std::find_if(scope->functionList.cbegin(), scope->functionList.cend(), [&](const Function& f) {
4156-
return f.type == FunctionType::eFunction && f.name() == var.name() && precedes(f.tokenDef, var.nameToken());
4156+
return f.type == FunctionType::eFunction && f.name() == var.name()
4157+
&& (scope->isClassOrStructOrUnion() || precedes(f.tokenDef, var.nameToken()));
41574158
});
41584159
if (it != scope->functionList.end())
41594160
return it->tokenDef;

lib/filesettings.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ class FileWithDetails
4646
throw std::runtime_error("empty path specified");
4747
}
4848

49-
void setPath(std::string path)
49+
void setPath(std::string p)
5050
{
51-
mPath = std::move(path);
51+
mPath = std::move(p);
5252
mPathSimplified = Path::simplifyPath(mPath);
5353
mPathAbsolute.clear();
5454
}
@@ -76,9 +76,9 @@ class FileWithDetails
7676
return mSize;
7777
}
7878

79-
void setLang(Standards::Language lang)
79+
void setLang(Standards::Language language)
8080
{
81-
mLang = lang;
81+
mLang = language;
8282
}
8383

8484
Standards::Language lang() const

lib/token.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -249,35 +249,35 @@ class CPPCHECKLIB Token {
249249
* For example index 1 would return next token, and 2
250250
* would return next from that one.
251251
*/
252-
const Token *tokAt(int index) const
252+
const Token *tokAt(int idx) const
253253
{
254-
return tokAtImpl(this, index);
254+
return tokAtImpl(this, idx);
255255
}
256-
Token *tokAt(int index)
256+
Token *tokAt(int idx)
257257
{
258-
return tokAtImpl(this, index);
258+
return tokAtImpl(this, idx);
259259
}
260260

261261
/**
262262
* @return the link to the token in given index, related to this token.
263263
* For example index 1 would return the link to next token.
264264
*/
265-
const Token *linkAt(int index) const
265+
const Token *linkAt(int idx) const
266266
{
267-
return linkAtImpl(this, index);
267+
return linkAtImpl(this, idx);
268268
}
269-
Token *linkAt(int index)
269+
Token *linkAt(int idx)
270270
{
271-
return linkAtImpl(this, index);
271+
return linkAtImpl(this, idx);
272272
}
273273

274274
/**
275275
* @return String of the token in given index, related to this token.
276276
* If that token does not exist, an empty string is being returned.
277277
*/
278-
const std::string &strAt(int index) const
278+
const std::string &strAt(int idx) const
279279
{
280-
const Token *tok = this->tokAt(index);
280+
const Token *tok = this->tokAt(idx);
281281
return tok ? tok->mStr : mEmptyString;
282282
}
283283

@@ -604,11 +604,11 @@ class CPPCHECKLIB Token {
604604
bool hasAttributeCleanup() const {
605605
return !mImpl->mAttributeCleanup.empty();
606606
}
607-
void setCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint value) {
608-
mImpl->setCppcheckAttribute(type, value);
607+
void setCppcheckAttribute(CppcheckAttributesType attrType, MathLib::bigint value) {
608+
mImpl->setCppcheckAttribute(attrType, value);
609609
}
610-
bool getCppcheckAttribute(CppcheckAttributesType type, MathLib::bigint &value) const {
611-
return mImpl->getCppcheckAttribute(type, value);
610+
bool getCppcheckAttribute(CppcheckAttributesType attrType, MathLib::bigint &value) const {
611+
return mImpl->getCppcheckAttribute(attrType, value);
612612
}
613613
// cppcheck-suppress unusedFunction
614614
bool hasCppcheckAttributes() const {
@@ -899,15 +899,15 @@ class CPPCHECKLIB Token {
899899

900900
private:
901901
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
902-
static T *tokAtImpl(T *tok, int index)
902+
static T *tokAtImpl(T *tok, int idx)
903903
{
904-
while (index > 0 && tok) {
904+
while (idx > 0 && tok) {
905905
tok = tok->next();
906-
--index;
906+
--idx;
907907
}
908-
while (index < 0 && tok) {
908+
while (idx < 0 && tok) {
909909
tok = tok->previous();
910-
++index;
910+
++idx;
911911
}
912912
return tok;
913913
}
@@ -916,9 +916,9 @@ class CPPCHECKLIB Token {
916916
* @throws InternalError thrown if index is out of range
917917
*/
918918
template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
919-
static T *linkAtImpl(T *thisTok, int index)
919+
static T *linkAtImpl(T *thisTok, int idx)
920920
{
921-
T *tok = thisTok->tokAt(index);
921+
T *tok = thisTok->tokAt(idx);
922922
if (!tok) {
923923
throw InternalError(thisTok, "Internal error. Token::linkAt called with index outside the tokens range.");
924924
}

lib/tokenize.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -568,23 +568,23 @@ namespace {
568568
const std::pair<const Token*, Token*> rangeBefore(start, Token::findsimplematch(start, "{"));
569569

570570
// find typedef name token
571-
Token* nameToken = rangeBefore.second->link()->next();
572-
while (Token::Match(nameToken, "%name%|* %name%|*"))
573-
nameToken = nameToken->next();
574-
const std::pair<const Token*, Token*> rangeQualifiers(rangeBefore.second->link()->next(), nameToken);
571+
Token* nameTok = rangeBefore.second->link()->next();
572+
while (Token::Match(nameTok, "%name%|* %name%|*"))
573+
nameTok = nameTok->next();
574+
const std::pair<const Token*, Token*> rangeQualifiers(rangeBefore.second->link()->next(), nameTok);
575575

576-
if (Token::Match(nameToken, "%name% ;")) {
576+
if (Token::Match(nameTok, "%name% ;")) {
577577
if (Token::Match(rangeBefore.second->previous(), "enum|struct|union|class {"))
578-
rangeBefore.second->previous()->insertToken(nameToken->str());
578+
rangeBefore.second->previous()->insertToken(nameTok->str());
579579
mRangeType = rangeBefore;
580580
mRangeTypeQualifiers = rangeQualifiers;
581581
Token* typeName = rangeBefore.second->previous();
582582
if (typeName->isKeyword()) {
583583
// TODO typeName->insertToken("T:" + std::to_string(num++));
584-
typeName->insertToken(nameToken->str());
584+
typeName->insertToken(nameTok->str());
585585
}
586-
mNameToken = nameToken;
587-
mEndToken = nameToken->next();
586+
mNameToken = nameTok;
587+
mEndToken = nameTok->next();
588588
return;
589589
}
590590
}

test/testother.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13207,6 +13207,9 @@ class TestOther : public TestFixture {
1320713207

1320813208
check("struct S { static int i(); static void f(int i) {} };\n");
1320913209
ASSERT_EQUALS("[test.cpp:1:23] -> [test.cpp:1:46]: (style) Argument 'i' shadows outer function [shadowFunction]\n", errout_str());
13210+
13211+
check("struct S { void g(float f) {} void f() {} };\n");
13212+
ASSERT_EQUALS("[test.cpp:1:36] -> [test.cpp:1:25]: (style) Argument 'f' shadows outer function [shadowFunction]\n", errout_str());
1321013213
}
1321113214

1321213215
void knownArgument() {

test/testpreprocessor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ class TestPreprocessor : public TestFixture {
146146
}
147147
for (const std::string & config : cfgs) {
148148
try {
149-
const bool writeLocations = (strstr(code, "#include") != nullptr);
150-
cfgcode[config] = preprocessor.getcode(config, files, writeLocations);
149+
const bool writeLocs = (strstr(code, "#include") != nullptr);
150+
cfgcode[config] = preprocessor.getcode(config, files, writeLocs);
151151
} catch (const simplecpp::Output &) {
152152
cfgcode[config] = "";
153153
}

0 commit comments

Comments
 (0)