forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckassignif.cpp
184 lines (144 loc) · 6.43 KB
/
checkassignif.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
// Check for assignment / condition mismatches
//---------------------------------------------------------------------------
#include "checkassignif.h"
#include "symboldatabase.h"
//---------------------------------------------------------------------------
// Register this check class (by creating a static instance of it)
namespace {
CheckAssignIf instance;
}
void CheckAssignIf::assignIf()
{
if (!_settings->isEnabled("style"))
return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (tok->str() != "=")
continue;
if (Token::Match(tok->tokAt(-2), "[;{}] %var% = %var% [&|] %num% ;")) {
const unsigned int varid(tok->previous()->varId());
if (varid == 0)
continue;
const char bitop(tok->strAt(2).at(0));
const MathLib::bigint num = MathLib::toLongNumber(tok->strAt(3));
if (num < 0)
continue;
for (const Token *tok2 = tok->tokAt(4); tok2; tok2 = tok2->next()) {
if (tok2->str() == "(" || tok2->str() == "}" || tok2->str() == "=")
break;
if (Token::Match(tok2,"if ( %varid% %any% %num% &&|%oror%|)", varid)) {
const std::string op(tok2->strAt(3));
const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4));
if (op == "==" && (num & num2) != ((bitop=='&') ? num2 : num))
assignIfError(tok2, false);
else if (op == "!=" && (num & num2) != ((bitop=='&') ? num2 : num))
assignIfError(tok2, true);
break;
}
}
}
}
}
void CheckAssignIf::assignIfError(const Token *tok, bool result)
{
reportError(tok, Severity::style,
"assignIfError",
"Mismatching assignment and comparison, comparison is always " + std::string(result ? "true" : "false"));
}
void CheckAssignIf::comparison()
{
if (!_settings->isEnabled("style"))
return;
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (tok->str() != "&")
continue;
if (Token::Match(tok, "& %num% )| ==|!= %num% &&|%oror%|)")) {
const MathLib::bigint num1 = MathLib::toLongNumber(tok->strAt(1));
if (num1 < 0)
continue;
const Token *compareToken = tok->tokAt(2);
if (compareToken->str() == ")") {
if (!Token::Match(compareToken->link()->previous(), "(|%oror%|&&"))
continue;
compareToken = compareToken->next();
}
const MathLib::bigint num2 = MathLib::toLongNumber(compareToken->strAt(1));
if (num2 < 0)
continue;
if ((num1 & num2) != num2) {
const std::string op(compareToken->str());
comparisonError(tok, num1, op, num2, op=="==" ? false : true);
}
}
}
}
void CheckAssignIf::comparisonError(const Token *tok, MathLib::bigint value1, const std::string &op, MathLib::bigint value2, bool result)
{
std::ostringstream expression;
expression << std::hex << "(X & 0x" << value1 << ") " << op << " 0x" << value2;
const std::string errmsg("Expression '" + expression.str() + "' is always " + (result?"true":"false") + "\n"
"The expression '" + expression.str() + "' is always " + (result?"true":"false") +
". Check carefully constants and operators used, these errors might be hard to "
"spot sometimes. In case of complex expression it might help to split it to "
"separate expressions.");
reportError(tok, Severity::style, "comparisonError", errmsg);
}
void CheckAssignIf::multiCondition()
{
if (!_settings->isEnabled("style"))
return;
const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase();
for (std::list<Scope>::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) {
if (i->type == Scope::eIf && Token::Match(i->classDef, "if ( %var% & %num% ) {")) {
const Token* const tok = i->classDef;
const unsigned int varid(tok->tokAt(2)->varId());
if (varid == 0)
continue;
const MathLib::bigint num1 = MathLib::toLongNumber(tok->strAt(4));
if (num1 < 0)
continue;
const Token *tok2 = tok->linkAt(6);
while (Token::simpleMatch(tok2, "} else { if (")) {
// Goto '('
const Token * const opar = tok2->tokAt(4);
// tok2: skip if-block
tok2 = opar->link();
if (Token::simpleMatch(tok2, ") {"))
tok2 = tok2->next()->link();
// check condition..
if (Token::Match(opar, "( %varid% ==|& %num% &&|%oror%|)", varid)) {
const MathLib::bigint num2 = MathLib::toLongNumber(opar->strAt(3));
if (num2 < 0)
continue;
if ((num1 & num2) == num2) {
multiConditionError(opar, tok->linenr());
}
}
}
}
}
}
void CheckAssignIf::multiConditionError(const Token *tok, unsigned int line1)
{
std::ostringstream errmsg;
errmsg << "'else if' condition matches previous condition at line "
<< line1;
reportError(tok, Severity::style, "multiCondition", errmsg.str());
}