|
| 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 "findtoken.h" |
| 20 | + |
| 21 | +#include "astutils.h" |
| 22 | +#include "token.h" |
| 23 | + |
| 24 | +template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )> |
| 25 | +static bool findTokensSkipDeadCodeImpl(const Library& library, |
| 26 | + T* start, |
| 27 | + const Token* end, |
| 28 | + const std::function<bool(const Token*)>& pred, |
| 29 | + const std::function<bool(T*)>& found, |
| 30 | + const std::function<std::vector<MathLib::bigint>(const Token*)>& evaluate, |
| 31 | + bool skipUnevaluated) |
| 32 | +{ |
| 33 | + for (T* tok = start; precedes(tok, end); tok = tok->next()) { |
| 34 | + if (pred(tok)) { |
| 35 | + if (found(tok)) |
| 36 | + return true; |
| 37 | + } |
| 38 | + if (Token::Match(tok, "if|for|while (") && Token::simpleMatch(tok->linkAt(1), ") {")) { |
| 39 | + const Token* condTok = getCondTok(tok); |
| 40 | + if (!condTok) |
| 41 | + continue; |
| 42 | + auto result = evaluate(condTok); |
| 43 | + if (result.empty()) |
| 44 | + continue; |
| 45 | + if (internal::findTokensSkipDeadCodeImpl(library, tok->next(), tok->linkAt(1), pred, found, evaluate, skipUnevaluated)) |
| 46 | + return true; |
| 47 | + T* thenStart = tok->linkAt(1)->next(); |
| 48 | + T* elseStart = nullptr; |
| 49 | + if (Token::simpleMatch(thenStart->link(), "} else {")) |
| 50 | + elseStart = thenStart->link()->tokAt(2); |
| 51 | + |
| 52 | + auto r = result.front(); |
| 53 | + if (r == 0) { |
| 54 | + if (elseStart) { |
| 55 | + if (internal::findTokensSkipDeadCodeImpl(library, elseStart, elseStart->link(), pred, found, evaluate, skipUnevaluated)) |
| 56 | + return true; |
| 57 | + if (isReturnScope(elseStart->link(), library)) |
| 58 | + return true; |
| 59 | + tok = elseStart->link(); |
| 60 | + } else { |
| 61 | + tok = thenStart->link(); |
| 62 | + } |
| 63 | + } else { |
| 64 | + if (internal::findTokensSkipDeadCodeImpl(library, thenStart, thenStart->link(), pred, found, evaluate, skipUnevaluated)) |
| 65 | + return true; |
| 66 | + if (isReturnScope(thenStart->link(), library)) |
| 67 | + return true; |
| 68 | + tok = thenStart->link(); |
| 69 | + } |
| 70 | + } else if (Token::Match(tok->astParent(), "&&|?|%oror%") && astIsLHS(tok)) { |
| 71 | + auto result = evaluate(tok); |
| 72 | + if (result.empty()) |
| 73 | + continue; |
| 74 | + const bool cond = result.front() != 0; |
| 75 | + T* next = nullptr; |
| 76 | + if ((cond && Token::simpleMatch(tok->astParent(), "||")) || |
| 77 | + (!cond && Token::simpleMatch(tok->astParent(), "&&"))) { |
| 78 | + next = nextAfterAstRightmostLeaf(tok->astParent()); |
| 79 | + } else if (Token::simpleMatch(tok->astParent(), "?")) { |
| 80 | + T* colon = tok->astParent()->astOperand2(); |
| 81 | + if (!cond) { |
| 82 | + next = colon; |
| 83 | + } else { |
| 84 | + if (internal::findTokensSkipDeadCodeImpl(library, tok->astParent()->next(), colon, pred, found, evaluate, skipUnevaluated)) |
| 85 | + return true; |
| 86 | + next = nextAfterAstRightmostLeaf(colon); |
| 87 | + } |
| 88 | + } |
| 89 | + if (next) |
| 90 | + tok = next; |
| 91 | + } else if (Token::simpleMatch(tok, "} else {")) { |
| 92 | + const Token* condTok = getCondTokFromEnd(tok); |
| 93 | + if (!condTok) |
| 94 | + continue; |
| 95 | + auto result = evaluate(condTok); |
| 96 | + if (result.empty()) |
| 97 | + continue; |
| 98 | + if (isReturnScope(tok->link(), library)) |
| 99 | + return true; |
| 100 | + auto r = result.front(); |
| 101 | + if (r != 0) { |
| 102 | + tok = tok->linkAt(2); |
| 103 | + } |
| 104 | + } else if (Token::simpleMatch(tok, "[") && Token::Match(tok->link(), "] (|{")) { |
| 105 | + T* afterCapture = tok->link()->next(); |
| 106 | + if (Token::simpleMatch(afterCapture, "(") && afterCapture->link()) |
| 107 | + tok = afterCapture->link()->next(); |
| 108 | + else |
| 109 | + tok = afterCapture; |
| 110 | + } |
| 111 | + if (skipUnevaluated && isUnevaluated(tok)) { |
| 112 | + T *next = tok->linkAt(1); |
| 113 | + if (!next) |
| 114 | + continue; |
| 115 | + tok = next; |
| 116 | + } |
| 117 | + } |
| 118 | + return false; |
| 119 | +} |
| 120 | + |
| 121 | +namespace internal { |
| 122 | + bool findTokensSkipDeadCodeImpl(const Library& library, |
| 123 | + Token* start, |
| 124 | + const Token* end, |
| 125 | + const std::function<bool(const Token*)>& pred, |
| 126 | + const std::function<bool(Token*)>& found, |
| 127 | + const std::function<std::vector<MathLib::bigint>(const Token*)>& evaluate, |
| 128 | + bool skipUnevaluated) |
| 129 | + { |
| 130 | + return ::findTokensSkipDeadCodeImpl(library, start, end, pred, found, evaluate, skipUnevaluated); |
| 131 | + } |
| 132 | + |
| 133 | + bool findTokensSkipDeadCodeImpl(const Library& library, |
| 134 | + const Token* start, |
| 135 | + const Token* end, |
| 136 | + const std::function<bool(const Token*)>& pred, |
| 137 | + const std::function<bool(const Token*)>& found, |
| 138 | + const std::function<std::vector<MathLib::bigint>(const Token*)>& evaluate, |
| 139 | + bool skipUnevaluated) |
| 140 | + { |
| 141 | + return ::findTokensSkipDeadCodeImpl(library, start, end, pred, found, evaluate, skipUnevaluated); |
| 142 | + } |
| 143 | +} |
0 commit comments