Cppcheck
checkpostfixoperator.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 checkpostfixoperatorH
22 #define checkpostfixoperatorH
23 //---------------------------------------------------------------------------
24 
25 #include "check.h"
26 #include "config.h"
27 #include "tokenize.h"
28 
29 #include <string>
30 
31 class ErrorLogger;
32 class Settings;
33 class Token;
34 
35 /// @addtogroup Checks
36 /// @{
37 
38 /**
39  * @brief Using postfix operators ++ or -- rather than postfix operator.
40  */
41 
43  friend class TestPostfixOperator;
44 
45 public:
46  /** This constructor is used when registering the CheckPostfixOperator */
47  CheckPostfixOperator() : Check(myName()) {}
48 
49 private:
50  /** This constructor is used when running checks. */
51  CheckPostfixOperator(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
52  : Check(myName(), tokenizer, settings, errorLogger) {}
53 
54  void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
55  if (tokenizer.isC())
56  return;
57 
58  CheckPostfixOperator checkPostfixOperator(&tokenizer, &tokenizer.getSettings(), errorLogger);
59  checkPostfixOperator.postfixOperator();
60  }
61 
62  /** Check postfix operators */
63  void postfixOperator();
64 
65  /** Report Error */
66  void postfixOperatorError(const Token *tok);
67 
68  void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
69  CheckPostfixOperator c(nullptr, settings, errorLogger);
70  c.postfixOperatorError(nullptr);
71  }
72 
73  static std::string myName() {
74  return "Using postfix operators";
75  }
76 
77  std::string classInfo() const override {
78  return "Warn if using postfix operators ++ or -- rather than prefix operator\n";
79  }
80 };
81 /// @}
82 //---------------------------------------------------------------------------
83 #endif // checkpostfixoperatorH
Using postfix operators ++ or – rather than postfix operator.
void postfixOperator()
Check postfix operators.
static std::string myName()
CheckPostfixOperator(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
std::string classInfo() const override
get information about this class, used to generate documentation
void postfixOperatorError(const Token *tok)
Report Error.
CheckPostfixOperator()
This constructor is used when registering the CheckPostfixOperator.
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
run checks, the token list is not simplified
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
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
bool isC() const
Is the code C.
Definition: tokenize.h:64
#define CPPCHECKLIB
Definition: config.h:35