Cppcheck
checkassert.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 checkassertH
22 #define checkassertH
23 //---------------------------------------------------------------------------
24 
25 #include "check.h"
26 #include "config.h"
27 #include "tokenize.h"
28 
29 #include <string>
30 
31 class ErrorLogger;
32 class Scope;
33 class Settings;
34 class Token;
35 
36 /// @addtogroup Checks
37 /// @{
38 
39 /**
40  * @brief Checking for side effects in assert statements
41  */
42 
43 class CPPCHECKLIB CheckAssert : public Check {
44 public:
45  CheckAssert() : Check(myName()) {}
46 
47 private:
48  CheckAssert(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
49  : Check(myName(), tokenizer, settings, errorLogger) {}
50 
51  /** run checks, the token list is not simplified */
52  void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override {
53  CheckAssert checkAssert(&tokenizer, &tokenizer.getSettings(), errorLogger);
54  checkAssert.assertWithSideEffects();
55  }
56 
57  void assertWithSideEffects();
58 
59  void checkVariableAssignment(const Token* assignTok, const Scope *assertionScope);
60  static bool inSameScope(const Token* returnTok, const Token* assignTok);
61 
62  void sideEffectInAssertError(const Token *tok, const std::string& functionName);
63  void assignmentInAssertError(const Token *tok, const std::string &varname);
64 
65  void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override {
66  CheckAssert c(nullptr, settings, errorLogger);
67  c.sideEffectInAssertError(nullptr, "function");
68  c.assignmentInAssertError(nullptr, "var");
69  }
70 
71  static std::string myName() {
72  return "Assert";
73  }
74 
75  std::string classInfo() const override {
76  return "Warn if there are side effects in assert statements (since this cause different behaviour in debug/release builds).\n";
77  }
78 };
79 /// @}
80 //---------------------------------------------------------------------------
81 #endif // checkassertH
Checking for side effects in assert statements.
Definition: checkassert.h:43
void assertWithSideEffects()
Definition: checkassert.cpp:43
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const override
get error messages
Definition: checkassert.h:65
void assignmentInAssertError(const Token *tok, const std::string &varname)
void runChecks(const Tokenizer &tokenizer, ErrorLogger *errorLogger) override
run checks, the token list is not simplified
Definition: checkassert.h:52
CheckAssert(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
Definition: checkassert.h:48
void sideEffectInAssertError(const Token *tok, const std::string &functionName)
std::string classInfo() const override
get information about this class, used to generate documentation
Definition: checkassert.h:75
static std::string myName()
Definition: checkassert.h:71
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