|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2013 Daniel Marjamäki and 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 "valueflow.h" |
| 20 | +#include "token.h" |
| 21 | +#include "mathlib.h" |
| 22 | + |
| 23 | +static void valueFlowBeforeCondition(Token *tokens) |
| 24 | +{ |
| 25 | + for (Token *tok = tokens; tok; tok = tok->next()) { |
| 26 | + unsigned int varid; |
| 27 | + MathLib::bigint num; |
| 28 | + if (Token::Match(tok, "==|!=|>=|<=") && tok->astOperand2()) { |
| 29 | + if (tok->astOperand1()->isName() && tok->astOperand2()->isNumber()) { |
| 30 | + varid = tok->astOperand1()->varId(); |
| 31 | + } else if (tok->astOperand1()->isNumber() && tok->astOperand2()->isName()) { |
| 32 | + varid = tok->astOperand2()->varId(); |
| 33 | + num = MathLib::toLongNumber(tok->astOperand1()->str()); |
| 34 | + } else { |
| 35 | + continue; |
| 36 | + } |
| 37 | + } else if (Token::Match(tok->previous(), "if|while ( %var% %oror%|&&|)") || |
| 38 | + Token::Match(tok, "%oror%|&& %var% %oror%|&&|)")) { |
| 39 | + varid = tok->next()->varId(); |
| 40 | + num = 0; |
| 41 | + } else if (tok->str() == "!" && tok->astOperand1() && tok->astOperand1()->isName()) { |
| 42 | + varid = tok->astOperand1()->varId(); |
| 43 | + num = 0; |
| 44 | + } else { |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + if (varid == 0U) |
| 49 | + continue; |
| 50 | + |
| 51 | + struct ValueFlow::Value val; |
| 52 | + val.link = tok; |
| 53 | + val.intvalue = MathLib::toLongNumber(tok->astOperand2()->str()); |
| 54 | + |
| 55 | + for (Token *tok2 = tok->previous(); tok2; tok2 = tok2->previous()) { |
| 56 | + if (tok2->varId() == varid) |
| 57 | + tok2->values.push_back(val); |
| 58 | + if (tok2->str() == "{") { |
| 59 | + if (!Token::simpleMatch(tok2->previous(), ") {")) |
| 60 | + break; |
| 61 | + if (!Token::simpleMatch(tok2->previous()->link()->previous(), "if (")) |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +void ValueFlow::setValues(Token *tokens) |
| 69 | +{ |
| 70 | + for (Token *tok = tokens; tok; tok = tok->next()) |
| 71 | + tok->values.clear(); |
| 72 | + |
| 73 | + valueFlowBeforeCondition(tokens); |
| 74 | +} |
0 commit comments