Skip to content

Commit c1c10f1

Browse files
committed
CheckInternal::checkRedundantNextPrevious(): Fix FN, simplify
1 parent ab9def9 commit c1c10f1

25 files changed

+177
-179
lines changed

lib/astutils.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ static bool isFunctionCall(const Token* tok)
484484
{
485485
if (Token::Match(tok, "%name% ("))
486486
return true;
487-
if (Token::Match(tok, "%name% <") && Token::simpleMatch(tok->next()->link(), "> ("))
487+
if (Token::Match(tok, "%name% <") && Token::simpleMatch(tok->linkAt(1), "> ("))
488488
return true;
489489
if (Token::Match(tok, "%name% ::"))
490490
return isFunctionCall(tok->tokAt(2));
@@ -538,7 +538,7 @@ static T* nextAfterAstRightmostLeafGeneric(T* tok)
538538
else
539539
break;
540540
} while (rightmostLeaf->astOperand1() || rightmostLeaf->astOperand2());
541-
while (Token::Match(rightmostLeaf->next(), "]|)") && !hasToken(rightmostLeaf->next()->link(), rightmostLeaf->next(), tok))
541+
while (Token::Match(rightmostLeaf->next(), "]|)") && !hasToken(rightmostLeaf->linkAt(1), rightmostLeaf->next(), tok))
542542
rightmostLeaf = rightmostLeaf->next();
543543
if (Token::Match(rightmostLeaf, "{|(|[") && rightmostLeaf->link())
544544
rightmostLeaf = rightmostLeaf->link();
@@ -1694,11 +1694,11 @@ bool isSameExpression(bool macro, const Token *tok1, const Token *tok2, const Se
16941694
}
16951695
}
16961696
// templates/casts
1697-
if ((tok1->next() && tok1->next()->link() && Token::Match(tok1, "%name% <")) ||
1698-
(tok2->next() && tok2->next()->link() && Token::Match(tok2, "%name% <"))) {
1697+
if ((tok1->next() && tok1->linkAt(1) && Token::Match(tok1, "%name% <")) ||
1698+
(tok2->next() && tok2->linkAt(1) && Token::Match(tok2, "%name% <"))) {
16991699

17001700
// non-const template function that is not a dynamic_cast => return false
1701-
if (pure && Token::simpleMatch(tok1->next()->link(), "> (") &&
1701+
if (pure && Token::simpleMatch(tok1->linkAt(1), "> (") &&
17021702
!(tok1->function() && tok1->function()->isConst()) &&
17031703
tok1->str() != "dynamic_cast")
17041704
return false;

lib/checkassert.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void CheckAssert::assertWithSideEffects()
5555
if (!Token::simpleMatch(tok, "assert ("))
5656
continue;
5757

58-
const Token *endTok = tok->next()->link();
58+
const Token *endTok = tok->linkAt(1);
5959
for (const Token* tmp = tok->next(); tmp != endTok; tmp = tmp->next()) {
6060
if (Token::simpleMatch(tmp, "sizeof ("))
6161
tmp = tmp->linkAt(1);

lib/checkboost.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void CheckBoost::checkBoostForeachModification()
4040
if (!Token::simpleMatch(tok, "BOOST_FOREACH ("))
4141
continue;
4242

43-
const Token *containerTok = tok->next()->link()->previous();
43+
const Token *containerTok = tok->linkAt(1)->previous();
4444
if (!Token::Match(containerTok, "%var% ) {"))
4545
continue;
4646

lib/checkbufferoverrun.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ void CheckBufferOverrun::stringNotZeroTerminated()
787787
}
788788
// Is the buffer zero terminated after the call?
789789
bool isZeroTerminated = false;
790-
for (const Token *tok2 = tok->next()->link(); tok2 != scope->bodyEnd; tok2 = tok2->next()) {
790+
for (const Token *tok2 = tok->linkAt(1); tok2 != scope->bodyEnd; tok2 = tok2->next()) {
791791
if (!Token::simpleMatch(tok2, "] ="))
792792
continue;
793793
const Token *rhs = tok2->next()->astOperand2();

lib/checkclass.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
949949
ftok = ftok->next();
950950

951951
// Passing "this" => assume that everything is initialized
952-
for (const Token *tok2 = ftok->next()->link(); tok2 && tok2 != ftok; tok2 = tok2->previous()) {
952+
for (const Token *tok2 = ftok->linkAt(1); tok2 && tok2 != ftok; tok2 = tok2->previous()) {
953953
if (tok2->str() == "this") {
954954
assignAllVar(usage);
955955
return;
@@ -1020,7 +1020,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
10201020
// the function is external and it's neither friend nor inherited virtual function.
10211021
// assume all variables that are passed to it are initialized..
10221022
else {
1023-
for (const Token *tok = ftok->tokAt(2); tok && tok != ftok->next()->link(); tok = tok->next()) {
1023+
for (const Token *tok = ftok->tokAt(2); tok && tok != ftok->linkAt(1); tok = tok->next()) {
10241024
if (tok->isName()) {
10251025
assignVar(usage, tok->varId());
10261026
}
@@ -1047,7 +1047,7 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
10471047
const Token *tok2 = ftok;
10481048
while (tok2) {
10491049
if (tok2->strAt(1) == "[")
1050-
tok2 = tok2->next()->link();
1050+
tok2 = tok2->linkAt(1);
10511051
else if (Token::Match(tok2->next(), ". %name%"))
10521052
tok2 = tok2->tokAt(2);
10531053
else
@@ -1637,7 +1637,7 @@ void CheckClass::checkReturnPtrThis(const Scope *scope, const Function *func, co
16371637
// avoid endless recursions
16381638
if (analyzedFunctions.find(&*it) == analyzedFunctions.end()) {
16391639
analyzedFunctions.insert(&*it);
1640-
checkReturnPtrThis(scope, &*it, it->arg->link()->next(), it->arg->link()->next()->link(),
1640+
checkReturnPtrThis(scope, &*it, it->arg->link()->next(), it->arg->link()->linkAt(1),
16411641
analyzedFunctions);
16421642
}
16431643
// just bail for now
@@ -1853,7 +1853,7 @@ const Token * CheckClass::getIfStmtBodyStart(const Token *tok, const Token *rhs)
18531853
case Bool::TRUE:
18541854
return top->link()->next();
18551855
case Bool::FALSE:
1856-
return top->link()->next()->link();
1856+
return top->link()->linkAt(1);
18571857
}
18581858
}
18591859
return nullptr;
@@ -2407,7 +2407,7 @@ bool CheckClass::checkConstFunc(const Scope *scope, const Function *func, Member
24072407
const Token *lpar = funcTok->next();
24082408
if (Token::simpleMatch(lpar, "( ) ("))
24092409
lpar = lpar->tokAt(2);
2410-
for (const Token* tok = lpar->next(); tok && tok != funcTok->next()->link(); tok = tok->next()) {
2410+
for (const Token* tok = lpar->next(); tok && tok != funcTok->linkAt(1); tok = tok->next()) {
24112411
if (tok->str() == "(")
24122412
tok = tok->link();
24132413
else if ((tok->isName() && isMemberVar(scope, tok)) || (tok->isUnaryOp("&") && (tok = tok->astOperand1()) && isMemberVar(scope, tok))) {

lib/checkcondition.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ bool CheckCondition::assignIfParseScope(const Token * const assignTok,
191191
ret = true;
192192
if (ret && tok2->str() == ";")
193193
return false;
194-
if (!islocal && Token::Match(tok2, "%name% (") && !Token::simpleMatch(tok2->next()->link(), ") {"))
194+
if (!islocal && Token::Match(tok2, "%name% (") && !Token::simpleMatch(tok2->linkAt(1), ") {"))
195195
return true;
196196
if (Token::Match(tok2, "if|while (")) {
197197
if (!islocal && tok2->str() == "while")
@@ -205,7 +205,7 @@ bool CheckCondition::assignIfParseScope(const Token * const assignTok,
205205
}
206206

207207
// parse condition
208-
const Token * const end = tok2->next()->link();
208+
const Token * const end = tok2->linkAt(1);
209209
for (; tok2 != end; tok2 = tok2->next()) {
210210
if (Token::Match(tok2, "[(,] &| %varid% [,)]", varid)) {
211211
return true;
@@ -227,8 +227,8 @@ bool CheckCondition::assignIfParseScope(const Token * const assignTok,
227227

228228
const bool ret1 = assignIfParseScope(assignTok, end->tokAt(2), varid, islocal, bitop, num);
229229
bool ret2 = false;
230-
if (Token::simpleMatch(end->next()->link(), "} else {"))
231-
ret2 = assignIfParseScope(assignTok, end->next()->link()->tokAt(3), varid, islocal, bitop, num);
230+
if (Token::simpleMatch(end->linkAt(1), "} else {"))
231+
ret2 = assignIfParseScope(assignTok, end->linkAt(1)->tokAt(3), varid, islocal, bitop, num);
232232
if (ret1 || ret2)
233233
return true;
234234
}
@@ -1803,9 +1803,9 @@ void CheckCondition::checkDuplicateConditionalAssign()
18031803
for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
18041804
if (!Token::simpleMatch(tok, "if ("))
18051805
continue;
1806-
if (!Token::simpleMatch(tok->next()->link(), ") {"))
1806+
if (!Token::simpleMatch(tok->linkAt(1), ") {"))
18071807
continue;
1808-
const Token *blockTok = tok->next()->link()->next();
1808+
const Token *blockTok = tok->linkAt(1)->next();
18091809
const Token *condTok = tok->next()->astOperand2();
18101810
const bool isBoolVar = Token::Match(condTok, "!| %var%");
18111811
if (!isBoolVar && !Token::Match(condTok, "==|!="))

lib/checkexceptionsafety.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ void CheckExceptionSafety::destructors()
6363
for (const Token *tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
6464
// Skip try blocks
6565
if (Token::simpleMatch(tok, "try {")) {
66-
tok = tok->next()->link();
66+
tok = tok->linkAt(1);
6767
}
6868

6969
// Skip uncaught exceptions
7070
else if (Token::simpleMatch(tok, "if ( ! std :: uncaught_exception ( ) ) {")) {
71-
tok = tok->next()->link(); // end of if ( ... )
72-
tok = tok->next()->link(); // end of { ... }
71+
tok = tok->linkAt(1); // end of if ( ... )
72+
tok = tok->linkAt(1); // end of { ... }
7373
}
7474

7575
// throw found within a destructor
@@ -183,8 +183,8 @@ void CheckExceptionSafety::checkRethrowCopy()
183183
const unsigned int varid = scope.bodyStart->tokAt(-2)->varId();
184184
if (varid) {
185185
for (const Token* tok = scope.bodyStart->next(); tok && tok != scope.bodyEnd; tok = tok->next()) {
186-
if (Token::simpleMatch(tok, "catch (") && tok->next()->link() && tok->next()->link()->next()) { // Don't check inner catch - it is handled in another iteration of outer loop.
187-
tok = tok->next()->link()->next()->link();
186+
if (Token::simpleMatch(tok, "catch (") && tok->linkAt(1) && tok->linkAt(1)->next()) { // Don't check inner catch - it is handled in another iteration of outer loop.
187+
tok = tok->linkAt(1)->linkAt(1);
188188
if (!tok)
189189
break;
190190
} else if (Token::Match(tok, "%varid% .", varid)) {

lib/checkinternal.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -296,22 +296,14 @@ void CheckInternal::checkRedundantNextPrevious()
296296
continue;
297297
tok = tok->next();
298298

299-
if (Token::Match(tok, "previous ( ) . next|tokAt|strAt|linkAt (") || Token::Match(tok, "next ( ) . previous|tokAt|strAt|linkAt (") ||
299+
if (Token::Match(tok, "previous ( ) . previous|next|tokAt|str|strAt|link|linkAt (") || Token::Match(tok, "next ( ) . previous|next|tokAt|str|strAt|link|linkAt (") ||
300300
(Token::simpleMatch(tok, "tokAt (") && Token::Match(tok->linkAt(1), ") . previous|next|tokAt|strAt|linkAt|str|link ("))) {
301301
const std::string& func1 = tok->str();
302302
const std::string& func2 = tok->linkAt(1)->strAt(2);
303303

304304
if ((func2 == "previous" || func2 == "next" || func2 == "str" || func2 == "link") && tok->linkAt(1)->strAt(4) != ")")
305305
continue;
306306

307-
redundantNextPreviousError(tok, func1, func2);
308-
} else if (Token::Match(tok, "next|previous ( ) . next|previous ( ) . next|previous|linkAt|strAt|link|str (")) {
309-
const std::string& func1 = tok->str();
310-
const std::string& func2 = tok->strAt(8);
311-
312-
if ((func2 == "previous" || func2 == "next" || func2 == "str" || func2 == "link") && tok->strAt(10) != ")")
313-
continue;
314-
315307
redundantNextPreviousError(tok, func1, func2);
316308
}
317309
}

lib/checkleakautovar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
701701
continue;
702702
functionCall(ftok, openingPar, varInfo, allocation, af);
703703

704-
tok = ftok->next()->link();
704+
tok = ftok->linkAt(1);
705705

706706
// Handle scopes that might be noreturn
707707
if (allocation.status == VarInfo::NOALLOC && Token::simpleMatch(tok, ") ; }")) {

lib/checkmemoryleak.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -860,20 +860,20 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable* const vari
860860
else if (Token::simpleMatch(tok3, "if (") &&
861861
notvar(tok3->next()->astOperand2(), structmemberid)) {
862862
// Goto the ")"
863-
tok3 = tok3->next()->link();
863+
tok3 = tok3->linkAt(1);
864864

865865
// make sure we have ") {".. it should be
866866
if (!Token::simpleMatch(tok3, ") {"))
867867
break;
868868

869869
// Goto the "}"
870-
tok3 = tok3->next()->link();
870+
tok3 = tok3->linkAt(1);
871871
}
872872

873873
// succeeded allocation
874874
else if (ifvar(tok3, structmemberid, "!=", "0")) {
875875
// goto the ")"
876-
tok3 = tok3->next()->link();
876+
tok3 = tok3->linkAt(1);
877877

878878
// check if the variable is deallocated or returned..
879879
int indentlevel4 = 0;
@@ -1103,7 +1103,7 @@ void CheckMemoryLeakNoVar::checkForUnsafeArgAlloc(const Scope *scope)
11031103

11041104
for (const Token *tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
11051105
if (Token::Match(tok, "%name% (")) {
1106-
const Token *endParamToken = tok->next()->link();
1106+
const Token *endParamToken = tok->linkAt(1);
11071107
const Token* pointerType = nullptr;
11081108
const Token* functionCalled = nullptr;
11091109

@@ -1112,12 +1112,12 @@ void CheckMemoryLeakNoVar::checkForUnsafeArgAlloc(const Scope *scope)
11121112
const Function *func = tok2->function();
11131113
const bool isNothrow = func && (func->isAttributeNothrow() || func->isThrow());
11141114

1115-
if (Token::Match(tok2, "shared_ptr|unique_ptr <") && Token::Match(tok2->next()->link(), "> ( new %name%")) {
1115+
if (Token::Match(tok2, "shared_ptr|unique_ptr <") && Token::Match(tok2->linkAt(1), "> ( new %name%")) {
11161116
pointerType = tok2;
11171117
} else if (!isNothrow) {
11181118
if (Token::Match(tok2, "%name% ("))
11191119
functionCalled = tok2;
1120-
else if (tok2->isName() && Token::simpleMatch(tok2->next()->link(), "> ("))
1120+
else if (tok2->isName() && Token::simpleMatch(tok2->linkAt(1), "> ("))
11211121
functionCalled = tok2;
11221122
}
11231123
}
@@ -1126,12 +1126,12 @@ void CheckMemoryLeakNoVar::checkForUnsafeArgAlloc(const Scope *scope)
11261126
std::string functionName = functionCalled->str();
11271127
if (functionCalled->strAt(1) == "<") {
11281128
functionName += '<';
1129-
for (const Token* tok2 = functionCalled->tokAt(2); tok2 != functionCalled->next()->link(); tok2 = tok2->next())
1129+
for (const Token* tok2 = functionCalled->tokAt(2); tok2 != functionCalled->linkAt(1); tok2 = tok2->next())
11301130
functionName += tok2->str();
11311131
functionName += '>';
11321132
}
11331133
std::string objectTypeName;
1134-
for (const Token* tok2 = pointerType->tokAt(2); tok2 != pointerType->next()->link(); tok2 = tok2->next())
1134+
for (const Token* tok2 = pointerType->tokAt(2); tok2 != pointerType->linkAt(1); tok2 = tok2->next())
11351135
objectTypeName += tok2->str();
11361136

11371137
unsafeArgAllocError(tok, functionName, pointerType->str(), objectTypeName);

0 commit comments

Comments
 (0)