Cppcheck
checkobsoletefunctions.cpp
Go to the documentation of this file.
00001 /*
00002  * Cppcheck - A tool for static C/C++ code analysis
00003  * Copyright (C) 2007-2013 Daniel Marjamäki and Cppcheck team.
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation, either version 3 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017  */
00018 
00019 //---------------------------------------------------------------------------
00020 // Obsolete functions
00021 //---------------------------------------------------------------------------
00022 
00023 #include "checkobsoletefunctions.h"
00024 #include "symboldatabase.h"
00025 
00026 //---------------------------------------------------------------------------
00027 
00028 
00029 // Register this check class (by creating a static instance of it)
00030 namespace {
00031     CheckObsoleteFunctions instance;
00032 }
00033 
00034 void CheckObsoleteFunctions::obsoleteFunctions()
00035 {
00036     if (!_settings->isEnabled("style"))
00037         return;
00038 
00039     const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
00040 
00041     // Functions defined somewhere?
00042     for (unsigned int i = 0; i < symbolDatabase->functionScopes.size(); i++) {
00043         const Scope* scope = symbolDatabase->functionScopes[i];
00044         _obsoleteStandardFunctions.erase(scope->className);
00045         _obsoletePosixFunctions.erase(scope->className);
00046         _obsoleteC99Functions.erase(scope->className);
00047     }
00048 
00049     for (unsigned int i = 0; i < symbolDatabase->functionScopes.size(); i++) {
00050         const Scope* scope = symbolDatabase->functionScopes[i];
00051         for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
00052             if (tok->isName() && tok->varId()==0 && (tok->next() && tok->next()->str() == "(") &&
00053                 (!Token::Match(tok->previous(), ".|::") || Token::simpleMatch(tok->tokAt(-2), "std ::"))) {
00054 
00055                 std::map<std::string,std::string>::const_iterator it = _obsoleteStandardFunctions.find(tok->str());
00056                 if (it != _obsoleteStandardFunctions.end()) {
00057                     // If checking an old code base it might be uninteresting to update obsolete functions.
00058                     reportError(tok, Severity::style, "obsoleteFunctions"+it->first, it->second);
00059                 } else {
00060                     if (_settings->standards.posix) {
00061                         it = _obsoletePosixFunctions.find(tok->str());
00062                         if (it != _obsoletePosixFunctions.end()) {
00063                             // If checking an old code base it might be uninteresting to update obsolete functions.
00064                             reportError(tok, Severity::style, "obsoleteFunctions"+it->first, it->second);
00065                         }
00066                     }
00067                     if (_settings->standards.c >= Standards::C99) {
00068                         // alloca : this function is obsolete in C but not in C++ (#4382)
00069                         it = _obsoleteC99Functions.find(tok->str());
00070                         if (it != _obsoleteC99Functions.end() && !(tok->str() == "alloca" && _tokenizer->isCPP())) {
00071                             reportError(tok, Severity::style, "obsoleteFunctions"+it->first, it->second);
00072                         }
00073                     }
00074                 }
00075             }
00076         }
00077     }
00078 }
00079 //---------------------------------------------------------------------------