2022-01-26 19:02:20 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2023-01-28 10:16:34 +01:00
|
|
|
* Copyright (C) 2007-2023 Cppcheck team.
|
2022-01-26 19:02:20 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-02-13 16:27:06 +01:00
|
|
|
#include "forwardanalyzer.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
|
2021-01-05 16:49:08 +01:00
|
|
|
#include "analyzer.h"
|
2020-02-13 16:27:06 +01:00
|
|
|
#include "astutils.h"
|
2022-01-27 19:03:20 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include "errortypes.h"
|
|
|
|
#include "mathlib.h"
|
2020-02-13 16:27:06 +01:00
|
|
|
#include "settings.h"
|
|
|
|
#include "symboldatabase.h"
|
2020-03-20 10:37:16 +01:00
|
|
|
#include "token.h"
|
2020-04-13 13:44:48 +02:00
|
|
|
#include "valueptr.h"
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2020-05-10 20:32:59 +02:00
|
|
|
#include <algorithm>
|
2021-08-09 07:45:41 +02:00
|
|
|
#include <cstdio>
|
2020-02-15 07:57:43 +01:00
|
|
|
#include <functional>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <list>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2021-06-09 09:20:43 +02:00
|
|
|
#include <tuple>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <type_traits>
|
2021-06-09 09:20:43 +02:00
|
|
|
#include <utility>
|
2022-01-27 19:03:20 +01:00
|
|
|
#include <vector>
|
2020-02-15 07:57:43 +01:00
|
|
|
|
2021-09-21 19:25:41 +02:00
|
|
|
struct OnExit {
|
|
|
|
std::function<void()> f;
|
|
|
|
|
|
|
|
~OnExit() {
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-13 16:27:06 +01:00
|
|
|
struct ForwardTraversal {
|
|
|
|
enum class Progress { Continue, Break, Skip };
|
2021-01-18 10:12:07 +01:00
|
|
|
enum class Terminate { None, Bail, Escape, Modified, Inconclusive, Conditional };
|
2023-03-07 12:26:17 +01:00
|
|
|
ForwardTraversal(const ValuePtr<Analyzer>& analyzer, const Settings& settings)
|
2021-01-25 17:24:36 +01:00
|
|
|
: analyzer(analyzer), settings(settings), actions(Analyzer::Action::None), analyzeOnly(false), analyzeTerminate(false)
|
2020-09-11 00:06:49 +02:00
|
|
|
{}
|
2020-11-10 16:00:55 +01:00
|
|
|
ValuePtr<Analyzer> analyzer;
|
2023-03-07 12:26:17 +01:00
|
|
|
const Settings& settings;
|
2020-11-10 16:00:55 +01:00
|
|
|
Analyzer::Action actions;
|
2020-09-11 00:06:49 +02:00
|
|
|
bool analyzeOnly;
|
2021-01-25 17:24:36 +01:00
|
|
|
bool analyzeTerminate;
|
2021-06-19 13:59:48 +02:00
|
|
|
Analyzer::Terminate terminate = Analyzer::Terminate::None;
|
2021-09-21 19:25:41 +02:00
|
|
|
std::vector<Token*> loopEnds = {};
|
2021-01-18 10:12:07 +01:00
|
|
|
|
2021-06-19 14:47:35 +02:00
|
|
|
Progress Break(Analyzer::Terminate t = Analyzer::Terminate::None) {
|
2021-06-19 13:59:48 +02:00
|
|
|
if ((!analyzeOnly || analyzeTerminate) && t != Analyzer::Terminate::None)
|
2021-01-18 10:12:07 +01:00
|
|
|
terminate = t;
|
|
|
|
return Progress::Break;
|
|
|
|
}
|
2020-09-11 00:06:49 +02:00
|
|
|
|
2021-01-05 16:49:08 +01:00
|
|
|
struct Branch {
|
2022-06-07 21:15:13 +02:00
|
|
|
explicit Branch(Token* tok = nullptr) : endBlock(tok) {}
|
2021-06-09 09:20:43 +02:00
|
|
|
Token* endBlock = nullptr;
|
2021-01-05 16:49:08 +01:00
|
|
|
Analyzer::Action action = Analyzer::Action::None;
|
|
|
|
bool check = false;
|
|
|
|
bool escape = false;
|
|
|
|
bool escapeUnknown = false;
|
2021-10-05 08:29:23 +02:00
|
|
|
bool active = false;
|
2021-01-05 16:49:08 +01:00
|
|
|
bool isEscape() const {
|
|
|
|
return escape || escapeUnknown;
|
|
|
|
}
|
|
|
|
bool isConclusiveEscape() const {
|
|
|
|
return escape && !escapeUnknown;
|
|
|
|
}
|
|
|
|
bool isModified() const {
|
|
|
|
return action.isModified() && !isConclusiveEscape();
|
|
|
|
}
|
|
|
|
bool isInconclusive() const {
|
|
|
|
return action.isInconclusive() && !isConclusiveEscape();
|
|
|
|
}
|
|
|
|
bool isDead() const {
|
|
|
|
return action.isModified() || action.isInconclusive() || isEscape();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-11 00:07:13 +02:00
|
|
|
bool stopUpdates() {
|
2020-09-11 00:06:49 +02:00
|
|
|
analyzeOnly = true;
|
|
|
|
return actions.isModified();
|
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2021-06-09 09:20:43 +02:00
|
|
|
std::pair<bool, bool> evalCond(const Token* tok, const Token* ctx = nullptr) const {
|
|
|
|
if (!tok)
|
|
|
|
return std::make_pair(false, false);
|
2021-10-11 19:10:37 +02:00
|
|
|
std::vector<MathLib::bigint> result = analyzer->evaluate(tok, ctx);
|
2020-11-10 16:00:55 +01:00
|
|
|
// TODO: We should convert to bool
|
2022-12-30 15:13:47 +01:00
|
|
|
const bool checkThen = std::any_of(result.cbegin(), result.cend(), [](int x) {
|
2021-09-18 22:23:05 +02:00
|
|
|
return x != 0;
|
2020-11-11 09:15:36 +01:00
|
|
|
});
|
2022-12-30 15:13:47 +01:00
|
|
|
const bool checkElse = std::any_of(result.cbegin(), result.cend(), [](int x) {
|
2020-11-11 09:15:36 +01:00
|
|
|
return x == 0;
|
|
|
|
});
|
2020-02-13 16:27:06 +01:00
|
|
|
return std::make_pair(checkThen, checkElse);
|
|
|
|
}
|
|
|
|
|
2021-06-12 11:09:15 +02:00
|
|
|
bool isConditionTrue(const Token* tok, const Token* ctx = nullptr) const {
|
|
|
|
return evalCond(tok, ctx).first;
|
|
|
|
}
|
2021-06-09 09:20:43 +02:00
|
|
|
|
2022-01-18 22:02:25 +01:00
|
|
|
// cppcheck-suppress unusedFunction
|
2021-06-12 11:09:15 +02:00
|
|
|
bool isConditionFalse(const Token* tok, const Token* ctx = nullptr) const {
|
|
|
|
return evalCond(tok, ctx).second;
|
|
|
|
}
|
2021-06-09 09:20:43 +02:00
|
|
|
|
2021-11-08 07:46:52 +01:00
|
|
|
template<class T, class F, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
|
|
|
|
Progress traverseTok(T* tok, F f, bool traverseUnknown, T** out = nullptr) {
|
2022-04-23 06:18:55 +02:00
|
|
|
if (Token::Match(tok, "asm|goto"))
|
2021-09-21 19:25:41 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2022-04-23 06:18:55 +02:00
|
|
|
else if (Token::Match(tok, "setjmp|longjmp (")) {
|
|
|
|
// Traverse the parameters of the function before escaping
|
|
|
|
traverseRecursive(tok->next()->astOperand2(), f, traverseUnknown);
|
|
|
|
return Break(Analyzer::Terminate::Bail);
|
|
|
|
} else if (Token::simpleMatch(tok, "continue")) {
|
2021-09-21 19:25:41 +02:00
|
|
|
if (loopEnds.empty())
|
|
|
|
return Break(Analyzer::Terminate::Escape);
|
|
|
|
// If we are in a loop then jump to the end
|
|
|
|
if (out)
|
|
|
|
*out = loopEnds.back();
|
2022-04-11 07:23:58 +02:00
|
|
|
} else if (Token::Match(tok, "return|throw")) {
|
2020-02-15 07:57:43 +01:00
|
|
|
traverseRecursive(tok->astOperand2(), f, traverseUnknown);
|
2022-04-11 07:23:58 +02:00
|
|
|
traverseRecursive(tok->astOperand1(), f, traverseUnknown);
|
|
|
|
return Break(Analyzer::Terminate::Escape);
|
2023-03-07 12:26:17 +01:00
|
|
|
} else if (Token::Match(tok, "%name% (") && isEscapeFunction(tok, &settings.library)) {
|
2022-04-11 07:23:58 +02:00
|
|
|
// Traverse the parameters of the function before escaping
|
|
|
|
traverseRecursive(tok->next()->astOperand2(), f, traverseUnknown);
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Escape);
|
2020-02-13 16:27:06 +01:00
|
|
|
} else if (isUnevaluated(tok)) {
|
|
|
|
if (out)
|
|
|
|
*out = tok->link();
|
|
|
|
return Progress::Skip;
|
2020-05-18 08:00:01 +02:00
|
|
|
} else if (tok->astOperand1() && tok->astOperand2() && Token::Match(tok, "?|&&|%oror%")) {
|
2020-02-15 07:57:43 +01:00
|
|
|
if (traverseConditional(tok, f, traverseUnknown) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-02-13 16:27:06 +01:00
|
|
|
if (out)
|
|
|
|
*out = nextAfterAstRightmostLeaf(tok);
|
|
|
|
return Progress::Skip;
|
|
|
|
// Skip lambdas
|
2020-02-15 07:57:43 +01:00
|
|
|
} else if (T* lambdaEndToken = findLambdaEndToken(tok)) {
|
2020-02-13 16:27:06 +01:00
|
|
|
if (checkScope(lambdaEndToken).isModified())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2020-02-13 16:27:06 +01:00
|
|
|
if (out)
|
2020-06-13 10:26:54 +02:00
|
|
|
*out = lambdaEndToken->next();
|
2020-03-22 10:12:53 +01:00
|
|
|
// Skip class scope
|
2020-05-18 08:00:01 +02:00
|
|
|
} else if (tok->str() == "{" && tok->scope() && tok->scope()->isClassOrStruct()) {
|
2020-03-22 10:12:53 +01:00
|
|
|
if (out)
|
|
|
|
*out = tok->link();
|
2020-02-13 16:27:06 +01:00
|
|
|
} else {
|
2020-02-15 07:57:43 +01:00
|
|
|
if (f(tok) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-02-13 16:27:06 +01:00
|
|
|
}
|
|
|
|
return Progress::Continue;
|
|
|
|
}
|
|
|
|
|
2021-11-08 07:46:52 +01:00
|
|
|
template<class T, class F, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
|
|
|
|
Progress traverseRecursive(T* tok, F f, bool traverseUnknown, unsigned int recursion=0) {
|
2020-02-13 16:27:06 +01:00
|
|
|
if (!tok)
|
|
|
|
return Progress::Continue;
|
2020-04-09 12:11:33 +02:00
|
|
|
if (recursion > 10000)
|
|
|
|
return Progress::Skip;
|
2020-09-03 07:17:36 +02:00
|
|
|
T* firstOp = tok->astOperand1();
|
|
|
|
T* secondOp = tok->astOperand2();
|
2021-05-23 14:36:45 +02:00
|
|
|
// Evaluate:
|
2021-05-22 23:33:13 +02:00
|
|
|
// 1. RHS of assignment before LHS
|
|
|
|
// 2. Unary op before operand
|
2021-12-05 15:46:52 +01:00
|
|
|
// 3. Function arguments before function call
|
|
|
|
if (tok->isAssignmentOp() || !secondOp || isFunctionCall(tok))
|
2020-09-03 07:17:36 +02:00
|
|
|
std::swap(firstOp, secondOp);
|
|
|
|
if (firstOp && traverseRecursive(firstOp, f, traverseUnknown, recursion+1) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2022-10-02 07:12:40 +02:00
|
|
|
const Progress p = tok->isAssignmentOp() ? Progress::Continue : traverseTok(tok, f, traverseUnknown);
|
2020-02-13 16:27:06 +01:00
|
|
|
if (p == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-09-03 07:17:36 +02:00
|
|
|
if (p == Progress::Continue && secondOp && traverseRecursive(secondOp, f, traverseUnknown, recursion+1) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-09-03 07:17:36 +02:00
|
|
|
if (tok->isAssignmentOp() && traverseTok(tok, f, traverseUnknown) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-02-13 16:27:06 +01:00
|
|
|
return Progress::Continue;
|
|
|
|
}
|
|
|
|
|
2021-08-07 20:51:18 +02:00
|
|
|
template<class T, class F, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
|
2020-02-15 07:57:43 +01:00
|
|
|
Progress traverseConditional(T* tok, F f, bool traverseUnknown) {
|
2020-04-09 14:38:10 +02:00
|
|
|
if (Token::Match(tok, "?|&&|%oror%") && tok->astOperand1() && tok->astOperand2()) {
|
2020-02-15 07:57:43 +01:00
|
|
|
T* condTok = tok->astOperand1();
|
|
|
|
T* childTok = tok->astOperand2();
|
2020-02-13 16:27:06 +01:00
|
|
|
bool checkThen, checkElse;
|
|
|
|
std::tie(checkThen, checkElse) = evalCond(condTok);
|
|
|
|
if (!checkThen && !checkElse) {
|
2021-08-21 18:52:11 +02:00
|
|
|
if (!traverseUnknown && analyzer->stopOnCondition(condTok) && stopUpdates()) {
|
|
|
|
return Progress::Continue;
|
2021-01-18 10:12:07 +01:00
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
checkThen = true;
|
|
|
|
checkElse = true;
|
|
|
|
}
|
2020-05-19 21:03:28 +02:00
|
|
|
if (childTok->str() == ":") {
|
2020-02-15 07:57:43 +01:00
|
|
|
if (checkThen && traverseRecursive(childTok->astOperand1(), f, traverseUnknown) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-02-15 07:57:43 +01:00
|
|
|
if (checkElse && traverseRecursive(childTok->astOperand2(), f, traverseUnknown) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-02-13 16:27:06 +01:00
|
|
|
} else {
|
2020-05-19 21:03:28 +02:00
|
|
|
if (!checkThen && tok->str() == "&&")
|
2020-02-13 16:27:06 +01:00
|
|
|
return Progress::Continue;
|
2020-05-19 21:03:28 +02:00
|
|
|
if (!checkElse && tok->str() == "||")
|
2020-02-13 16:27:06 +01:00
|
|
|
return Progress::Continue;
|
2020-02-15 07:57:43 +01:00
|
|
|
if (traverseRecursive(childTok, f, traverseUnknown) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-02-13 16:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Progress::Continue;
|
|
|
|
}
|
|
|
|
|
2020-02-15 07:57:43 +01:00
|
|
|
Progress update(Token* tok) {
|
2020-11-10 16:00:55 +01:00
|
|
|
Analyzer::Action action = analyzer->analyze(tok, Analyzer::Direction::Forward);
|
2020-09-10 04:29:26 +02:00
|
|
|
actions |= action;
|
2021-10-04 07:50:23 +02:00
|
|
|
if (!action.isNone() && !analyzeOnly)
|
2020-11-10 16:00:55 +01:00
|
|
|
analyzer->update(tok, action, Analyzer::Direction::Forward);
|
2020-05-22 22:57:20 +02:00
|
|
|
if (action.isInconclusive() && !analyzer->lowerToInconclusive())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Inconclusive);
|
2020-02-15 07:57:43 +01:00
|
|
|
if (action.isInvalid())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Modified);
|
2020-06-08 21:17:12 +02:00
|
|
|
if (action.isWrite() && !action.isRead())
|
|
|
|
// Analysis of this write will continue separately
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Modified);
|
2020-02-15 07:57:43 +01:00
|
|
|
return Progress::Continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Progress updateTok(Token* tok, Token** out = nullptr) {
|
2021-11-08 07:46:52 +01:00
|
|
|
auto f = [this](Token* tok2) {
|
2020-02-15 09:14:14 +01:00
|
|
|
return update(tok2);
|
|
|
|
};
|
|
|
|
return traverseTok(tok, f, false, out);
|
2020-02-15 07:57:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Progress updateRecursive(Token* tok) {
|
2021-11-08 07:46:52 +01:00
|
|
|
auto f = [this](Token* tok2) {
|
2020-02-15 09:14:14 +01:00
|
|
|
return update(tok2);
|
|
|
|
};
|
|
|
|
return traverseRecursive(tok, f, false);
|
2020-02-15 07:57:43 +01:00
|
|
|
}
|
|
|
|
|
2020-11-11 09:15:36 +01:00
|
|
|
Analyzer::Action analyzeRecursive(const Token* start) {
|
2020-11-10 16:00:55 +01:00
|
|
|
Analyzer::Action result = Analyzer::Action::None;
|
2021-11-08 07:46:52 +01:00
|
|
|
auto f = [&](const Token* tok) {
|
2020-11-10 16:00:55 +01:00
|
|
|
result = analyzer->analyze(tok, Analyzer::Direction::Forward);
|
2020-02-15 07:57:43 +01:00
|
|
|
if (result.isModified() || result.isInconclusive())
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-02-15 07:57:43 +01:00
|
|
|
return Progress::Continue;
|
|
|
|
};
|
|
|
|
traverseRecursive(start, f, true);
|
|
|
|
return result;
|
2020-02-13 16:27:06 +01:00
|
|
|
}
|
|
|
|
|
2023-03-03 11:02:51 +01:00
|
|
|
Analyzer::Action analyzeRange(const Token* start, const Token* end) const {
|
2020-11-10 16:00:55 +01:00
|
|
|
Analyzer::Action result = Analyzer::Action::None;
|
2020-02-13 16:27:06 +01:00
|
|
|
for (const Token* tok = start; tok && tok != end; tok = tok->next()) {
|
2020-11-10 16:00:55 +01:00
|
|
|
Analyzer::Action action = analyzer->analyze(tok, Analyzer::Direction::Forward);
|
2020-02-13 16:27:06 +01:00
|
|
|
if (action.isModified() || action.isInconclusive())
|
|
|
|
return action;
|
2021-01-18 10:12:07 +01:00
|
|
|
result |= action;
|
2020-02-13 16:27:06 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:20:43 +02:00
|
|
|
ForwardTraversal fork(bool analyze = false) const {
|
2021-01-25 17:24:36 +01:00
|
|
|
ForwardTraversal ft = *this;
|
|
|
|
if (analyze) {
|
|
|
|
ft.analyzeOnly = true;
|
|
|
|
ft.analyzeTerminate = true;
|
|
|
|
}
|
2021-06-09 09:20:43 +02:00
|
|
|
ft.actions = Analyzer::Action::None;
|
2021-01-25 17:24:36 +01:00
|
|
|
return ft;
|
|
|
|
}
|
|
|
|
|
2023-03-02 21:51:58 +01:00
|
|
|
std::vector<ForwardTraversal> tryForkScope(Token* endBlock, bool isModified = false) const {
|
2020-02-13 16:27:06 +01:00
|
|
|
if (analyzer->updateScope(endBlock, isModified)) {
|
2021-06-09 09:20:43 +02:00
|
|
|
ForwardTraversal ft = fork();
|
2022-09-29 21:47:17 +02:00
|
|
|
return {std::move(ft)};
|
2020-02-13 16:27:06 +01:00
|
|
|
}
|
2021-01-22 21:47:24 +01:00
|
|
|
return std::vector<ForwardTraversal> {};
|
2020-02-13 16:27:06 +01:00
|
|
|
}
|
|
|
|
|
2023-03-02 21:51:58 +01:00
|
|
|
std::vector<ForwardTraversal> tryForkUpdateScope(Token* endBlock, bool isModified = false) const {
|
2021-06-09 09:20:43 +02:00
|
|
|
std::vector<ForwardTraversal> result = tryForkScope(endBlock, isModified);
|
|
|
|
for (ForwardTraversal& ft : result)
|
|
|
|
ft.updateScope(endBlock);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
static bool hasGoto(const Token* endBlock) {
|
|
|
|
return Token::findsimplematch(endBlock->link(), "goto", endBlock);
|
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2022-03-02 07:46:47 +01:00
|
|
|
static bool hasJump(const Token* endBlock) {
|
2021-12-15 19:32:14 +01:00
|
|
|
return Token::findmatch(endBlock->link(), "goto|break", endBlock);
|
|
|
|
}
|
|
|
|
|
2021-01-25 17:24:36 +01:00
|
|
|
bool hasInnerReturnScope(const Token* start, const Token* end) const {
|
2021-01-25 22:51:50 +01:00
|
|
|
for (const Token* tok=start; tok != end; tok = tok->previous()) {
|
2021-01-25 17:24:36 +01:00
|
|
|
if (Token::simpleMatch(tok, "}")) {
|
|
|
|
const Token* ftok = nullptr;
|
2023-03-07 12:26:17 +01:00
|
|
|
const bool r = isReturnScope(tok, &settings.library, &ftok);
|
2021-01-25 17:24:36 +01:00
|
|
|
if (r)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-08 20:01:41 +02:00
|
|
|
bool isEscapeScope(const Token* endBlock, bool& unknown) const {
|
2020-02-13 16:27:06 +01:00
|
|
|
const Token* ftok = nullptr;
|
2023-03-07 12:26:17 +01:00
|
|
|
const bool r = isReturnScope(endBlock, &settings.library, &ftok);
|
2020-02-13 16:27:06 +01:00
|
|
|
if (!r && ftok)
|
2021-01-05 16:49:08 +01:00
|
|
|
unknown = true;
|
2020-02-13 16:27:06 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class Status {
|
|
|
|
None,
|
|
|
|
Escaped,
|
|
|
|
Modified,
|
|
|
|
Inconclusive,
|
|
|
|
};
|
|
|
|
|
2023-03-03 11:02:51 +01:00
|
|
|
Analyzer::Action analyzeScope(const Token* endBlock) const {
|
2020-11-11 09:15:36 +01:00
|
|
|
return analyzeRange(endBlock->link(), endBlock);
|
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2023-03-03 11:02:51 +01:00
|
|
|
Analyzer::Action checkScope(Token* endBlock) const {
|
2020-11-10 16:00:55 +01:00
|
|
|
Analyzer::Action a = analyzeScope(endBlock);
|
2021-06-09 09:20:43 +02:00
|
|
|
tryForkUpdateScope(endBlock, a.isModified());
|
2020-02-13 16:27:06 +01:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2023-03-03 11:02:51 +01:00
|
|
|
Analyzer::Action checkScope(const Token* endBlock) const {
|
2020-11-10 16:00:55 +01:00
|
|
|
Analyzer::Action a = analyzeScope(endBlock);
|
2020-02-15 07:57:43 +01:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2023-03-03 11:02:51 +01:00
|
|
|
bool checkBranch(Branch& branch) const {
|
2021-06-09 09:20:43 +02:00
|
|
|
Analyzer::Action a = analyzeScope(branch.endBlock);
|
2021-01-25 17:24:36 +01:00
|
|
|
branch.action = a;
|
2021-06-09 09:20:43 +02:00
|
|
|
std::vector<ForwardTraversal> ft1 = tryForkUpdateScope(branch.endBlock, a.isModified());
|
2022-10-02 07:12:40 +02:00
|
|
|
const bool bail = hasGoto(branch.endBlock);
|
2021-01-25 17:24:36 +01:00
|
|
|
if (!a.isModified() && !bail) {
|
|
|
|
if (ft1.empty()) {
|
|
|
|
// Traverse into the branch to see if there is a conditional escape
|
2021-06-09 09:20:43 +02:00
|
|
|
if (!branch.escape && hasInnerReturnScope(branch.endBlock->previous(), branch.endBlock->link())) {
|
|
|
|
ForwardTraversal ft2 = fork(true);
|
|
|
|
ft2.updateScope(branch.endBlock);
|
2021-06-19 13:59:48 +02:00
|
|
|
if (ft2.terminate == Analyzer::Terminate::Escape) {
|
2021-01-25 17:24:36 +01:00
|
|
|
branch.escape = true;
|
|
|
|
branch.escapeUnknown = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-06-19 13:59:48 +02:00
|
|
|
if (ft1.front().terminate == Analyzer::Terminate::Escape) {
|
2021-01-25 17:24:36 +01:00
|
|
|
branch.escape = true;
|
|
|
|
branch.escapeUnknown = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bail;
|
|
|
|
}
|
|
|
|
|
2022-09-08 20:01:41 +02:00
|
|
|
bool reentersLoop(Token* endBlock, const Token* condTok, const Token* stepTok) const {
|
2021-06-09 09:20:43 +02:00
|
|
|
if (!condTok)
|
|
|
|
return true;
|
|
|
|
if (Token::simpleMatch(condTok, ":"))
|
|
|
|
return true;
|
2022-12-09 07:15:47 +01:00
|
|
|
bool stepChangesCond = false;
|
2021-06-09 09:20:43 +02:00
|
|
|
if (stepTok) {
|
|
|
|
std::pair<const Token*, const Token*> exprToks = stepTok->findExpressionStartEndTokens();
|
|
|
|
if (exprToks.first != nullptr && exprToks.second != nullptr)
|
2023-03-07 12:26:17 +01:00
|
|
|
stepChangesCond |= isExpressionChanged(condTok, exprToks.first, exprToks.second->next(), &settings, true);
|
2021-01-18 10:12:07 +01:00
|
|
|
}
|
2023-03-07 12:26:17 +01:00
|
|
|
const bool bodyChangesCond = isExpressionChanged(condTok, endBlock->link(), endBlock, &settings, true);
|
2021-06-09 09:20:43 +02:00
|
|
|
// Check for mutation in the condition
|
2022-12-09 07:15:47 +01:00
|
|
|
const bool condChanged =
|
|
|
|
nullptr != findAstNode(condTok, [&](const Token* tok) {
|
2023-03-07 12:26:17 +01:00
|
|
|
return isVariableChanged(tok, 0, &settings, true);
|
2021-06-12 11:09:15 +02:00
|
|
|
});
|
2022-12-09 07:15:47 +01:00
|
|
|
const bool changed = stepChangesCond || bodyChangesCond || condChanged;
|
2021-06-09 09:20:43 +02:00
|
|
|
if (!changed)
|
|
|
|
return true;
|
|
|
|
ForwardTraversal ft = fork(true);
|
|
|
|
ft.updateScope(endBlock);
|
2022-12-09 07:15:47 +01:00
|
|
|
return ft.isConditionTrue(condTok) && bodyChangesCond;
|
2021-06-09 09:20:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Progress updateInnerLoop(Token* endBlock, Token* stepTok, Token* condTok) {
|
2021-09-21 19:25:41 +02:00
|
|
|
loopEnds.push_back(endBlock);
|
|
|
|
OnExit oe{[&] {
|
|
|
|
loopEnds.pop_back();
|
|
|
|
}};
|
2021-06-09 09:20:43 +02:00
|
|
|
if (endBlock && updateScope(endBlock) == Progress::Break)
|
|
|
|
return Break();
|
|
|
|
if (stepTok && updateRecursive(stepTok) == Progress::Break)
|
|
|
|
return Break();
|
|
|
|
if (condTok && updateRecursive(condTok) == Progress::Break)
|
|
|
|
return Break();
|
|
|
|
return Progress::Continue;
|
2021-01-18 10:12:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Progress updateLoop(const Token* endToken,
|
|
|
|
Token* endBlock,
|
|
|
|
Token* condTok,
|
|
|
|
Token* initTok = nullptr,
|
2021-07-03 09:12:26 +02:00
|
|
|
Token* stepTok = nullptr,
|
|
|
|
bool exit = false) {
|
2021-06-09 09:20:43 +02:00
|
|
|
if (initTok && updateRecursive(initTok) == Progress::Break)
|
|
|
|
return Break();
|
2020-08-26 07:02:15 +02:00
|
|
|
const bool isDoWhile = precedes(endBlock, condTok);
|
2021-07-03 09:12:26 +02:00
|
|
|
bool checkThen = true;
|
|
|
|
bool checkElse = false;
|
|
|
|
if (condTok && !Token::simpleMatch(condTok, ":"))
|
2021-07-26 22:21:56 +02:00
|
|
|
std::tie(checkThen, checkElse) = evalCond(condTok, isDoWhile ? endBlock->previous() : nullptr);
|
2021-12-17 21:51:47 +01:00
|
|
|
// exiting a do while(false)
|
|
|
|
if (checkElse && exit) {
|
|
|
|
if (hasJump(endBlock)) {
|
|
|
|
if (!analyzer->lowerToPossible())
|
|
|
|
return Break(Analyzer::Terminate::Bail);
|
|
|
|
if (analyzer->isConditional() && stopUpdates())
|
|
|
|
return Break(Analyzer::Terminate::Conditional);
|
|
|
|
}
|
2021-07-03 09:12:26 +02:00
|
|
|
return Progress::Continue;
|
2021-12-17 21:51:47 +01:00
|
|
|
}
|
2020-11-10 16:00:55 +01:00
|
|
|
Analyzer::Action bodyAnalysis = analyzeScope(endBlock);
|
|
|
|
Analyzer::Action allAnalysis = bodyAnalysis;
|
2021-08-27 05:46:57 +02:00
|
|
|
Analyzer::Action condAnalysis;
|
|
|
|
if (condTok) {
|
|
|
|
condAnalysis = analyzeRecursive(condTok);
|
|
|
|
allAnalysis |= condAnalysis;
|
|
|
|
}
|
2020-02-15 07:57:43 +01:00
|
|
|
if (stepTok)
|
|
|
|
allAnalysis |= analyzeRecursive(stepTok);
|
2020-09-10 04:29:26 +02:00
|
|
|
actions |= allAnalysis;
|
2021-08-27 05:46:57 +02:00
|
|
|
// do while(false) is not really a loop
|
2021-12-17 21:51:47 +01:00
|
|
|
if (checkElse && isDoWhile &&
|
2021-10-15 10:59:01 +02:00
|
|
|
(condTok->hasKnownIntValue() ||
|
|
|
|
(!bodyAnalysis.isModified() && !condAnalysis.isModified() && condAnalysis.isRead()))) {
|
2021-08-27 05:46:57 +02:00
|
|
|
if (updateRange(endBlock->link(), endBlock) == Progress::Break)
|
|
|
|
return Break();
|
|
|
|
return updateRecursive(condTok);
|
|
|
|
}
|
2020-02-15 07:57:43 +01:00
|
|
|
if (allAnalysis.isInconclusive()) {
|
2020-02-13 16:27:06 +01:00
|
|
|
if (!analyzer->lowerToInconclusive())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2021-08-27 05:46:57 +02:00
|
|
|
} else if (allAnalysis.isModified() || (exit && allAnalysis.isIdempotent())) {
|
2020-02-13 16:27:06 +01:00
|
|
|
if (!analyzer->lowerToPossible())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2020-02-13 16:27:06 +01:00
|
|
|
}
|
2021-07-03 09:12:26 +02:00
|
|
|
|
2021-06-09 09:20:43 +02:00
|
|
|
if (condTok && !Token::simpleMatch(condTok, ":")) {
|
|
|
|
if (!isDoWhile || (!bodyAnalysis.isModified() && !bodyAnalysis.isIdempotent()))
|
|
|
|
if (updateRecursive(condTok) == Progress::Break)
|
|
|
|
return Break();
|
2020-06-05 18:06:03 +02:00
|
|
|
}
|
2021-08-25 20:33:41 +02:00
|
|
|
if (!checkThen && !checkElse && !isDoWhile && analyzer->stopOnCondition(condTok) && stopUpdates())
|
|
|
|
return Break(Analyzer::Terminate::Conditional);
|
2021-06-09 09:20:43 +02:00
|
|
|
// condition is false, we don't enter the loop
|
|
|
|
if (checkElse)
|
|
|
|
return Progress::Continue;
|
|
|
|
if (checkThen || isDoWhile) {
|
2021-08-27 05:46:57 +02:00
|
|
|
// Since we are re-entering the loop then assume the condition is true to update the state
|
|
|
|
if (exit)
|
|
|
|
analyzer->assume(condTok, true, Analyzer::Assume::Quiet | Analyzer::Assume::Absolute);
|
2021-06-09 09:20:43 +02:00
|
|
|
if (updateInnerLoop(endBlock, stepTok, condTok) == Progress::Break)
|
|
|
|
return Break();
|
|
|
|
// If loop re-enters then it could be modified again
|
|
|
|
if (allAnalysis.isModified() && reentersLoop(endBlock, condTok, stepTok))
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2021-06-09 09:20:43 +02:00
|
|
|
if (allAnalysis.isIncremental())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2021-07-18 07:48:21 +02:00
|
|
|
} else if (allAnalysis.isModified()) {
|
2021-06-09 09:20:43 +02:00
|
|
|
std::vector<ForwardTraversal> ftv = tryForkScope(endBlock, allAnalysis.isModified());
|
|
|
|
bool forkContinue = true;
|
|
|
|
for (ForwardTraversal& ft : ftv) {
|
|
|
|
if (condTok)
|
|
|
|
ft.analyzer->assume(condTok, false, Analyzer::Assume::Quiet);
|
|
|
|
if (ft.updateInnerLoop(endBlock, stepTok, condTok) == Progress::Break)
|
|
|
|
forkContinue = false;
|
2021-01-18 10:12:07 +01:00
|
|
|
}
|
2021-06-09 09:20:43 +02:00
|
|
|
|
|
|
|
if (allAnalysis.isModified() || !forkContinue) {
|
2021-07-02 17:41:51 +02:00
|
|
|
// TODO: Don't bail on missing condition
|
2021-06-09 09:20:43 +02:00
|
|
|
if (!condTok)
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2021-06-09 09:20:43 +02:00
|
|
|
if (analyzer->isConditional() && stopUpdates())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Conditional);
|
2021-06-09 09:20:43 +02:00
|
|
|
analyzer->assume(condTok, false);
|
|
|
|
}
|
|
|
|
if (forkContinue) {
|
|
|
|
for (ForwardTraversal& ft : ftv) {
|
|
|
|
if (!ft.actions.isIncremental())
|
|
|
|
ft.updateRange(endBlock, endToken);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (allAnalysis.isIncremental())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2021-07-18 07:48:21 +02:00
|
|
|
} else {
|
|
|
|
if (updateInnerLoop(endBlock, stepTok, condTok) == Progress::Break)
|
|
|
|
return Progress::Break;
|
|
|
|
if (allAnalysis.isIncremental())
|
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2020-02-13 16:27:06 +01:00
|
|
|
}
|
|
|
|
return Progress::Continue;
|
|
|
|
}
|
|
|
|
|
2021-07-03 09:12:26 +02:00
|
|
|
Progress updateLoopExit(const Token* endToken,
|
|
|
|
Token* endBlock,
|
|
|
|
Token* condTok,
|
|
|
|
Token* initTok = nullptr,
|
2021-07-03 09:12:51 +02:00
|
|
|
Token* stepTok = nullptr) {
|
2021-07-03 09:12:26 +02:00
|
|
|
return updateLoop(endToken, endBlock, condTok, initTok, stepTok, true);
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:20:43 +02:00
|
|
|
Progress updateScope(Token* endBlock) {
|
|
|
|
return updateRange(endBlock->link(), endBlock);
|
|
|
|
}
|
|
|
|
|
2021-01-23 08:47:39 +01:00
|
|
|
Progress updateRange(Token* start, const Token* end, int depth = 20) {
|
|
|
|
if (depth < 0)
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2021-03-20 14:02:07 +01:00
|
|
|
std::size_t i = 0;
|
2021-07-07 08:20:32 +02:00
|
|
|
for (Token* tok = start; precedes(tok, end); tok = tok->next()) {
|
2020-02-13 16:27:06 +01:00
|
|
|
Token* next = nullptr;
|
2021-03-20 14:02:07 +01:00
|
|
|
if (tok->index() <= i)
|
|
|
|
throw InternalError(tok, "Cyclic forward analysis.");
|
|
|
|
i = tok->index();
|
2020-02-13 16:27:06 +01:00
|
|
|
|
2020-05-18 08:00:01 +02:00
|
|
|
if (tok->link()) {
|
|
|
|
// Skip casts..
|
|
|
|
if (tok->str() == "(" && !tok->astOperand2() && tok->isCast()) {
|
|
|
|
tok = tok->link();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Skip template arguments..
|
|
|
|
if (tok->str() == "<") {
|
|
|
|
tok = tok->link();
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-10 10:35:49 +01:00
|
|
|
}
|
2020-03-08 11:49:14 +01:00
|
|
|
|
2020-02-13 16:27:06 +01:00
|
|
|
// Evaluate RHS of assignment before LHS
|
|
|
|
if (Token* assignTok = assignExpr(tok)) {
|
2020-09-03 07:17:36 +02:00
|
|
|
if (updateRecursive(assignTok) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-02-13 16:27:06 +01:00
|
|
|
tok = nextAfterAstRightmostLeaf(assignTok);
|
|
|
|
if (!tok)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2022-06-17 07:49:45 +02:00
|
|
|
} else if (Token::simpleMatch(tok, ") {") && Token::Match(tok->link()->previous(), "for|while (") &&
|
|
|
|
!Token::simpleMatch(tok->link()->astOperand2(), ":")) {
|
2022-06-16 19:26:23 +02:00
|
|
|
// In the middle of a loop structure so bail
|
|
|
|
return Break(Analyzer::Terminate::Bail);
|
|
|
|
} else if (tok->str() == ";" && tok->astParent()) {
|
|
|
|
Token* top = tok->astTop();
|
|
|
|
if (top && Token::Match(top->previous(), "for|while (") && Token::simpleMatch(top->link(), ") {")) {
|
|
|
|
Token* endCond = top->link();
|
|
|
|
Token* endBlock = endCond->linkAt(1);
|
|
|
|
Token* condTok = getCondTok(top);
|
|
|
|
Token* stepTok = getStepTok(top);
|
|
|
|
// The semicolon should belong to the initTok otherwise something went wrong, so just bail
|
|
|
|
if (tok->astOperand2() != condTok && !Token::simpleMatch(tok->astOperand2(), ";"))
|
|
|
|
return Break(Analyzer::Terminate::Bail);
|
|
|
|
if (updateLoop(end, endBlock, condTok, nullptr, stepTok) == Progress::Break)
|
|
|
|
return Break();
|
|
|
|
}
|
|
|
|
} else if (tok->str() == "break") {
|
2020-12-25 08:43:14 +01:00
|
|
|
const Token *scopeEndToken = findNextTokenFromBreak(tok);
|
|
|
|
if (!scopeEndToken)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-12-25 08:43:14 +01:00
|
|
|
tok = skipTo(tok, scopeEndToken, end);
|
2022-06-12 07:16:28 +02:00
|
|
|
if (!precedes(tok, end))
|
|
|
|
return Break(Analyzer::Terminate::Escape);
|
2020-02-13 16:27:06 +01:00
|
|
|
if (!analyzer->lowerToPossible())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2020-02-17 18:28:58 +01:00
|
|
|
// TODO: Don't break, instead move to the outer scope
|
2020-02-13 16:27:06 +01:00
|
|
|
if (!tok)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-05-18 08:00:01 +02:00
|
|
|
} else if (Token::Match(tok, "%name% :") || tok->str() == "case") {
|
2020-02-13 16:27:06 +01:00
|
|
|
if (!analyzer->lowerToPossible())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2020-05-18 08:00:01 +02:00
|
|
|
} else if (tok->link() && tok->str() == "}") {
|
2020-06-13 10:26:54 +02:00
|
|
|
const Scope* scope = tok->scope();
|
2020-06-14 10:06:54 +02:00
|
|
|
if (!scope)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-05-18 08:00:01 +02:00
|
|
|
if (Token::Match(tok->link()->previous(), ")|else {")) {
|
2020-09-14 18:43:11 +02:00
|
|
|
const Token* tok2 = tok->link()->previous();
|
|
|
|
const bool inElse = Token::simpleMatch(tok2, "else {");
|
|
|
|
const bool inLoop = inElse ? false : Token::Match(tok2->link()->previous(), "while|for (");
|
2020-09-03 07:17:36 +02:00
|
|
|
Token* condTok = getCondTokFromEnd(tok);
|
2020-05-18 08:00:01 +02:00
|
|
|
if (!condTok)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-09-14 18:43:11 +02:00
|
|
|
if (!condTok->hasKnownIntValue() || inLoop) {
|
2020-05-18 08:00:01 +02:00
|
|
|
if (!analyzer->lowerToPossible())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2020-06-09 08:16:53 +02:00
|
|
|
} else if (condTok->values().front().intvalue == inElse) {
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-05-18 08:00:01 +02:00
|
|
|
}
|
2022-06-16 17:40:09 +02:00
|
|
|
// Handle loop
|
|
|
|
if (inLoop) {
|
|
|
|
Token* stepTok = getStepTokFromEnd(tok);
|
|
|
|
bool checkThen, checkElse;
|
|
|
|
std::tie(checkThen, checkElse) = evalCond(condTok);
|
|
|
|
if (stepTok && !checkElse) {
|
|
|
|
if (updateRecursive(stepTok) == Progress::Break)
|
|
|
|
return Break();
|
|
|
|
if (updateRecursive(condTok) == Progress::Break)
|
|
|
|
return Break();
|
|
|
|
// Reevaluate condition
|
|
|
|
std::tie(checkThen, checkElse) = evalCond(condTok);
|
|
|
|
}
|
|
|
|
if (!checkElse) {
|
|
|
|
if (updateLoopExit(end, tok, condTok, nullptr, stepTok) == Progress::Break)
|
|
|
|
return Break();
|
|
|
|
}
|
2020-09-03 07:17:36 +02:00
|
|
|
}
|
2021-06-09 09:20:43 +02:00
|
|
|
analyzer->assume(condTok, !inElse, Analyzer::Assume::Quiet);
|
2020-05-18 08:00:01 +02:00
|
|
|
if (Token::simpleMatch(tok, "} else {"))
|
|
|
|
tok = tok->linkAt(2);
|
2020-06-13 10:26:54 +02:00
|
|
|
} else if (scope->type == Scope::eTry) {
|
2020-02-13 16:27:06 +01:00
|
|
|
if (!analyzer->lowerToPossible())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2020-06-13 10:26:54 +02:00
|
|
|
} else if (scope->type == Scope::eLambda) {
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-07-24 08:12:36 +02:00
|
|
|
} else if (scope->type == Scope::eDo && Token::simpleMatch(tok, "} while (")) {
|
2021-07-03 09:12:26 +02:00
|
|
|
if (updateLoopExit(end, tok, tok->tokAt(2)->astOperand2()) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-07-24 08:12:36 +02:00
|
|
|
tok = tok->linkAt(2);
|
2020-05-18 08:00:01 +02:00
|
|
|
} else if (Token::simpleMatch(tok->next(), "else {")) {
|
2020-02-13 16:27:06 +01:00
|
|
|
tok = tok->linkAt(2);
|
2020-05-18 08:00:01 +02:00
|
|
|
}
|
2022-06-16 19:26:23 +02:00
|
|
|
} else if (tok->isControlFlowKeyword() && Token::Match(tok, "if|while|for (") &&
|
|
|
|
Token::simpleMatch(tok->next()->link(), ") {")) {
|
2020-02-13 16:27:06 +01:00
|
|
|
Token* endCond = tok->next()->link();
|
|
|
|
Token* endBlock = endCond->next()->link();
|
|
|
|
Token* condTok = getCondTok(tok);
|
|
|
|
Token* initTok = getInitTok(tok);
|
|
|
|
if (initTok && updateRecursive(initTok) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-02-13 16:27:06 +01:00
|
|
|
if (Token::Match(tok, "for|while (")) {
|
2020-08-22 09:16:26 +02:00
|
|
|
// For-range loop
|
|
|
|
if (Token::simpleMatch(condTok, ":")) {
|
|
|
|
Token* conTok = condTok->astOperand2();
|
|
|
|
if (conTok && updateRecursive(conTok) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2021-07-16 18:49:07 +02:00
|
|
|
bool isEmpty = false;
|
2021-10-11 19:10:37 +02:00
|
|
|
std::vector<MathLib::bigint> result =
|
|
|
|
analyzer->evaluate(Analyzer::Evaluate::ContainerEmpty, conTok);
|
2021-07-16 18:49:07 +02:00
|
|
|
if (result.empty())
|
|
|
|
analyzer->assume(conTok, false, Analyzer::Assume::ContainerEmpty);
|
|
|
|
else
|
|
|
|
isEmpty = result.front() != 0;
|
|
|
|
if (!isEmpty && updateLoop(end, endBlock, condTok) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-08-22 09:16:26 +02:00
|
|
|
} else {
|
|
|
|
Token* stepTok = getStepTok(tok);
|
2021-01-18 10:12:07 +01:00
|
|
|
if (updateLoop(end, endBlock, condTok, initTok, stepTok) == Progress::Break)
|
|
|
|
return Break();
|
2020-08-22 09:16:26 +02:00
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
tok = endBlock;
|
|
|
|
} else {
|
|
|
|
// Traverse condition
|
|
|
|
if (updateRecursive(condTok) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2021-06-09 09:20:43 +02:00
|
|
|
Branch thenBranch{endBlock};
|
|
|
|
Branch elseBranch{endBlock->tokAt(2) ? endBlock->linkAt(2) : nullptr};
|
2020-02-13 16:27:06 +01:00
|
|
|
// Check if condition is true or false
|
2021-01-05 16:49:08 +01:00
|
|
|
std::tie(thenBranch.check, elseBranch.check) = evalCond(condTok);
|
2021-08-21 18:52:11 +02:00
|
|
|
if (!thenBranch.check && !elseBranch.check && analyzer->stopOnCondition(condTok) && stopUpdates())
|
|
|
|
return Break(Analyzer::Terminate::Conditional);
|
2022-10-02 07:12:40 +02:00
|
|
|
const bool hasElse = Token::simpleMatch(endBlock, "} else {");
|
2020-02-13 16:27:06 +01:00
|
|
|
bool bail = false;
|
|
|
|
|
|
|
|
// Traverse then block
|
2021-01-05 16:49:08 +01:00
|
|
|
thenBranch.escape = isEscapeScope(endBlock, thenBranch.escapeUnknown);
|
|
|
|
if (thenBranch.check) {
|
2021-10-05 08:29:23 +02:00
|
|
|
thenBranch.active = true;
|
2021-01-23 08:47:39 +01:00
|
|
|
if (updateRange(endCond->next(), endBlock, depth - 1) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2021-01-05 16:49:08 +01:00
|
|
|
} else if (!elseBranch.check) {
|
2021-10-05 08:29:23 +02:00
|
|
|
thenBranch.active = true;
|
2021-06-09 09:20:43 +02:00
|
|
|
if (checkBranch(thenBranch))
|
2020-02-13 16:27:06 +01:00
|
|
|
bail = true;
|
|
|
|
}
|
|
|
|
// Traverse else block
|
2020-02-16 16:02:22 +01:00
|
|
|
if (hasElse) {
|
2021-01-05 16:49:08 +01:00
|
|
|
elseBranch.escape = isEscapeScope(endBlock->linkAt(2), elseBranch.escapeUnknown);
|
|
|
|
if (elseBranch.check) {
|
2021-10-05 08:29:23 +02:00
|
|
|
elseBranch.active = true;
|
2022-10-02 07:12:40 +02:00
|
|
|
const Progress result = updateRange(endBlock->tokAt(2), endBlock->linkAt(2), depth - 1);
|
2020-02-13 16:27:06 +01:00
|
|
|
if (result == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2021-01-05 16:49:08 +01:00
|
|
|
} else if (!thenBranch.check) {
|
2021-10-05 08:29:23 +02:00
|
|
|
elseBranch.active = true;
|
2021-06-09 09:20:43 +02:00
|
|
|
if (checkBranch(elseBranch))
|
2020-02-13 16:27:06 +01:00
|
|
|
bail = true;
|
|
|
|
}
|
|
|
|
tok = endBlock->linkAt(2);
|
|
|
|
} else {
|
|
|
|
tok = endBlock;
|
|
|
|
}
|
2021-10-05 08:29:23 +02:00
|
|
|
if (thenBranch.active)
|
|
|
|
actions |= thenBranch.action;
|
|
|
|
if (elseBranch.active)
|
|
|
|
actions |= elseBranch.action;
|
2020-02-13 16:27:06 +01:00
|
|
|
if (bail)
|
2021-09-21 19:25:41 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2021-01-18 10:12:07 +01:00
|
|
|
if (thenBranch.isDead() && elseBranch.isDead()) {
|
|
|
|
if (thenBranch.isModified() && elseBranch.isModified())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Modified);
|
2021-01-18 10:12:07 +01:00
|
|
|
if (thenBranch.isConclusiveEscape() && elseBranch.isConclusiveEscape())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Escape);
|
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2021-01-18 10:12:07 +01:00
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
// Conditional return
|
2021-10-05 08:29:23 +02:00
|
|
|
if (thenBranch.active && thenBranch.isEscape() && !hasElse) {
|
2021-01-05 16:49:08 +01:00
|
|
|
if (!thenBranch.isConclusiveEscape()) {
|
|
|
|
if (!analyzer->lowerToInconclusive())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2021-01-06 17:13:44 +01:00
|
|
|
} else if (thenBranch.check) {
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-02-13 16:27:06 +01:00
|
|
|
} else {
|
2020-09-11 00:06:49 +02:00
|
|
|
if (analyzer->isConditional() && stopUpdates())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Conditional);
|
2020-02-13 16:27:06 +01:00
|
|
|
analyzer->assume(condTok, false);
|
|
|
|
}
|
|
|
|
}
|
2021-01-05 16:49:08 +01:00
|
|
|
if (thenBranch.isInconclusive() || elseBranch.isInconclusive()) {
|
2020-02-13 16:27:06 +01:00
|
|
|
if (!analyzer->lowerToInconclusive())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2021-01-05 16:49:08 +01:00
|
|
|
} else if (thenBranch.isModified() || elseBranch.isModified()) {
|
2020-09-11 00:06:49 +02:00
|
|
|
if (!hasElse && analyzer->isConditional() && stopUpdates())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Conditional);
|
2020-02-13 16:27:06 +01:00
|
|
|
if (!analyzer->lowerToPossible())
|
2021-06-19 13:59:48 +02:00
|
|
|
return Break(Analyzer::Terminate::Bail);
|
2021-01-05 16:49:08 +01:00
|
|
|
analyzer->assume(condTok, elseBranch.isModified());
|
2020-02-13 16:27:06 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-22 19:20:03 +02:00
|
|
|
} else if (Token::simpleMatch(tok, "try {")) {
|
|
|
|
Token* endBlock = tok->next()->link();
|
2021-09-03 23:07:08 +02:00
|
|
|
ForwardTraversal tryTraversal = fork();
|
|
|
|
tryTraversal.updateRange(tok->next(), endBlock, depth - 1);
|
|
|
|
bool bail = tryTraversal.actions.isModified();
|
|
|
|
if (bail)
|
2023-01-14 20:14:38 +01:00
|
|
|
return Break();
|
2021-09-03 23:07:08 +02:00
|
|
|
|
|
|
|
while (Token::simpleMatch(endBlock, "} catch (")) {
|
|
|
|
Token* endCatch = endBlock->linkAt(2);
|
|
|
|
if (!Token::simpleMatch(endCatch, ") {"))
|
|
|
|
return Break();
|
|
|
|
endBlock = endCatch->linkAt(1);
|
|
|
|
ForwardTraversal ft = fork();
|
|
|
|
ft.updateRange(endBlock->link(), endBlock, depth - 1);
|
|
|
|
bail |= ft.terminate != Analyzer::Terminate::None || ft.actions.isModified();
|
|
|
|
}
|
|
|
|
if (bail)
|
|
|
|
return Break();
|
2020-04-22 19:20:03 +02:00
|
|
|
tok = endBlock;
|
2020-02-13 16:27:06 +01:00
|
|
|
} else if (Token::simpleMatch(tok, "do {")) {
|
|
|
|
Token* endBlock = tok->next()->link();
|
2020-08-24 13:10:36 +02:00
|
|
|
Token* condTok = Token::simpleMatch(endBlock, "} while (") ? endBlock->tokAt(2)->astOperand2() : nullptr;
|
2021-01-18 10:12:07 +01:00
|
|
|
if (updateLoop(end, endBlock, condTok) == Progress::Break)
|
|
|
|
return Break();
|
2020-08-24 13:10:36 +02:00
|
|
|
if (condTok)
|
|
|
|
tok = endBlock->linkAt(2)->next();
|
|
|
|
else
|
|
|
|
tok = endBlock;
|
2020-02-13 16:27:06 +01:00
|
|
|
} else if (Token::Match(tok, "assert|ASSERT (")) {
|
|
|
|
const Token* condTok = tok->next()->astOperand2();
|
|
|
|
bool checkThen, checkElse;
|
|
|
|
std::tie(checkThen, checkElse) = evalCond(condTok);
|
|
|
|
if (checkElse)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-02-13 16:27:06 +01:00
|
|
|
if (!checkThen)
|
2021-06-09 09:20:43 +02:00
|
|
|
analyzer->assume(condTok, true, Analyzer::Assume::Quiet | Analyzer::Assume::Absolute);
|
2020-02-13 16:27:06 +01:00
|
|
|
} else if (Token::simpleMatch(tok, "switch (")) {
|
|
|
|
if (updateRecursive(tok->next()->astOperand2()) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
|
|
|
return Break();
|
2022-12-18 22:07:43 +01:00
|
|
|
} else if (Token* callTok = callExpr(tok)) {
|
|
|
|
// Since the call could be an unknown macro, traverse the tokens as a range instead of recursively
|
|
|
|
if (!Token::simpleMatch(callTok, "( )") &&
|
|
|
|
updateRange(callTok->next(), callTok->link(), depth - 1) == Progress::Break)
|
|
|
|
return Break();
|
|
|
|
if (updateTok(callTok) == Progress::Break)
|
|
|
|
return Break();
|
|
|
|
if (start != callTok && updateRecursive(callTok->astOperand1()) == Progress::Break)
|
|
|
|
return Break();
|
|
|
|
tok = callTok->link();
|
|
|
|
if (!tok)
|
|
|
|
return Break();
|
2020-02-13 16:27:06 +01:00
|
|
|
} else {
|
|
|
|
if (updateTok(tok, &next) == Progress::Break)
|
2021-01-18 10:12:07 +01:00
|
|
|
return Break();
|
2020-05-21 08:47:48 +02:00
|
|
|
if (next) {
|
|
|
|
if (precedes(next, end))
|
|
|
|
tok = next->previous();
|
|
|
|
else
|
2022-12-18 22:07:43 +01:00
|
|
|
return Progress::Continue;
|
2020-05-21 08:47:48 +02:00
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
}
|
|
|
|
// Prevent infinite recursion
|
|
|
|
if (tok->next() == start)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Progress::Continue;
|
|
|
|
}
|
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
static bool isUnevaluated(const Token* tok) {
|
2023-03-17 13:51:55 +01:00
|
|
|
return Token::Match(tok->previous(), "sizeof|decltype (");
|
2020-02-13 16:27:06 +01:00
|
|
|
}
|
|
|
|
|
2021-12-05 15:46:52 +01:00
|
|
|
static bool isFunctionCall(const Token* tok)
|
|
|
|
{
|
|
|
|
if (!Token::simpleMatch(tok, "("))
|
|
|
|
return false;
|
|
|
|
if (tok->isCast())
|
|
|
|
return false;
|
|
|
|
if (!tok->isBinaryOp())
|
|
|
|
return false;
|
|
|
|
if (Token::simpleMatch(tok->link(), ") {"))
|
|
|
|
return false;
|
|
|
|
if (isUnevaluated(tok))
|
|
|
|
return false;
|
|
|
|
return Token::Match(tok->previous(), "%name%|)|]|>");
|
|
|
|
}
|
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
static Token* assignExpr(Token* tok) {
|
2020-02-13 16:27:06 +01:00
|
|
|
while (tok->astParent() && astIsLHS(tok)) {
|
2020-05-19 21:03:28 +02:00
|
|
|
if (tok->astParent()->isAssignmentOp())
|
2020-02-13 16:27:06 +01:00
|
|
|
return tok->astParent();
|
|
|
|
tok = tok->astParent();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-12-18 22:07:43 +01:00
|
|
|
static Token* callExpr(Token* tok)
|
|
|
|
{
|
|
|
|
while (tok->astParent() && astIsLHS(tok)) {
|
|
|
|
if (!Token::Match(tok, "%name%|::|<|."))
|
|
|
|
break;
|
|
|
|
if (Token::simpleMatch(tok, "<") && !tok->link())
|
|
|
|
break;
|
|
|
|
tok = tok->astParent();
|
|
|
|
}
|
|
|
|
if (isFunctionCall(tok))
|
|
|
|
return tok;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
static Token* skipTo(Token* tok, const Token* dest, const Token* end = nullptr) {
|
2020-02-13 16:27:06 +01:00
|
|
|
if (end && dest->index() > end->index())
|
|
|
|
return nullptr;
|
2022-10-02 07:12:40 +02:00
|
|
|
const int i = dest->index() - tok->index();
|
2020-02-13 16:27:06 +01:00
|
|
|
if (i > 0)
|
|
|
|
return tok->tokAt(dest->index() - tok->index());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-13 17:04:05 +01:00
|
|
|
static bool isConditional(const Token* tok) {
|
2020-02-13 16:27:06 +01:00
|
|
|
const Token* parent = tok->astParent();
|
|
|
|
while (parent && !Token::Match(parent, "%oror%|&&|:")) {
|
|
|
|
tok = parent;
|
|
|
|
parent = parent->astParent();
|
|
|
|
}
|
|
|
|
return parent && (parent->str() == ":" || parent->astOperand2() == tok);
|
|
|
|
}
|
|
|
|
|
2020-09-03 07:17:36 +02:00
|
|
|
static Token* getStepTokFromEnd(Token* tok) {
|
|
|
|
if (!Token::simpleMatch(tok, "}"))
|
|
|
|
return nullptr;
|
|
|
|
Token* end = tok->link()->previous();
|
|
|
|
if (!Token::simpleMatch(end, ")"))
|
|
|
|
return nullptr;
|
|
|
|
return getStepTok(end->link());
|
|
|
|
}
|
2020-02-13 16:27:06 +01:00
|
|
|
};
|
|
|
|
|
2023-03-07 12:26:17 +01:00
|
|
|
Analyzer::Result valueFlowGenericForward(Token* start, const Token* end, const ValuePtr<Analyzer>& a, const Settings& settings)
|
2020-02-13 16:27:06 +01:00
|
|
|
{
|
2020-11-10 16:00:55 +01:00
|
|
|
ForwardTraversal ft{a, settings};
|
2020-02-13 16:27:06 +01:00
|
|
|
ft.updateRange(start, end);
|
2022-06-07 21:15:13 +02:00
|
|
|
return Analyzer::Result{ ft.actions, ft.terminate };
|
2020-02-13 16:27:06 +01:00
|
|
|
}
|
2020-11-10 16:00:55 +01:00
|
|
|
|
2023-03-07 12:26:17 +01:00
|
|
|
Analyzer::Result valueFlowGenericForward(Token* start, const ValuePtr<Analyzer>& a, const Settings& settings)
|
2020-11-10 16:00:55 +01:00
|
|
|
{
|
2022-11-12 12:47:54 +01:00
|
|
|
if (Settings::terminated())
|
|
|
|
throw TerminateException();
|
2020-11-10 16:00:55 +01:00
|
|
|
ForwardTraversal ft{a, settings};
|
|
|
|
ft.updateRecursive(start);
|
2022-06-07 21:15:13 +02:00
|
|
|
return Analyzer::Result{ ft.actions, ft.terminate };
|
2020-11-10 16:00:55 +01:00
|
|
|
}
|