Cppcheck
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
checkobsolescentfunctions.cpp
Go to the documentation of this file.
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 // Obsolete functions
21 //---------------------------------------------------------------------------
22 
24 #include "symboldatabase.h"
25 
26 //---------------------------------------------------------------------------
27 
28 std::map<std::string, std::string> CheckObsoleteFunctions::_obsoleteStandardFunctions;
29 std::map<std::string, std::string> CheckObsoleteFunctions::_obsoletePosixFunctions;
30 std::map<std::string, std::string> CheckObsoleteFunctions::_obsoleteC99Functions;
31 
32 
33 // Register this check class (by creating a static instance of it)
34 namespace {
35  CheckObsoleteFunctions instance;
36 }
37 
39 {
40  if (!_settings->isEnabled("style"))
41  return;
42 
43  const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
44  const bool cStandard = _settings->standards.c >= Standards::C99 ;
45 
46  for (unsigned int i = 0; i < symbolDatabase->functionScopes.size(); i++) {
47  const Scope* scope = symbolDatabase->functionScopes[i];
48  for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) {
49  if (tok->isName() && tok->varId()==0 && (!tok->function() || !tok->function()->hasBody()) && tok->strAt(1) == "(" &&
50  tok->strAt(-1) != "." && (!Token::Match(tok->tokAt(-2), "%name% ::") || Token::simpleMatch(tok->tokAt(-2), "std ::"))) {
51 
52  std::map<std::string,std::string>::const_iterator it = _obsoleteStandardFunctions.find(tok->str());
53  if (it != _obsoleteStandardFunctions.end()) {
54  // If checking an old code base it might be uninteresting to update obsolete functions.
55  reportError(tok, Severity::style, "obsoleteFunctions"+it->first, it->second);
56  } else {
57  if (_settings->standards.posix) {
58  it = _obsoletePosixFunctions.find(tok->str());
59  if (it != _obsoletePosixFunctions.end()) {
60  // If checking an old code base it might be uninteresting to update obsolete functions.
61  reportError(tok, Severity::style, "obsoleteFunctions"+it->first, it->second);
62  }
63  }
64  if (cStandard) {
65  // alloca : this function is obsolete in C but not in C++ (#4382)
66  it = _obsoleteC99Functions.find(tok->str());
67  if (it != _obsoleteC99Functions.end() && !(tok->str() == "alloca" && _tokenizer->isCPP())) {
68  reportError(tok, Severity::style, "obsoleteFunctions"+it->first, it->second);
69  }
70  }
71  }
72  }
73  }
74  }
75 }
76 //---------------------------------------------------------------------------