Cppcheck
checktype.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 checktypeH
22 #define checktypeH
23 //---------------------------------------------------------------------------
24 
25 #include "check.h"
26 #include "config.h"
27 #include "tokenize.h"
28 #include "vfvalue.h"
29 
30 #include <list>
31 #include <string>
32 
33 class ErrorLogger;
34 class Settings;
35 class Token;
36 class ValueType;
37 
38 /// @addtogroup Checks
39 /// @{
40 
41 
42 /** @brief Various small checks */
43 
44 class CPPCHECKLIB CheckType : public Check {
45 public:
46  /** @brief This constructor is used when registering the CheckClass */
47  CheckType() : Check(myName()) {}
48 
49 private:
50  /** @brief This constructor is used when running checks. */
51  CheckType(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
52  : Check(myName(), tokenizer, settings, errorLogger) {}
53 
54  /** @brief Run checks against the normal token list */
55  void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
56  // These are not "simplified" because casts can't be ignored
57  CheckType checkType(&tokenizer, &tokenizer.getSettings(), errorLogger);
58  checkType.checkTooBigBitwiseShift();
59  checkType.checkIntegerOverflow();
60  checkType.checkSignConversion();
61  checkType.checkLongCast();
62  checkType.checkFloatToIntegerOverflow();
63  }
64 
65  /** @brief %Check for bitwise shift with too big right operand */
66  void checkTooBigBitwiseShift();
67 
68  /** @brief %Check for integer overflow */
69  void checkIntegerOverflow();
70 
71  /** @brief %Check for dangerous sign conversion */
72  void checkSignConversion();
73 
74  /** @brief %Check for implicit long cast of int result */
75  void checkLongCast();
76 
77  /** @brief %Check for float to integer overflow */
78  void checkFloatToIntegerOverflow();
79  void checkFloatToIntegerOverflow(const Token *tok, const ValueType *vtint, const ValueType *vtfloat, const std::list<ValueFlow::Value> &floatValues);
80 
81  // Error messages..
82  void tooBigBitwiseShiftError(const Token *tok, int lhsbits, const ValueFlow::Value &rhsbits);
83  void tooBigSignedBitwiseShiftError(const Token *tok, int lhsbits, const ValueFlow::Value &rhsbits);
84  void integerOverflowError(const Token *tok, const ValueFlow::Value &value);
85  void signConversionError(const Token *tok, const ValueFlow::Value *negativeValue, const bool constvalue);
86  void longCastAssignError(const Token *tok, const ValueType* src = nullptr, const ValueType* tgt = nullptr);
87  void longCastReturnError(const Token *tok, const ValueType* src = nullptr, const ValueType* tgt = nullptr);
88  void floatToIntegerOverflowError(const Token *tok, const ValueFlow::Value &value);
89 
90  void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
91  CheckType c(nullptr, settings, errorLogger);
92  c.tooBigBitwiseShiftError(nullptr, 32, ValueFlow::Value(64));
94  c.integerOverflowError(nullptr, ValueFlow::Value(1LL<<32));
95  c.signConversionError(nullptr, nullptr, false);
96  c.longCastAssignError(nullptr);
97  c.longCastReturnError(nullptr);
100  f.floatValue = 1E100;
101  c.floatToIntegerOverflowError(nullptr, f);
102  }
103 
104  static std::string myName() {
105  return "Type";
106  }
107 
108  std::string classInfo() const override {
109  return "Type checks\n"
110  "- bitwise shift by too many bits (only enabled when --platform is used)\n"
111  "- signed integer overflow (only enabled when --platform is used)\n"
112  "- dangerous sign conversion, when signed value can be negative\n"
113  "- possible loss of information when assigning int result to long variable\n"
114  "- possible loss of information when returning int result as long return value\n"
115  "- float conversion overflow\n";
116  }
117 };
118 /// @}
119 //---------------------------------------------------------------------------
120 #endif // checktypeH
Various small checks.
Definition: checktype.h:44
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
Definition: checktype.h:90
void tooBigSignedBitwiseShiftError(const Token *tok, int lhsbits, const ValueFlow::Value &rhsbits)
Definition: checktype.cpp:132
CheckType(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
This constructor is used when running checks.
Definition: checktype.h:51
void signConversionError(const Token *tok, const ValueFlow::Value *negativeValue, const bool constvalue)
Definition: checktype.cpp:273
CheckType()
This constructor is used when registering the CheckClass.
Definition: checktype.h:47
void checkFloatToIntegerOverflow()
Check for float to integer overflow
Definition: checktype.cpp:431
void checkIntegerOverflow()
Check for integer overflow
Definition: checktype.cpp:167
void longCastAssignError(const Token *tok, const ValueType *src=nullptr, const ValueType *tgt=nullptr)
Definition: checktype.cpp:401
void checkSignConversion()
Check for dangerous sign conversion
Definition: checktype.cpp:241
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
Run checks against the normal token list.
Definition: checktype.h:55
void integerOverflowError(const Token *tok, const ValueFlow::Value &value)
Definition: checktype.cpp:215
std::string classInfo() const override
get information about this class, used to generate documentation
Definition: checktype.h:108
static std::string myName()
Definition: checktype.h:104
void floatToIntegerOverflowError(const Token *tok, const ValueFlow::Value &value)
Definition: checktype.cpp:502
void checkLongCast()
Check for implicit long cast of int result
Definition: checktype.cpp:326
void longCastReturnError(const Token *tok, const ValueType *src=nullptr, const ValueType *tgt=nullptr)
Definition: checktype.cpp:414
void checkTooBigBitwiseShift()
Check for bitwise shift with too big right operand
Definition: checktype.cpp:61
void tooBigBitwiseShiftError(const Token *tok, int lhsbits, const ValueFlow::Value &rhsbits)
Definition: checktype.cpp:113
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
enum ValueFlow::Value::ValueType valueType
double floatValue
float value
Definition: vfvalue.h:274
Value type.
#define CPPCHECKLIB
Definition: config.h:35