@@ -84,7 +84,8 @@ bool findTokensSkipDeadCodeImpl(const Library& library,
8484 const Token* end,
8585 const Predicate& pred,
8686 Found found,
87- const Evaluate& evaluate)
87+ const Evaluate& evaluate,
88+ bool skipUnevaluated)
8889{
8990 for (T* tok = start; precedes (tok, end); tok = tok->next ()) {
9091 if (pred (tok)) {
@@ -98,7 +99,7 @@ bool findTokensSkipDeadCodeImpl(const Library& library,
9899 auto result = evaluate (condTok);
99100 if (result.empty ())
100101 continue ;
101- if (findTokensSkipDeadCodeImpl (library, tok->next (), tok->linkAt (1 ), pred, found, evaluate))
102+ if (findTokensSkipDeadCodeImpl (library, tok->next (), tok->linkAt (1 ), pred, found, evaluate, skipUnevaluated ))
102103 return true ;
103104 T* thenStart = tok->linkAt (1 )->next ();
104105 T* elseStart = nullptr ;
@@ -108,7 +109,7 @@ bool findTokensSkipDeadCodeImpl(const Library& library,
108109 int r = result.front ();
109110 if (r == 0 ) {
110111 if (elseStart) {
111- if (findTokensSkipDeadCodeImpl (library, elseStart, elseStart->link (), pred, found, evaluate))
112+ if (findTokensSkipDeadCodeImpl (library, elseStart, elseStart->link (), pred, found, evaluate, skipUnevaluated ))
112113 return true ;
113114 if (isReturnScope (elseStart->link (), library))
114115 return true ;
@@ -117,7 +118,7 @@ bool findTokensSkipDeadCodeImpl(const Library& library,
117118 tok = thenStart->link ();
118119 }
119120 } else {
120- if (findTokensSkipDeadCodeImpl (library, thenStart, thenStart->link (), pred, found, evaluate))
121+ if (findTokensSkipDeadCodeImpl (library, thenStart, thenStart->link (), pred, found, evaluate, skipUnevaluated ))
121122 return true ;
122123 if (isReturnScope (thenStart->link (), library))
123124 return true ;
@@ -137,7 +138,7 @@ bool findTokensSkipDeadCodeImpl(const Library& library,
137138 if (!cond) {
138139 next = colon;
139140 } else {
140- if (findTokensSkipDeadCodeImpl (library, tok->astParent ()->next (), colon, pred, found, evaluate))
141+ if (findTokensSkipDeadCodeImpl (library, tok->astParent ()->next (), colon, pred, found, evaluate, skipUnevaluated ))
141142 return true ;
142143 next = nextAfterAstRightmostLeaf (colon);
143144 }
@@ -164,6 +165,12 @@ bool findTokensSkipDeadCodeImpl(const Library& library,
164165 else
165166 tok = afterCapture;
166167 }
168+ if (skipUnevaluated && isUnevaluated (tok)) {
169+ T *next = tok->linkAt (1 );
170+ if (!next)
171+ continue ;
172+ tok = next;
173+ }
167174 }
168175 return false ;
169176}
@@ -185,7 +192,8 @@ std::vector<T*> findTokensSkipDeadCode(const Library& library,
185192 result.push_back (tok);
186193 return false ;
187194 },
188- evaluate);
195+ evaluate,
196+ false );
189197 return result;
190198}
191199
@@ -195,6 +203,35 @@ std::vector<T*> findTokensSkipDeadCode(const Library& library, T* start, const T
195203 return findTokensSkipDeadCode (library, start, end, pred, &evaluateKnownValues);
196204}
197205
206+ template <class T , class Predicate , class Evaluate , REQUIRES(" T must be a Token class" , std::is_convertible<T*, const Token*> )>
207+ std::vector<T*> findTokensSkipDeadAndUnevaluatedCode (const Library& library,
208+ T* start,
209+ const Token* end,
210+ const Predicate& pred,
211+ const Evaluate& evaluate)
212+ {
213+ std::vector<T*> result;
214+ (void )findTokensSkipDeadCodeImpl (
215+ library,
216+ start,
217+ end,
218+ pred,
219+ [&](T* tok) {
220+ result.push_back (tok);
221+ return false ;
222+ },
223+ evaluate,
224+ true );
225+ return result;
226+ }
227+
228+ template <class T , class Predicate , REQUIRES(" T must be a Token class" , std::is_convertible<T*, const Token*> )>
229+ std::vector<T*> findTokensSkipDeadAndUnevaluatedCode (const Library& library, T* start, const Token* end, const Predicate& pred)
230+ {
231+ return findTokensSkipDeadAndUnevaluatedCode (library, start, end, pred, &evaluateKnownValues);
232+ }
233+
234+
198235template <class T , class Predicate , class Evaluate , REQUIRES(" T must be a Token class" , std::is_convertible<T*, const Token*> )>
199236T* findTokenSkipDeadCode (const Library& library, T* start, const Token* end, const Predicate& pred, const Evaluate& evaluate)
200237{
@@ -208,7 +245,8 @@ T* findTokenSkipDeadCode(const Library& library, T* start, const Token* end, con
208245 result = tok;
209246 return true ;
210247 },
211- evaluate);
248+ evaluate,
249+ false );
212250 return result;
213251}
214252
0 commit comments