Cppcheck
checkpostfixoperator.cpp
Go to the documentation of this file.
1 /*
2  * Cppcheck - A tool for static C/C++ code analysis
3  * Copyright (C) 2007-2023 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 // You should use ++ and -- as prefix whenever possible as these are more
21 // efficient than postfix operators
22 //---------------------------------------------------------------------------
23 
24 #include "checkpostfixoperator.h"
25 
26 #include "errortypes.h"
27 #include "settings.h"
28 #include "symboldatabase.h"
29 #include "token.h"
30 
31 #include <vector>
32 
33 //---------------------------------------------------------------------------
34 
35 
36 // Register this check class (by creating a static instance of it)
37 namespace {
38  CheckPostfixOperator instance;
39 }
40 
41 
42 // CWE ids used
43 static const CWE CWE398(398U); // Indicator of Poor Code Quality
44 
45 
47 {
49  return;
50 
51  logChecker("CheckPostfixOperator::postfixOperator"); // performance
52 
53  const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
54 
55  for (const Scope * scope : symbolDatabase->functionScopes) {
56  for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
57  const Variable *var = tok->variable();
58  if (!var || !Token::Match(tok, "%var% ++|--"))
59  continue;
60 
61  const Token* parent = tok->next()->astParent();
62  if (!parent || parent->str() == ";" || (parent->str() == "," && (!parent->astParent() || parent->astParent()->str() != "("))) {
63  if (var->isPointer() || var->isArray())
64  continue;
65 
66  if (Token::Match(var->nameToken()->previous(), "iterator|const_iterator|reverse_iterator|const_reverse_iterator")) {
67  // the variable is an iterator
69  } else if (var->type()) {
70  // the variable is an instance of class
72  }
73  }
74  }
75  }
76 }
77 //---------------------------------------------------------------------------
78 
79 
81 {
82  reportError(tok, Severity::performance, "postfixOperator",
83  "Prefer prefix ++/-- operators for non-primitive types.\n"
84  "Prefix ++/-- operators should be preferred for non-primitive types. "
85  "Pre-increment/decrement can be more efficient than "
86  "post-increment/decrement. Post-increment/decrement usually "
87  "involves keeping a copy of the previous value around and "
88  "adds a little extra code.", CWE398, Certainty::normal);
89 }
static const CWE CWE398(398U)
Using postfix operators ++ or – rather than postfix operator.
void postfixOperator()
Check postfix operators.
void postfixOperatorError(const Token *tok)
Report Error.
void reportError(const Token *tok, const Severity severity, const std::string &id, const std::string &msg)
report an error
Definition: check.h:138
const Settings *const mSettings
Definition: check.h:134
const Tokenizer *const mTokenizer
Definition: check.h:133
void logChecker(const char id[])
log checker
Definition: check.cpp:129
const Token * bodyStart
'{' token
const Token * bodyEnd
'}' token
SimpleEnableGroup< Severity > severity
Definition: settings.h:358
bool isEnabled(T flag) const
Definition: settings.h:66
std::vector< const Scope * > functionScopes
Fast access to function scopes.
The token list that the TokenList generates is a linked-list of this class.
Definition: token.h:150
void str(T &&s)
Definition: token.h:179
static bool Match(const Token *tok, const char pattern[], nonneg int varid=0)
Match given token (or list of tokens) to a pattern list.
Definition: token.cpp:688
Token * previous()
Definition: token.h:862
Token * next()
Definition: token.h:830
void astParent(Token *tok)
Definition: token.cpp:1437
const SymbolDatabase * getSymbolDatabase() const
Definition: tokenize.h:563
Information about a member variable.
const Type * type() const
Get Type pointer of known type.
bool isArray() const
Is variable an array.
const Token * nameToken() const
Get name token.
bool isPointer() const
Is pointer variable.
@ performance
Performance warning.