MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / assignIfParseScope

Method assignIfParseScope

lib/checkcondition.cpp:165–241  ·  view source on GitHub ↗

parse scopes recursively */

Source from the content-addressed store, hash-verified

163
164/** parse scopes recursively */
165bool CheckConditionImpl::assignIfParseScope(const Token * const assignTok,
166 const Token * const startTok,
167 const nonneg int varid,
168 const bool islocal,
169 const char bitop,
170 const MathLib::bigint num)
171{
172 bool ret = false;
173
174 for (const Token *tok2 = startTok; tok2; tok2 = tok2->next()) {
175 if ((bitop == '&') && Token::Match(tok2->tokAt(2), "%varid% %cop% %num% ;", varid) && tok2->strAt(3) == std::string(1U, bitop)) {
176 const MathLib::bigint num2 = MathLib::toBigNumber(tok2->tokAt(4));
177 if (0 == (num & num2))
178 mismatchingBitAndError(assignTok, num, tok2, num2);
179 }
180 if (Token::Match(tok2, "%varid% =", varid)) {
181 return true;
182 }
183 if (bitop == '&' && Token::Match(tok2, "%varid% &= %num% ;", varid)) {
184 const MathLib::bigint num2 = MathLib::toBigNumber(tok2->tokAt(2));
185 if (0 == (num & num2))
186 mismatchingBitAndError(assignTok, num, tok2, num2);
187 }
188 if (Token::Match(tok2, "++|-- %varid%", varid) || Token::Match(tok2, "%varid% ++|--", varid))
189 return true;
190 if (Token::Match(tok2, "[(,] &| %varid% [,)]", varid) && isParameterChanged(tok2))
191 return true;
192 if (tok2->str() == "}")
193 return false;
194 if (Token::Match(tok2, "break|continue|return"))
195 ret = true;
196 if (ret && tok2->str() == ";")
197 return false;
198 if (!islocal && Token::Match(tok2, "%name% (") && !Token::simpleMatch(tok2->linkAt(1), ") {"))
199 return true;
200 if (Token::Match(tok2, "if|while (")) {
201 if (!islocal && tok2->str() == "while")
202 continue;
203 if (tok2->str() == "while") {
204 // is variable changed in loop?
205 const Token *bodyStart = tok2->linkAt(1)->next();
206 const Token *bodyEnd = bodyStart ? bodyStart->link() : nullptr;
207 if (!bodyEnd || bodyEnd->str() != "}" || isVariableChanged(bodyStart, bodyEnd, varid, !islocal, mSettings))
208 continue;
209 }
210
211 // parse condition
212 const Token * const end = tok2->linkAt(1);
213 for (; tok2 != end; tok2 = tok2->next()) {
214 if (Token::Match(tok2, "[(,] &| %varid% [,)]", varid)) {
215 return true;
216 }
217 if (Token::Match(tok2,"&&|%oror%|( %varid% ==|!= %num% &&|%oror%|)", varid)) {
218 const Token *vartok = tok2->next();
219 const MathLib::bigint num2 = MathLib::toBigNumber(vartok->tokAt(2));
220 if ((num & num2) != ((bitop=='&') ? num2 : num)) {
221 const std::string& op(vartok->strAt(1));
222 const bool alwaysTrue = op == "!=";

Callers

nothing calls this directly

Calls 8

isParameterChangedFunction · 0.85
isVariableChangedFunction · 0.85
nextMethod · 0.80
linkAtMethod · 0.80
isAssignmentOpMethod · 0.80
simpleMatchFunction · 0.70
tokAtMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected