|
| 1 | +/* |
| 2 | + * Cppcheck - A tool for static C/C++ code analysis |
| 3 | + * Copyright (C) 2007-2015 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 | +// Check functions |
| 21 | +//--------------------------------------------------------------------------- |
| 22 | + |
| 23 | +#include "checkfunctions.h" |
| 24 | +#include "symboldatabase.h" |
| 25 | + |
| 26 | +//--------------------------------------------------------------------------- |
| 27 | + |
| 28 | + |
| 29 | +// Register this check class (by creating a static instance of it) |
| 30 | +namespace { |
| 31 | + CheckFunctions instance; |
| 32 | +} |
| 33 | + |
| 34 | +void CheckFunctions::check() |
| 35 | +{ |
| 36 | + const bool checkAlloca = (_settings->standards.c >= Standards::C99 && _tokenizer->isC()) || _settings->standards.cpp >= Standards::CPP11; |
| 37 | + |
| 38 | + const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); |
| 39 | + for (unsigned int i = 0; i < symbolDatabase->functionScopes.size(); i++) { |
| 40 | + const Scope* scope = symbolDatabase->functionScopes[i]; |
| 41 | + for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { |
| 42 | + if (tok->isName() && tok->varId() == 0 && tok->strAt(1) == "(") { |
| 43 | + // alloca() is special as it depends on the code being C or C++, so it is not in Library |
| 44 | + if (checkAlloca && Token::Match(tok, "alloca (") && (!tok->function() || tok->function()->nestedIn->type == Scope::eGlobal)) { |
| 45 | + if (_tokenizer->isC()) |
| 46 | + reportError(tok, Severity::warning, "allocaCalled", |
| 47 | + "Obsolete function 'alloca' called. In C99 and later it is recommended to use a variable length array instead.\n" |
| 48 | + "The obsolete function 'alloca' is called. In C99 and later it is recommended to use a variable length array or " |
| 49 | + "a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons " |
| 50 | + "(http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca)."); |
| 51 | + else |
| 52 | + reportError(tok, Severity::warning, "allocaCalled", |
| 53 | + "Obsolete function 'alloca' called. In C++11 and later it is recommended to use std::array<> instead.\n" |
| 54 | + "The obsolete function 'alloca' is called. In C++11 and later it is recommended to use std::array<> or " |
| 55 | + "a dynamically allocated array instead. The function 'alloca' is dangerous for many reasons " |
| 56 | + "(http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice and http://linux.die.net/man/3/alloca)."); |
| 57 | + } else { |
| 58 | + if (tok->function() && tok->function()->hasBody()) |
| 59 | + continue; |
| 60 | + |
| 61 | + const Library::WarnInfo* wi = _settings->library.getWarnInfo(tok); |
| 62 | + if (wi) { |
| 63 | + if (_settings->isEnabled(Severity::toString(wi->severity)) && _settings->standards.c >= wi->standards.c && _settings->standards.cpp >= wi->standards.cpp) { |
| 64 | + reportError(tok, wi->severity, tok->str() + "Called", wi->message); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments