Cppcheck
checkinternal.h
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 //---------------------------------------------------------------------------
00021 #ifndef CHECKINTERNAL_H
00022 #define CHECKINTERNAL_H
00023 //---------------------------------------------------------------------------
00024 
00025 #include "check.h"
00026 #include "config.h"
00027 
00028 class Token;
00029 
00030 /// @addtogroup Checks
00031 /// @{
00032 
00033 
00034 /** @brief %Check Internal cppcheck API usage */
00035 class CPPCHECKLIB CheckInternal : public Check {
00036 public:
00037     /** This constructor is used when registering the CheckClass */
00038     CheckInternal() : Check(myName())
00039     { }
00040 
00041     /** This constructor is used when running checks. */
00042     CheckInternal(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
00043         : Check(myName(), tokenizer, settings, errorLogger)
00044     { }
00045 
00046     /** Simplified checks. The token list is simplified. */
00047     void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
00048         if (!settings->isEnabled("internal"))
00049             return;
00050 
00051         CheckInternal checkInternal(tokenizer, settings, errorLogger);
00052 
00053         checkInternal.checkTokenMatchPatterns();
00054         checkInternal.checkTokenSimpleMatchPatterns();
00055         checkInternal.checkMissingPercentCharacter();
00056         checkInternal.checkUnknownPattern();
00057         checkInternal.checkRedundantNextPrevious();
00058     }
00059 
00060     /** @brief %Check if a simple pattern is used inside Token::Match or Token::findmatch */
00061     void checkTokenMatchPatterns();
00062 
00063     /** @brief %Check if a complex pattern is used inside Token::simpleMatch or Token::findsimplematch */
00064     void checkTokenSimpleMatchPatterns();
00065 
00066     /** @brief %Check for missing % end character in Token::Match pattern */
00067     void checkMissingPercentCharacter();
00068 
00069     /** @brief %Check for unknown (invalid) complex patterns like "%typ%" */
00070     void checkUnknownPattern();
00071 
00072     /** @brief %Check for inefficient usage of Token::next(), Token::previous() and Token::tokAt() */
00073     void checkRedundantNextPrevious();
00074 
00075 private:
00076     void simplePatternError(const Token *tok, const std::string &pattern, const std::string &funcname);
00077     void complexPatternError(const Token *tok, const std::string &pattern, const std::string &funcname);
00078     void missingPercentCharacterError(const Token *tok, const std::string &pattern, const std::string &funcname);
00079     void unknownPatternError(const Token* tok, const std::string& pattern);
00080     void redundantNextPreviousError(const Token* tok, const std::string& func1, const std::string& func2);
00081 
00082     void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
00083         CheckInternal c(0, settings, errorLogger);
00084         c.simplePatternError(0, "class {", "Match");
00085         c.complexPatternError(0, "%type% ( )", "Match");
00086         c.missingPercentCharacterError(0, "%num", "Match");
00087         c.unknownPatternError(0, "%typ");
00088         c.redundantNextPreviousError(0, "previous", "next");
00089     }
00090 
00091     static std::string myName() {
00092         return "cppcheck internal API usage";
00093     }
00094 
00095     std::string classInfo() const {
00096         // Don't include these checks on the WIKI where people can read what
00097         // checks there are. These checks are not intended for users.
00098         return "";
00099     }
00100 };
00101 /// @}
00102 //---------------------------------------------------------------------------
00103 #endif