blob: 6e5258bf5b8c939729167aa4790e1463bbd2a185 [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;
Jens Geyer40c28d32015-10-20 23:13:02 +020074t_type* g_type_i8;
Mark Sleef0712dc2006-10-25 19:03:57 +000075t_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;
Jens Geyer40c28d32015-10-20 23:13:02 +0200639 if (tbase->get_base() == t_base_type::TYPE_I8) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100640 pwarning(1, "Consider using the more efficient \"binary\" type instead of \"list<byte>\".");
Jens Geyer6fe77e82014-03-16 16:48:53 +0200641 }
642 }
643}
644
Jens Geyer40c28d32015-10-20 23:13:02 +0200645
646static bool g_byte_warning_emitted = false;
647
648/**
649 * Emits a one-time warning on byte type, promoting the new i8 type instead
650 */
651void 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 Reiss18bf22d2007-08-28 20:49:17 +0000659/**
David Reissdd08f6d2008-06-30 20:24:24 +0000660 * Prints the version number
661 */
662void version() {
Bryan Duxburya1e268c2010-05-03 21:33:00 +0000663 printf("Thrift version %s\n", THRIFT_VERSION);
David Reissdd08f6d2008-06-30 20:24:24 +0000664}
665
666/**
Jake Farrell2fd8a152012-09-29 00:26:36 +0000667 * Display the usage message and then exit with an error code.
Mark Slee31985722006-05-24 21:45:31 +0000668 */
669void usage() {
Jake Farrell2fd8a152012-09-29 00:26:36 +0000670 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 */
678void help() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000679 fprintf(stderr, "Usage: thrift [options] file\n");
680 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000681 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000682 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
683 fprintf(stderr, " (default: current directory)\n");
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000684 fprintf(stderr, " -out dir Set the ouput location for generated files.\n");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100685 fprintf(stderr, " (no gen-* folder will be created)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000686 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000687 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000688 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 Grochowski16a23a62014-11-13 15:33:38 +0100693 fprintf(stderr,
694 " --allow-neg-keys Allow negative field keys (Used to "
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000695 "preserve protocol\n");
696 fprintf(stderr, " compatibility with older .thrift files)\n");
Roger Meier887ff752011-08-19 11:25:39 +0000697 fprintf(stderr, " --allow-64bit-consts Do not print warnings about using 64-bit constants\n");
David Reissbd0db882008-02-27 01:54:51 +0000698 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
Jens Geyere8c51ed2014-04-18 02:27:57 +0200699 fprintf(stderr, " STR has the form language[:key1=val1[,key2[,key3=val3]]].\n");
David Reissbd0db882008-02-27 01:54:51 +0000700 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 Craig262cfb42015-07-08 20:37:15 -0500703 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 Reissbd0db882008-02-27 01:54:51 +0000710 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 Grochowski16a23a62014-11-13 15:33:38 +0100715 fprintf(stderr,
716 " %s (%s):\n",
717 iter->second->get_short_name().c_str(),
718 iter->second->get_long_name().c_str());
David Reissbd0db882008-02-27 01:54:51 +0000719 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
720 }
Mark Slee31985722006-05-24 21:45:31 +0000721 exit(1);
722}
723
724/**
Mark Slee30152872006-11-28 01:24:07 +0000725 * 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 Reiss3bb5e052010-01-25 19:31:31 +0000730 * runtime.
Mark Slee30152872006-11-28 01:24:07 +0000731 */
732void 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 Geyer40c28d32015-10-20 23:13:02 +0200750 case t_base_type::TYPE_I8:
Mark Slee30152872006-11-28 01:24:07 +0000751 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 Grochowski16a23a62014-11-13 15:33:38 +0100771 if (value->get_type() != t_const_value::CV_INTEGER
772 && value->get_type() != t_const_value::CV_DOUBLE) {
Mark Slee30152872006-11-28 01:24:07 +0000773 throw "type error: const \"" + name + "\" was declared as double";
774 }
775 break;
776 default:
David Reissdd7796f2007-08-28 21:09:06 +0000777 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000778 }
779 } else if (type->is_enum()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000780 if (value->get_type() != t_const_value::CV_IDENTIFIER) {
Mark Slee30152872006-11-28 01:24:07 +0000781 throw "type error: const \"" + name + "\" was declared as enum";
782 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000783
Bryan Duxbury1606f252010-11-24 00:25:57 +0000784 // see if there's a dot in the identifier
785 std::string name_portion = value->get_identifier_name();
786
Bryan Duxbury2d804702009-12-18 19:41:11 +0000787 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 Duxbury9f0a7862010-09-12 14:38:36 +0000790
Bryan Duxbury1606f252010-11-24 00:25:57 +0000791 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000792 if ((*c_iter)->get_name() == name_portion) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000793 found = true;
794 break;
795 }
796 }
797 if (!found) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100798 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 Duxbury2d804702009-12-18 19:41:11 +0000801 }
Mark Slee30152872006-11-28 01:24:07 +0000802 } 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 Slee2c44d202007-05-16 02:18:07 +0000835 }
Mark Slee30152872006-11-28 01:24:07 +0000836 } 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 Geyer12c09f42013-08-25 14:16:27 +0200852 * Check simple identifier names
853 * It's easier to do it this way instead of rewriting the whole grammar etc.
854 */
855void validate_simple_identifier(const char* identifier) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100856 string name(identifier);
857 if (name.find(".") != string::npos) {
Jens Geyer12c09f42013-08-25 14:16:27 +0200858 yyerror("Identifier %s can't have a dot.", identifier);
859 exit(1);
860 }
861}
862
863/**
Mark Slee30152872006-11-28 01:24:07 +0000864 * Check the type of the parsed const information against its declared type
865 */
866void validate_const_type(t_const* c) {
867 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
868}
869
870/**
Mark Slee7ff32452007-02-01 05:26:18 +0000871 * Check the type of a default value assigned to a field.
872 */
873void 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 Slee91f2b7b2008-01-31 01:49:16 +0000878 * Check that all the elements of a throws block are actually exceptions.
879 */
880bool 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 Duxburycff83572011-08-24 20:53:03 +0000884 if (!t_generator::get_true_type((*m_iter)->get_type())->is_xception()) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000885 return false;
886 }
887 }
888 return true;
889}
890
891/**
Jens Geyer03d49442013-09-04 22:34:41 +0200892 * Skips UTF-8 BOM if there is one
893 */
894bool skip_utf8_bom(FILE* f) {
895
896 // pretty straightforward, but works
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100897 if (fgetc(f) == 0xEF) {
898 if (fgetc(f) == 0xBB) {
899 if (fgetc(f) == 0xBF) {
Jens Geyer03d49442013-09-04 22:34:41 +0200900 return true;
Roger Meier4f4b15b2014-11-05 16:51:04 +0100901 }
902 }
903 }
904
905 rewind(f);
Jens Geyer03d49442013-09-04 22:34:41 +0200906 return false;
907}
908
909/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000910 * Parses a program
911 */
Mark Slee2c44d202007-05-16 02:18:07 +0000912void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000913 // Get scope file path
914 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000915
Mark Sleef0712dc2006-10-25 19:03:57 +0000916 // 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 Geyer03d49442013-09-04 22:34:41 +0200921 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000922 yyin = fopen(path.c_str(), "r");
923 if (yyin == 0) {
924 failure("Could not open input file: \"%s\"", path.c_str());
925 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100926 if (skip_utf8_bom(yyin))
Jens Geyer03d49442013-09-04 22:34:41 +0200927 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
Roger Meier4f4b15b2014-11-05 16:51:04 +0100928
Mark Sleef0712dc2006-10-25 19:03:57 +0000929 // Create new scope and scan for includes
930 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000931 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000932 g_program = program;
933 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000934 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000935 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000936 if (yyparse() != 0) {
937 failure("Parser error during include pass.");
938 }
939 } catch (string x) {
940 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000941 }
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 Geyere8379b52014-01-25 00:59:45 +0100951 // reset program doctext status before parsing a new file
952 reset_program_doctext_info();
953
David Reiss204420f2008-01-11 20:59:03 +0000954 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000955 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 Geyer03d49442013-09-04 22:34:41 +0200961
962 // Open the file
963 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000964 yyin = fopen(path.c_str(), "r");
965 if (yyin == 0) {
966 failure("Could not open input file: \"%s\"", path.c_str());
967 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100968 if (skip_utf8_bom(yyin))
Jens Geyer03d49442013-09-04 22:34:41 +0200969 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
Roger Meier4f4b15b2014-11-05 16:51:04 +0100970
Mark Sleef0712dc2006-10-25 19:03:57 +0000971 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000972 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000973 try {
974 if (yyparse() != 0) {
975 failure("Parser error during types pass.");
976 }
977 } catch (string x) {
978 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000979 }
980 fclose(yyin);
981}
982
983/**
984 * Generate code
985 */
David Reissbd0db882008-02-27 01:54:51 +0000986void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000987 // 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 Grochowski3b5dacb2014-11-24 10:55:31 +0100991 // Propagate output path from parent to child programs
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000992 includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute());
Mark Slee5b743072007-11-13 04:00:29 +0000993
David Reissbd0db882008-02-27 01:54:51 +0000994 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000995 }
996 }
997
998 // Generate code!
999 try {
1000 pverbose("Program: %s\n", program->get_path().c_str());
1001
Jens Geyer83767a72013-09-23 22:09:12 +02001002 // Compute fingerprints. - not anymore, we do it on the fly now
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001003 // generate_all_fingerprints(program);
David Reiss18bf22d2007-08-28 20:49:17 +00001004
David Reiss1ac05802007-07-30 22:00:27 +00001005 if (dump_docs) {
1006 dump_docstrings(program);
1007 }
David Reissbd0db882008-02-27 01:54:51 +00001008
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 Reissc9342682008-03-27 21:39:49 +00001018 delete generator;
David Reissbd0db882008-02-27 01:54:51 +00001019 }
1020 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001021 } catch (string s) {
1022 printf("Error: %s\n", s.c_str());
1023 } catch (const char* exc) {
1024 printf("Error: %s\n", exc);
1025 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001026}
1027
Ben Craig262cfb42015-07-08 20:37:15 -05001028void 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 Sleef0712dc2006-10-25 19:03:57 +00001052/**
Mark Sleef5377b32006-10-10 01:42:59 +00001053 * 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 Slee31985722006-05-24 21:45:31 +00001055 */
1056int main(int argc, char** argv) {
1057 int i;
dweatherford65b70752007-10-31 02:18:14 +00001058 std::string out_path;
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001059 bool out_path_is_absolute = false;
Mark Sleef5377b32006-10-10 01:42:59 +00001060
Mark Sleeb15a68b2006-06-07 06:46:24 +00001061 // Setup time string
1062 time_t now = time(NULL);
1063 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +00001064
Mark Sleef0712dc2006-10-25 19:03:57 +00001065 // Check for necessary arguments, you gotta have at least a filename and
1066 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +00001067 if (argc < 2) {
1068 usage();
1069 }
Mark Slee31985722006-05-24 21:45:31 +00001070
David Reissbd0db882008-02-27 01:54:51 +00001071 vector<string> generator_strings;
Ben Craig262cfb42015-07-08 20:37:15 -05001072 string old_thrift_include_path;
1073 string new_thrift_include_path;
1074 string old_input_file;
David Reissbd0db882008-02-27 01:54:51 +00001075
David Reiss9cc2c132008-02-27 01:54:47 +00001076 // Set the current path to a dummy value to make warning messages clearer.
1077 g_curpath = "arguments";
1078
Mark Sleef5377b32006-10-10 01:42:59 +00001079 // Hacky parameter handling... I didn't feel like using a library sorry!
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001080 for (i = 1; i < argc - 1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +00001081 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +00001082
Mark Sleefdbee812006-09-27 18:50:48 +00001083 arg = strtok(argv[i], " ");
1084 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +00001085 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +00001086 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +00001087 ++arg;
1088 }
1089
Jake Farrell2fd8a152012-09-29 00:26:36 +00001090 if (strcmp(arg, "-help") == 0) {
1091 help();
1092 } else if (strcmp(arg, "-version") == 0) {
David Reissdd08f6d2008-06-30 20:24:24 +00001093 version();
jfarrell70969422013-09-09 20:33:38 -04001094 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001095 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001096 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001097 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001098 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +00001099 } else if (strcmp(arg, "-strict") == 0) {
Bryan Duxburya145b4d2009-04-03 17:29:25 +00001100 g_strict = 255;
Mark Sleef0712dc2006-10-25 19:03:57 +00001101 g_warn = 2;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001102 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001103 g_verbose = 1;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001104 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001105 gen_recurse = true;
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001106 } else if (strcmp(arg, "-allow-neg-keys") == 0) {
1107 g_allow_neg_field_keys = true;
Roger Meier887ff752011-08-19 11:25:39 +00001108 } else if (strcmp(arg, "-allow-64bit-consts") == 0) {
1109 g_allow_64bit_consts = true;
David Reissbd0db882008-02-27 01:54:51 +00001110 } else if (strcmp(arg, "-gen") == 0) {
1111 arg = argv[++i];
1112 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001113 fprintf(stderr, "Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +00001114 usage();
1115 }
1116 generator_strings.push_back(arg);
Martin Kraemer32c66e12006-11-09 00:06:36 +00001117 } 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 Farrell2fd8a152012-09-29 00:26:36 +00001122 fprintf(stderr, "Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001123 usage();
1124 }
1125 g_incl_searchpath.push_back(arg);
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001126 } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) {
1127 out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false;
Roger Meier6d7473d2013-05-06 01:08:36 +02001128 arg = argv[++i];
dweatherford65b70752007-10-31 02:18:14 +00001129 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001130 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001131 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001132 }
dweatherford65b70752007-10-31 02:18:14 +00001133 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001134
Ben Craige9576752013-10-11 08:19:16 -05001135#ifdef _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001136 // strip out trailing \ on Windows
Jim King9de9b1f2015-04-30 16:03:34 -04001137 std::string::size_type last = out_path.length() - 1;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001138 if (out_path[last] == '\\') {
David Reiss204420f2008-01-11 20:59:03 +00001139 out_path.erase(last);
1140 }
1141#endif
Roger Meier061d4a22012-10-07 11:51:00 +00001142 if (!check_is_directory(out_path.c_str()))
dweatherford65b70752007-10-31 02:18:14 +00001143 return -1;
Ben Craig262cfb42015-07-08 20:37:15 -05001144 } 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 Sleefdbee812006-09-27 18:50:48 +00001173 } else {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001174 fprintf(stderr, "Unrecognized option: %s\n", arg);
Mark Sleefdbee812006-09-27 18:50:48 +00001175 usage();
1176 }
1177
1178 // Tokenize more
1179 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001180 }
1181 }
Mark Slee2c44d202007-05-16 02:18:07 +00001182
Jake Farrell2fd8a152012-09-29 00:26:36 +00001183 // display help
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001184 if ((strcmp(argv[argc - 1], "-help") == 0) || (strcmp(argv[argc - 1], "--help") == 0)) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001185 help();
1186 }
1187
David Reissdd08f6d2008-06-30 20:24:24 +00001188 // if you're asking for version, you have a right not to pass a file
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001189 if ((strcmp(argv[argc - 1], "-version") == 0) || (strcmp(argv[argc - 1], "--version") == 0)) {
David Reissdd08f6d2008-06-30 20:24:24 +00001190 version();
jfarrell8b1799f2014-04-10 22:06:11 -04001191 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001192 }
1193
Mark Sleef0712dc2006-10-25 19:03:57 +00001194 // Initialize global types
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001195 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
Mark Sleef0712dc2006-10-25 19:03:57 +00001196 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001197 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1198 ((t_base_type*)g_type_binary)->set_binary(true);
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001199 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Sleeb6200d82007-01-19 19:14:36 +00001200 ((t_base_type*)g_type_slist)->set_string_list(true);
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001201 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
Jens Geyer40c28d32015-10-20 23:13:02 +02001202 g_type_i8 = new t_base_type("i8", t_base_type::TYPE_I8);
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001203 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 Sleef0712dc2006-10-25 19:03:57 +00001206 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001207
Ben Craig262cfb42015-07-08 20:37:15 -05001208 if(g_audit)
1209 {
1210 // Audit operation
Mark Slee31985722006-05-24 21:45:31 +00001211
Ben Craig262cfb42015-07-08 20:37:15 -05001212 if (old_input_file.empty()) {
1213 fprintf(stderr, "Missing file name of old thrift file for audit\n");
1214 usage();
1215 }
David Reiss9cc2c132008-02-27 01:54:47 +00001216
Ben Craig262cfb42015-07-08 20:37:15 -05001217 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 Sleeb15a68b2006-06-07 06:46:24 +00001284
Mark Sleef0712dc2006-10-25 19:03:57 +00001285 // 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 Sleeb15a68b2006-06-07 06:46:24 +00001288
Mark Sleef0712dc2006-10-25 19:03:57 +00001289 delete g_type_void;
1290 delete g_type_string;
1291 delete g_type_bool;
Jens Geyer40c28d32015-10-20 23:13:02 +02001292 delete g_type_i8;
Mark Sleef0712dc2006-10-25 19:03:57 +00001293 delete g_type_i16;
1294 delete g_type_i32;
1295 delete g_type_i64;
1296 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001297
1298 // Finished
Ben Craig262cfb42015-07-08 20:37:15 -05001299 if (g_return_failure && g_audit_fatal) {
1300 exit(2);
1301 }
1302 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001303 return 0;
1304}