|
| 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 | +//--------------------------------------------------------------------------- |
| 20 | +// You should not write statements with side effects in assert() |
| 21 | +//--------------------------------------------------------------------------- |
| 22 | + |
| 23 | +#include "checkassert.h" |
| 24 | +#include "symboldatabase.h" |
| 25 | + |
| 26 | +//--------------------------------------------------------------------------- |
| 27 | + |
| 28 | + |
| 29 | +// Register this check class (by creating a static instance of it) |
| 30 | +namespace { |
| 31 | + CheckAssert instance; |
| 32 | +} |
| 33 | + |
| 34 | +void CheckAssert::assertWithSideEffects() |
| 35 | +{ |
| 36 | + if (!_settings->isEnabled("warning")) |
| 37 | + return; |
| 38 | + |
| 39 | + const Token *tok = findAssertPattern(_tokenizer->tokens()); |
| 40 | + const Token *endTok = tok ? tok->next()->link() : NULL; |
| 41 | + |
| 42 | + while (tok && endTok) { |
| 43 | + for (const Token* tmp = tok->tokAt(1); tmp != endTok; tmp = tmp->next()) { |
| 44 | + checkVariableAssignment(tmp, true); |
| 45 | + |
| 46 | + if (tmp->isName() && tmp->type() == Token::eFunction) { |
| 47 | + const Function* f = tmp->function(); |
| 48 | + if (f->argCount() == 0 && f->isConst) continue; |
| 49 | + |
| 50 | + // functions with non-const references |
| 51 | + else if (f->argCount() != 0) { |
| 52 | + for (std::list<Variable>::const_iterator it = f->argumentList.begin(); it != f->argumentList.end(); ++it) { |
| 53 | + if (it->isConst() || it->isLocal()) continue; |
| 54 | + else if (it->isReference()) { |
| 55 | + const Token* next = it->nameToken()->next(); |
| 56 | + bool isAssigned = checkVariableAssignment(next, false); |
| 57 | + if (isAssigned) |
| 58 | + sideEffectInAssertError(tmp, f->name()); |
| 59 | + continue; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // variables in function scope |
| 65 | + const Scope* scope = f->functionScope; |
| 66 | + if (!scope) continue; |
| 67 | + |
| 68 | + for (const Token *tok2 = scope->classStart; tok2 != scope->classEnd; tok2 = tok2->next()) { |
| 69 | + if (tok2 && tok2->type() != Token::eAssignmentOp && tok2->type() != Token::eIncDecOp) continue; |
| 70 | + const Variable* var = tok2->previous()->variable(); |
| 71 | + if (!var) continue; |
| 72 | + |
| 73 | + std::vector<const Token*> returnTokens; // find all return statements |
| 74 | + for (const Token *rt = scope->classStart; rt != scope->classEnd; rt = rt->next()) { |
| 75 | + if (!Token::Match(rt, "return %any%")) continue; |
| 76 | + returnTokens.push_back(rt); |
| 77 | + } |
| 78 | + |
| 79 | + bool noReturnInScope = true; |
| 80 | + for (std::vector<const Token*>::iterator rt = returnTokens.begin(); rt != returnTokens.end(); ++rt) { |
| 81 | + noReturnInScope &= !inSameScope(*rt, tok2); |
| 82 | + } |
| 83 | + if (noReturnInScope) continue; |
| 84 | + bool isAssigned = checkVariableAssignment(tok2, false); |
| 85 | + if (isAssigned) |
| 86 | + sideEffectInAssertError(tmp, f->name()); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + tok = findAssertPattern(endTok->next()); |
| 92 | + endTok = tok ? tok->next()->link() : NULL; |
| 93 | + } |
| 94 | +} |
| 95 | +//--------------------------------------------------------------------------- |
| 96 | + |
| 97 | + |
| 98 | +void CheckAssert::sideEffectInAssertError(const Token *tok, const std::string& functionName) |
| 99 | +{ |
| 100 | + reportError(tok, Severity::warning, |
| 101 | + "assertWithSideEffect", "Assert statement calls a function which may have desired side effects: '" + functionName + "'.\n" |
| 102 | + "Non-pure function: '" + functionName + "' is called inside assert statement. " |
| 103 | + "Assert statements are removed from release builds so the code inside " |
| 104 | + "assert statement is not executed. If the code is needed also in release " |
| 105 | + "builds, this is a bug."); |
| 106 | +} |
| 107 | + |
| 108 | +void CheckAssert::assignmentInAssertError(const Token *tok, const std::string& varname) |
| 109 | +{ |
| 110 | + reportError(tok, Severity::warning, |
| 111 | + "assignmentInAssert", "Assert statement modifies '" + varname + "'.\n" |
| 112 | + "Variable '" + varname + "' is modified insert assert statement. " |
| 113 | + "Assert statements are removed from release builds so the code inside " |
| 114 | + "assert statement is not executed. If the code is needed also in release " |
| 115 | + "builds, this is a bug."); |
| 116 | +} |
| 117 | + |
| 118 | +// checks if side effects happen on the variable prior to tmp |
| 119 | +bool CheckAssert::checkVariableAssignment(const Token* assignTok, bool reportError /*= true*/) |
| 120 | +{ |
| 121 | + const Variable* v = assignTok->previous()->variable(); |
| 122 | + if (!v) return false; |
| 123 | + |
| 124 | + // assignment |
| 125 | + if (assignTok->isAssignmentOp() || assignTok->type() == Token::eIncDecOp) { |
| 126 | + |
| 127 | + if (v->isConst()) return false; |
| 128 | + |
| 129 | + if (reportError) // report as variable assignment error |
| 130 | + assignmentInAssertError(assignTok, v->name()); |
| 131 | + |
| 132 | + return true; |
| 133 | + } |
| 134 | + return false; |
| 135 | + // TODO: function calls on v |
| 136 | +} |
| 137 | + |
| 138 | +const Token* CheckAssert::findAssertPattern(const Token* start) |
| 139 | +{ |
| 140 | + return Token::findmatch(start, "assert ( %any%"); |
| 141 | +} |
| 142 | + |
| 143 | +bool CheckAssert::inSameScope(const Token* returnTok, const Token* assignTok) |
| 144 | +{ |
| 145 | + // TODO: even if a return is in the same scope, the assignment might not affect it. |
| 146 | + return returnTok->scope() == assignTok->scope(); |
| 147 | +} |
0 commit comments