Cppcheck
checkinternal.h
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2024 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 //---------------------------------------------------------------------------
21 #ifndef checkinternalH
22 #define checkinternalH
23 //---------------------------------------------------------------------------
24 
25 #include "check.h"
26 #include "config.h"
27 #include "errortypes.h"
28 #include "settings.h"
29 #include "tokenize.h"
30 
31 #include <string>
32 
33 class ErrorLogger;
34 class Token;
35 
36 /// @addtogroup Checks
37 /// @{
38 
39 
40 /** @brief %Check Internal cppcheck API usage */
42 public:
43  /** This constructor is used when registering the CheckClass */
44  CheckInternal() : Check(myName()) {}
45 
46 private:
47  /** This constructor is used when running checks. */
48  CheckInternal(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
49  : Check(myName(), tokenizer, settings, errorLogger) {}
50 
51  void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
53  return;
54 
55  CheckInternal checkInternal(&tokenizer, &tokenizer.getSettings(), errorLogger);
56 
57  checkInternal.checkTokenMatchPatterns();
58  checkInternal.checkTokenSimpleMatchPatterns();
59  checkInternal.checkMissingPercentCharacter();
60  checkInternal.checkUnknownPattern();
61  checkInternal.checkRedundantNextPrevious();
62  checkInternal.checkExtraWhitespace();
63  checkInternal.checkRedundantTokCheck();
64  }
65 
66  /** @brief %Check if a simple pattern is used inside Token::Match or Token::findmatch */
68 
69  /** @brief %Check if a complex pattern is used inside Token::simpleMatch or Token::findsimplematch */
71 
72  /** @brief %Check for missing % end character in Token::Match pattern */
74 
75  /** @brief %Check for unknown (invalid) complex patterns like "%typ%" */
77 
78  /** @brief %Check for inefficient usage of Token::next(), Token::previous() and Token::tokAt() */
80 
81  /** @brief %Check if there is whitespace at the beginning or at the end of a pattern */
83 
84  /** @brief %Check if there is a redundant check for none-nullness of parameter before Match functions, such as (tok && Token::Match(tok, "foo")) */
86 
87  void multiComparePatternError(const Token *tok, const std::string &pattern, const std::string &funcname);
88  void simplePatternError(const Token *tok, const std::string &pattern, const std::string &funcname);
89  void complexPatternError(const Token *tok, const std::string &pattern, const std::string &funcname);
90  void missingPercentCharacterError(const Token *tok, const std::string &pattern, const std::string &funcname);
91  void unknownPatternError(const Token* tok, const std::string& pattern);
92  void redundantNextPreviousError(const Token* tok, const std::string& func1, const std::string& func2);
93  void orInComplexPattern(const Token *tok, const std::string &pattern, const std::string &funcname);
94  void extraWhitespaceError(const Token *tok, const std::string &pattern, const std::string &funcname);
96 
97  void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
98  CheckInternal c(nullptr, settings, errorLogger);
99  c.multiComparePatternError(nullptr, ";|%type%", "Match");
100  c.simplePatternError(nullptr, "class {", "Match");
101  c.complexPatternError(nullptr, "%type% ( )", "Match");
102  c.missingPercentCharacterError(nullptr, "%num", "Match");
103  c.unknownPatternError(nullptr, "%typ");
104  c.redundantNextPreviousError(nullptr, "previous", "next");
105  c.orInComplexPattern(nullptr, "||", "Match");
106  c.extraWhitespaceError(nullptr, "%str% ", "Match");
107  c.checkRedundantTokCheckError(nullptr);
108  }
109 
110  static std::string myName() {
111  return "cppcheck internal API usage";
112  }
113 
114  std::string classInfo() const override {
115  // Don't include these checks on the WIKI where people can read what
116  // checks there are. These checks are not intended for users.
117  return "";
118  }
119 };
120 /// @}
121 //---------------------------------------------------------------------------
122 #endif // checkinternalH
Check Internal cppcheck API usage
Definition: checkinternal.h:41
void checkRedundantNextPrevious()
Check for inefficient usage of Token::next(), Token::previous() and Token::tokAt()
CheckInternal(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
Definition: checkinternal.h:48
void checkRedundantTokCheckError(const Token *tok)
void redundantNextPreviousError(const Token *tok, const std::string &func1, const std::string &func2)
void complexPatternError(const Token *tok, const std::string &pattern, const std::string &funcname)
void checkUnknownPattern()
Check for unknown (invalid) complex patterns like "%typ%"
std::string classInfo() const override
get information about this class, used to generate documentation
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
run checks, the token list is not simplified
Definition: checkinternal.h:51
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
Definition: checkinternal.h:97
void orInComplexPattern(const Token *tok, const std::string &pattern, const std::string &funcname)
void multiComparePatternError(const Token *tok, const std::string &pattern, const std::string &funcname)
void missingPercentCharacterError(const Token *tok, const std::string &pattern, const std::string &funcname)
void checkTokenSimpleMatchPatterns()
Check if a complex pattern is used inside Token::simpleMatch or Token::findsimplematch
void checkTokenMatchPatterns()
Check if a simple pattern is used inside Token::Match or Token::findmatch
void checkMissingPercentCharacter()
Check for missing % end character in Token::Match pattern
void unknownPatternError(const Token *tok, const std::string &pattern)
void checkExtraWhitespace()
Check if there is whitespace at the beginning or at the end of a pattern
void checkRedundantTokCheck()
Check if there is a redundant check for none-nullness of parameter before Match functions,...
static std::string myName()
CheckInternal()
This constructor is used when registering the CheckClass.
Definition: checkinternal.h:44
void simplePatternError(const Token *tok, const std::string &pattern, const std::string &funcname)
void extraWhitespaceError(const Token *tok, const std::string &pattern, const std::string &funcname)
Interface class that cppcheck uses to communicate with the checks.
Definition: check.h:59
This is an interface, which the class responsible of error logging should implement.
Definition: errorlogger.h:214
This is just a container for general settings so that we don't need to pass individual values to func...
Definition: settings.h:95
SimpleEnableGroup< Checks > checks
Definition: settings.h:360
bool isEnabled(T flag) const
Definition: settings.h:66
The token list that the TokenList generates is a linked-list of this class.
Definition: token.h:150
The main purpose is to tokenize the source code.
Definition: tokenize.h:46
const Settings & getSettings() const
Definition: tokenize.h:615
#define CPPCHECKLIB
Definition: config.h:35
@ internalCheck