blob: e11d9b04cb329af6223e811dc4130045bfc65eca [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Sleee9ce01c2007-05-16 02:29:53 +000019
Mark Slee31985722006-05-24 21:45:31 +000020/**
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 Sleef5377b32006-10-10 01:42:59 +000025 * 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 Slee31985722006-05-24 21:45:31 +000027 *
Mark Slee31985722006-05-24 21:45:31 +000028 */
29
David Reissf10984b2008-03-27 21:39:52 +000030#include <cassert>
Mark Slee31985722006-05-24 21:45:31 +000031#include <stdlib.h>
32#include <stdio.h>
33#include <stdarg.h>
David Reiss5ad12602010-08-31 16:51:30 +000034#include <time.h>
Mark Slee31985722006-05-24 21:45:31 +000035#include <string>
David Reiss739cbe22008-04-15 05:44:00 +000036#include <algorithm>
Mark Sleef0712dc2006-10-25 19:03:57 +000037#include <sys/types.h>
38#include <sys/stat.h>
dweatherford65b70752007-10-31 02:18:14 +000039#include <errno.h>
David Reissab55ed52008-06-11 01:17:00 +000040#include <limits.h>
Mark Slee31985722006-05-24 21:45:31 +000041
Ben Craige9576752013-10-11 08:19:16 -050042#ifdef _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +010043#include <windows.h> /* for GetFullPathName */
David Reiss204420f2008-01-11 20:59:03 +000044#endif
45
Mark Sleef0712dc2006-10-25 19:03:57 +000046// Careful: must include globals first for extern definitions
Mark Slee31985722006-05-24 21:45:31 +000047#include "globals.h"
48
Ben Craige9576752013-10-11 08:19:16 -050049#include "platform.h"
Mark Slee31985722006-05-24 21:45:31 +000050#include "main.h"
51#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000052#include "parse/t_scope.h"
David Reissbbbbe882009-02-17 20:27:48 +000053#include "generate/t_generator.h"
Ben Craig262cfb42015-07-08 20:37:15 -050054#include "audit/t_audit.h"
Mark Slee31985722006-05-24 21:45:31 +000055
David Reissdd08f6d2008-06-30 20:24:24 +000056#include "version.h"
57
Mark Slee31985722006-05-24 21:45:31 +000058using namespace std;
59
Mark Sleef5377b32006-10-10 01:42:59 +000060/**
61 * Global program tree
62 */
Mark Slee31985722006-05-24 21:45:31 +000063t_program* g_program;
64
Mark Sleef5377b32006-10-10 01:42:59 +000065/**
Mark Sleef0712dc2006-10-25 19:03:57 +000066 * Global types
67 */
68
69t_type* g_type_void;
70t_type* g_type_string;
Mark Slee8d725a22007-04-13 01:57:12 +000071t_type* g_type_binary;
Mark Sleeb6200d82007-01-19 19:14:36 +000072t_type* g_type_slist;
Mark Sleef0712dc2006-10-25 19:03:57 +000073t_type* g_type_bool;
74t_type* g_type_byte;
75t_type* g_type_i16;
76t_type* g_type_i32;
77t_type* g_type_i64;
78t_type* g_type_double;
79
80/**
81 * Global scope
82 */
83t_scope* g_scope;
84
85/**
86 * Parent scope to also parse types
87 */
88t_scope* g_parent_scope;
89
90/**
91 * Prefix for putting types in parent scope
92 */
93string g_parent_prefix;
94
95/**
96 * Parsing pass
97 */
98PARSE_MODE g_parse_mode;
99
100/**
101 * Current directory of file being parsed
102 */
103string g_curdir;
104
105/**
106 * Current file being parsed
107 */
108string g_curpath;
109
110/**
Martin Kraemer32c66e12006-11-09 00:06:36 +0000111 * Search path for inclusions
112 */
Mark Slee2329a832006-11-09 00:23:30 +0000113vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000114
115/**
Mark Sleef5377b32006-10-10 01:42:59 +0000116 * Global debug state
117 */
Mark Slee31985722006-05-24 21:45:31 +0000118int g_debug = 0;
119
Mark Sleef5377b32006-10-10 01:42:59 +0000120/**
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000121 * Strictness level
122 */
123int g_strict = 127;
124
125/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000126 * Warning level
127 */
128int g_warn = 1;
129
130/**
131 * Verbose output
132 */
133int g_verbose = 0;
134
135/**
Mark Sleef5377b32006-10-10 01:42:59 +0000136 * Global time string
137 */
Mark Slee31985722006-05-24 21:45:31 +0000138char* g_time_str;
139
Mark Slee31985722006-05-24 21:45:31 +0000140/**
David Reisscbd4bac2007-08-14 17:12:33 +0000141 * The last parsed doctext comment.
142 */
143char* g_doctext;
144
145/**
146 * The location of the last parsed doctext comment.
147 */
148int g_doctext_lineno;
149
Roger Meier4f4b15b2014-11-05 16:51:04 +0100150/**
Jens Geyere8379b52014-01-25 00:59:45 +0100151 * The First doctext comment
152 */
153char* g_program_doctext_candidate;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100154int g_program_doctext_lineno = 0;
155PROGDOCTEXT_STATUS g_program_doctext_status = INVALID;
Jens Geyere8379b52014-01-25 00:59:45 +0100156
David Reisscbd4bac2007-08-14 17:12:33 +0000157/**
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000158 * Whether or not negative field keys are accepted.
159 */
160int g_allow_neg_field_keys;
161
162/**
Roger Meier887ff752011-08-19 11:25:39 +0000163 * Whether or not 64-bit constants will generate a warning.
164 */
165int g_allow_64bit_consts = 0;
166
167/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000168 * Flags to control code generation
169 */
Mark Sleef0712dc2006-10-25 19:03:57 +0000170bool gen_recurse = false;
171
172/**
Ben Craig262cfb42015-07-08 20:37:15 -0500173 * Flags to control thrift audit
174 */
175bool g_audit = false;
176
177/**
178 * Flag to control return status
179 */
180bool g_return_failure = false;
181bool g_audit_fatal = true;
182
183/**
Ben Craige9576752013-10-11 08:19:16 -0500184 * Win32 doesn't have realpath, so use fallback implementation in that case,
David Reiss204420f2008-01-11 20:59:03 +0000185 * otherwise this just calls through to realpath
186 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100187char* saferealpath(const char* path, char* resolved_path) {
Ben Craige9576752013-10-11 08:19:16 -0500188#ifdef _WIN32
David Reiss204420f2008-01-11 20:59:03 +0000189 char buf[MAX_PATH];
190 char* basename;
191 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100192 if (len == 0 || len > MAX_PATH - 1) {
David Reiss204420f2008-01-11 20:59:03 +0000193 strcpy(resolved_path, path);
194 } else {
David Reiss204420f2008-01-11 20:59:03 +0000195 strcpy(resolved_path, buf);
196 }
Bryan Duxbury0137af62010-04-22 21:21:46 +0000197
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 Reiss204420f2008-01-11 20:59:03 +0000206 return resolved_path;
207#else
208 return realpath(path, resolved_path);
209#endif
210}
211
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100212bool check_is_directory(const char* dir_name) {
Ben Craige9576752013-10-11 08:19:16 -0500213#ifdef _WIN32
Roger Meier061d4a22012-10-07 11:51:00 +0000214 DWORD attributes = ::GetFileAttributesA(dir_name);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100215 if (attributes == INVALID_FILE_ATTRIBUTES) {
216 fprintf(stderr,
217 "Output directory %s is unusable: GetLastError() = %ld\n",
218 dir_name,
219 GetLastError());
Roger Meier061d4a22012-10-07 11:51:00 +0000220 return false;
221 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100222 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) {
Roger Meier061d4a22012-10-07 11:51:00 +0000223 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 Grochowski16a23a62014-11-13 15:33:38 +0100233 if (!S_ISDIR(sb.st_mode)) {
Roger Meier061d4a22012-10-07 11:51:00 +0000234 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 Reiss204420f2008-01-11 20:59:03 +0000240
241/**
Mark Slee31985722006-05-24 21:45:31 +0000242 * 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 Reiss0babe402008-06-10 22:56:12 +0000249void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000250 va_list args;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100251 fprintf(stderr, "[ERROR:%s:%d] (last token was '%s')\n", g_curpath.c_str(), yylineno, yytext);
Mark Slee31985722006-05-24 21:45:31 +0000252
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 Reiss0babe402008-06-10 22:56:12 +0000265void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000266 if (g_debug == 0) {
267 return;
268 }
269 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000270 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000271 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 Reiss0babe402008-06-10 22:56:12 +0000282void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000283 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 Reiss0babe402008-06-10 22:56:12 +0000297void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000298 if (g_warn < level) {
299 return;
300 }
301 va_list args;
302 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000303 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 Slee30152872006-11-28 01:24:07 +0000314void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000315 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000316 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000317 va_start(args, fmt);
318 vfprintf(stderr, fmt, args);
319 va_end(args);
320 printf("\n");
321 exit(1);
322}
323
324/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000325 * Converts a string filename into a thrift program name
326 */
327string program_name(string filename) {
328 string::size_type slash = filename.rfind("/");
329 if (slash != string::npos) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100330 filename = filename.substr(slash + 1);
Mark Sleef0712dc2006-10-25 19:03:57 +0000331 }
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 */
342string 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 */
354string include_file(string filename) {
355 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000356 if (filename[0] == '/') {
357 // Realpath!
Ben Craige9576752013-10-11 08:19:16 -0500358 char rp[THRIFT_PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000359 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000360 pwarning(0, "Cannot open include file %s\n", filename.c_str());
361 return std::string();
362 }
Mark Slee2c44d202007-05-16 02:18:07 +0000363
364 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000365 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 Slee2c44d202007-05-16 02:18:07 +0000373
Martin Kraemer32c66e12006-11-09 00:06:36 +0000374 // iterate through paths
375 vector<string>::iterator it;
376 for (it = sp.begin(); it != sp.end(); it++) {
377 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000378
Martin Kraemer32c66e12006-11-09 00:06:36 +0000379 // Realpath!
Ben Craige9576752013-10-11 08:19:16 -0500380 char rp[THRIFT_PATH_MAX];
David Reiss204420f2008-01-11 20:59:03 +0000381 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000382 continue;
383 }
Mark Slee2c44d202007-05-16 02:18:07 +0000384
Martin Kraemer32c66e12006-11-09 00:06:36 +0000385 // Stat this files
386 struct stat finfo;
387 if (stat(rp, &finfo) == 0) {
388 return rp;
389 }
390 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000391 }
Mark Slee2c44d202007-05-16 02:18:07 +0000392
Mark Sleef0712dc2006-10-25 19:03:57 +0000393 // Uh oh
394 pwarning(0, "Could not find include file %s\n", filename.c_str());
395 return std::string();
396}
397
398/**
David Reisscbd4bac2007-08-14 17:12:33 +0000399 * Clears any previously stored doctext string.
400 * Also prints a warning if we are discarding information.
401 */
402void 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 Geyere8379b52014-01-25 00:59:45 +0100411 * Reset program doctext information after processing a file
412 */
413void reset_program_doctext_info() {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100414 if (g_program_doctext_candidate != NULL) {
Jens Geyere8379b52014-01-25 00:59:45 +0100415 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 Grochowski16a23a62014-11-13 15:33:38 +0100420 pdebug("%s", "program doctext set to INVALID");
Jens Geyere8379b52014-01-25 00:59:45 +0100421}
422
423/**
424 * We are sure the program doctext candidate is really the program doctext.
425 */
426void declare_valid_program_doctext() {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100427 if ((g_program_doctext_candidate != NULL) && (g_program_doctext_status == STILL_CANDIDATE)) {
Roger Meier4f4b15b2014-11-05 16:51:04 +0100428 g_program_doctext_status = ABSOLUTELY_SURE;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100429 pdebug("%s", "program doctext set to ABSOLUTELY_SURE");
Jens Geyer813749d2014-01-31 23:42:57 +0100430 } else {
Roger Meier4f4b15b2014-11-05 16:51:04 +0100431 g_program_doctext_status = NO_PROGRAM_DOCTEXT;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100432 pdebug("%s", "program doctext set to NO_PROGRAM_DOCTEXT");
Jens Geyere8379b52014-01-25 00:59:45 +0100433 }
434}
435
436/**
David Reiss1ac05802007-07-30 22:00:27 +0000437 * 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 */
442char* clean_up_doctext(char* doctext) {
443 // Convert to C++ string, and remove Windows's carriage returns.
444 string docstring = doctext;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100445 docstring.erase(remove(docstring.begin(), docstring.end(), '\r'), docstring.end());
David Reiss1ac05802007-07-30 22:00:27 +0000446
447 // Separate into lines.
448 vector<string> lines;
449 string::size_type pos = string::npos;
450 string::size_type last;
451 while (true) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100452 last = (pos == string::npos) ? 0 : pos + 1;
David Reiss1ac05802007-07-30 22:00:27 +0000453 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 Grochowski16a23a62014-11-13 15:33:38 +0100462 lines.push_back(docstring.substr(last, pos - last));
David Reiss1ac05802007-07-30 22:00:27 +0000463 }
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 Grochowski16a23a62014-11-13 15:33:38 +0100480 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000481 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 Grochowski16a23a62014-11-13 15:33:38 +0100499 } else if (l_iter->size() > pos && l_iter->at(pos) == '*' && pos == prefix_len) {
David Reiss1ac05802007-07-30 22:00:27 +0000500 // 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 Grochowski16a23a62014-11-13 15:33:38 +0100515 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000516 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 Grochowski16a23a62014-11-13 15:33:38 +0100522 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000523 if (l_iter->empty()) {
524 continue;
525 }
526 pos = l_iter->find_first_not_of(" \t");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100527 if (pos != string::npos && (prefix_len == string::npos || pos < prefix_len)) {
David Reiss1ac05802007-07-30 22:00:27 +0000528 prefix_len = pos;
529 }
530 }
531
532 // If our prefix survived, delete it from every line.
533 if (prefix_len != string::npos) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100534 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000535 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 Grochowski16a23a62014-11-13 15:33:38 +0100542 if (pos != string::npos && pos != l_iter->length() - 1) {
543 l_iter->erase(pos + 1);
David Reiss1ac05802007-07-30 22:00:27 +0000544 }
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 Grochowski16a23a62014-11-13 15:33:38 +0100560 // assert(docstring.length() <= strlen(doctext)); may happen, see THRIFT-1755
561 if (docstring.length() <= strlen(doctext)) {
Jens Geyer8cd3efe2013-09-16 22:17:52 +0200562 strcpy(doctext, docstring.c_str());
563 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100564 free(doctext); // too short
Jens Geyer8cd3efe2013-09-16 22:17:52 +0200565 doctext = strdup(docstring.c_str());
566 }
David Reiss1ac05802007-07-30 22:00:27 +0000567 return doctext;
568}
569
570/** Set to true to debug docstring parsing */
571static bool dump_docs = false;
572
573/**
574 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000575 * Only works for top-level definitions and the whole program doc
576 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000577 */
578void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000579 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000580 if (!progdoc.empty()) {
581 printf("Whole program doc:\n%s\n", progdoc.c_str());
582 }
David Reiss1ac05802007-07-30 22:00:27 +0000583 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 Reisscdffe262007-08-14 17:12:31 +0000588 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 Reiss1ac05802007-07-30 22:00:27 +0000629 }
630 }
631}
632
633/**
Jens Geyer6fe77e82014-03-16 16:48:53 +0200634 * Emits a warning on list<byte>, binary type is typically a much better choice.
635 */
636void check_for_list_of_bytes(t_type* list_elem_type) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100637 if ((g_parse_mode == PROGRAM) && (list_elem_type != NULL) && list_elem_type->is_base_type()) {
Jens Geyer6fe77e82014-03-16 16:48:53 +0200638 t_base_type* tbase = (t_base_type*)list_elem_type;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100639 if (tbase->get_base() == t_base_type::TYPE_BYTE) {
640 pwarning(1, "Consider using the more efficient \"binary\" type instead of \"list<byte>\".");
Jens Geyer6fe77e82014-03-16 16:48:53 +0200641 }
642 }
643}
644
David Reiss18bf22d2007-08-28 20:49:17 +0000645/**
David Reissdd08f6d2008-06-30 20:24:24 +0000646 * Prints the version number
647 */
648void version() {
Bryan Duxburya1e268c2010-05-03 21:33:00 +0000649 printf("Thrift version %s\n", THRIFT_VERSION);
David Reissdd08f6d2008-06-30 20:24:24 +0000650}
651
652/**
Jake Farrell2fd8a152012-09-29 00:26:36 +0000653 * Display the usage message and then exit with an error code.
Mark Slee31985722006-05-24 21:45:31 +0000654 */
655void usage() {
Jake Farrell2fd8a152012-09-29 00:26:36 +0000656 fprintf(stderr, "Usage: thrift [options] file\n\n");
657 fprintf(stderr, "Use thrift -help for a list of options\n");
658 exit(1);
659}
660
661/**
662 * Diplays the help message and then exits with an error code.
663 */
664void help() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000665 fprintf(stderr, "Usage: thrift [options] file\n");
666 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000667 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000668 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
669 fprintf(stderr, " (default: current directory)\n");
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000670 fprintf(stderr, " -out dir Set the ouput location for generated files.\n");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100671 fprintf(stderr, " (no gen-* folder will be created)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000672 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000673 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000674 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
675 fprintf(stderr, " -strict Strict compiler warnings on\n");
676 fprintf(stderr, " -v[erbose] Verbose mode\n");
677 fprintf(stderr, " -r[ecurse] Also generate included files\n");
678 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100679 fprintf(stderr,
680 " --allow-neg-keys Allow negative field keys (Used to "
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000681 "preserve protocol\n");
682 fprintf(stderr, " compatibility with older .thrift files)\n");
Roger Meier887ff752011-08-19 11:25:39 +0000683 fprintf(stderr, " --allow-64bit-consts Do not print warnings about using 64-bit constants\n");
David Reissbd0db882008-02-27 01:54:51 +0000684 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
Jens Geyere8c51ed2014-04-18 02:27:57 +0200685 fprintf(stderr, " STR has the form language[:key1=val1[,key2[,key3=val3]]].\n");
David Reissbd0db882008-02-27 01:54:51 +0000686 fprintf(stderr, " Keys and values are options passed to the generator.\n");
687 fprintf(stderr, " Many options will not require values.\n");
688 fprintf(stderr, "\n");
Ben Craig262cfb42015-07-08 20:37:15 -0500689 fprintf(stderr, "Options related to audit operation\n");
690 fprintf(stderr, " --audit OldFile Old Thrift file to be audited with 'file'\n");
691 fprintf(stderr, " -Iold dir Add a directory to the list of directories\n");
692 fprintf(stderr, " searched for include directives for old thrift file\n");
693 fprintf(stderr, " -Inew dir Add a directory to the list of directories\n");
694 fprintf(stderr, " searched for include directives for new thrift file\n");
695 fprintf(stderr, "\n");
David Reissbd0db882008-02-27 01:54:51 +0000696 fprintf(stderr, "Available generators (and options):\n");
697
698 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
699 t_generator_registry::gen_map_t::iterator iter;
700 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100701 fprintf(stderr,
702 " %s (%s):\n",
703 iter->second->get_short_name().c_str(),
704 iter->second->get_long_name().c_str());
David Reissbd0db882008-02-27 01:54:51 +0000705 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
706 }
Mark Slee31985722006-05-24 21:45:31 +0000707 exit(1);
708}
709
710/**
Mark Slee30152872006-11-28 01:24:07 +0000711 * You know, when I started working on Thrift I really thought it wasn't going
712 * to become a programming language because it was just a generator and it
713 * wouldn't need runtime type information and all that jazz. But then we
714 * decided to add constants, and all of a sudden that means runtime type
715 * validation and inference, except the "runtime" is the code generator
David Reiss3bb5e052010-01-25 19:31:31 +0000716 * runtime.
Mark Slee30152872006-11-28 01:24:07 +0000717 */
718void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
719 if (type->is_void()) {
720 throw "type error: cannot declare a void const: " + name;
721 }
722
723 if (type->is_base_type()) {
724 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
725 switch (tbase) {
726 case t_base_type::TYPE_STRING:
727 if (value->get_type() != t_const_value::CV_STRING) {
728 throw "type error: const \"" + name + "\" was declared as string";
729 }
730 break;
731 case t_base_type::TYPE_BOOL:
732 if (value->get_type() != t_const_value::CV_INTEGER) {
733 throw "type error: const \"" + name + "\" was declared as bool";
734 }
735 break;
736 case t_base_type::TYPE_BYTE:
737 if (value->get_type() != t_const_value::CV_INTEGER) {
738 throw "type error: const \"" + name + "\" was declared as byte";
739 }
740 break;
741 case t_base_type::TYPE_I16:
742 if (value->get_type() != t_const_value::CV_INTEGER) {
743 throw "type error: const \"" + name + "\" was declared as i16";
744 }
745 break;
746 case t_base_type::TYPE_I32:
747 if (value->get_type() != t_const_value::CV_INTEGER) {
748 throw "type error: const \"" + name + "\" was declared as i32";
749 }
750 break;
751 case t_base_type::TYPE_I64:
752 if (value->get_type() != t_const_value::CV_INTEGER) {
753 throw "type error: const \"" + name + "\" was declared as i64";
754 }
755 break;
756 case t_base_type::TYPE_DOUBLE:
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100757 if (value->get_type() != t_const_value::CV_INTEGER
758 && value->get_type() != t_const_value::CV_DOUBLE) {
Mark Slee30152872006-11-28 01:24:07 +0000759 throw "type error: const \"" + name + "\" was declared as double";
760 }
761 break;
762 default:
David Reissdd7796f2007-08-28 21:09:06 +0000763 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000764 }
765 } else if (type->is_enum()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000766 if (value->get_type() != t_const_value::CV_IDENTIFIER) {
Mark Slee30152872006-11-28 01:24:07 +0000767 throw "type error: const \"" + name + "\" was declared as enum";
768 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000769
Bryan Duxbury1606f252010-11-24 00:25:57 +0000770 // see if there's a dot in the identifier
771 std::string name_portion = value->get_identifier_name();
772
Bryan Duxbury2d804702009-12-18 19:41:11 +0000773 const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants();
774 vector<t_enum_value*>::const_iterator c_iter;
775 bool found = false;
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000776
Bryan Duxbury1606f252010-11-24 00:25:57 +0000777 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000778 if ((*c_iter)->get_name() == name_portion) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000779 found = true;
780 break;
781 }
782 }
783 if (!found) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100784 throw "type error: const " + name + " was declared as type " + type->get_name()
785 + " which is an enum, but " + value->get_identifier()
786 + " is not a valid value for that enum";
Bryan Duxbury2d804702009-12-18 19:41:11 +0000787 }
Mark Slee30152872006-11-28 01:24:07 +0000788 } else if (type->is_struct() || type->is_xception()) {
789 if (value->get_type() != t_const_value::CV_MAP) {
790 throw "type error: const \"" + name + "\" was declared as struct/xception";
791 }
792 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
793 vector<t_field*>::const_iterator f_iter;
794
795 const map<t_const_value*, t_const_value*>& val = value->get_map();
796 map<t_const_value*, t_const_value*>::const_iterator v_iter;
797 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
798 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
799 throw "type error: " + name + " struct key must be string";
800 }
801 t_type* field_type = NULL;
802 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
803 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
804 field_type = (*f_iter)->get_type();
805 }
806 }
807 if (field_type == NULL) {
808 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
809 }
810
811 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
812 }
813 } else if (type->is_map()) {
814 t_type* k_type = ((t_map*)type)->get_key_type();
815 t_type* v_type = ((t_map*)type)->get_val_type();
816 const map<t_const_value*, t_const_value*>& val = value->get_map();
817 map<t_const_value*, t_const_value*>::const_iterator v_iter;
818 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
819 validate_const_rec(name + "<key>", k_type, v_iter->first);
820 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000821 }
Mark Slee30152872006-11-28 01:24:07 +0000822 } else if (type->is_list() || type->is_set()) {
823 t_type* e_type;
824 if (type->is_list()) {
825 e_type = ((t_list*)type)->get_elem_type();
826 } else {
827 e_type = ((t_set*)type)->get_elem_type();
828 }
829 const vector<t_const_value*>& val = value->get_list();
830 vector<t_const_value*>::const_iterator v_iter;
831 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
832 validate_const_rec(name + "<elem>", e_type, *v_iter);
833 }
834 }
835}
836
837/**
Jens Geyer12c09f42013-08-25 14:16:27 +0200838 * Check simple identifier names
839 * It's easier to do it this way instead of rewriting the whole grammar etc.
840 */
841void validate_simple_identifier(const char* identifier) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100842 string name(identifier);
843 if (name.find(".") != string::npos) {
Jens Geyer12c09f42013-08-25 14:16:27 +0200844 yyerror("Identifier %s can't have a dot.", identifier);
845 exit(1);
846 }
847}
848
849/**
Mark Slee30152872006-11-28 01:24:07 +0000850 * Check the type of the parsed const information against its declared type
851 */
852void validate_const_type(t_const* c) {
853 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
854}
855
856/**
Mark Slee7ff32452007-02-01 05:26:18 +0000857 * Check the type of a default value assigned to a field.
858 */
859void validate_field_value(t_field* field, t_const_value* cv) {
860 validate_const_rec(field->get_name(), field->get_type(), cv);
861}
862
863/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000864 * Check that all the elements of a throws block are actually exceptions.
865 */
866bool validate_throws(t_struct* throws) {
867 const vector<t_field*>& members = throws->get_members();
868 vector<t_field*>::const_iterator m_iter;
869 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
Bryan Duxburycff83572011-08-24 20:53:03 +0000870 if (!t_generator::get_true_type((*m_iter)->get_type())->is_xception()) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000871 return false;
872 }
873 }
874 return true;
875}
876
877/**
Jens Geyer03d49442013-09-04 22:34:41 +0200878 * Skips UTF-8 BOM if there is one
879 */
880bool skip_utf8_bom(FILE* f) {
881
882 // pretty straightforward, but works
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100883 if (fgetc(f) == 0xEF) {
884 if (fgetc(f) == 0xBB) {
885 if (fgetc(f) == 0xBF) {
Jens Geyer03d49442013-09-04 22:34:41 +0200886 return true;
Roger Meier4f4b15b2014-11-05 16:51:04 +0100887 }
888 }
889 }
890
891 rewind(f);
Jens Geyer03d49442013-09-04 22:34:41 +0200892 return false;
893}
894
895/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000896 * Parses a program
897 */
Mark Slee2c44d202007-05-16 02:18:07 +0000898void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000899 // Get scope file path
900 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000901
Mark Sleef0712dc2006-10-25 19:03:57 +0000902 // Set current dir global, which is used in the include_file function
903 g_curdir = directory_name(path);
904 g_curpath = path;
905
906 // Open the file
Jens Geyer03d49442013-09-04 22:34:41 +0200907 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000908 yyin = fopen(path.c_str(), "r");
909 if (yyin == 0) {
910 failure("Could not open input file: \"%s\"", path.c_str());
911 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100912 if (skip_utf8_bom(yyin))
Jens Geyer03d49442013-09-04 22:34:41 +0200913 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
Roger Meier4f4b15b2014-11-05 16:51:04 +0100914
Mark Sleef0712dc2006-10-25 19:03:57 +0000915 // Create new scope and scan for includes
916 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000917 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000918 g_program = program;
919 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000920 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000921 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000922 if (yyparse() != 0) {
923 failure("Parser error during include pass.");
924 }
925 } catch (string x) {
926 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000927 }
928 fclose(yyin);
929
930 // Recursively parse all the include programs
931 vector<t_program*>& includes = program->get_includes();
932 vector<t_program*>::iterator iter;
933 for (iter = includes.begin(); iter != includes.end(); ++iter) {
934 parse(*iter, program);
935 }
936
Jens Geyere8379b52014-01-25 00:59:45 +0100937 // reset program doctext status before parsing a new file
938 reset_program_doctext_info();
939
David Reiss204420f2008-01-11 20:59:03 +0000940 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000941 g_parse_mode = PROGRAM;
942 g_program = program;
943 g_scope = program->scope();
944 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
945 g_parent_prefix = program->get_name() + ".";
946 g_curpath = path;
Jens Geyer03d49442013-09-04 22:34:41 +0200947
948 // Open the file
949 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000950 yyin = fopen(path.c_str(), "r");
951 if (yyin == 0) {
952 failure("Could not open input file: \"%s\"", path.c_str());
953 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100954 if (skip_utf8_bom(yyin))
Jens Geyer03d49442013-09-04 22:34:41 +0200955 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
Roger Meier4f4b15b2014-11-05 16:51:04 +0100956
Mark Sleef0712dc2006-10-25 19:03:57 +0000957 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000958 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000959 try {
960 if (yyparse() != 0) {
961 failure("Parser error during types pass.");
962 }
963 } catch (string x) {
964 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000965 }
966 fclose(yyin);
967}
968
969/**
970 * Generate code
971 */
David Reissbd0db882008-02-27 01:54:51 +0000972void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000973 // Oooohh, recursive code generation, hot!!
974 if (gen_recurse) {
975 const vector<t_program*>& includes = program->get_includes();
976 for (size_t i = 0; i < includes.size(); ++i) {
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100977 // Propagate output path from parent to child programs
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000978 includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute());
Mark Slee5b743072007-11-13 04:00:29 +0000979
David Reissbd0db882008-02-27 01:54:51 +0000980 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000981 }
982 }
983
984 // Generate code!
985 try {
986 pverbose("Program: %s\n", program->get_path().c_str());
987
Jens Geyer83767a72013-09-23 22:09:12 +0200988 // Compute fingerprints. - not anymore, we do it on the fly now
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100989 // generate_all_fingerprints(program);
David Reiss18bf22d2007-08-28 20:49:17 +0000990
David Reiss1ac05802007-07-30 22:00:27 +0000991 if (dump_docs) {
992 dump_docstrings(program);
993 }
David Reissbd0db882008-02-27 01:54:51 +0000994
995 vector<string>::const_iterator iter;
996 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
997 t_generator* generator = t_generator_registry::get_generator(program, *iter);
998
999 if (generator == NULL) {
1000 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
1001 } else {
1002 pverbose("Generating \"%s\"\n", iter->c_str());
1003 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +00001004 delete generator;
David Reissbd0db882008-02-27 01:54:51 +00001005 }
1006 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001007 } catch (string s) {
1008 printf("Error: %s\n", s.c_str());
1009 } catch (const char* exc) {
1010 printf("Error: %s\n", exc);
1011 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001012}
1013
Ben Craig262cfb42015-07-08 20:37:15 -05001014void audit(t_program* new_program, t_program* old_program, string new_thrift_include_path, string old_thrift_include_path)
1015{
1016 vector<string> temp_incl_searchpath = g_incl_searchpath;
1017 if(!old_thrift_include_path.empty()) {
1018 g_incl_searchpath.push_back(old_thrift_include_path);
1019 }
1020
1021 parse(old_program, NULL);
1022
1023 g_incl_searchpath = temp_incl_searchpath;
1024 if(!new_thrift_include_path.empty()) {
1025 g_incl_searchpath.push_back(new_thrift_include_path);
1026 }
1027
1028 parse(new_program, NULL);
1029
1030 compare_namespace(new_program, old_program);
1031 compare_services(new_program->get_services(), old_program->get_services());
1032 compare_enums(new_program->get_enums(), old_program->get_enums());
1033 compare_structs(new_program->get_structs(), old_program->get_structs());
1034 compare_structs(new_program->get_xceptions(), old_program->get_xceptions());
1035 compare_consts(new_program->get_consts(), old_program->get_consts());
1036}
1037
Mark Sleef0712dc2006-10-25 19:03:57 +00001038/**
Mark Sleef5377b32006-10-10 01:42:59 +00001039 * Parse it up.. then spit it back out, in pretty much every language. Alright
1040 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +00001041 */
1042int main(int argc, char** argv) {
1043 int i;
dweatherford65b70752007-10-31 02:18:14 +00001044 std::string out_path;
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001045 bool out_path_is_absolute = false;
Mark Sleef5377b32006-10-10 01:42:59 +00001046
Mark Sleeb15a68b2006-06-07 06:46:24 +00001047 // Setup time string
1048 time_t now = time(NULL);
1049 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +00001050
Mark Sleef0712dc2006-10-25 19:03:57 +00001051 // Check for necessary arguments, you gotta have at least a filename and
1052 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +00001053 if (argc < 2) {
1054 usage();
1055 }
Mark Slee31985722006-05-24 21:45:31 +00001056
David Reissbd0db882008-02-27 01:54:51 +00001057 vector<string> generator_strings;
Ben Craig262cfb42015-07-08 20:37:15 -05001058 string old_thrift_include_path;
1059 string new_thrift_include_path;
1060 string old_input_file;
David Reissbd0db882008-02-27 01:54:51 +00001061
David Reiss9cc2c132008-02-27 01:54:47 +00001062 // Set the current path to a dummy value to make warning messages clearer.
1063 g_curpath = "arguments";
1064
Mark Sleef5377b32006-10-10 01:42:59 +00001065 // Hacky parameter handling... I didn't feel like using a library sorry!
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001066 for (i = 1; i < argc - 1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +00001067 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +00001068
Mark Sleefdbee812006-09-27 18:50:48 +00001069 arg = strtok(argv[i], " ");
1070 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +00001071 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +00001072 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +00001073 ++arg;
1074 }
1075
Jake Farrell2fd8a152012-09-29 00:26:36 +00001076 if (strcmp(arg, "-help") == 0) {
1077 help();
1078 } else if (strcmp(arg, "-version") == 0) {
David Reissdd08f6d2008-06-30 20:24:24 +00001079 version();
jfarrell70969422013-09-09 20:33:38 -04001080 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001081 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001082 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001083 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001084 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +00001085 } else if (strcmp(arg, "-strict") == 0) {
Bryan Duxburya145b4d2009-04-03 17:29:25 +00001086 g_strict = 255;
Mark Sleef0712dc2006-10-25 19:03:57 +00001087 g_warn = 2;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001088 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001089 g_verbose = 1;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001090 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001091 gen_recurse = true;
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001092 } else if (strcmp(arg, "-allow-neg-keys") == 0) {
1093 g_allow_neg_field_keys = true;
Roger Meier887ff752011-08-19 11:25:39 +00001094 } else if (strcmp(arg, "-allow-64bit-consts") == 0) {
1095 g_allow_64bit_consts = true;
David Reissbd0db882008-02-27 01:54:51 +00001096 } else if (strcmp(arg, "-gen") == 0) {
1097 arg = argv[++i];
1098 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001099 fprintf(stderr, "Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +00001100 usage();
1101 }
1102 generator_strings.push_back(arg);
Martin Kraemer32c66e12006-11-09 00:06:36 +00001103 } else if (strcmp(arg, "-I") == 0) {
1104 // An argument of "-I\ asdf" is invalid and has unknown results
1105 arg = argv[++i];
1106
1107 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001108 fprintf(stderr, "Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001109 usage();
1110 }
1111 g_incl_searchpath.push_back(arg);
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001112 } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) {
1113 out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false;
Roger Meier6d7473d2013-05-06 01:08:36 +02001114 arg = argv[++i];
dweatherford65b70752007-10-31 02:18:14 +00001115 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001116 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001117 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001118 }
dweatherford65b70752007-10-31 02:18:14 +00001119 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001120
Ben Craige9576752013-10-11 08:19:16 -05001121#ifdef _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001122 // strip out trailing \ on Windows
Jim King9de9b1f2015-04-30 16:03:34 -04001123 std::string::size_type last = out_path.length() - 1;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001124 if (out_path[last] == '\\') {
David Reiss204420f2008-01-11 20:59:03 +00001125 out_path.erase(last);
1126 }
1127#endif
Roger Meier061d4a22012-10-07 11:51:00 +00001128 if (!check_is_directory(out_path.c_str()))
dweatherford65b70752007-10-31 02:18:14 +00001129 return -1;
Ben Craig262cfb42015-07-08 20:37:15 -05001130 } else if (strcmp(arg, "-audit") == 0) {
1131 g_audit = true;
1132 arg = argv[++i];
1133 if (arg == NULL) {
1134 fprintf(stderr, "Missing old thrift file name for audit operation\n");
1135 usage();
1136 }
1137 char old_thrift_file_rp[THRIFT_PATH_MAX];
1138
1139 if (saferealpath(arg, old_thrift_file_rp) == NULL) {
1140 failure("Could not open input file with realpath: %s", arg);
1141 }
1142 old_input_file = string(old_thrift_file_rp);
1143 } else if(strcmp(arg, "-audit-nofatal") == 0){
1144 g_audit_fatal = false;
1145 } else if (strcmp(arg, "-Iold") == 0) {
1146 arg = argv[++i];
1147 if (arg == NULL) {
1148 fprintf(stderr, "Missing Include directory for old thrift file\n");
1149 usage();
1150 }
1151 old_thrift_include_path = string(arg);
1152 } else if (strcmp(arg, "-Inew") == 0) {
1153 arg = argv[++i];
1154 if(arg == NULL) {
1155 fprintf(stderr, "Missing Include directory for new thrift file\n");
1156 usage();
1157 }
1158 new_thrift_include_path = string(arg);
Mark Sleefdbee812006-09-27 18:50:48 +00001159 } else {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001160 fprintf(stderr, "Unrecognized option: %s\n", arg);
Mark Sleefdbee812006-09-27 18:50:48 +00001161 usage();
1162 }
1163
1164 // Tokenize more
1165 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001166 }
1167 }
Mark Slee2c44d202007-05-16 02:18:07 +00001168
Jake Farrell2fd8a152012-09-29 00:26:36 +00001169 // display help
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001170 if ((strcmp(argv[argc - 1], "-help") == 0) || (strcmp(argv[argc - 1], "--help") == 0)) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001171 help();
1172 }
1173
David Reissdd08f6d2008-06-30 20:24:24 +00001174 // if you're asking for version, you have a right not to pass a file
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001175 if ((strcmp(argv[argc - 1], "-version") == 0) || (strcmp(argv[argc - 1], "--version") == 0)) {
David Reissdd08f6d2008-06-30 20:24:24 +00001176 version();
jfarrell8b1799f2014-04-10 22:06:11 -04001177 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001178 }
1179
Mark Sleef0712dc2006-10-25 19:03:57 +00001180 // Initialize global types
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001181 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
Mark Sleef0712dc2006-10-25 19:03:57 +00001182 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001183 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1184 ((t_base_type*)g_type_binary)->set_binary(true);
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001185 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Sleeb6200d82007-01-19 19:14:36 +00001186 ((t_base_type*)g_type_slist)->set_string_list(true);
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001187 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1188 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1189 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1190 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1191 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
Mark Sleef0712dc2006-10-25 19:03:57 +00001192 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001193
Ben Craig262cfb42015-07-08 20:37:15 -05001194 if(g_audit)
1195 {
1196 // Audit operation
Mark Slee31985722006-05-24 21:45:31 +00001197
Ben Craig262cfb42015-07-08 20:37:15 -05001198 if (old_input_file.empty()) {
1199 fprintf(stderr, "Missing file name of old thrift file for audit\n");
1200 usage();
1201 }
David Reiss9cc2c132008-02-27 01:54:47 +00001202
Ben Craig262cfb42015-07-08 20:37:15 -05001203 char new_thrift_file_rp[THRIFT_PATH_MAX];
1204 if (argv[i] == NULL) {
1205 fprintf(stderr, "Missing file name of new thrift file for audit\n");
1206 usage();
1207 }
1208 if (saferealpath(argv[i], new_thrift_file_rp) == NULL) {
1209 failure("Could not open input file with realpath: %s", argv[i]);
1210 }
1211 string new_input_file(new_thrift_file_rp);
1212
1213 t_program new_program(new_input_file);
1214 t_program old_program(old_input_file);
1215
1216 audit(&new_program, &old_program, new_thrift_include_path, old_thrift_include_path);
1217
1218 } else {
1219 // Generate options
1220
1221 // You gotta generate something!
1222 if (generator_strings.empty()) {
1223 fprintf(stderr, "No output language(s) specified\n");
1224 usage();
1225 }
1226
1227 // Real-pathify it
1228 char rp[THRIFT_PATH_MAX];
1229 if (argv[i] == NULL) {
1230 fprintf(stderr, "Missing file name\n");
1231 usage();
1232 }
1233 if (saferealpath(argv[i], rp) == NULL) {
1234 failure("Could not open input file with realpath: %s", argv[i]);
1235 }
1236 string input_file(rp);
1237
1238 // Instance of the global parse tree
1239 t_program* program = new t_program(input_file);
1240 if (out_path.size()) {
1241 program->set_out_path(out_path, out_path_is_absolute);
1242 }
1243
1244 // Compute the cpp include prefix.
1245 // infer this from the filename passed in
1246 string input_filename = argv[i];
1247 string include_prefix;
1248
1249 string::size_type last_slash = string::npos;
1250 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1251 include_prefix = input_filename.substr(0, last_slash);
1252 }
1253
1254 program->set_include_prefix(include_prefix);
1255
1256 // Parse it!
1257 parse(program, NULL);
1258
1259 // The current path is not really relevant when we are doing generation.
1260 // Reset the variable to make warning messages clearer.
1261 g_curpath = "generation";
1262 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1263 // That is what shows up during argument parsing.
1264 yylineno = 1;
1265
1266 // Generate it!
1267 generate(program, generator_strings);
1268 delete program;
1269 }
Mark Sleeb15a68b2006-06-07 06:46:24 +00001270
Mark Sleef0712dc2006-10-25 19:03:57 +00001271 // Clean up. Who am I kidding... this program probably orphans heap memory
1272 // all over the place, but who cares because it is about to exit and it is
1273 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001274
Mark Sleef0712dc2006-10-25 19:03:57 +00001275 delete g_type_void;
1276 delete g_type_string;
1277 delete g_type_bool;
1278 delete g_type_byte;
1279 delete g_type_i16;
1280 delete g_type_i32;
1281 delete g_type_i64;
1282 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001283
1284 // Finished
Ben Craig262cfb42015-07-08 20:37:15 -05001285 if (g_return_failure && g_audit_fatal) {
1286 exit(2);
1287 }
1288 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001289 return 0;
1290}