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

Function findTokensSkipDeadCodeImpl

lib/findtoken.cpp:26–121  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24class Library;
25
26template<class T, REQUIRES("T must be a Token class", std::is_convertible<T*, const Token*> )>
27static bool findTokensSkipDeadCodeImpl(const Library& library,
28 T* start,
29 const Token* end,
30 const std::function<bool(const Token*)>& pred,
31 const std::function<bool(T*)>& found,
32 const std::function<std::vector<MathLib::bigint>(const Token*)>& evaluate,
33 bool skipUnevaluated)
34{
35 for (T* tok = start; precedes(tok, end); tok = tok->next()) {
36 if (pred(tok)) {
37 if (found(tok))
38 return true;
39 }
40 if (Token::Match(tok, "if|for|while (") && Token::simpleMatch(tok->linkAt(1), ") {")) {
41 const Token* condTok = getCondTok(tok);
42 if (!condTok)
43 continue;
44 auto result = evaluate(condTok);
45 if (result.empty())
46 continue;
47 if (internal::findTokensSkipDeadCodeImpl(library, tok->next(), tok->linkAt(1), pred, found, evaluate, skipUnevaluated))
48 return true;
49 T* thenStart = tok->linkAt(1)->next();
50 T* elseStart = nullptr;
51 if (Token::simpleMatch(thenStart->link(), "} else {"))
52 elseStart = thenStart->link()->tokAt(2);
53
54 auto r = result.front();
55 if (r == 0) {
56 if (elseStart) {
57 if (internal::findTokensSkipDeadCodeImpl(library, elseStart, elseStart->link(), pred, found, evaluate, skipUnevaluated))
58 return true;
59 if (isReturnScope(elseStart->link(), library))
60 return true;
61 tok = elseStart->link();
62 } else {
63 tok = thenStart->link();
64 }
65 } else {
66 if (internal::findTokensSkipDeadCodeImpl(library, thenStart, thenStart->link(), pred, found, evaluate, skipUnevaluated))
67 return true;
68 if (isReturnScope(thenStart->link(), library))
69 return true;
70 tok = thenStart->link();
71 }
72 } else if (Token::Match(tok->astParent(), "&&|?|%oror%") && astIsLHS(tok)) {
73 auto result = evaluate(tok);
74 if (result.empty())
75 continue;
76 const bool cond = result.front() != 0;
77 T* next = nullptr;
78 if ((cond && Token::simpleMatch(tok->astParent(), "||")) ||
79 (!cond && Token::simpleMatch(tok->astParent(), "&&"))) {
80 next = nextAfterAstRightmostLeaf(tok->astParent());
81 } else if (Token::simpleMatch(tok->astParent(), "?")) {
82 T* colon = tok->astParent()->astOperand2();
83 if (!cond) {

Callers 3

findTokensSkipDeadCodeFunction · 0.85
findTokenSkipDeadCodeFunction · 0.85

Calls 15

precedesFunction · 0.85
getCondTokFunction · 0.85
isReturnScopeFunction · 0.85
astIsLHSFunction · 0.85
getCondTokFromEndFunction · 0.85
isUnevaluatedFunction · 0.85
nextMethod · 0.80
linkAtMethod · 0.80
frontMethod · 0.80
astParentMethod · 0.80
astOperand2Method · 0.80

Tested by

no test coverage detected