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