|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2024 Cppcheck team. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "vf_forward.h" |
| 20 | + |
| 21 | +#include "forwardanalyzer.h" |
| 22 | +#include "settings.h" |
| 23 | +#include "symboldatabase.h" |
| 24 | +#include "token.h" |
| 25 | +#include "vfvalue.h" |
| 26 | + |
| 27 | +#include "vf_common.h" |
| 28 | +#include "vf_analyzers.h" |
| 29 | + |
| 30 | +#include <utility> |
| 31 | + |
| 32 | +namespace ValueFlow |
| 33 | +{ |
| 34 | + Analyzer::Result valueFlowForward(Token* startToken, |
| 35 | + const Token* endToken, |
| 36 | + const Token* exprTok, |
| 37 | + ValueFlow::Value value, |
| 38 | + const TokenList& tokenlist, |
| 39 | + ErrorLogger& errorLogger, |
| 40 | + const Settings& settings, |
| 41 | + SourceLocation loc) |
| 42 | + { |
| 43 | + if (settings.debugnormal) |
| 44 | + setSourceLocation(value, loc, startToken); |
| 45 | + return valueFlowGenericForward(startToken, |
| 46 | + endToken, |
| 47 | + makeAnalyzer(exprTok, std::move(value), settings), |
| 48 | + tokenlist, |
| 49 | + errorLogger, |
| 50 | + settings); |
| 51 | + } |
| 52 | + |
| 53 | + Analyzer::Result valueFlowForward(Token* startToken, |
| 54 | + const Token* endToken, |
| 55 | + const Token* exprTok, |
| 56 | + std::list<ValueFlow::Value> values, |
| 57 | + const TokenList& tokenlist, |
| 58 | + ErrorLogger& errorLogger, |
| 59 | + const Settings& settings, |
| 60 | + SourceLocation loc) |
| 61 | + { |
| 62 | + Analyzer::Result result{}; |
| 63 | + for (ValueFlow::Value& v : values) { |
| 64 | + result.update(valueFlowForward(startToken, endToken, exprTok, std::move(v), tokenlist, errorLogger, settings, loc)); |
| 65 | + } |
| 66 | + return result; |
| 67 | + } |
| 68 | + |
| 69 | + Analyzer::Result valueFlowForward(Token* startToken, |
| 70 | + const Token* exprTok, |
| 71 | + ValueFlow::Value v, |
| 72 | + const TokenList& tokenlist, |
| 73 | + ErrorLogger& errorLogger, |
| 74 | + const Settings& settings, |
| 75 | + SourceLocation loc) |
| 76 | + { |
| 77 | + const Token* endToken = nullptr; |
| 78 | + const Function* f = Scope::nestedInFunction(startToken->scope()); |
| 79 | + if (f && f->functionScope) |
| 80 | + endToken = f->functionScope->bodyEnd; |
| 81 | + if (!endToken && exprTok && exprTok->variable() && !exprTok->variable()->isLocal()) |
| 82 | + endToken = startToken->scope()->bodyEnd; |
| 83 | + return valueFlowForward(startToken, endToken, exprTok, std::move(v), tokenlist, errorLogger, settings, loc); |
| 84 | + } |
| 85 | + |
| 86 | + Analyzer::Result valueFlowForwardRecursive(Token* top, |
| 87 | + const Token* exprTok, |
| 88 | + std::list<ValueFlow::Value> values, |
| 89 | + const TokenList& tokenlist, |
| 90 | + ErrorLogger& errorLogger, |
| 91 | + const Settings& settings, |
| 92 | + SourceLocation loc) |
| 93 | + { |
| 94 | + Analyzer::Result result{}; |
| 95 | + for (ValueFlow::Value& v : values) { |
| 96 | + if (settings.debugnormal) |
| 97 | + setSourceLocation(v, loc, top); |
| 98 | + result.update( |
| 99 | + valueFlowGenericForward(top, makeAnalyzer(exprTok, std::move(v), settings), tokenlist, errorLogger, settings)); |
| 100 | + } |
| 101 | + return result; |
| 102 | + } |
| 103 | +} |
0 commit comments