/src/bloaty/third_party/re2/re2/walker-inl.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2006 The RE2 Authors. All Rights Reserved. |
2 | | // Use of this source code is governed by a BSD-style |
3 | | // license that can be found in the LICENSE file. |
4 | | |
5 | | #ifndef RE2_WALKER_INL_H_ |
6 | | #define RE2_WALKER_INL_H_ |
7 | | |
8 | | // Helper class for traversing Regexps without recursion. |
9 | | // Clients should declare their own subclasses that override |
10 | | // the PreVisit and PostVisit methods, which are called before |
11 | | // and after visiting the subexpressions. |
12 | | |
13 | | // Not quite the Visitor pattern, because (among other things) |
14 | | // the Visitor pattern is recursive. |
15 | | |
16 | | #include <stack> |
17 | | |
18 | | #include "util/logging.h" |
19 | | #include "re2/regexp.h" |
20 | | |
21 | | namespace re2 { |
22 | | |
23 | | template<typename T> struct WalkState; |
24 | | |
25 | | template<typename T> class Regexp::Walker { |
26 | | public: |
27 | | Walker(); |
28 | | virtual ~Walker(); |
29 | | |
30 | | // Virtual method called before visiting re's children. |
31 | | // PreVisit passes ownership of its return value to its caller. |
32 | | // The Arg* that PreVisit returns will be passed to PostVisit as pre_arg |
33 | | // and passed to the child PreVisits and PostVisits as parent_arg. |
34 | | // At the top-most Regexp, parent_arg is arg passed to walk. |
35 | | // If PreVisit sets *stop to true, the walk does not recurse |
36 | | // into the children. Instead it behaves as though the return |
37 | | // value from PreVisit is the return value from PostVisit. |
38 | | // The default PreVisit returns parent_arg. |
39 | | virtual T PreVisit(Regexp* re, T parent_arg, bool* stop); |
40 | | |
41 | | // Virtual method called after visiting re's children. |
42 | | // The pre_arg is the T that PreVisit returned. |
43 | | // The child_args is a vector of the T that the child PostVisits returned. |
44 | | // PostVisit takes ownership of pre_arg. |
45 | | // PostVisit takes ownership of the Ts |
46 | | // in *child_args, but not the vector itself. |
47 | | // PostVisit passes ownership of its return value |
48 | | // to its caller. |
49 | | // The default PostVisit simply returns pre_arg. |
50 | | virtual T PostVisit(Regexp* re, T parent_arg, T pre_arg, |
51 | | T* child_args, int nchild_args); |
52 | | |
53 | | // Virtual method called to copy a T, |
54 | | // when Walk notices that more than one child is the same re. |
55 | | virtual T Copy(T arg); |
56 | | |
57 | | // Virtual method called to do a "quick visit" of the re, |
58 | | // but not its children. Only called once the visit budget |
59 | | // has been used up and we're trying to abort the walk |
60 | | // as quickly as possible. Should return a value that |
61 | | // makes sense for the parent PostVisits still to be run. |
62 | | // This function is (hopefully) only called by |
63 | | // WalkExponential, but must be implemented by all clients, |
64 | | // just in case. |
65 | | virtual T ShortVisit(Regexp* re, T parent_arg) = 0; |
66 | | |
67 | | // Walks over a regular expression. |
68 | | // Top_arg is passed as parent_arg to PreVisit and PostVisit of re. |
69 | | // Returns the T returned by PostVisit on re. |
70 | | T Walk(Regexp* re, T top_arg); |
71 | | |
72 | | // Like Walk, but doesn't use Copy. This can lead to |
73 | | // exponential runtimes on cross-linked Regexps like the |
74 | | // ones generated by Simplify. To help limit this, |
75 | | // at most max_visits nodes will be visited and then |
76 | | // the walk will be cut off early. |
77 | | // If the walk *is* cut off early, ShortVisit(re) |
78 | | // will be called on regexps that cannot be fully |
79 | | // visited rather than calling PreVisit/PostVisit. |
80 | | T WalkExponential(Regexp* re, T top_arg, int max_visits); |
81 | | |
82 | | // Clears the stack. Should never be necessary, since |
83 | | // Walk always enters and exits with an empty stack. |
84 | | // Logs DFATAL if stack is not already clear. |
85 | | void Reset(); |
86 | | |
87 | | // Returns whether walk was cut off. |
88 | 0 | bool stopped_early() { return stopped_early_; } |
89 | | |
90 | | private: |
91 | | // Walk state for the entire traversal. |
92 | | std::stack<WalkState<T> >* stack_; |
93 | | bool stopped_early_; |
94 | | int max_visits_; |
95 | | |
96 | | T WalkInternal(Regexp* re, T top_arg, bool use_copy); |
97 | | |
98 | | Walker(const Walker&) = delete; |
99 | | Walker& operator=(const Walker&) = delete; |
100 | | }; |
101 | | |
102 | | template<typename T> T Regexp::Walker<T>::PreVisit(Regexp* re, |
103 | | T parent_arg, |
104 | 0 | bool* stop) { |
105 | 0 | return parent_arg; |
106 | 0 | } Unexecuted instantiation: re2::Regexp::Walker<int>::PreVisit(re2::Regexp*, int, bool*) Unexecuted instantiation: re2::Regexp::Walker<re2::Frag>::PreVisit(re2::Regexp*, re2::Frag, bool*) Unexecuted instantiation: re2::Regexp::Walker<re2::Regexp*>::PreVisit(re2::Regexp*, re2::Regexp*, bool*) |
107 | | |
108 | | template<typename T> T Regexp::Walker<T>::PostVisit(Regexp* re, |
109 | | T parent_arg, |
110 | | T pre_arg, |
111 | | T* child_args, |
112 | 0 | int nchild_args) { |
113 | 0 | return pre_arg; |
114 | 0 | } Unexecuted instantiation: re2::Regexp::Walker<int>::PostVisit(re2::Regexp*, int, int, int*, int) Unexecuted instantiation: re2::Regexp::Walker<re2::Frag>::PostVisit(re2::Regexp*, re2::Frag, re2::Frag, re2::Frag*, int) Unexecuted instantiation: re2::Regexp::Walker<re2::Regexp*>::PostVisit(re2::Regexp*, re2::Regexp*, re2::Regexp*, re2::Regexp**, int) |
115 | | |
116 | 0 | template<typename T> T Regexp::Walker<T>::Copy(T arg) { |
117 | 0 | return arg; |
118 | 0 | } Unexecuted instantiation: re2::Regexp::Walker<int>::Copy(int) Unexecuted instantiation: re2::Regexp::Walker<re2::Frag>::Copy(re2::Frag) Unexecuted instantiation: re2::Regexp::Walker<re2::Regexp*>::Copy(re2::Regexp*) |
119 | | |
120 | | // State about a single level in the traversal. |
121 | | template<typename T> struct WalkState { |
122 | | WalkState<T>(Regexp* re, T parent) |
123 | | : re(re), |
124 | | n(-1), |
125 | | parent_arg(parent), |
126 | 0 | child_args(NULL) { } Unexecuted instantiation: re2::WalkState<int>::WalkState(re2::Regexp*, int) Unexecuted instantiation: re2::WalkState<re2::Frag>::WalkState(re2::Regexp*, re2::Frag) Unexecuted instantiation: re2::WalkState<re2::Regexp*>::WalkState(re2::Regexp*, re2::Regexp*) |
127 | | |
128 | | Regexp* re; // The regexp |
129 | | int n; // The index of the next child to process; -1 means need to PreVisit |
130 | | T parent_arg; // Accumulated arguments. |
131 | | T pre_arg; |
132 | | T child_arg; // One-element buffer for child_args. |
133 | | T* child_args; |
134 | | }; |
135 | | |
136 | 0 | template<typename T> Regexp::Walker<T>::Walker() { |
137 | 0 | stack_ = new std::stack<WalkState<T> >; |
138 | 0 | stopped_early_ = false; |
139 | 0 | } Unexecuted instantiation: re2::Regexp::Walker<int>::Walker() Unexecuted instantiation: re2::Regexp::Walker<re2::Frag>::Walker() Unexecuted instantiation: re2::Regexp::Walker<re2::Regexp*>::Walker() |
140 | | |
141 | 0 | template<typename T> Regexp::Walker<T>::~Walker() { |
142 | 0 | Reset(); |
143 | 0 | delete stack_; |
144 | 0 | } Unexecuted instantiation: re2::Regexp::Walker<int>::~Walker() Unexecuted instantiation: re2::Regexp::Walker<re2::Frag>::~Walker() Unexecuted instantiation: re2::Regexp::Walker<re2::Regexp*>::~Walker() |
145 | | |
146 | | // Clears the stack. Should never be necessary, since |
147 | | // Walk always enters and exits with an empty stack. |
148 | | // Logs DFATAL if stack is not already clear. |
149 | 0 | template<typename T> void Regexp::Walker<T>::Reset() { |
150 | 0 | if (stack_ && stack_->size() > 0) { |
151 | 0 | LOG(DFATAL) << "Stack not empty."; |
152 | 0 | while (stack_->size() > 0) { |
153 | 0 | delete stack_->top().child_args; |
154 | 0 | stack_->pop(); |
155 | 0 | } |
156 | 0 | } |
157 | 0 | } Unexecuted instantiation: re2::Regexp::Walker<int>::Reset() Unexecuted instantiation: re2::Regexp::Walker<re2::Frag>::Reset() Unexecuted instantiation: re2::Regexp::Walker<re2::Regexp*>::Reset() |
158 | | |
159 | | template<typename T> T Regexp::Walker<T>::WalkInternal(Regexp* re, T top_arg, |
160 | 0 | bool use_copy) { |
161 | 0 | Reset(); |
162 | |
|
163 | 0 | if (re == NULL) { |
164 | 0 | LOG(DFATAL) << "Walk NULL"; |
165 | 0 | return top_arg; |
166 | 0 | } |
167 | | |
168 | 0 | stack_->push(WalkState<T>(re, top_arg)); |
169 | |
|
170 | 0 | WalkState<T>* s; |
171 | 0 | for (;;) { |
172 | 0 | T t; |
173 | 0 | s = &stack_->top(); |
174 | 0 | Regexp* re = s->re; |
175 | 0 | switch (s->n) { |
176 | 0 | case -1: { |
177 | 0 | if (--max_visits_ < 0) { |
178 | 0 | stopped_early_ = true; |
179 | 0 | t = ShortVisit(re, s->parent_arg); |
180 | 0 | break; |
181 | 0 | } |
182 | 0 | bool stop = false; |
183 | 0 | s->pre_arg = PreVisit(re, s->parent_arg, &stop); |
184 | 0 | if (stop) { |
185 | 0 | t = s->pre_arg; |
186 | 0 | break; |
187 | 0 | } |
188 | 0 | s->n = 0; |
189 | 0 | s->child_args = NULL; |
190 | 0 | if (re->nsub_ == 1) |
191 | 0 | s->child_args = &s->child_arg; |
192 | 0 | else if (re->nsub_ > 1) |
193 | 0 | s->child_args = new T[re->nsub_]; |
194 | 0 | FALLTHROUGH_INTENDED; |
195 | 0 | } |
196 | 0 | default: { |
197 | 0 | if (re->nsub_ > 0) { |
198 | 0 | Regexp** sub = re->sub(); |
199 | 0 | if (s->n < re->nsub_) { |
200 | 0 | if (use_copy && s->n > 0 && sub[s->n - 1] == sub[s->n]) { |
201 | 0 | s->child_args[s->n] = Copy(s->child_args[s->n - 1]); |
202 | 0 | s->n++; |
203 | 0 | } else { |
204 | 0 | stack_->push(WalkState<T>(sub[s->n], s->pre_arg)); |
205 | 0 | } |
206 | 0 | continue; |
207 | 0 | } |
208 | 0 | } |
209 | | |
210 | 0 | t = PostVisit(re, s->parent_arg, s->pre_arg, s->child_args, s->n); |
211 | 0 | if (re->nsub_ > 1) |
212 | 0 | delete[] s->child_args; |
213 | 0 | break; |
214 | 0 | } |
215 | 0 | } |
216 | | |
217 | | // We've finished stack_->top(). |
218 | | // Update next guy down. |
219 | 0 | stack_->pop(); |
220 | 0 | if (stack_->size() == 0) |
221 | 0 | return t; |
222 | 0 | s = &stack_->top(); |
223 | 0 | if (s->child_args != NULL) |
224 | 0 | s->child_args[s->n] = t; |
225 | 0 | else |
226 | 0 | s->child_arg = t; |
227 | 0 | s->n++; |
228 | 0 | } |
229 | 0 | } Unexecuted instantiation: re2::Regexp::Walker<int>::WalkInternal(re2::Regexp*, int, bool) Unexecuted instantiation: re2::Regexp::Walker<re2::Frag>::WalkInternal(re2::Regexp*, re2::Frag, bool) Unexecuted instantiation: re2::Regexp::Walker<re2::Regexp*>::WalkInternal(re2::Regexp*, re2::Regexp*, bool) |
230 | | |
231 | 0 | template<typename T> T Regexp::Walker<T>::Walk(Regexp* re, T top_arg) { |
232 | | // Without the exponential walking behavior, |
233 | | // this budget should be more than enough for any |
234 | | // regexp, and yet not enough to get us in trouble |
235 | | // as far as CPU time. |
236 | 0 | max_visits_ = 1000000; |
237 | 0 | return WalkInternal(re, top_arg, true); |
238 | 0 | } Unexecuted instantiation: re2::Regexp::Walker<int>::Walk(re2::Regexp*, int) Unexecuted instantiation: re2::Regexp::Walker<re2::Regexp*>::Walk(re2::Regexp*, re2::Regexp*) |
239 | | |
240 | | template<typename T> T Regexp::Walker<T>::WalkExponential(Regexp* re, T top_arg, |
241 | 0 | int max_visits) { |
242 | 0 | max_visits_ = max_visits; |
243 | 0 | return WalkInternal(re, top_arg, false); |
244 | 0 | } Unexecuted instantiation: re2::Regexp::Walker<re2::Frag>::WalkExponential(re2::Regexp*, re2::Frag, int) Unexecuted instantiation: re2::Regexp::Walker<int>::WalkExponential(re2::Regexp*, int, int) |
245 | | |
246 | | } // namespace re2 |
247 | | |
248 | | #endif // RE2_WALKER_INL_H_ |