Cppcheck
checkboost.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 checkboostH
22 #define checkboostH
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 %Check Boost usage */
40 class CPPCHECKLIB CheckBoost : public Check {
41 public:
42  /** This constructor is used when registering the CheckClass */
43  CheckBoost() : Check(myName()) {}
44 
45 private:
46  /** This constructor is used when running checks. */
47  CheckBoost(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
48  : Check(myName(), tokenizer, settings, errorLogger) {}
49 
50  /** @brief Run checks against the normal token list */
51  void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
52  if (!tokenizer.isCPP())
53  return;
54 
55  CheckBoost checkBoost(&tokenizer, &tokenizer.getSettings(), errorLogger);
56  checkBoost.checkBoostForeachModification();
57  }
58 
59  /** @brief %Check for container modification while using the BOOST_FOREACH macro */
60  void checkBoostForeachModification();
61 
62  void boostForeachError(const Token *tok);
63 
64  void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
65  CheckBoost c(nullptr, settings, errorLogger);
66  c.boostForeachError(nullptr);
67  }
68 
69  static std::string myName() {
70  return "Boost usage";
71  }
72 
73  std::string classInfo() const override {
74  return "Check for invalid usage of Boost:\n"
75  "- container modification during BOOST_FOREACH\n";
76  }
77 };
78 /// @}
79 //---------------------------------------------------------------------------
80 #endif // checkboostH
Check Boost usage
Definition: checkboost.h:40
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
Run checks against the normal token list.
Definition: checkboost.h:51
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
Definition: checkboost.h:64
static std::string myName()
Definition: checkboost.h:69
void checkBoostForeachModification()
Check for container modification while using the BOOST_FOREACH macro
Definition: checkboost.cpp:34
CheckBoost(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
Definition: checkboost.h:47
std::string classInfo() const override
get information about this class, used to generate documentation
Definition: checkboost.h:73
void boostForeachError(const Token *tok)
Definition: checkboost.cpp:61
CheckBoost()
This constructor is used when registering the CheckClass.
Definition: checkboost.h:43
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 isCPP() const
Is the code CPP.
Definition: tokenize.h:69
#define CPPCHECKLIB
Definition: config.h:35