Cppcheck
checkbool.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 checkboolH
22 #define checkboolH
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 checks dealing with suspicious usage of boolean type (not for evaluating conditions) */
40 
41 class CPPCHECKLIB CheckBool : public Check {
42 public:
43  /** @brief This constructor is used when registering the CheckClass */
44  CheckBool() : Check(myName()) {}
45 
46 private:
47  /** @brief This constructor is used when running checks. */
48  CheckBool(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
49  : Check(myName(), tokenizer, settings, errorLogger) {}
50 
51  /** @brief Run checks against the normal token list */
52  void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
53  CheckBool checkBool(&tokenizer, &tokenizer.getSettings(), errorLogger);
54 
55  // Checks
57  checkBool.checkComparisonOfBoolWithInt();
58  checkBool.checkAssignBoolToFloat();
59  checkBool.pointerArithBool();
63  checkBool.checkIncrementBoolean();
64  checkBool.checkAssignBoolToPointer();
65  checkBool.checkBitwiseOnBoolean();
66  }
67 
68  /** @brief %Check for comparison of function returning bool*/
69  void checkComparisonOfFuncReturningBool();
70 
71  /** @brief %Check for comparison of variable of type bool*/
72  void checkComparisonOfBoolWithBool();
73 
74  /** @brief %Check for using postfix increment on bool */
75  void checkIncrementBoolean();
76 
77  /** @brief %Check for suspicious comparison of a bool and a non-zero (and non-one) value (e.g. "if (!x==4)") */
78  void checkComparisonOfBoolWithInt();
79 
80  /** @brief assigning bool to pointer */
81  void checkAssignBoolToPointer();
82 
83  /** @brief assigning bool to float */
84  void checkAssignBoolToFloat();
85 
86  /** @brief %Check for using bool in bitwise expression */
87  void checkBitwiseOnBoolean();
88 
89  /** @brief %Check for comparing a bool expression with an integer other than 0 or 1 */
90  void checkComparisonOfBoolExpressionWithInt();
91 
92  /** @brief %Check for 'if (p+1)' etc. either somebody forgot to dereference, or else somebody uses pointer overflow */
93  void pointerArithBool();
94  void pointerArithBoolCond(const Token *tok);
95 
96  /** @brief %Check if a function returning bool returns an integer other than 0 or 1 */
97  void returnValueOfFunctionReturningBool();
98 
99  // Error messages..
100  void comparisonOfFuncReturningBoolError(const Token *tok, const std::string &expression);
101  void comparisonOfTwoFuncsReturningBoolError(const Token *tok, const std::string &expression1, const std::string &expression2);
102  void comparisonOfBoolWithBoolError(const Token *tok, const std::string &expression);
103  void incrementBooleanError(const Token *tok);
104  void comparisonOfBoolWithInvalidComparator(const Token *tok, const std::string &expression);
105  void assignBoolToPointerError(const Token *tok);
106  void assignBoolToFloatError(const Token *tok);
107  void bitwiseOnBooleanError(const Token* tok, const std::string& expression, const std::string& op, bool isCompound = false);
108  void comparisonOfBoolExpressionWithIntError(const Token *tok, bool not0or1);
109  void pointerArithBoolError(const Token *tok);
110  void returnValueBoolError(const Token *tok);
111 
112  void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
113  CheckBool c(nullptr, settings, errorLogger);
114  c.assignBoolToPointerError(nullptr);
115  c.assignBoolToFloatError(nullptr);
116  c.comparisonOfFuncReturningBoolError(nullptr, "func_name");
117  c.comparisonOfTwoFuncsReturningBoolError(nullptr, "func_name1", "func_name2");
118  c.comparisonOfBoolWithBoolError(nullptr, "var_name");
119  c.incrementBooleanError(nullptr);
120  c.bitwiseOnBooleanError(nullptr, "expression", "&&");
122  c.pointerArithBoolError(nullptr);
123  c.comparisonOfBoolWithInvalidComparator(nullptr, "expression");
124  c.returnValueBoolError(nullptr);
125  }
126 
127  static std::string myName() {
128  return "Boolean";
129  }
130 
131  std::string classInfo() const override {
132  return "Boolean type checks\n"
133  "- using increment on boolean\n"
134  "- comparison of a boolean expression with an integer other than 0 or 1\n"
135  "- comparison of a function returning boolean value using relational operator\n"
136  "- comparison of a boolean value with boolean value using relational operator\n"
137  "- using bool in bitwise expression\n"
138  "- pointer addition in condition (either dereference is forgot or pointer overflow is required to make the condition false)\n"
139  "- Assigning bool value to pointer or float\n"
140  "- Returning an integer other than 0 or 1 from a function with boolean return value\n";
141  }
142 };
143 /// @}
144 //---------------------------------------------------------------------------
145 #endif // checkboolH
checks dealing with suspicious usage of boolean type (not for evaluating conditions)
Definition: checkbool.h:41
CheckBool()
This constructor is used when registering the CheckClass.
Definition: checkbool.h:44
void assignBoolToPointerError(const Token *tok)
Definition: checkbool.cpp:334
void checkBitwiseOnBoolean()
Check for using bool in bitwise expression
Definition: checkbool.cpp:91
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
Definition: checkbool.h:112
static std::string myName()
Definition: checkbool.h:127
void pointerArithBoolError(const Token *tok)
Definition: checkbool.cpp:456
CheckBool(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
Definition: checkbool.h:48
std::string classInfo() const override
get information about this class, used to generate documentation
Definition: checkbool.h:131
void comparisonOfFuncReturningBoolError(const Token *tok, const std::string &expression)
Definition: checkbool.cpp:248
void pointerArithBool()
Check for 'if (p+1)' etc.
Definition: checkbool.cpp:413
void checkComparisonOfBoolWithBool()
Check for comparison of variable of type bool
Definition: checkbool.cpp:270
void incrementBooleanError(const Token *tok)
Definition: checkbool.cpp:68
void checkComparisonOfBoolWithInt()
Check for suspicious comparison of a bool and a non-zero (and non-one) value (e.g.
Definition: checkbool.cpp:153
void comparisonOfBoolWithBoolError(const Token *tok, const std::string &expression)
Definition: checkbool.cpp:311
void checkAssignBoolToPointer()
assigning bool to pointer
Definition: checkbool.cpp:321
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
Run checks against the normal token list.
Definition: checkbool.h:52
void comparisonOfBoolExpressionWithIntError(const Token *tok, bool not0or1)
Definition: checkbool.cpp:402
void bitwiseOnBooleanError(const Token *tok, const std::string &expression, const std::string &op, bool isCompound=false)
Definition: checkbool.cpp:136
void returnValueBoolError(const Token *tok)
Definition: checkbool.cpp:516
void checkComparisonOfFuncReturningBool()
Check for comparison of function returning bool
Definition: checkbool.cpp:204
void comparisonOfBoolWithInvalidComparator(const Token *tok, const std::string &expression)
Definition: checkbool.cpp:180
void checkComparisonOfBoolExpressionWithInt()
Check for comparing a bool expression with an integer other than 0 or 1
Definition: checkbool.cpp:342
void checkIncrementBoolean()
Check for using postfix increment on bool
Definition: checkbool.cpp:51
void returnValueOfFunctionReturningBool()
Check if a function returning bool returns an integer other than 0 or 1
Definition: checkbool.cpp:488
void assignBoolToFloatError(const Token *tok)
Definition: checkbool.cpp:482
void comparisonOfTwoFuncsReturningBoolError(const Token *tok, const std::string &expression1, const std::string &expression2)
Definition: checkbool.cpp:257
void checkAssignBoolToFloat()
assigning bool to float
Definition: checkbool.cpp:465
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
#define CPPCHECKLIB
Definition: config.h:35