|
Cppcheck
|
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 #ifndef checkH 00020 #define checkH 00021 00022 #include "config.h" 00023 #include "token.h" 00024 #include "tokenize.h" 00025 #include "settings.h" 00026 #include "errorlogger.h" 00027 00028 #include <list> 00029 #include <iostream> 00030 #include <set> 00031 00032 /// @addtogroup Core 00033 /// @{ 00034 00035 /** 00036 * @brief Interface class that cppcheck uses to communicate with the checks. 00037 * All checking classes must inherit from this class 00038 */ 00039 class CPPCHECKLIB Check { 00040 public: 00041 /** This constructor is used when registering the CheckClass */ 00042 explicit Check(const std::string &aname); 00043 00044 /** This constructor is used when running checks. */ 00045 Check(const std::string &aname, const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) 00046 : _tokenizer(tokenizer), _settings(settings), _errorLogger(errorLogger), _name(aname) 00047 { } 00048 00049 virtual ~Check() { 00050 #if !defined(DJGPP) && !defined(__sun) 00051 instances().remove(this); 00052 #endif 00053 } 00054 00055 /** List of registered check classes. This is used by Cppcheck to run checks and generate documentation */ 00056 static std::list<Check *> &instances() { 00057 static std::list<Check *> _instances; 00058 return _instances; 00059 } 00060 00061 /** 00062 * analyse code - must be thread safe 00063 * @param tokens The tokens to analyse 00064 * @param result container where results are stored 00065 */ 00066 virtual void analyse(const Token *tokens, std::set<std::string> &result) const { 00067 // suppress compiler warnings 00068 (void)tokens; 00069 (void)result; 00070 } 00071 00072 /** 00073 * Save analysis data - the caller ensures thread safety 00074 * @param data The data where the results are saved 00075 */ 00076 virtual void saveAnalysisData(const std::set<std::string> &data) const { 00077 // suppress compiler warnings 00078 (void)data; 00079 } 00080 00081 /** run checks, the token list is not simplified */ 00082 virtual void runChecks(const Tokenizer *, const Settings *, ErrorLogger *) 00083 { } 00084 00085 /** run checks, the token list is simplified */ 00086 virtual void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) = 0; 00087 00088 /** get error messages */ 00089 virtual void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const = 0; 00090 00091 /** class name, used to generate documentation */ 00092 const std::string& name() const { 00093 return _name; 00094 } 00095 00096 /** get information about this class, used to generate documentation */ 00097 virtual std::string classInfo() const = 0; 00098 00099 /** 00100 * Write given error to errorlogger or to out stream in xml format. 00101 * This is for for printout out the error list with --errorlist 00102 * @param errmsg Error message to write 00103 */ 00104 static void reportError(const ErrorLogger::ErrorMessage &errmsg) { 00105 std::cout << errmsg.toXML(true, 1) << std::endl; 00106 } 00107 00108 bool inconclusiveFlag() const { 00109 return _settings && _settings->inconclusive; 00110 } 00111 00112 protected: 00113 const Tokenizer * const _tokenizer; 00114 const Settings * const _settings; 00115 ErrorLogger * const _errorLogger; 00116 00117 /** report an error */ 00118 void reportError(const Token *tok, const Severity::SeverityType severity, const std::string &id, const std::string &msg, bool inconclusive = false) { 00119 std::list<const Token *> callstack(1, tok); 00120 reportError(callstack, severity, id, msg, inconclusive); 00121 } 00122 00123 /** report an error */ 00124 void reportError(const std::list<const Token *> &callstack, Severity::SeverityType severity, const std::string &id, const std::string& msg, bool inconclusive = false) { 00125 ErrorLogger::ErrorMessage errmsg(callstack, _tokenizer?&_tokenizer->list:0, severity, id, msg, inconclusive); 00126 if (_errorLogger) 00127 _errorLogger->reportErr(errmsg); 00128 else 00129 reportError(errmsg); 00130 } 00131 00132 private: 00133 const std::string _name; 00134 00135 /** disabled assignment operator and copy constructor */ 00136 void operator=(const Check &); 00137 Check(const Check &); 00138 }; 00139 00140 namespace std { 00141 /** compare the names of Check classes, used when sorting the Check descendants */ 00142 template <> struct less<Check *> { 00143 bool operator()(const Check *p1, const Check *p2) const { 00144 return (p1->name() < p2->name()); 00145 } 00146 }; 00147 } 00148 00149 inline Check::Check(const std::string &aname) 00150 : _tokenizer(0), _settings(0), _errorLogger(0), _name(aname) 00151 { 00152 instances().push_back(this); 00153 instances().sort(std::less<Check *>()); 00154 } 00155 00156 /// @} 00157 00158 #endif
1.7.6.1