2011-10-28 22:39:46 +02:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2016-01-01 14:34:45 +01:00
|
|
|
* Copyright (C) 2007-2016 Cppcheck team.
|
2011-10-28 22:39:46 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#ifndef checkinternalH
|
|
|
|
#define checkinternalH
|
2011-10-28 22:39:46 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "check.h"
|
2012-06-10 14:19:09 +02:00
|
|
|
#include "config.h"
|
2011-10-28 22:39:46 +02:00
|
|
|
|
|
|
|
/// @addtogroup Checks
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief %Check Internal cppcheck API usage */
|
2012-06-10 14:19:09 +02:00
|
|
|
class CPPCHECKLIB CheckInternal : public Check {
|
2011-10-28 22:39:46 +02:00
|
|
|
public:
|
|
|
|
/** This constructor is used when registering the CheckClass */
|
2014-11-20 14:20:09 +01:00
|
|
|
CheckInternal() : Check(myName()) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2011-10-28 22:39:46 +02:00
|
|
|
|
|
|
|
/** This constructor is used when running checks. */
|
|
|
|
CheckInternal(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
|
2014-11-20 14:20:09 +01:00
|
|
|
: Check(myName(), tokenizer, settings, errorLogger) {
|
2013-08-07 16:27:37 +02:00
|
|
|
}
|
2011-10-28 22:39:46 +02:00
|
|
|
|
|
|
|
/** Simplified checks. The token list is simplified. */
|
2014-11-20 14:20:09 +01:00
|
|
|
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger) {
|
2017-04-11 11:49:09 +02:00
|
|
|
if (!settings->isEnabled(Settings::INTERNAL))
|
2011-10-28 22:39:46 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
CheckInternal checkInternal(tokenizer, settings, errorLogger);
|
|
|
|
|
|
|
|
checkInternal.checkTokenMatchPatterns();
|
|
|
|
checkInternal.checkTokenSimpleMatchPatterns();
|
2011-10-29 12:13:46 +02:00
|
|
|
checkInternal.checkMissingPercentCharacter();
|
2012-07-11 17:45:16 +02:00
|
|
|
checkInternal.checkUnknownPattern();
|
2012-09-07 12:36:40 +02:00
|
|
|
checkInternal.checkRedundantNextPrevious();
|
2014-12-30 14:21:18 +01:00
|
|
|
checkInternal.checkExtraWhitespace();
|
2017-05-18 15:21:29 +02:00
|
|
|
checkInternal.checkRedundantTokCheck();
|
2011-10-28 22:39:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief %Check if a simple pattern is used inside Token::Match or Token::findmatch */
|
|
|
|
void checkTokenMatchPatterns();
|
|
|
|
|
|
|
|
/** @brief %Check if a complex pattern is used inside Token::simpleMatch or Token::findsimplematch */
|
|
|
|
void checkTokenSimpleMatchPatterns();
|
|
|
|
|
2011-10-29 12:13:46 +02:00
|
|
|
/** @brief %Check for missing % end character in Token::Match pattern */
|
|
|
|
void checkMissingPercentCharacter();
|
|
|
|
|
2012-09-07 12:36:40 +02:00
|
|
|
/** @brief %Check for unknown (invalid) complex patterns like "%typ%" */
|
2012-07-11 17:45:16 +02:00
|
|
|
void checkUnknownPattern();
|
|
|
|
|
2012-09-07 12:36:40 +02:00
|
|
|
/** @brief %Check for inefficient usage of Token::next(), Token::previous() and Token::tokAt() */
|
|
|
|
void checkRedundantNextPrevious();
|
|
|
|
|
2014-12-30 14:21:18 +01:00
|
|
|
/** @brief %Check if there is whitespace at the beginning or at the end of a pattern */
|
|
|
|
void checkExtraWhitespace();
|
|
|
|
|
2017-05-18 15:21:29 +02:00
|
|
|
/** @brief %Check if there is a redundant check for none-nullness of parameter before Match functions, such as (tok && Token::Match(tok, "foo")) */
|
|
|
|
void checkRedundantTokCheck();
|
2011-10-28 22:39:46 +02:00
|
|
|
private:
|
2014-01-05 21:15:41 +01:00
|
|
|
void multiComparePatternError(const Token *tok, const std::string &pattern, const std::string &funcname);
|
2011-10-28 22:39:46 +02:00
|
|
|
void simplePatternError(const Token *tok, const std::string &pattern, const std::string &funcname);
|
|
|
|
void complexPatternError(const Token *tok, const std::string &pattern, const std::string &funcname);
|
2011-10-29 12:13:46 +02:00
|
|
|
void missingPercentCharacterError(const Token *tok, const std::string &pattern, const std::string &funcname);
|
2012-07-11 17:45:16 +02:00
|
|
|
void unknownPatternError(const Token* tok, const std::string& pattern);
|
2012-09-07 12:36:40 +02:00
|
|
|
void redundantNextPreviousError(const Token* tok, const std::string& func1, const std::string& func2);
|
2014-07-02 15:50:49 +02:00
|
|
|
void orInComplexPattern(const Token *tok, const std::string &pattern, const std::string &funcname);
|
2014-12-30 14:21:18 +01:00
|
|
|
void extraWhitespaceError(const Token *tok, const std::string &pattern, const std::string &funcname);
|
2017-05-18 15:21:29 +02:00
|
|
|
void checkRedundantTokCheckError(const Token *tok);
|
2011-10-28 22:39:46 +02:00
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
|
2016-05-07 16:30:54 +02:00
|
|
|
CheckInternal c(nullptr, settings, errorLogger);
|
|
|
|
c.multiComparePatternError(nullptr, ";|%type%", "Match");
|
|
|
|
c.simplePatternError(nullptr, "class {", "Match");
|
|
|
|
c.complexPatternError(nullptr, "%type% ( )", "Match");
|
|
|
|
c.missingPercentCharacterError(nullptr, "%num", "Match");
|
|
|
|
c.unknownPatternError(nullptr, "%typ");
|
|
|
|
c.redundantNextPreviousError(nullptr, "previous", "next");
|
|
|
|
c.orInComplexPattern(nullptr, "||", "Match");
|
|
|
|
c.extraWhitespaceError(nullptr, "%str% ", "Match");
|
2017-05-18 15:21:29 +02:00
|
|
|
c.checkRedundantTokCheckError(nullptr);
|
2011-10-28 22:39:46 +02:00
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
static std::string myName() {
|
2011-10-28 22:39:46 +02:00
|
|
|
return "cppcheck internal API usage";
|
|
|
|
}
|
|
|
|
|
2014-11-20 14:20:09 +01:00
|
|
|
std::string classInfo() const {
|
2011-12-10 18:39:25 +01:00
|
|
|
// Don't include these checks on the WIKI where people can read what
|
|
|
|
// checks there are. These checks are not intended for users.
|
|
|
|
return "";
|
2011-10-28 22:39:46 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
/// @}
|
|
|
|
//---------------------------------------------------------------------------
|
2013-09-04 20:59:49 +02:00
|
|
|
#endif // checkinternalH
|