David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | e9ce01c | 2007-05-16 02:29:53 +0000 | [diff] [blame] | 19 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 20 | /** |
| 21 | * thrift - a lightweight cross-language rpc/serialization tool |
| 22 | * |
| 23 | * This file contains the main compiler engine for Thrift, which invokes the |
| 24 | * scanner/parser to build the thrift object tree. The interface generation |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 25 | * code for each language lives in a file by the language name under the |
| 26 | * generate/ folder, and all parse structures live in parse/ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 27 | * |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 28 | */ |
| 29 | |
David Reiss | f10984b | 2008-03-27 21:39:52 +0000 | [diff] [blame] | 30 | #include <cassert> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdarg.h> |
David Reiss | 5ad1260 | 2010-08-31 16:51:30 +0000 | [diff] [blame] | 34 | #include <time.h> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 35 | #include <string> |
David Reiss | 739cbe2 | 2008-04-15 05:44:00 +0000 | [diff] [blame] | 36 | #include <algorithm> |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 37 | #include <sys/types.h> |
| 38 | #include <sys/stat.h> |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 39 | #include <errno.h> |
David Reiss | ab55ed5 | 2008-06-11 01:17:00 +0000 | [diff] [blame] | 40 | #include <limits.h> |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 41 | |
Ben Craig | e957675 | 2013-10-11 08:19:16 -0500 | [diff] [blame] | 42 | #ifdef _WIN32 |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 43 | #include <windows.h> /* for GetFullPathName */ |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 46 | // Careful: must include globals first for extern definitions |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 47 | #include "globals.h" |
| 48 | |
Ben Craig | e957675 | 2013-10-11 08:19:16 -0500 | [diff] [blame] | 49 | #include "platform.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 50 | #include "main.h" |
| 51 | #include "parse/t_program.h" |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 52 | #include "parse/t_scope.h" |
David Reiss | bbbbe88 | 2009-02-17 20:27:48 +0000 | [diff] [blame] | 53 | #include "generate/t_generator.h" |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 54 | |
David Reiss | dd08f6d | 2008-06-30 20:24:24 +0000 | [diff] [blame] | 55 | #include "version.h" |
| 56 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 57 | using namespace std; |
| 58 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 59 | /** |
| 60 | * Global program tree |
| 61 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 62 | t_program* g_program; |
| 63 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 64 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 65 | * Global types |
| 66 | */ |
| 67 | |
| 68 | t_type* g_type_void; |
| 69 | t_type* g_type_string; |
Mark Slee | 8d725a2 | 2007-04-13 01:57:12 +0000 | [diff] [blame] | 70 | t_type* g_type_binary; |
Mark Slee | b6200d8 | 2007-01-19 19:14:36 +0000 | [diff] [blame] | 71 | t_type* g_type_slist; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 72 | t_type* g_type_bool; |
| 73 | t_type* g_type_byte; |
| 74 | t_type* g_type_i16; |
| 75 | t_type* g_type_i32; |
| 76 | t_type* g_type_i64; |
| 77 | t_type* g_type_double; |
| 78 | |
| 79 | /** |
| 80 | * Global scope |
| 81 | */ |
| 82 | t_scope* g_scope; |
| 83 | |
| 84 | /** |
| 85 | * Parent scope to also parse types |
| 86 | */ |
| 87 | t_scope* g_parent_scope; |
| 88 | |
| 89 | /** |
| 90 | * Prefix for putting types in parent scope |
| 91 | */ |
| 92 | string g_parent_prefix; |
| 93 | |
| 94 | /** |
| 95 | * Parsing pass |
| 96 | */ |
| 97 | PARSE_MODE g_parse_mode; |
| 98 | |
| 99 | /** |
| 100 | * Current directory of file being parsed |
| 101 | */ |
| 102 | string g_curdir; |
| 103 | |
| 104 | /** |
| 105 | * Current file being parsed |
| 106 | */ |
| 107 | string g_curpath; |
| 108 | |
| 109 | /** |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 110 | * Search path for inclusions |
| 111 | */ |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 112 | vector<string> g_incl_searchpath; |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 113 | |
| 114 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 115 | * Global debug state |
| 116 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 117 | int g_debug = 0; |
| 118 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 119 | /** |
Bryan Duxbury | a145b4d | 2009-04-03 17:29:25 +0000 | [diff] [blame] | 120 | * Strictness level |
| 121 | */ |
| 122 | int g_strict = 127; |
| 123 | |
| 124 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 125 | * Warning level |
| 126 | */ |
| 127 | int g_warn = 1; |
| 128 | |
| 129 | /** |
| 130 | * Verbose output |
| 131 | */ |
| 132 | int g_verbose = 0; |
| 133 | |
| 134 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 135 | * Global time string |
| 136 | */ |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 137 | char* g_time_str; |
| 138 | |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 139 | /** |
David Reiss | cbd4bac | 2007-08-14 17:12:33 +0000 | [diff] [blame] | 140 | * The last parsed doctext comment. |
| 141 | */ |
| 142 | char* g_doctext; |
| 143 | |
| 144 | /** |
| 145 | * The location of the last parsed doctext comment. |
| 146 | */ |
| 147 | int g_doctext_lineno; |
| 148 | |
Roger Meier | 4f4b15b | 2014-11-05 16:51:04 +0100 | [diff] [blame] | 149 | /** |
Jens Geyer | e8379b5 | 2014-01-25 00:59:45 +0100 | [diff] [blame] | 150 | * The First doctext comment |
| 151 | */ |
| 152 | char* g_program_doctext_candidate; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 153 | int g_program_doctext_lineno = 0; |
| 154 | PROGDOCTEXT_STATUS g_program_doctext_status = INVALID; |
Jens Geyer | e8379b5 | 2014-01-25 00:59:45 +0100 | [diff] [blame] | 155 | |
David Reiss | cbd4bac | 2007-08-14 17:12:33 +0000 | [diff] [blame] | 156 | /** |
Bryan Duxbury | c7206a4 | 2011-08-17 23:17:04 +0000 | [diff] [blame] | 157 | * Whether or not negative field keys are accepted. |
| 158 | */ |
| 159 | int g_allow_neg_field_keys; |
| 160 | |
| 161 | /** |
Roger Meier | 887ff75 | 2011-08-19 11:25:39 +0000 | [diff] [blame] | 162 | * Whether or not 64-bit constants will generate a warning. |
| 163 | */ |
| 164 | int g_allow_64bit_consts = 0; |
| 165 | |
| 166 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 167 | * Flags to control code generation |
| 168 | */ |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 169 | bool gen_recurse = false; |
| 170 | |
| 171 | /** |
Ben Craig | e957675 | 2013-10-11 08:19:16 -0500 | [diff] [blame] | 172 | * Win32 doesn't have realpath, so use fallback implementation in that case, |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 173 | * otherwise this just calls through to realpath |
| 174 | */ |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 175 | char* saferealpath(const char* path, char* resolved_path) { |
Ben Craig | e957675 | 2013-10-11 08:19:16 -0500 | [diff] [blame] | 176 | #ifdef _WIN32 |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 177 | char buf[MAX_PATH]; |
| 178 | char* basename; |
| 179 | DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 180 | if (len == 0 || len > MAX_PATH - 1) { |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 181 | strcpy(resolved_path, path); |
| 182 | } else { |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 183 | strcpy(resolved_path, buf); |
| 184 | } |
Bryan Duxbury | 0137af6 | 2010-04-22 21:21:46 +0000 | [diff] [blame] | 185 | |
| 186 | // Replace backslashes with forward slashes so the |
| 187 | // rest of the code behaves correctly. |
| 188 | size_t resolved_len = strlen(resolved_path); |
| 189 | for (size_t i = 0; i < resolved_len; i++) { |
| 190 | if (resolved_path[i] == '\\') { |
| 191 | resolved_path[i] = '/'; |
| 192 | } |
| 193 | } |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 194 | return resolved_path; |
| 195 | #else |
| 196 | return realpath(path, resolved_path); |
| 197 | #endif |
| 198 | } |
| 199 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 200 | bool check_is_directory(const char* dir_name) { |
Ben Craig | e957675 | 2013-10-11 08:19:16 -0500 | [diff] [blame] | 201 | #ifdef _WIN32 |
Roger Meier | 061d4a2 | 2012-10-07 11:51:00 +0000 | [diff] [blame] | 202 | DWORD attributes = ::GetFileAttributesA(dir_name); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 203 | if (attributes == INVALID_FILE_ATTRIBUTES) { |
| 204 | fprintf(stderr, |
| 205 | "Output directory %s is unusable: GetLastError() = %ld\n", |
| 206 | dir_name, |
| 207 | GetLastError()); |
Roger Meier | 061d4a2 | 2012-10-07 11:51:00 +0000 | [diff] [blame] | 208 | return false; |
| 209 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 210 | if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) { |
Roger Meier | 061d4a2 | 2012-10-07 11:51:00 +0000 | [diff] [blame] | 211 | fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name); |
| 212 | return false; |
| 213 | } |
| 214 | return true; |
| 215 | #else |
| 216 | struct stat sb; |
| 217 | if (stat(dir_name, &sb) < 0) { |
| 218 | fprintf(stderr, "Output directory %s is unusable: %s\n", dir_name, strerror(errno)); |
| 219 | return false; |
| 220 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 221 | if (!S_ISDIR(sb.st_mode)) { |
Roger Meier | 061d4a2 | 2012-10-07 11:51:00 +0000 | [diff] [blame] | 222 | fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name); |
| 223 | return false; |
| 224 | } |
| 225 | return true; |
| 226 | #endif |
| 227 | } |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 228 | |
| 229 | /** |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 230 | * Report an error to the user. This is called yyerror for historical |
| 231 | * reasons (lex and yacc expect the error reporting routine to be called |
| 232 | * this). Call this function to report any errors to the user. |
| 233 | * yyerror takes printf style arguments. |
| 234 | * |
| 235 | * @param fmt C format string followed by additional arguments |
| 236 | */ |
David Reiss | 0babe40 | 2008-06-10 22:56:12 +0000 | [diff] [blame] | 237 | void yyerror(const char* fmt, ...) { |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 238 | va_list args; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 239 | fprintf(stderr, "[ERROR:%s:%d] (last token was '%s')\n", g_curpath.c_str(), yylineno, yytext); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 240 | |
| 241 | va_start(args, fmt); |
| 242 | vfprintf(stderr, fmt, args); |
| 243 | va_end(args); |
| 244 | |
| 245 | fprintf(stderr, "\n"); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Prints a debug message from the parser. |
| 250 | * |
| 251 | * @param fmt C format string followed by additional arguments |
| 252 | */ |
David Reiss | 0babe40 | 2008-06-10 22:56:12 +0000 | [diff] [blame] | 253 | void pdebug(const char* fmt, ...) { |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 254 | if (g_debug == 0) { |
| 255 | return; |
| 256 | } |
| 257 | va_list args; |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 258 | printf("[PARSE:%d] ", yylineno); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 259 | va_start(args, fmt); |
| 260 | vprintf(fmt, args); |
| 261 | va_end(args); |
| 262 | printf("\n"); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Prints a verbose output mode message |
| 267 | * |
| 268 | * @param fmt C format string followed by additional arguments |
| 269 | */ |
David Reiss | 0babe40 | 2008-06-10 22:56:12 +0000 | [diff] [blame] | 270 | void pverbose(const char* fmt, ...) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 271 | if (g_verbose == 0) { |
| 272 | return; |
| 273 | } |
| 274 | va_list args; |
| 275 | va_start(args, fmt); |
| 276 | vprintf(fmt, args); |
| 277 | va_end(args); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Prints a warning message |
| 282 | * |
| 283 | * @param fmt C format string followed by additional arguments |
| 284 | */ |
David Reiss | 0babe40 | 2008-06-10 22:56:12 +0000 | [diff] [blame] | 285 | void pwarning(int level, const char* fmt, ...) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 286 | if (g_warn < level) { |
| 287 | return; |
| 288 | } |
| 289 | va_list args; |
| 290 | printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 291 | va_start(args, fmt); |
| 292 | vprintf(fmt, args); |
| 293 | va_end(args); |
| 294 | printf("\n"); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Prints a failure message and exits |
| 299 | * |
| 300 | * @param fmt C format string followed by additional arguments |
| 301 | */ |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 302 | void failure(const char* fmt, ...) { |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 303 | va_list args; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 304 | fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 305 | va_start(args, fmt); |
| 306 | vfprintf(stderr, fmt, args); |
| 307 | va_end(args); |
| 308 | printf("\n"); |
| 309 | exit(1); |
| 310 | } |
| 311 | |
| 312 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 313 | * Converts a string filename into a thrift program name |
| 314 | */ |
| 315 | string program_name(string filename) { |
| 316 | string::size_type slash = filename.rfind("/"); |
| 317 | if (slash != string::npos) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 318 | filename = filename.substr(slash + 1); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 319 | } |
| 320 | string::size_type dot = filename.rfind("."); |
| 321 | if (dot != string::npos) { |
| 322 | filename = filename.substr(0, dot); |
| 323 | } |
| 324 | return filename; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Gets the directory path of a filename |
| 329 | */ |
| 330 | string directory_name(string filename) { |
| 331 | string::size_type slash = filename.rfind("/"); |
| 332 | // No slash, just use the current directory |
| 333 | if (slash == string::npos) { |
| 334 | return "."; |
| 335 | } |
| 336 | return filename.substr(0, slash); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Finds the appropriate file path for the given filename |
| 341 | */ |
| 342 | string include_file(string filename) { |
| 343 | // Absolute path? Just try that |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 344 | if (filename[0] == '/') { |
| 345 | // Realpath! |
Ben Craig | e957675 | 2013-10-11 08:19:16 -0500 | [diff] [blame] | 346 | char rp[THRIFT_PATH_MAX]; |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 347 | if (saferealpath(filename.c_str(), rp) == NULL) { |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 348 | pwarning(0, "Cannot open include file %s\n", filename.c_str()); |
| 349 | return std::string(); |
| 350 | } |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 351 | |
| 352 | // Stat this file |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 353 | struct stat finfo; |
| 354 | if (stat(rp, &finfo) == 0) { |
| 355 | return rp; |
| 356 | } |
| 357 | } else { // relative path, start searching |
| 358 | // new search path with current dir global |
| 359 | vector<string> sp = g_incl_searchpath; |
| 360 | sp.insert(sp.begin(), g_curdir); |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 361 | |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 362 | // iterate through paths |
| 363 | vector<string>::iterator it; |
| 364 | for (it = sp.begin(); it != sp.end(); it++) { |
| 365 | string sfilename = *(it) + "/" + filename; |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 366 | |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 367 | // Realpath! |
Ben Craig | e957675 | 2013-10-11 08:19:16 -0500 | [diff] [blame] | 368 | char rp[THRIFT_PATH_MAX]; |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 369 | if (saferealpath(sfilename.c_str(), rp) == NULL) { |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 370 | continue; |
| 371 | } |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 372 | |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 373 | // Stat this files |
| 374 | struct stat finfo; |
| 375 | if (stat(rp, &finfo) == 0) { |
| 376 | return rp; |
| 377 | } |
| 378 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 379 | } |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 380 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 381 | // Uh oh |
| 382 | pwarning(0, "Could not find include file %s\n", filename.c_str()); |
| 383 | return std::string(); |
| 384 | } |
| 385 | |
| 386 | /** |
David Reiss | cbd4bac | 2007-08-14 17:12:33 +0000 | [diff] [blame] | 387 | * Clears any previously stored doctext string. |
| 388 | * Also prints a warning if we are discarding information. |
| 389 | */ |
| 390 | void clear_doctext() { |
| 391 | if (g_doctext != NULL) { |
| 392 | pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno); |
| 393 | } |
| 394 | free(g_doctext); |
| 395 | g_doctext = NULL; |
| 396 | } |
| 397 | |
| 398 | /** |
Jens Geyer | e8379b5 | 2014-01-25 00:59:45 +0100 | [diff] [blame] | 399 | * Reset program doctext information after processing a file |
| 400 | */ |
| 401 | void reset_program_doctext_info() { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 402 | if (g_program_doctext_candidate != NULL) { |
Jens Geyer | e8379b5 | 2014-01-25 00:59:45 +0100 | [diff] [blame] | 403 | free(g_program_doctext_candidate); |
| 404 | g_program_doctext_candidate = NULL; |
| 405 | } |
| 406 | g_program_doctext_lineno = 0; |
| 407 | g_program_doctext_status = INVALID; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 408 | pdebug("%s", "program doctext set to INVALID"); |
Jens Geyer | e8379b5 | 2014-01-25 00:59:45 +0100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /** |
| 412 | * We are sure the program doctext candidate is really the program doctext. |
| 413 | */ |
| 414 | void declare_valid_program_doctext() { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 415 | if ((g_program_doctext_candidate != NULL) && (g_program_doctext_status == STILL_CANDIDATE)) { |
Roger Meier | 4f4b15b | 2014-11-05 16:51:04 +0100 | [diff] [blame] | 416 | g_program_doctext_status = ABSOLUTELY_SURE; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 417 | pdebug("%s", "program doctext set to ABSOLUTELY_SURE"); |
Jens Geyer | 813749d | 2014-01-31 23:42:57 +0100 | [diff] [blame] | 418 | } else { |
Roger Meier | 4f4b15b | 2014-11-05 16:51:04 +0100 | [diff] [blame] | 419 | g_program_doctext_status = NO_PROGRAM_DOCTEXT; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 420 | pdebug("%s", "program doctext set to NO_PROGRAM_DOCTEXT"); |
Jens Geyer | e8379b5 | 2014-01-25 00:59:45 +0100 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | /** |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 425 | * Cleans up text commonly found in doxygen-like comments |
| 426 | * |
| 427 | * Warning: if you mix tabs and spaces in a non-uniform way, |
| 428 | * you will get what you deserve. |
| 429 | */ |
| 430 | char* clean_up_doctext(char* doctext) { |
| 431 | // Convert to C++ string, and remove Windows's carriage returns. |
| 432 | string docstring = doctext; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 433 | docstring.erase(remove(docstring.begin(), docstring.end(), '\r'), docstring.end()); |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 434 | |
| 435 | // Separate into lines. |
| 436 | vector<string> lines; |
| 437 | string::size_type pos = string::npos; |
| 438 | string::size_type last; |
| 439 | while (true) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 440 | last = (pos == string::npos) ? 0 : pos + 1; |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 441 | pos = docstring.find('\n', last); |
| 442 | if (pos == string::npos) { |
| 443 | // First bit of cleaning. If the last line is only whitespace, drop it. |
| 444 | string::size_type nonwhite = docstring.find_first_not_of(" \t", last); |
| 445 | if (nonwhite != string::npos) { |
| 446 | lines.push_back(docstring.substr(last)); |
| 447 | } |
| 448 | break; |
| 449 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 450 | lines.push_back(docstring.substr(last, pos - last)); |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | // A very profound docstring. |
| 454 | if (lines.empty()) { |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | // Clear leading whitespace from the first line. |
| 459 | pos = lines.front().find_first_not_of(" \t"); |
| 460 | lines.front().erase(0, pos); |
| 461 | |
| 462 | // If every nonblank line after the first has the same number of spaces/tabs, |
| 463 | // then a star, remove them. |
| 464 | bool have_prefix = true; |
| 465 | bool found_prefix = false; |
| 466 | string::size_type prefix_len = 0; |
| 467 | vector<string>::iterator l_iter; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 468 | for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) { |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 469 | if (l_iter->empty()) { |
| 470 | continue; |
| 471 | } |
| 472 | |
| 473 | pos = l_iter->find_first_not_of(" \t"); |
| 474 | if (!found_prefix) { |
| 475 | if (pos != string::npos) { |
| 476 | if (l_iter->at(pos) == '*') { |
| 477 | found_prefix = true; |
| 478 | prefix_len = pos; |
| 479 | } else { |
| 480 | have_prefix = false; |
| 481 | break; |
| 482 | } |
| 483 | } else { |
| 484 | // Whitespace-only line. Truncate it. |
| 485 | l_iter->clear(); |
| 486 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 487 | } else if (l_iter->size() > pos && l_iter->at(pos) == '*' && pos == prefix_len) { |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 488 | // Business as usual. |
| 489 | } else if (pos == string::npos) { |
| 490 | // Whitespace-only line. Let's truncate it for them. |
| 491 | l_iter->clear(); |
| 492 | } else { |
| 493 | // The pattern has been broken. |
| 494 | have_prefix = false; |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | // If our prefix survived, delete it from every line. |
| 500 | if (have_prefix) { |
| 501 | // Get the star too. |
| 502 | prefix_len++; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 503 | for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) { |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 504 | l_iter->erase(0, prefix_len); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | // Now delete the minimum amount of leading whitespace from each line. |
| 509 | prefix_len = string::npos; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 510 | for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) { |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 511 | if (l_iter->empty()) { |
| 512 | continue; |
| 513 | } |
| 514 | pos = l_iter->find_first_not_of(" \t"); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 515 | if (pos != string::npos && (prefix_len == string::npos || pos < prefix_len)) { |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 516 | prefix_len = pos; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | // If our prefix survived, delete it from every line. |
| 521 | if (prefix_len != string::npos) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 522 | for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) { |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 523 | l_iter->erase(0, prefix_len); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // Remove trailing whitespace from every line. |
| 528 | for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) { |
| 529 | pos = l_iter->find_last_not_of(" \t"); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 530 | if (pos != string::npos && pos != l_iter->length() - 1) { |
| 531 | l_iter->erase(pos + 1); |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
| 535 | // If the first line is empty, remove it. |
| 536 | // Don't do this earlier because a lot of steps skip the first line. |
| 537 | if (lines.front().empty()) { |
| 538 | lines.erase(lines.begin()); |
| 539 | } |
| 540 | |
| 541 | // Now rejoin the lines and copy them back into doctext. |
| 542 | docstring.clear(); |
| 543 | for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) { |
| 544 | docstring += *l_iter; |
| 545 | docstring += '\n'; |
| 546 | } |
| 547 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 548 | // assert(docstring.length() <= strlen(doctext)); may happen, see THRIFT-1755 |
| 549 | if (docstring.length() <= strlen(doctext)) { |
Jens Geyer | 8cd3efe | 2013-09-16 22:17:52 +0200 | [diff] [blame] | 550 | strcpy(doctext, docstring.c_str()); |
| 551 | } else { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 552 | free(doctext); // too short |
Jens Geyer | 8cd3efe | 2013-09-16 22:17:52 +0200 | [diff] [blame] | 553 | doctext = strdup(docstring.c_str()); |
| 554 | } |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 555 | return doctext; |
| 556 | } |
| 557 | |
| 558 | /** Set to true to debug docstring parsing */ |
| 559 | static bool dump_docs = false; |
| 560 | |
| 561 | /** |
| 562 | * Dumps docstrings to stdout |
David Reiss | cdffe26 | 2007-08-14 17:12:31 +0000 | [diff] [blame] | 563 | * Only works for top-level definitions and the whole program doc |
| 564 | * (i.e., not enum constants, struct fields, or functions. |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 565 | */ |
| 566 | void dump_docstrings(t_program* program) { |
David Reiss | cdffe26 | 2007-08-14 17:12:31 +0000 | [diff] [blame] | 567 | string progdoc = program->get_doc(); |
David Reiss | c2532a9 | 2007-07-30 23:46:11 +0000 | [diff] [blame] | 568 | if (!progdoc.empty()) { |
| 569 | printf("Whole program doc:\n%s\n", progdoc.c_str()); |
| 570 | } |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 571 | const vector<t_typedef*>& typedefs = program->get_typedefs(); |
| 572 | vector<t_typedef*>::const_iterator t_iter; |
| 573 | for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) { |
| 574 | t_typedef* td = *t_iter; |
| 575 | if (td->has_doc()) { |
David Reiss | cdffe26 | 2007-08-14 17:12:31 +0000 | [diff] [blame] | 576 | printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str()); |
| 577 | } |
| 578 | } |
| 579 | const vector<t_enum*>& enums = program->get_enums(); |
| 580 | vector<t_enum*>::const_iterator e_iter; |
| 581 | for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) { |
| 582 | t_enum* en = *e_iter; |
| 583 | if (en->has_doc()) { |
| 584 | printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str()); |
| 585 | } |
| 586 | } |
| 587 | const vector<t_const*>& consts = program->get_consts(); |
| 588 | vector<t_const*>::const_iterator c_iter; |
| 589 | for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) { |
| 590 | t_const* co = *c_iter; |
| 591 | if (co->has_doc()) { |
| 592 | printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str()); |
| 593 | } |
| 594 | } |
| 595 | const vector<t_struct*>& structs = program->get_structs(); |
| 596 | vector<t_struct*>::const_iterator s_iter; |
| 597 | for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) { |
| 598 | t_struct* st = *s_iter; |
| 599 | if (st->has_doc()) { |
| 600 | printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str()); |
| 601 | } |
| 602 | } |
| 603 | const vector<t_struct*>& xceptions = program->get_xceptions(); |
| 604 | vector<t_struct*>::const_iterator x_iter; |
| 605 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { |
| 606 | t_struct* xn = *x_iter; |
| 607 | if (xn->has_doc()) { |
| 608 | printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str()); |
| 609 | } |
| 610 | } |
| 611 | const vector<t_service*>& services = program->get_services(); |
| 612 | vector<t_service*>::const_iterator v_iter; |
| 613 | for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) { |
| 614 | t_service* sv = *v_iter; |
| 615 | if (sv->has_doc()) { |
| 616 | printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str()); |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | /** |
David Reiss | 3c5d2fd | 2008-02-08 21:58:06 +0000 | [diff] [blame] | 622 | * Call generate_fingerprint for every structure and enum. |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 623 | */ |
| 624 | void generate_all_fingerprints(t_program* program) { |
| 625 | const vector<t_struct*>& structs = program->get_structs(); |
| 626 | vector<t_struct*>::const_iterator s_iter; |
| 627 | for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) { |
| 628 | t_struct* st = *s_iter; |
| 629 | st->generate_fingerprint(); |
| 630 | } |
| 631 | |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 632 | const vector<t_struct*>& xceptions = program->get_xceptions(); |
| 633 | vector<t_struct*>::const_iterator x_iter; |
| 634 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { |
| 635 | t_struct* st = *x_iter; |
| 636 | st->generate_fingerprint(); |
| 637 | } |
| 638 | |
David Reiss | 3c5d2fd | 2008-02-08 21:58:06 +0000 | [diff] [blame] | 639 | const vector<t_enum*>& enums = program->get_enums(); |
| 640 | vector<t_enum*>::const_iterator e_iter; |
| 641 | for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) { |
| 642 | t_enum* e = *e_iter; |
| 643 | e->generate_fingerprint(); |
| 644 | } |
| 645 | |
David Reiss | 47557bc | 2007-09-04 21:31:04 +0000 | [diff] [blame] | 646 | g_type_void->generate_fingerprint(); |
| 647 | |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 648 | // If you want to generate fingerprints for implicit structures, start here. |
| 649 | /* |
| 650 | const vector<t_service*>& services = program->get_services(); |
| 651 | vector<t_service*>::const_iterator v_iter; |
| 652 | for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) { |
| 653 | t_service* sv = *v_iter; |
| 654 | } |
| 655 | */ |
| 656 | } |
| 657 | |
Jens Geyer | 6fe77e8 | 2014-03-16 16:48:53 +0200 | [diff] [blame] | 658 | /** |
| 659 | * Emits a warning on list<byte>, binary type is typically a much better choice. |
| 660 | */ |
| 661 | void check_for_list_of_bytes(t_type* list_elem_type) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 662 | if ((g_parse_mode == PROGRAM) && (list_elem_type != NULL) && list_elem_type->is_base_type()) { |
Jens Geyer | 6fe77e8 | 2014-03-16 16:48:53 +0200 | [diff] [blame] | 663 | t_base_type* tbase = (t_base_type*)list_elem_type; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 664 | if (tbase->get_base() == t_base_type::TYPE_BYTE) { |
| 665 | pwarning(1, "Consider using the more efficient \"binary\" type instead of \"list<byte>\"."); |
Jens Geyer | 6fe77e8 | 2014-03-16 16:48:53 +0200 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | } |
| 669 | |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 670 | /** |
David Reiss | dd08f6d | 2008-06-30 20:24:24 +0000 | [diff] [blame] | 671 | * Prints the version number |
| 672 | */ |
| 673 | void version() { |
Bryan Duxbury | a1e268c | 2010-05-03 21:33:00 +0000 | [diff] [blame] | 674 | printf("Thrift version %s\n", THRIFT_VERSION); |
David Reiss | dd08f6d | 2008-06-30 20:24:24 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /** |
Jake Farrell | 2fd8a15 | 2012-09-29 00:26:36 +0000 | [diff] [blame] | 678 | * Display the usage message and then exit with an error code. |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 679 | */ |
| 680 | void usage() { |
Jake Farrell | 2fd8a15 | 2012-09-29 00:26:36 +0000 | [diff] [blame] | 681 | fprintf(stderr, "Usage: thrift [options] file\n\n"); |
| 682 | fprintf(stderr, "Use thrift -help for a list of options\n"); |
| 683 | exit(1); |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Diplays the help message and then exits with an error code. |
| 688 | */ |
| 689 | void help() { |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 690 | fprintf(stderr, "Usage: thrift [options] file\n"); |
| 691 | fprintf(stderr, "Options:\n"); |
David Reiss | dd08f6d | 2008-06-30 20:24:24 +0000 | [diff] [blame] | 692 | fprintf(stderr, " -version Print the compiler version\n"); |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 693 | fprintf(stderr, " -o dir Set the output directory for gen-* packages\n"); |
| 694 | fprintf(stderr, " (default: current directory)\n"); |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 695 | fprintf(stderr, " -out dir Set the ouput location for generated files.\n"); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 696 | fprintf(stderr, " (no gen-* folder will be created)\n"); |
David Reiss | d779cbe | 2007-08-31 01:42:55 +0000 | [diff] [blame] | 697 | fprintf(stderr, " -I dir Add a directory to the list of directories\n"); |
Mark Slee | 227ac2c | 2007-03-07 05:46:50 +0000 | [diff] [blame] | 698 | fprintf(stderr, " searched for include directives\n"); |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 699 | fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n"); |
| 700 | fprintf(stderr, " -strict Strict compiler warnings on\n"); |
| 701 | fprintf(stderr, " -v[erbose] Verbose mode\n"); |
| 702 | fprintf(stderr, " -r[ecurse] Also generate included files\n"); |
| 703 | fprintf(stderr, " -debug Parse debug trace to stdout\n"); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 704 | fprintf(stderr, |
| 705 | " --allow-neg-keys Allow negative field keys (Used to " |
Bryan Duxbury | c7206a4 | 2011-08-17 23:17:04 +0000 | [diff] [blame] | 706 | "preserve protocol\n"); |
| 707 | fprintf(stderr, " compatibility with older .thrift files)\n"); |
Roger Meier | 887ff75 | 2011-08-19 11:25:39 +0000 | [diff] [blame] | 708 | fprintf(stderr, " --allow-64bit-consts Do not print warnings about using 64-bit constants\n"); |
David Reiss | bd0db88 | 2008-02-27 01:54:51 +0000 | [diff] [blame] | 709 | fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n"); |
Jens Geyer | e8c51ed | 2014-04-18 02:27:57 +0200 | [diff] [blame] | 710 | fprintf(stderr, " STR has the form language[:key1=val1[,key2[,key3=val3]]].\n"); |
David Reiss | bd0db88 | 2008-02-27 01:54:51 +0000 | [diff] [blame] | 711 | fprintf(stderr, " Keys and values are options passed to the generator.\n"); |
| 712 | fprintf(stderr, " Many options will not require values.\n"); |
| 713 | fprintf(stderr, "\n"); |
| 714 | fprintf(stderr, "Available generators (and options):\n"); |
| 715 | |
| 716 | t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map(); |
| 717 | t_generator_registry::gen_map_t::iterator iter; |
| 718 | for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 719 | fprintf(stderr, |
| 720 | " %s (%s):\n", |
| 721 | iter->second->get_short_name().c_str(), |
| 722 | iter->second->get_long_name().c_str()); |
David Reiss | bd0db88 | 2008-02-27 01:54:51 +0000 | [diff] [blame] | 723 | fprintf(stderr, "%s", iter->second->get_documentation().c_str()); |
| 724 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 725 | exit(1); |
| 726 | } |
| 727 | |
| 728 | /** |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 729 | * You know, when I started working on Thrift I really thought it wasn't going |
| 730 | * to become a programming language because it was just a generator and it |
| 731 | * wouldn't need runtime type information and all that jazz. But then we |
| 732 | * decided to add constants, and all of a sudden that means runtime type |
| 733 | * validation and inference, except the "runtime" is the code generator |
David Reiss | 3bb5e05 | 2010-01-25 19:31:31 +0000 | [diff] [blame] | 734 | * runtime. |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 735 | */ |
| 736 | void validate_const_rec(std::string name, t_type* type, t_const_value* value) { |
| 737 | if (type->is_void()) { |
| 738 | throw "type error: cannot declare a void const: " + name; |
| 739 | } |
| 740 | |
| 741 | if (type->is_base_type()) { |
| 742 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); |
| 743 | switch (tbase) { |
| 744 | case t_base_type::TYPE_STRING: |
| 745 | if (value->get_type() != t_const_value::CV_STRING) { |
| 746 | throw "type error: const \"" + name + "\" was declared as string"; |
| 747 | } |
| 748 | break; |
| 749 | case t_base_type::TYPE_BOOL: |
| 750 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 751 | throw "type error: const \"" + name + "\" was declared as bool"; |
| 752 | } |
| 753 | break; |
| 754 | case t_base_type::TYPE_BYTE: |
| 755 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 756 | throw "type error: const \"" + name + "\" was declared as byte"; |
| 757 | } |
| 758 | break; |
| 759 | case t_base_type::TYPE_I16: |
| 760 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 761 | throw "type error: const \"" + name + "\" was declared as i16"; |
| 762 | } |
| 763 | break; |
| 764 | case t_base_type::TYPE_I32: |
| 765 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 766 | throw "type error: const \"" + name + "\" was declared as i32"; |
| 767 | } |
| 768 | break; |
| 769 | case t_base_type::TYPE_I64: |
| 770 | if (value->get_type() != t_const_value::CV_INTEGER) { |
| 771 | throw "type error: const \"" + name + "\" was declared as i64"; |
| 772 | } |
| 773 | break; |
| 774 | case t_base_type::TYPE_DOUBLE: |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 775 | if (value->get_type() != t_const_value::CV_INTEGER |
| 776 | && value->get_type() != t_const_value::CV_DOUBLE) { |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 777 | throw "type error: const \"" + name + "\" was declared as double"; |
| 778 | } |
| 779 | break; |
| 780 | default: |
David Reiss | dd7796f | 2007-08-28 21:09:06 +0000 | [diff] [blame] | 781 | throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name; |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 782 | } |
| 783 | } else if (type->is_enum()) { |
Bryan Duxbury | 2d80470 | 2009-12-18 19:41:11 +0000 | [diff] [blame] | 784 | if (value->get_type() != t_const_value::CV_IDENTIFIER) { |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 785 | throw "type error: const \"" + name + "\" was declared as enum"; |
| 786 | } |
Bryan Duxbury | 2d80470 | 2009-12-18 19:41:11 +0000 | [diff] [blame] | 787 | |
Bryan Duxbury | 1606f25 | 2010-11-24 00:25:57 +0000 | [diff] [blame] | 788 | // see if there's a dot in the identifier |
| 789 | std::string name_portion = value->get_identifier_name(); |
| 790 | |
Bryan Duxbury | 2d80470 | 2009-12-18 19:41:11 +0000 | [diff] [blame] | 791 | const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants(); |
| 792 | vector<t_enum_value*>::const_iterator c_iter; |
| 793 | bool found = false; |
Bryan Duxbury | 9f0a786 | 2010-09-12 14:38:36 +0000 | [diff] [blame] | 794 | |
Bryan Duxbury | 1606f25 | 2010-11-24 00:25:57 +0000 | [diff] [blame] | 795 | for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) { |
Bryan Duxbury | 9f0a786 | 2010-09-12 14:38:36 +0000 | [diff] [blame] | 796 | if ((*c_iter)->get_name() == name_portion) { |
Bryan Duxbury | 2d80470 | 2009-12-18 19:41:11 +0000 | [diff] [blame] | 797 | found = true; |
| 798 | break; |
| 799 | } |
| 800 | } |
| 801 | if (!found) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 802 | throw "type error: const " + name + " was declared as type " + type->get_name() |
| 803 | + " which is an enum, but " + value->get_identifier() |
| 804 | + " is not a valid value for that enum"; |
Bryan Duxbury | 2d80470 | 2009-12-18 19:41:11 +0000 | [diff] [blame] | 805 | } |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 806 | } else if (type->is_struct() || type->is_xception()) { |
| 807 | if (value->get_type() != t_const_value::CV_MAP) { |
| 808 | throw "type error: const \"" + name + "\" was declared as struct/xception"; |
| 809 | } |
| 810 | const vector<t_field*>& fields = ((t_struct*)type)->get_members(); |
| 811 | vector<t_field*>::const_iterator f_iter; |
| 812 | |
| 813 | const map<t_const_value*, t_const_value*>& val = value->get_map(); |
| 814 | map<t_const_value*, t_const_value*>::const_iterator v_iter; |
| 815 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 816 | if (v_iter->first->get_type() != t_const_value::CV_STRING) { |
| 817 | throw "type error: " + name + " struct key must be string"; |
| 818 | } |
| 819 | t_type* field_type = NULL; |
| 820 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { |
| 821 | if ((*f_iter)->get_name() == v_iter->first->get_string()) { |
| 822 | field_type = (*f_iter)->get_type(); |
| 823 | } |
| 824 | } |
| 825 | if (field_type == NULL) { |
| 826 | throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string(); |
| 827 | } |
| 828 | |
| 829 | validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second); |
| 830 | } |
| 831 | } else if (type->is_map()) { |
| 832 | t_type* k_type = ((t_map*)type)->get_key_type(); |
| 833 | t_type* v_type = ((t_map*)type)->get_val_type(); |
| 834 | const map<t_const_value*, t_const_value*>& val = value->get_map(); |
| 835 | map<t_const_value*, t_const_value*>::const_iterator v_iter; |
| 836 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 837 | validate_const_rec(name + "<key>", k_type, v_iter->first); |
| 838 | validate_const_rec(name + "<val>", v_type, v_iter->second); |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 839 | } |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 840 | } else if (type->is_list() || type->is_set()) { |
| 841 | t_type* e_type; |
| 842 | if (type->is_list()) { |
| 843 | e_type = ((t_list*)type)->get_elem_type(); |
| 844 | } else { |
| 845 | e_type = ((t_set*)type)->get_elem_type(); |
| 846 | } |
| 847 | const vector<t_const_value*>& val = value->get_list(); |
| 848 | vector<t_const_value*>::const_iterator v_iter; |
| 849 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { |
| 850 | validate_const_rec(name + "<elem>", e_type, *v_iter); |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | /** |
Jens Geyer | 12c09f4 | 2013-08-25 14:16:27 +0200 | [diff] [blame] | 856 | * Check simple identifier names |
| 857 | * It's easier to do it this way instead of rewriting the whole grammar etc. |
| 858 | */ |
| 859 | void validate_simple_identifier(const char* identifier) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 860 | string name(identifier); |
| 861 | if (name.find(".") != string::npos) { |
Jens Geyer | 12c09f4 | 2013-08-25 14:16:27 +0200 | [diff] [blame] | 862 | yyerror("Identifier %s can't have a dot.", identifier); |
| 863 | exit(1); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | /** |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 868 | * Check the type of the parsed const information against its declared type |
| 869 | */ |
| 870 | void validate_const_type(t_const* c) { |
| 871 | validate_const_rec(c->get_name(), c->get_type(), c->get_value()); |
| 872 | } |
| 873 | |
| 874 | /** |
Mark Slee | 7ff3245 | 2007-02-01 05:26:18 +0000 | [diff] [blame] | 875 | * Check the type of a default value assigned to a field. |
| 876 | */ |
| 877 | void validate_field_value(t_field* field, t_const_value* cv) { |
| 878 | validate_const_rec(field->get_name(), field->get_type(), cv); |
| 879 | } |
| 880 | |
| 881 | /** |
Mark Slee | 91f2b7b | 2008-01-31 01:49:16 +0000 | [diff] [blame] | 882 | * Check that all the elements of a throws block are actually exceptions. |
| 883 | */ |
| 884 | bool validate_throws(t_struct* throws) { |
| 885 | const vector<t_field*>& members = throws->get_members(); |
| 886 | vector<t_field*>::const_iterator m_iter; |
| 887 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { |
Bryan Duxbury | cff8357 | 2011-08-24 20:53:03 +0000 | [diff] [blame] | 888 | if (!t_generator::get_true_type((*m_iter)->get_type())->is_xception()) { |
Mark Slee | 91f2b7b | 2008-01-31 01:49:16 +0000 | [diff] [blame] | 889 | return false; |
| 890 | } |
| 891 | } |
| 892 | return true; |
| 893 | } |
| 894 | |
| 895 | /** |
Jens Geyer | 03d4944 | 2013-09-04 22:34:41 +0200 | [diff] [blame] | 896 | * Skips UTF-8 BOM if there is one |
| 897 | */ |
| 898 | bool skip_utf8_bom(FILE* f) { |
| 899 | |
| 900 | // pretty straightforward, but works |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 901 | if (fgetc(f) == 0xEF) { |
| 902 | if (fgetc(f) == 0xBB) { |
| 903 | if (fgetc(f) == 0xBF) { |
Jens Geyer | 03d4944 | 2013-09-04 22:34:41 +0200 | [diff] [blame] | 904 | return true; |
Roger Meier | 4f4b15b | 2014-11-05 16:51:04 +0100 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | rewind(f); |
Jens Geyer | 03d4944 | 2013-09-04 22:34:41 +0200 | [diff] [blame] | 910 | return false; |
| 911 | } |
| 912 | |
| 913 | /** |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 914 | * Parses a program |
| 915 | */ |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 916 | void parse(t_program* program, t_program* parent_program) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 917 | // Get scope file path |
| 918 | string path = program->get_path(); |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 919 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 920 | // Set current dir global, which is used in the include_file function |
| 921 | g_curdir = directory_name(path); |
| 922 | g_curpath = path; |
| 923 | |
| 924 | // Open the file |
Jens Geyer | 03d4944 | 2013-09-04 22:34:41 +0200 | [diff] [blame] | 925 | // skip UTF-8 BOM if there is one |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 926 | yyin = fopen(path.c_str(), "r"); |
| 927 | if (yyin == 0) { |
| 928 | failure("Could not open input file: \"%s\"", path.c_str()); |
| 929 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 930 | if (skip_utf8_bom(yyin)) |
Jens Geyer | 03d4944 | 2013-09-04 22:34:41 +0200 | [diff] [blame] | 931 | pverbose("Skipped UTF-8 BOM at %s\n", path.c_str()); |
Roger Meier | 4f4b15b | 2014-11-05 16:51:04 +0100 | [diff] [blame] | 932 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 933 | // Create new scope and scan for includes |
| 934 | pverbose("Scanning %s for includes\n", path.c_str()); |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 935 | g_parse_mode = INCLUDES; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 936 | g_program = program; |
| 937 | g_scope = program->scope(); |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 938 | try { |
Mark Slee | 36bfa2e | 2007-01-19 20:09:51 +0000 | [diff] [blame] | 939 | yylineno = 1; |
Mark Slee | 3015287 | 2006-11-28 01:24:07 +0000 | [diff] [blame] | 940 | if (yyparse() != 0) { |
| 941 | failure("Parser error during include pass."); |
| 942 | } |
| 943 | } catch (string x) { |
| 944 | failure(x.c_str()); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 945 | } |
| 946 | fclose(yyin); |
| 947 | |
| 948 | // Recursively parse all the include programs |
| 949 | vector<t_program*>& includes = program->get_includes(); |
| 950 | vector<t_program*>::iterator iter; |
| 951 | for (iter = includes.begin(); iter != includes.end(); ++iter) { |
| 952 | parse(*iter, program); |
| 953 | } |
| 954 | |
Jens Geyer | e8379b5 | 2014-01-25 00:59:45 +0100 | [diff] [blame] | 955 | // reset program doctext status before parsing a new file |
| 956 | reset_program_doctext_info(); |
| 957 | |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 958 | // Parse the program file |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 959 | g_parse_mode = PROGRAM; |
| 960 | g_program = program; |
| 961 | g_scope = program->scope(); |
| 962 | g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL; |
| 963 | g_parent_prefix = program->get_name() + "."; |
| 964 | g_curpath = path; |
Jens Geyer | 03d4944 | 2013-09-04 22:34:41 +0200 | [diff] [blame] | 965 | |
| 966 | // Open the file |
| 967 | // skip UTF-8 BOM if there is one |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 968 | yyin = fopen(path.c_str(), "r"); |
| 969 | if (yyin == 0) { |
| 970 | failure("Could not open input file: \"%s\"", path.c_str()); |
| 971 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 972 | if (skip_utf8_bom(yyin)) |
Jens Geyer | 03d4944 | 2013-09-04 22:34:41 +0200 | [diff] [blame] | 973 | pverbose("Skipped UTF-8 BOM at %s\n", path.c_str()); |
Roger Meier | 4f4b15b | 2014-11-05 16:51:04 +0100 | [diff] [blame] | 974 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 975 | pverbose("Parsing %s for types\n", path.c_str()); |
Mark Slee | 36bfa2e | 2007-01-19 20:09:51 +0000 | [diff] [blame] | 976 | yylineno = 1; |
David Reiss | 877237a | 2007-07-27 00:40:19 +0000 | [diff] [blame] | 977 | try { |
| 978 | if (yyparse() != 0) { |
| 979 | failure("Parser error during types pass."); |
| 980 | } |
| 981 | } catch (string x) { |
| 982 | failure(x.c_str()); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 983 | } |
| 984 | fclose(yyin); |
| 985 | } |
| 986 | |
| 987 | /** |
| 988 | * Generate code |
| 989 | */ |
David Reiss | bd0db88 | 2008-02-27 01:54:51 +0000 | [diff] [blame] | 990 | void generate(t_program* program, const vector<string>& generator_strings) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 991 | // Oooohh, recursive code generation, hot!! |
| 992 | if (gen_recurse) { |
| 993 | const vector<t_program*>& includes = program->get_includes(); |
| 994 | for (size_t i = 0; i < includes.size(); ++i) { |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 995 | // Propagate output path from parent to child programs |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 996 | includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute()); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 997 | |
David Reiss | bd0db88 | 2008-02-27 01:54:51 +0000 | [diff] [blame] | 998 | generate(includes[i], generator_strings); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | // Generate code! |
| 1003 | try { |
| 1004 | pverbose("Program: %s\n", program->get_path().c_str()); |
| 1005 | |
Jens Geyer | 83767a7 | 2013-09-23 22:09:12 +0200 | [diff] [blame] | 1006 | // Compute fingerprints. - not anymore, we do it on the fly now |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1007 | // generate_all_fingerprints(program); |
David Reiss | 18bf22d | 2007-08-28 20:49:17 +0000 | [diff] [blame] | 1008 | |
David Reiss | 1ac0580 | 2007-07-30 22:00:27 +0000 | [diff] [blame] | 1009 | if (dump_docs) { |
| 1010 | dump_docstrings(program); |
| 1011 | } |
David Reiss | bd0db88 | 2008-02-27 01:54:51 +0000 | [diff] [blame] | 1012 | |
| 1013 | vector<string>::const_iterator iter; |
| 1014 | for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) { |
| 1015 | t_generator* generator = t_generator_registry::get_generator(program, *iter); |
| 1016 | |
| 1017 | if (generator == NULL) { |
| 1018 | pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str()); |
| 1019 | } else { |
| 1020 | pverbose("Generating \"%s\"\n", iter->c_str()); |
| 1021 | generator->generate_program(); |
David Reiss | c934268 | 2008-03-27 21:39:49 +0000 | [diff] [blame] | 1022 | delete generator; |
David Reiss | bd0db88 | 2008-02-27 01:54:51 +0000 | [diff] [blame] | 1023 | } |
| 1024 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1025 | } catch (string s) { |
| 1026 | printf("Error: %s\n", s.c_str()); |
| 1027 | } catch (const char* exc) { |
| 1028 | printf("Error: %s\n", exc); |
| 1029 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | /** |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 1033 | * Parse it up.. then spit it back out, in pretty much every language. Alright |
| 1034 | * not that many languages, but the cool ones that we care about. |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1035 | */ |
| 1036 | int main(int argc, char** argv) { |
| 1037 | int i; |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 1038 | std::string out_path; |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 1039 | bool out_path_is_absolute = false; |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 1040 | |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 1041 | // Setup time string |
| 1042 | time_t now = time(NULL); |
| 1043 | g_time_str = ctime(&now); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1044 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1045 | // Check for necessary arguments, you gotta have at least a filename and |
| 1046 | // an output language flag |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 1047 | if (argc < 2) { |
| 1048 | usage(); |
| 1049 | } |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1050 | |
David Reiss | bd0db88 | 2008-02-27 01:54:51 +0000 | [diff] [blame] | 1051 | vector<string> generator_strings; |
| 1052 | |
David Reiss | 9cc2c13 | 2008-02-27 01:54:47 +0000 | [diff] [blame] | 1053 | // Set the current path to a dummy value to make warning messages clearer. |
| 1054 | g_curpath = "arguments"; |
| 1055 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 1056 | // Hacky parameter handling... I didn't feel like using a library sorry! |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1057 | for (i = 1; i < argc - 1; i++) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 1058 | char* arg; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 1059 | |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 1060 | arg = strtok(argv[i], " "); |
| 1061 | while (arg != NULL) { |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 1062 | // Treat double dashes as single dashes |
Mark Slee | 52cb223 | 2006-11-10 22:32:07 +0000 | [diff] [blame] | 1063 | if (arg[0] == '-' && arg[1] == '-') { |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 1064 | ++arg; |
| 1065 | } |
| 1066 | |
Jake Farrell | 2fd8a15 | 2012-09-29 00:26:36 +0000 | [diff] [blame] | 1067 | if (strcmp(arg, "-help") == 0) { |
| 1068 | help(); |
| 1069 | } else if (strcmp(arg, "-version") == 0) { |
David Reiss | dd08f6d | 2008-06-30 20:24:24 +0000 | [diff] [blame] | 1070 | version(); |
jfarrell | 7096942 | 2013-09-09 20:33:38 -0400 | [diff] [blame] | 1071 | exit(0); |
David Reiss | dd08f6d | 2008-06-30 20:24:24 +0000 | [diff] [blame] | 1072 | } else if (strcmp(arg, "-debug") == 0) { |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 1073 | g_debug = 1; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 1074 | } else if (strcmp(arg, "-nowarn") == 0) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1075 | g_warn = 0; |
Mark Slee | 2329a83 | 2006-11-09 00:23:30 +0000 | [diff] [blame] | 1076 | } else if (strcmp(arg, "-strict") == 0) { |
Bryan Duxbury | a145b4d | 2009-04-03 17:29:25 +0000 | [diff] [blame] | 1077 | g_strict = 255; |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1078 | g_warn = 2; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1079 | } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1080 | g_verbose = 1; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1081 | } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0) { |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1082 | gen_recurse = true; |
Bryan Duxbury | c7206a4 | 2011-08-17 23:17:04 +0000 | [diff] [blame] | 1083 | } else if (strcmp(arg, "-allow-neg-keys") == 0) { |
| 1084 | g_allow_neg_field_keys = true; |
Roger Meier | 887ff75 | 2011-08-19 11:25:39 +0000 | [diff] [blame] | 1085 | } else if (strcmp(arg, "-allow-64bit-consts") == 0) { |
| 1086 | g_allow_64bit_consts = true; |
David Reiss | bd0db88 | 2008-02-27 01:54:51 +0000 | [diff] [blame] | 1087 | } else if (strcmp(arg, "-gen") == 0) { |
| 1088 | arg = argv[++i]; |
| 1089 | if (arg == NULL) { |
Jake Farrell | 2fd8a15 | 2012-09-29 00:26:36 +0000 | [diff] [blame] | 1090 | fprintf(stderr, "Missing generator specification\n"); |
David Reiss | bd0db88 | 2008-02-27 01:54:51 +0000 | [diff] [blame] | 1091 | usage(); |
| 1092 | } |
| 1093 | generator_strings.push_back(arg); |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 1094 | } else if (strcmp(arg, "-I") == 0) { |
| 1095 | // An argument of "-I\ asdf" is invalid and has unknown results |
| 1096 | arg = argv[++i]; |
| 1097 | |
| 1098 | if (arg == NULL) { |
Jake Farrell | 2fd8a15 | 2012-09-29 00:26:36 +0000 | [diff] [blame] | 1099 | fprintf(stderr, "Missing Include directory\n"); |
Martin Kraemer | 32c66e1 | 2006-11-09 00:06:36 +0000 | [diff] [blame] | 1100 | usage(); |
| 1101 | } |
| 1102 | g_incl_searchpath.push_back(arg); |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 1103 | } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) { |
| 1104 | out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false; |
Roger Meier | 6d7473d | 2013-05-06 01:08:36 +0200 | [diff] [blame] | 1105 | arg = argv[++i]; |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 1106 | if (arg == NULL) { |
David Reiss | 9d866ac | 2008-06-10 22:56:19 +0000 | [diff] [blame] | 1107 | fprintf(stderr, "-o: missing output directory\n"); |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 1108 | usage(); |
Mark Slee | 5b74307 | 2007-11-13 04:00:29 +0000 | [diff] [blame] | 1109 | } |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 1110 | out_path = arg; |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 1111 | |
Ben Craig | e957675 | 2013-10-11 08:19:16 -0500 | [diff] [blame] | 1112 | #ifdef _WIN32 |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1113 | // strip out trailing \ on Windows |
| 1114 | int last = out_path.length() - 1; |
| 1115 | if (out_path[last] == '\\') { |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 1116 | out_path.erase(last); |
| 1117 | } |
| 1118 | #endif |
Roger Meier | 061d4a2 | 2012-10-07 11:51:00 +0000 | [diff] [blame] | 1119 | if (!check_is_directory(out_path.c_str())) |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 1120 | return -1; |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 1121 | } else { |
Jake Farrell | 2fd8a15 | 2012-09-29 00:26:36 +0000 | [diff] [blame] | 1122 | fprintf(stderr, "Unrecognized option: %s\n", arg); |
Mark Slee | fdbee81 | 2006-09-27 18:50:48 +0000 | [diff] [blame] | 1123 | usage(); |
| 1124 | } |
| 1125 | |
| 1126 | // Tokenize more |
| 1127 | arg = strtok(NULL, " "); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1128 | } |
| 1129 | } |
Mark Slee | 2c44d20 | 2007-05-16 02:18:07 +0000 | [diff] [blame] | 1130 | |
Jake Farrell | 2fd8a15 | 2012-09-29 00:26:36 +0000 | [diff] [blame] | 1131 | // display help |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1132 | if ((strcmp(argv[argc - 1], "-help") == 0) || (strcmp(argv[argc - 1], "--help") == 0)) { |
Jake Farrell | 2fd8a15 | 2012-09-29 00:26:36 +0000 | [diff] [blame] | 1133 | help(); |
| 1134 | } |
| 1135 | |
David Reiss | dd08f6d | 2008-06-30 20:24:24 +0000 | [diff] [blame] | 1136 | // if you're asking for version, you have a right not to pass a file |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1137 | if ((strcmp(argv[argc - 1], "-version") == 0) || (strcmp(argv[argc - 1], "--version") == 0)) { |
David Reiss | dd08f6d | 2008-06-30 20:24:24 +0000 | [diff] [blame] | 1138 | version(); |
jfarrell | 8b1799f | 2014-04-10 22:06:11 -0400 | [diff] [blame] | 1139 | exit(0); |
David Reiss | dd08f6d | 2008-06-30 20:24:24 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1142 | // You gotta generate something! |
David Reiss | a9ea68b | 2009-02-17 20:28:24 +0000 | [diff] [blame] | 1143 | if (generator_strings.empty()) { |
Jake Farrell | 2fd8a15 | 2012-09-29 00:26:36 +0000 | [diff] [blame] | 1144 | fprintf(stderr, "No output language(s) specified\n"); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 1145 | usage(); |
| 1146 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1147 | |
| 1148 | // Real-pathify it |
Ben Craig | e957675 | 2013-10-11 08:19:16 -0500 | [diff] [blame] | 1149 | char rp[THRIFT_PATH_MAX]; |
David Reiss | 5245f40 | 2008-06-10 22:56:26 +0000 | [diff] [blame] | 1150 | if (argv[i] == NULL) { |
Jake Farrell | 2fd8a15 | 2012-09-29 00:26:36 +0000 | [diff] [blame] | 1151 | fprintf(stderr, "Missing file name\n"); |
David Reiss | 5245f40 | 2008-06-10 22:56:26 +0000 | [diff] [blame] | 1152 | usage(); |
| 1153 | } |
David Reiss | 204420f | 2008-01-11 20:59:03 +0000 | [diff] [blame] | 1154 | if (saferealpath(argv[i], rp) == NULL) { |
| 1155 | failure("Could not open input file with realpath: %s", argv[i]); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1156 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1157 | string input_file(rp); |
| 1158 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 1159 | // Instance of the global parse tree |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1160 | t_program* program = new t_program(input_file); |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 1161 | if (out_path.size()) { |
Bryan Duxbury | bdca9f6 | 2011-03-01 19:53:07 +0000 | [diff] [blame] | 1162 | program->set_out_path(out_path, out_path_is_absolute); |
dweatherford | 65b7075 | 2007-10-31 02:18:14 +0000 | [diff] [blame] | 1163 | } |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 1164 | |
David Reiss | 4b6a3c7 | 2008-02-27 22:28:12 +0000 | [diff] [blame] | 1165 | // Compute the cpp include prefix. |
| 1166 | // infer this from the filename passed in |
| 1167 | string input_filename = argv[i]; |
| 1168 | string include_prefix; |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 1169 | |
David Reiss | 4b6a3c7 | 2008-02-27 22:28:12 +0000 | [diff] [blame] | 1170 | string::size_type last_slash = string::npos; |
| 1171 | if ((last_slash = input_filename.rfind("/")) != string::npos) { |
| 1172 | include_prefix = input_filename.substr(0, last_slash); |
kholst | 76f2c88 | 2008-01-16 02:47:41 +0000 | [diff] [blame] | 1173 | } |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1174 | |
David Reiss | 4b6a3c7 | 2008-02-27 22:28:12 +0000 | [diff] [blame] | 1175 | program->set_include_prefix(include_prefix); |
| 1176 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1177 | // Initialize global types |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1178 | g_type_void = new t_base_type("void", t_base_type::TYPE_VOID); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1179 | g_type_string = new t_base_type("string", t_base_type::TYPE_STRING); |
Mark Slee | 8d725a2 | 2007-04-13 01:57:12 +0000 | [diff] [blame] | 1180 | g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING); |
| 1181 | ((t_base_type*)g_type_binary)->set_binary(true); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1182 | g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING); |
Mark Slee | b6200d8 | 2007-01-19 19:14:36 +0000 | [diff] [blame] | 1183 | ((t_base_type*)g_type_slist)->set_string_list(true); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1184 | g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL); |
| 1185 | g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE); |
| 1186 | g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16); |
| 1187 | g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32); |
| 1188 | g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64); |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1189 | g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE); |
Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame] | 1190 | |
Mark Slee | f5377b3 | 2006-10-10 01:42:59 +0000 | [diff] [blame] | 1191 | // Parse it! |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1192 | parse(program, NULL); |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1193 | |
David Reiss | 9cc2c13 | 2008-02-27 01:54:47 +0000 | [diff] [blame] | 1194 | // The current path is not really relevant when we are doing generation. |
| 1195 | // Reset the variable to make warning messages clearer. |
| 1196 | g_curpath = "generation"; |
| 1197 | // Reset yylineno for the heck of it. Use 1 instead of 0 because |
| 1198 | // That is what shows up during argument parsing. |
| 1199 | yylineno = 1; |
| 1200 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1201 | // Generate it! |
David Reiss | bd0db88 | 2008-02-27 01:54:51 +0000 | [diff] [blame] | 1202 | generate(program, generator_strings); |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 1203 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1204 | // Clean up. Who am I kidding... this program probably orphans heap memory |
| 1205 | // all over the place, but who cares because it is about to exit and it is |
| 1206 | // all referenced and used by this wacky parse tree up until now anyways. |
Mark Slee | b15a68b | 2006-06-07 06:46:24 +0000 | [diff] [blame] | 1207 | |
Mark Slee | f0712dc | 2006-10-25 19:03:57 +0000 | [diff] [blame] | 1208 | delete program; |
| 1209 | delete g_type_void; |
| 1210 | delete g_type_string; |
| 1211 | delete g_type_bool; |
| 1212 | delete g_type_byte; |
| 1213 | delete g_type_i16; |
| 1214 | delete g_type_i32; |
| 1215 | delete g_type_i64; |
| 1216 | delete g_type_double; |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1217 | |
| 1218 | // Finished |
Mark Slee | 3198572 | 2006-05-24 21:45:31 +0000 | [diff] [blame] | 1219 | return 0; |
| 1220 | } |