Shinichiro Hamaji | 1d545aa | 2015-06-23 15:29:13 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | // +build ignore |
| 16 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 17 | #include "func.h" |
| 18 | |
Shinichiro Hamaji | 62b16e7 | 2015-07-02 01:42:31 +0900 | [diff] [blame] | 19 | #include <errno.h> |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 20 | #include <limits.h> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 21 | #include <stdio.h> |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 22 | #include <stdlib.h> |
Shinichiro Hamaji | 62b16e7 | 2015-07-02 01:42:31 +0900 | [diff] [blame] | 23 | #include <unistd.h> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 24 | |
Shinichiro Hamaji | d87e59e | 2015-06-17 18:18:34 +0900 | [diff] [blame] | 25 | #include <algorithm> |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 26 | #include <iterator> |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 27 | #include <memory> |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 28 | #include <unordered_map> |
| 29 | |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 30 | #include "ast.h" |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 31 | #include "eval.h" |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 32 | #include "fileutil.h" |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 33 | #include "find.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 34 | #include "log.h" |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 35 | #include "parser.h" |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 36 | #include "stats.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 37 | #include "strutil.h" |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 38 | #include "symtab.h" |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 39 | #include "var.h" |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 40 | |
| 41 | namespace { |
| 42 | |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 43 | void PatsubstFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 44 | shared_ptr<string> pat_str = args[0]->Eval(ev); |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 45 | shared_ptr<string> repl = args[1]->Eval(ev); |
| 46 | shared_ptr<string> str = args[2]->Eval(ev); |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 47 | WordWriter ww(s); |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 48 | Pattern pat(*pat_str); |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 49 | for (StringPiece tok : WordScanner(*str)) { |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 50 | ww.MaybeAddWhitespace(); |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 51 | pat.AppendSubst(tok, *repl, s); |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 52 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 53 | } |
| 54 | |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 55 | void StripFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 56 | shared_ptr<string> str = args[0]->Eval(ev); |
| 57 | WordWriter ww(s); |
| 58 | for (StringPiece tok : WordScanner(*str)) { |
| 59 | ww.Write(tok); |
| 60 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 61 | } |
| 62 | |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 63 | void SubstFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 64 | shared_ptr<string> pat = args[0]->Eval(ev); |
| 65 | shared_ptr<string> repl = args[1]->Eval(ev); |
| 66 | shared_ptr<string> str = args[2]->Eval(ev); |
Shinichiro Hamaji | 5af931d | 2015-07-06 15:37:59 +0900 | [diff] [blame] | 67 | if (pat->empty()) { |
| 68 | *s += *str; |
| 69 | *s += *repl; |
| 70 | return; |
| 71 | } |
Shinichiro Hamaji | 37591ce | 2015-06-16 19:36:05 +0900 | [diff] [blame] | 72 | size_t index = 0; |
| 73 | while (index < str->size()) { |
| 74 | size_t found = str->find(*pat, index); |
| 75 | if (found == string::npos) |
| 76 | break; |
| 77 | AppendString(StringPiece(*str).substr(index, found - index), s); |
| 78 | AppendString(*repl, s); |
| 79 | index = found + pat->size(); |
| 80 | } |
| 81 | AppendString(StringPiece(*str).substr(index), s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 82 | } |
| 83 | |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 84 | void FindstringFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 85 | shared_ptr<string> find = args[0]->Eval(ev); |
| 86 | shared_ptr<string> in = args[1]->Eval(ev); |
| 87 | if (in->find(*find) != string::npos) |
| 88 | AppendString(*find, s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 89 | } |
| 90 | |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 91 | void FilterFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 92 | shared_ptr<string> pat_buf = args[0]->Eval(ev); |
| 93 | shared_ptr<string> text = args[1]->Eval(ev); |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 94 | vector<Pattern> pats; |
| 95 | for (StringPiece pat : WordScanner(*pat_buf)) { |
| 96 | pats.push_back(Pattern(pat)); |
| 97 | } |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 98 | WordWriter ww(s); |
| 99 | for (StringPiece tok : WordScanner(*text)) { |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 100 | for (const Pattern& pat : pats) { |
| 101 | if (pat.Match(tok)) { |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 102 | ww.Write(tok); |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void FilterOutFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 110 | shared_ptr<string> pat_buf = args[0]->Eval(ev); |
| 111 | shared_ptr<string> text = args[1]->Eval(ev); |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 112 | vector<Pattern> pats; |
| 113 | for (StringPiece pat : WordScanner(*pat_buf)) { |
| 114 | pats.push_back(Pattern(pat)); |
| 115 | } |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 116 | WordWriter ww(s); |
| 117 | for (StringPiece tok : WordScanner(*text)) { |
| 118 | bool matched = false; |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 119 | for (const Pattern& pat : pats) { |
| 120 | if (pat.Match(tok)) { |
Shinichiro Hamaji | 00cc658 | 2015-06-17 18:12:46 +0900 | [diff] [blame] | 121 | matched = true; |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | if (!matched) |
| 126 | ww.Write(tok); |
| 127 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 128 | } |
| 129 | |
Shinichiro Hamaji | d87e59e | 2015-06-17 18:18:34 +0900 | [diff] [blame] | 130 | void SortFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 131 | shared_ptr<string> list = args[0]->Eval(ev); |
| 132 | vector<StringPiece> toks; |
| 133 | WordScanner(*list).Split(&toks); |
| 134 | sort(toks.begin(), toks.end()); |
| 135 | WordWriter ww(s); |
| 136 | StringPiece prev; |
| 137 | for (StringPiece tok : toks) { |
| 138 | if (prev != tok) { |
| 139 | ww.Write(tok); |
| 140 | prev = tok; |
| 141 | } |
| 142 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 143 | } |
| 144 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 145 | static int GetNumericValueForFunc(const string& buf) { |
| 146 | StringPiece s = TrimLeftSpace(buf); |
| 147 | char* end; |
| 148 | long n = strtol(s.data(), &end, 10); |
| 149 | if (n < 0 || n == LONG_MAX || s.data() + s.size() != end) { |
| 150 | return -1; |
| 151 | } |
| 152 | return n; |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 153 | } |
| 154 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 155 | void WordFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 156 | shared_ptr<string> n_str = args[0]->Eval(ev); |
| 157 | int n = GetNumericValueForFunc(*n_str); |
| 158 | if (n < 0) { |
| 159 | ev->Error(StringPrintf( |
| 160 | "*** non-numeric first argument to `word' function: '%s'.", |
| 161 | n_str->c_str())); |
| 162 | } |
| 163 | if (n == 0) { |
| 164 | ev->Error("*** first argument to `word' function must be greater than 0."); |
| 165 | } |
| 166 | |
| 167 | shared_ptr<string> text = args[1]->Eval(ev); |
| 168 | for (StringPiece tok : WordScanner(*text)) { |
| 169 | n--; |
| 170 | if (n == 0) { |
| 171 | AppendString(tok, s); |
| 172 | break; |
| 173 | } |
| 174 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 175 | } |
| 176 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 177 | void WordlistFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 178 | shared_ptr<string> s_str = args[0]->Eval(ev); |
| 179 | int si = GetNumericValueForFunc(*s_str); |
| 180 | if (si < 0) { |
| 181 | ev->Error(StringPrintf( |
| 182 | "*** non-numeric first argument to `wordlist' function: '%s'.", |
| 183 | s_str->c_str())); |
| 184 | } |
| 185 | if (si == 0) { |
| 186 | ev->Error(StringPrintf( |
| 187 | "*** invalid first argument to `wordlist' function: %s`", |
| 188 | s_str->c_str())); |
| 189 | } |
| 190 | |
| 191 | shared_ptr<string> e_str = args[1]->Eval(ev); |
| 192 | int ei = GetNumericValueForFunc(*e_str); |
| 193 | if (ei < 0) { |
| 194 | ev->Error(StringPrintf( |
| 195 | "*** non-numeric second argument to `wordlist' function: '%s'.", |
| 196 | e_str->c_str())); |
| 197 | } |
| 198 | |
| 199 | shared_ptr<string> text = args[2]->Eval(ev); |
| 200 | int i = 0; |
| 201 | WordWriter ww(s); |
| 202 | for (StringPiece tok : WordScanner(*text)) { |
| 203 | i++; |
| 204 | if (si <= i && i <= ei) { |
| 205 | ww.Write(tok); |
| 206 | } |
| 207 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 208 | } |
| 209 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 210 | void WordsFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 211 | shared_ptr<string> text = args[0]->Eval(ev); |
| 212 | WordScanner ws(*text); |
| 213 | int n = 0; |
| 214 | for (auto iter = ws.begin(); iter != ws.end(); ++iter) |
| 215 | n++; |
| 216 | char buf[32]; |
| 217 | sprintf(buf, "%d", n); |
| 218 | *s += buf; |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 219 | } |
| 220 | |
Shinichiro Hamaji | d527145 | 2015-06-17 18:50:03 +0900 | [diff] [blame] | 221 | void FirstwordFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 222 | shared_ptr<string> text = args[0]->Eval(ev); |
| 223 | for (StringPiece tok : WordScanner(*text)) { |
| 224 | AppendString(tok, s); |
| 225 | return; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void LastwordFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 230 | shared_ptr<string> text = args[0]->Eval(ev); |
| 231 | StringPiece last; |
| 232 | for (StringPiece tok : WordScanner(*text)) { |
| 233 | last = tok; |
| 234 | } |
| 235 | AppendString(last, s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 236 | } |
| 237 | |
Shinichiro Hamaji | 30b8e60 | 2015-06-17 19:28:54 +0900 | [diff] [blame] | 238 | void JoinFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 239 | shared_ptr<string> list1 = args[0]->Eval(ev); |
| 240 | shared_ptr<string> list2 = args[1]->Eval(ev); |
| 241 | WordScanner ws1(*list1); |
| 242 | WordScanner ws2(*list2); |
| 243 | WordWriter ww(s); |
| 244 | for (WordScanner::Iterator iter1 = ws1.begin(), iter2 = ws2.begin(); |
| 245 | iter1 != ws1.end() && iter2 != ws2.end(); |
| 246 | ++iter1, ++iter2) { |
| 247 | ww.Write(*iter1); |
| 248 | // Use |AppendString| not to append extra ' '. |
| 249 | AppendString(*iter2, s); |
| 250 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 251 | } |
| 252 | |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 253 | void WildcardFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | 0d8e79b | 2015-06-30 03:29:35 +0900 | [diff] [blame] | 254 | COLLECT_STATS("func wildcard time"); |
Shinichiro Hamaji | 284f3d1 | 2015-06-17 19:29:01 +0900 | [diff] [blame] | 255 | shared_ptr<string> pat = args[0]->Eval(ev); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 256 | if (ev->avoid_io()) { |
| 257 | *s += "$(/bin/ls -d "; |
| 258 | *s += *pat; |
| 259 | *s += " 2> /dev/null)"; |
| 260 | return; |
| 261 | } |
| 262 | |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 263 | WordWriter ww(s); |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 264 | vector<string>* files; |
Shinichiro Hamaji | 284f3d1 | 2015-06-17 19:29:01 +0900 | [diff] [blame] | 265 | for (StringPiece tok : WordScanner(*pat)) { |
Shinichiro Hamaji | 8f68bd3 | 2015-06-18 11:01:51 +0900 | [diff] [blame] | 266 | ScopedTerminator st(tok); |
Shinichiro Hamaji | 0e3873a | 2015-07-05 15:48:28 +0900 | [diff] [blame] | 267 | Glob(tok.data(), &files); |
| 268 | sort(files->begin(), files->end()); |
| 269 | for (const string& file : *files) { |
Shinichiro Hamaji | fe97c41 | 2015-06-29 16:57:57 +0900 | [diff] [blame] | 270 | ww.Write(file); |
| 271 | } |
Shinichiro Hamaji | 284f3d1 | 2015-06-17 19:29:01 +0900 | [diff] [blame] | 272 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 273 | } |
| 274 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 275 | void DirFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 276 | shared_ptr<string> text = args[0]->Eval(ev); |
| 277 | WordWriter ww(s); |
| 278 | for (StringPiece tok : WordScanner(*text)) { |
| 279 | ww.Write(Dirname(tok)); |
Shinichiro Hamaji | 5590685 | 2015-06-29 16:40:33 +0900 | [diff] [blame] | 280 | s->push_back('/'); |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 281 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 282 | } |
| 283 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 284 | void NotdirFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 285 | shared_ptr<string> text = args[0]->Eval(ev); |
| 286 | WordWriter ww(s); |
| 287 | for (StringPiece tok : WordScanner(*text)) { |
| 288 | if (tok == "/") { |
Shinichiro Hamaji | 388e858 | 2015-07-03 16:51:46 +0900 | [diff] [blame] | 289 | ww.Write(StringPiece("")); |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 290 | } else { |
| 291 | ww.Write(Basename(tok)); |
| 292 | } |
| 293 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 294 | } |
| 295 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 296 | void SuffixFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 297 | shared_ptr<string> text = args[0]->Eval(ev); |
| 298 | WordWriter ww(s); |
| 299 | for (StringPiece tok : WordScanner(*text)) { |
| 300 | StringPiece suf = GetExt(tok); |
| 301 | if (!suf.empty()) |
| 302 | ww.Write(suf); |
| 303 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 304 | } |
| 305 | |
Shinichiro Hamaji | 67f9a70 | 2015-06-18 06:00:57 +0900 | [diff] [blame] | 306 | void BasenameFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 307 | shared_ptr<string> text = args[0]->Eval(ev); |
| 308 | WordWriter ww(s); |
| 309 | for (StringPiece tok : WordScanner(*text)) { |
| 310 | ww.Write(StripExt(tok)); |
| 311 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 312 | } |
| 313 | |
Shinichiro Hamaji | 5d694f0 | 2015-06-18 06:05:36 +0900 | [diff] [blame] | 314 | void AddsuffixFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 315 | shared_ptr<string> suf = args[0]->Eval(ev); |
| 316 | shared_ptr<string> text = args[1]->Eval(ev); |
| 317 | WordWriter ww(s); |
| 318 | for (StringPiece tok : WordScanner(*text)) { |
| 319 | ww.Write(tok); |
| 320 | *s += *suf; |
| 321 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 322 | } |
| 323 | |
Shinichiro Hamaji | 5d694f0 | 2015-06-18 06:05:36 +0900 | [diff] [blame] | 324 | void AddprefixFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 325 | shared_ptr<string> pre = args[0]->Eval(ev); |
| 326 | shared_ptr<string> text = args[1]->Eval(ev); |
| 327 | WordWriter ww(s); |
| 328 | for (StringPiece tok : WordScanner(*text)) { |
| 329 | ww.Write(*pre); |
| 330 | AppendString(tok, s); |
| 331 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 332 | } |
| 333 | |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 334 | void RealpathFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 335 | shared_ptr<string> text = args[0]->Eval(ev); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 336 | if (ev->avoid_io()) { |
| 337 | *s += "KATI_TODO(realpath)"; |
| 338 | return; |
| 339 | } |
| 340 | |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 341 | WordWriter ww(s); |
| 342 | for (StringPiece tok : WordScanner(*text)) { |
Shinichiro Hamaji | 8f68bd3 | 2015-06-18 11:01:51 +0900 | [diff] [blame] | 343 | ScopedTerminator st(tok); |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 344 | char buf[PATH_MAX]; |
| 345 | if (realpath(tok.data(), buf)) |
| 346 | *s += buf; |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 347 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 348 | } |
| 349 | |
Shinichiro Hamaji | 8a96358 | 2015-06-18 07:05:58 +0900 | [diff] [blame] | 350 | void AbspathFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 351 | shared_ptr<string> text = args[0]->Eval(ev); |
| 352 | WordWriter ww(s); |
| 353 | string buf; |
| 354 | for (StringPiece tok : WordScanner(*text)) { |
| 355 | AbsPath(tok, &buf); |
| 356 | ww.Write(buf); |
| 357 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 358 | } |
| 359 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 360 | void IfFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 361 | shared_ptr<string> cond = args[0]->Eval(ev); |
| 362 | if (cond->empty()) { |
| 363 | if (args.size() > 2) |
| 364 | args[2]->Eval(ev, s); |
| 365 | } else { |
| 366 | args[1]->Eval(ev, s); |
| 367 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 368 | } |
| 369 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 370 | void AndFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 371 | shared_ptr<string> cond; |
| 372 | for (Value* a : args) { |
| 373 | cond = a->Eval(ev); |
| 374 | if (cond->empty()) |
| 375 | return; |
| 376 | } |
| 377 | if (cond.get()) { |
| 378 | *s += *cond; |
| 379 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 380 | } |
| 381 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 382 | void OrFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 383 | for (Value* a : args) { |
| 384 | shared_ptr<string> cond = a->Eval(ev); |
| 385 | if (!cond->empty()) { |
| 386 | *s += *cond; |
| 387 | return; |
| 388 | } |
| 389 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 390 | } |
| 391 | |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 392 | void ValueFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 393 | shared_ptr<string> var_name = args[0]->Eval(ev); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 394 | Var* var = ev->LookupVar(Intern(*var_name)); |
Shinichiro Hamaji | 81699be | 2015-06-22 18:07:38 +0900 | [diff] [blame] | 395 | AppendString(var->String().as_string(), s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 396 | } |
| 397 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 398 | void EvalFunc(const vector<Value*>& args, Evaluator* ev, string*) { |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 399 | // TODO: eval leaks everything... for now. |
| 400 | //shared_ptr<string> text = args[0]->Eval(ev); |
| 401 | string* text = new string; |
| 402 | args[0]->Eval(ev, text); |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 403 | vector<AST*> asts; |
| 404 | Parse(*text, ev->loc(), &asts); |
| 405 | for (AST* ast : asts) { |
| 406 | LOG("%s", ast->DebugString().c_str()); |
| 407 | ast->Eval(ev); |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 408 | //delete ast; |
Shinichiro Hamaji | 80456fb | 2015-06-18 14:56:10 +0900 | [diff] [blame] | 409 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 410 | } |
| 411 | |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 412 | //#define TEST_FIND_EMULATOR |
| 413 | |
| 414 | #ifdef TEST_FIND_EMULATOR |
| 415 | static string SortWordsInString(StringPiece s) { |
| 416 | vector<string> toks; |
| 417 | for (StringPiece tok : WordScanner(s)) { |
| 418 | toks.push_back(tok.as_string()); |
| 419 | } |
| 420 | sort(toks.begin(), toks.end()); |
| 421 | return JoinStrings(toks, " "); |
| 422 | } |
| 423 | #endif |
| 424 | |
Shinichiro Hamaji | 68e712b | 2015-07-17 06:11:08 +0900 | [diff] [blame] | 425 | |
| 426 | // A hack for Android build. We need to evaluate things like $((3+4)) |
| 427 | // when we emit ninja file, because the result of such expressions |
| 428 | // will be passed to other make functions. |
| 429 | // TODO: Maybe we should introduce a helper binary which evaluate |
| 430 | // make expressions at ninja-time. |
| 431 | static bool HasNoIoInShellScript(const string& cmd) { |
| 432 | if (cmd.empty()) |
| 433 | return true; |
| 434 | if (HasPrefix(cmd, "echo $((") && cmd[cmd.size()-1] == ')') |
| 435 | return true; |
| 436 | return false; |
| 437 | } |
| 438 | |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 439 | void ShellFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 440 | shared_ptr<string> cmd = args[0]->Eval(ev); |
Shinichiro Hamaji | 68e712b | 2015-07-17 06:11:08 +0900 | [diff] [blame] | 441 | if (ev->avoid_io() && !HasNoIoInShellScript(*cmd)) { |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 442 | *s += "$("; |
| 443 | *s += *cmd; |
| 444 | *s += ")"; |
| 445 | return; |
| 446 | } |
| 447 | |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 448 | LOG("ShellFunc: %s", cmd->c_str()); |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 449 | |
| 450 | #ifdef TEST_FIND_EMULATOR |
| 451 | bool need_check = false; |
| 452 | string out2; |
Shinichiro Hamaji | 4e950e6 | 2015-07-02 02:59:55 +0900 | [diff] [blame] | 453 | if (FindEmulator::Get() && FindEmulator::Get()->HandleFind(*cmd, &out2)) { |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 454 | need_check = true; |
| 455 | } |
| 456 | #else |
Shinichiro Hamaji | 4e950e6 | 2015-07-02 02:59:55 +0900 | [diff] [blame] | 457 | if (FindEmulator::Get() && FindEmulator::Get()->HandleFind(*cmd, s)) |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 458 | return; |
| 459 | #endif |
| 460 | |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 461 | COLLECT_STATS_WITH_SLOW_REPORT("func shell time", cmd->c_str()); |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 462 | string out; |
Shinichiro Hamaji | 94d6f2a | 2015-07-05 05:32:25 +0900 | [diff] [blame] | 463 | shared_ptr<string> shell = ev->EvalVar(kShellSym); |
| 464 | RunCommand(*shell, *cmd, false, &out); |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 465 | |
| 466 | while (out[out.size()-1] == '\n') |
| 467 | out.pop_back(); |
| 468 | for (size_t i = 0; i < out.size(); i++) { |
| 469 | if (out[i] == '\n') |
| 470 | out[i] = ' '; |
| 471 | } |
Shinichiro Hamaji | 5f57a99 | 2015-06-30 19:39:39 +0900 | [diff] [blame] | 472 | |
| 473 | #ifdef TEST_FIND_EMULATOR |
| 474 | if (need_check) { |
| 475 | string sorted = SortWordsInString(out); |
| 476 | out2 = SortWordsInString(out2); |
| 477 | if (sorted != out2) { |
| 478 | ERROR("FindEmulator is broken: %s\n%s\nvs\n%s", |
| 479 | cmd->c_str(), sorted.c_str(), out2.c_str()); |
| 480 | } |
| 481 | } |
| 482 | #endif |
| 483 | |
Shinichiro Hamaji | fcf1b76 | 2015-06-18 03:43:54 +0900 | [diff] [blame] | 484 | *s += out; |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 485 | } |
| 486 | |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 487 | void CallFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 488 | static const char* tmpvar_names[] = { |
| 489 | "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" |
| 490 | }; |
| 491 | |
| 492 | shared_ptr<string> func_name = args[0]->Eval(ev); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 493 | Var* func = ev->LookupVar(Intern(*func_name)); |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 494 | vector<unique_ptr<SimpleVar>> av; |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 495 | for (size_t i = 1; i < args.size(); i++) { |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 496 | unique_ptr<SimpleVar> s( |
| 497 | new SimpleVar(args[i]->Eval(ev), VarOrigin::AUTOMATIC)); |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 498 | av.push_back(move(s)); |
| 499 | } |
Shinichiro Hamaji | 5d53bc7 | 2015-06-26 08:31:54 +0900 | [diff] [blame] | 500 | vector<unique_ptr<ScopedVar>> sv; |
| 501 | for (size_t i = 1; i < args.size(); i++) { |
| 502 | sv.push_back(move(unique_ptr<ScopedVar>( |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 503 | new ScopedVar(ev->mutable_vars(), |
| 504 | Intern(tmpvar_names[i]), av[i-1].get())))); |
Shinichiro Hamaji | 5d53bc7 | 2015-06-26 08:31:54 +0900 | [diff] [blame] | 505 | } |
Shinichiro Hamaji | 3064f1f | 2015-06-18 16:32:09 +0900 | [diff] [blame] | 506 | func->Eval(ev, s); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 507 | } |
| 508 | |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 509 | void ForeachFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 510 | shared_ptr<string> varname = args[0]->Eval(ev); |
| 511 | shared_ptr<string> list = args[1]->Eval(ev); |
| 512 | WordWriter ww(s); |
| 513 | for (StringPiece tok : WordScanner(*list)) { |
| 514 | unique_ptr<SimpleVar> v(new SimpleVar( |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 515 | make_shared<string>(tok.data(), tok.size()), VarOrigin::AUTOMATIC)); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 516 | ScopedVar sv(ev->mutable_vars(), Intern(*varname), v.get()); |
Shinichiro Hamaji | cf0cd68 | 2015-06-18 16:18:13 +0900 | [diff] [blame] | 517 | ww.MaybeAddWhitespace(); |
| 518 | args[2]->Eval(ev, s); |
| 519 | } |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 520 | } |
| 521 | |
Shinichiro Hamaji | c22fdb4 | 2015-06-22 18:10:40 +0900 | [diff] [blame] | 522 | void OriginFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 523 | shared_ptr<string> var_name = args[0]->Eval(ev); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 524 | Var* var = ev->LookupVar(Intern(*var_name)); |
Shinichiro Hamaji | f62e9a7 | 2015-06-26 04:18:21 +0900 | [diff] [blame] | 525 | *s += GetOriginStr(var->Origin()); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 526 | } |
| 527 | |
Shinichiro Hamaji | c22fdb4 | 2015-06-22 18:10:40 +0900 | [diff] [blame] | 528 | void FlavorFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
| 529 | shared_ptr<string> var_name = args[0]->Eval(ev); |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 530 | Var* var = ev->LookupVar(Intern(*var_name)); |
Shinichiro Hamaji | c22fdb4 | 2015-06-22 18:10:40 +0900 | [diff] [blame] | 531 | *s += var->Flavor(); |
Shinichiro Hamaji | 4f22f5c | 2015-06-16 16:28:25 +0900 | [diff] [blame] | 532 | } |
| 533 | |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 534 | void InfoFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 535 | shared_ptr<string> a = args[0]->Eval(ev); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 536 | if (ev->avoid_io()) { |
| 537 | *s += "KATI_TODO(info)"; |
| 538 | return; |
| 539 | } |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 540 | printf("%s\n", a->c_str()); |
| 541 | fflush(stdout); |
| 542 | } |
| 543 | |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 544 | void WarningFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 545 | shared_ptr<string> a = args[0]->Eval(ev); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 546 | if (ev->avoid_io()) { |
| 547 | *s += "KATI_TODO(warning)"; |
| 548 | return; |
| 549 | } |
Shinichiro Hamaji | 8ee8c37 | 2015-06-16 16:19:40 +0900 | [diff] [blame] | 550 | printf("%s:%d: %s\n", LOCF(ev->loc()), a->c_str()); |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 551 | fflush(stdout); |
| 552 | } |
| 553 | |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 554 | void ErrorFunc(const vector<Value*>& args, Evaluator* ev, string* s) { |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 555 | shared_ptr<string> a = args[0]->Eval(ev); |
Shinichiro Hamaji | df1fc8b | 2015-06-29 15:45:21 +0900 | [diff] [blame] | 556 | if (ev->avoid_io()) { |
| 557 | *s += "KATI_TODO(error)"; |
| 558 | return; |
| 559 | } |
Shinichiro Hamaji | 9619b36 | 2015-06-16 16:13:25 +0900 | [diff] [blame] | 560 | ev->Error(StringPrintf("*** %s.", a->c_str())); |
| 561 | } |
| 562 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 563 | FuncInfo g_func_infos[] = { |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 564 | { "patsubst", &PatsubstFunc, 3, 3, false, false }, |
| 565 | { "strip", &StripFunc, 1, 1, false, false }, |
| 566 | { "subst", &SubstFunc, 3, 3, false, false }, |
| 567 | { "findstring", &FindstringFunc, 2, 2, false, false }, |
| 568 | { "filter", &FilterFunc, 2, 2, false, false }, |
| 569 | { "filter-out", &FilterOutFunc, 2, 2, false, false }, |
| 570 | { "sort", &SortFunc, 1, 1, false, false }, |
| 571 | { "word", &WordFunc, 2, 2, false, false }, |
| 572 | { "wordlist", &WordlistFunc, 3, 3, false, false }, |
| 573 | { "words", &WordsFunc, 1, 1, false, false }, |
| 574 | { "firstword", &FirstwordFunc, 1, 1, false, false }, |
| 575 | { "lastword", &LastwordFunc, 1, 1, false, false }, |
Shinichiro Hamaji | 2e6cbfc | 2015-06-16 18:46:50 +0900 | [diff] [blame] | 576 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 577 | { "join", &JoinFunc, 2, 2, false, false }, |
| 578 | { "wildcard", &WildcardFunc, 1, 1, false, false }, |
| 579 | { "dir", &DirFunc, 1, 1, false, false }, |
| 580 | { "notdir", &NotdirFunc, 1, 1, false, false }, |
| 581 | { "suffix", &SuffixFunc, 1, 1, false, false }, |
| 582 | { "basename", &BasenameFunc, 1, 1, false, false }, |
| 583 | { "addsuffix", &AddsuffixFunc, 2, 2, false, false }, |
| 584 | { "addprefix", &AddprefixFunc, 2, 2, false, false }, |
| 585 | { "realpath", &RealpathFunc, 1, 1, false, false }, |
| 586 | { "abspath", &AbspathFunc, 1, 1, false, false }, |
Shinichiro Hamaji | 30b8e60 | 2015-06-17 19:28:54 +0900 | [diff] [blame] | 587 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 588 | { "if", &IfFunc, 3, 2, false, true }, |
| 589 | { "and", &AndFunc, 0, 0, true, false }, |
| 590 | { "or", &OrFunc, 0, 0, true, false }, |
Shinichiro Hamaji | e22fe8e | 2015-06-18 07:11:54 +0900 | [diff] [blame] | 591 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 592 | { "value", &ValueFunc, 1, 1, false, false }, |
| 593 | { "eval", &EvalFunc, 1, 1, false, false }, |
| 594 | { "shell", &ShellFunc, 1, 1, false, false }, |
| 595 | { "call", &CallFunc, 0, 0, false, false }, |
| 596 | { "foreach", &ForeachFunc, 3, 3, false, false }, |
Shinichiro Hamaji | e22fe8e | 2015-06-18 07:11:54 +0900 | [diff] [blame] | 597 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 598 | { "origin", &OriginFunc, 1, 1, false, false }, |
| 599 | { "flavor", &FlavorFunc, 1, 1, false, false }, |
Shinichiro Hamaji | e22fe8e | 2015-06-18 07:11:54 +0900 | [diff] [blame] | 600 | |
Shinichiro Hamaji | fead3b7 | 2015-06-18 15:31:15 +0900 | [diff] [blame] | 601 | { "info", &InfoFunc, 1, 1, false, false }, |
| 602 | { "warning", &WarningFunc, 1, 1, false, false }, |
| 603 | { "error", &ErrorFunc, 1, 1, false, false }, |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 604 | }; |
| 605 | |
| 606 | unordered_map<StringPiece, FuncInfo*>* g_func_info_map; |
| 607 | |
| 608 | } // namespace |
| 609 | |
| 610 | void InitFuncTable() { |
| 611 | g_func_info_map = new unordered_map<StringPiece, FuncInfo*>; |
| 612 | for (size_t i = 0; i < sizeof(g_func_infos) / sizeof(g_func_infos[0]); i++) { |
| 613 | FuncInfo* fi = &g_func_infos[i]; |
Shinichiro Hamaji | e799275 | 2015-06-29 18:38:35 +0900 | [diff] [blame] | 614 | bool ok = g_func_info_map->emplace(fi->name, fi).second; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 615 | CHECK(ok); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | void QuitFuncTable() { |
| 620 | delete g_func_info_map; |
| 621 | } |
| 622 | |
| 623 | FuncInfo* GetFuncInfo(StringPiece name) { |
| 624 | auto found = g_func_info_map->find(name); |
| 625 | if (found == g_func_info_map->end()) |
| 626 | return NULL; |
| 627 | return found->second; |
| 628 | } |