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

Function isReturnScope

lib/astutils.cpp:2280–2326  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2278}
2279
2280bool isReturnScope(const Token* const endToken, const Library& library, const Token** unknownFunc, bool functionScope)
2281{
2282 if (!endToken || endToken->str() != "}")
2283 return false;
2284
2285 const Token *prev = endToken->previous();
2286 while (prev && Token::simpleMatch(prev->previous(), "; ;"))
2287 prev = prev->previous();
2288 if (prev && Token::simpleMatch(prev->previous(), "} ;"))
2289 prev = prev->previous();
2290
2291 if (Token::simpleMatch(prev, "}")) {
2292 if (Token::simpleMatch(prev->link()->tokAt(-2), "} else {"))
2293 return isReturnScope(prev, library, unknownFunc, functionScope) &&
2294 isReturnScope(prev->link()->tokAt(-2), library, unknownFunc, functionScope);
2295 // TODO: Check all cases
2296 if (!functionScope && Token::simpleMatch(prev->link()->previous(), ") {") &&
2297 Token::simpleMatch(prev->link()->linkAt(-1)->previous(), "switch (") &&
2298 !Token::findsimplematch(prev->link(), "break", prev)) {
2299 return isReturnScope(prev, library, unknownFunc, functionScope);
2300 }
2301 if (isEscaped(prev->link()->astTop(), functionScope, library))
2302 return true;
2303 if (Token::Match(prev->link()->previous(), "[;{}] {"))
2304 return isReturnScope(prev, library, unknownFunc, functionScope);
2305 } else if (Token::simpleMatch(prev, ";")) {
2306 if (prev->tokAt(-2) && hasNoreturnFunction(prev->tokAt(-2)->astTop(), library, unknownFunc))
2307 return true;
2308 // Unknown symbol
2309 if (Token::Match(prev->tokAt(-2), ";|}|{ %name% ;") && prev->previous()->isIncompleteVar()) {
2310 if (unknownFunc)
2311 *unknownFunc = prev->previous();
2312 return false;
2313 }
2314 if (Token::simpleMatch(prev->previous(), ") ;") && prev->linkAt(-1) &&
2315 isEscaped(prev->linkAt(-1)->astTop(), functionScope, library))
2316 return true;
2317 if (isEscaped(prev->previous()->astTop(), functionScope, library))
2318 return true;
2319 // return/goto statement
2320 prev = prev->previous();
2321 while (prev && !Token::Match(prev, ";|{|}") && !isEscapedOrJump(prev, functionScope, library))
2322 prev = prev->previous();
2323 return prev && prev->isName();
2324 }
2325 return false;
2326}
2327
2328bool isWithinScope(const Token* tok, const Variable* var, ScopeType type)
2329{

Callers 9

checkRecursiveMethod · 0.85
checkFileUsageMethod · 0.85
afterConditionMethod · 0.85
hasInnerReturnScopeFunction · 0.85
isEscapeScopeFunction · 0.85
invalidContainerMethod · 0.85
isReturnScopeTestMethod · 0.85

Calls 9

findsimplematchFunction · 0.85
isEscapedFunction · 0.85
hasNoreturnFunctionFunction · 0.85
isEscapedOrJumpFunction · 0.85
linkAtMethod · 0.80
astTopMethod · 0.80
simpleMatchFunction · 0.70
strMethod · 0.45
tokAtMethod · 0.45

Tested by 1

isReturnScopeTestMethod · 0.68