blob: a337cc693a0d18cad01f7755cb33ab455bf2388c [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/**
David Reiss3c5d2fd2008-02-08 21:58:06 +0000634 * Call generate_fingerprint for every structure and enum.
David Reiss18bf22d2007-08-28 20:49:17 +0000635 */
636void generate_all_fingerprints(t_program* program) {
637 const vector<t_struct*>& structs = program->get_structs();
638 vector<t_struct*>::const_iterator s_iter;
639 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
640 t_struct* st = *s_iter;
641 st->generate_fingerprint();
642 }
643
David Reissd779cbe2007-08-31 01:42:55 +0000644 const vector<t_struct*>& xceptions = program->get_xceptions();
645 vector<t_struct*>::const_iterator x_iter;
646 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
647 t_struct* st = *x_iter;
648 st->generate_fingerprint();
649 }
650
David Reiss3c5d2fd2008-02-08 21:58:06 +0000651 const vector<t_enum*>& enums = program->get_enums();
652 vector<t_enum*>::const_iterator e_iter;
653 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
654 t_enum* e = *e_iter;
655 e->generate_fingerprint();
656 }
657
David Reiss47557bc2007-09-04 21:31:04 +0000658 g_type_void->generate_fingerprint();
659
David Reiss18bf22d2007-08-28 20:49:17 +0000660 // If you want to generate fingerprints for implicit structures, start here.
661 /*
662 const vector<t_service*>& services = program->get_services();
663 vector<t_service*>::const_iterator v_iter;
664 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
665 t_service* sv = *v_iter;
666 }
667 */
668}
669
Jens Geyer6fe77e82014-03-16 16:48:53 +0200670/**
671 * Emits a warning on list<byte>, binary type is typically a much better choice.
672 */
673void check_for_list_of_bytes(t_type* list_elem_type) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100674 if ((g_parse_mode == PROGRAM) && (list_elem_type != NULL) && list_elem_type->is_base_type()) {
Jens Geyer6fe77e82014-03-16 16:48:53 +0200675 t_base_type* tbase = (t_base_type*)list_elem_type;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100676 if (tbase->get_base() == t_base_type::TYPE_BYTE) {
677 pwarning(1, "Consider using the more efficient \"binary\" type instead of \"list<byte>\".");
Jens Geyer6fe77e82014-03-16 16:48:53 +0200678 }
679 }
680}
681
David Reiss18bf22d2007-08-28 20:49:17 +0000682/**
David Reissdd08f6d2008-06-30 20:24:24 +0000683 * Prints the version number
684 */
685void version() {
Bryan Duxburya1e268c2010-05-03 21:33:00 +0000686 printf("Thrift version %s\n", THRIFT_VERSION);
David Reissdd08f6d2008-06-30 20:24:24 +0000687}
688
689/**
Jake Farrell2fd8a152012-09-29 00:26:36 +0000690 * Display the usage message and then exit with an error code.
Mark Slee31985722006-05-24 21:45:31 +0000691 */
692void usage() {
Jake Farrell2fd8a152012-09-29 00:26:36 +0000693 fprintf(stderr, "Usage: thrift [options] file\n\n");
694 fprintf(stderr, "Use thrift -help for a list of options\n");
695 exit(1);
696}
697
698/**
699 * Diplays the help message and then exits with an error code.
700 */
701void help() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000702 fprintf(stderr, "Usage: thrift [options] file\n");
703 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000704 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000705 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
706 fprintf(stderr, " (default: current directory)\n");
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000707 fprintf(stderr, " -out dir Set the ouput location for generated files.\n");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100708 fprintf(stderr, " (no gen-* folder will be created)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000709 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000710 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000711 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
712 fprintf(stderr, " -strict Strict compiler warnings on\n");
713 fprintf(stderr, " -v[erbose] Verbose mode\n");
714 fprintf(stderr, " -r[ecurse] Also generate included files\n");
715 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100716 fprintf(stderr,
717 " --allow-neg-keys Allow negative field keys (Used to "
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000718 "preserve protocol\n");
719 fprintf(stderr, " compatibility with older .thrift files)\n");
Roger Meier887ff752011-08-19 11:25:39 +0000720 fprintf(stderr, " --allow-64bit-consts Do not print warnings about using 64-bit constants\n");
David Reissbd0db882008-02-27 01:54:51 +0000721 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
Jens Geyere8c51ed2014-04-18 02:27:57 +0200722 fprintf(stderr, " STR has the form language[:key1=val1[,key2[,key3=val3]]].\n");
David Reissbd0db882008-02-27 01:54:51 +0000723 fprintf(stderr, " Keys and values are options passed to the generator.\n");
724 fprintf(stderr, " Many options will not require values.\n");
725 fprintf(stderr, "\n");
Ben Craig262cfb42015-07-08 20:37:15 -0500726 fprintf(stderr, "Options related to audit operation\n");
727 fprintf(stderr, " --audit OldFile Old Thrift file to be audited with 'file'\n");
728 fprintf(stderr, " -Iold dir Add a directory to the list of directories\n");
729 fprintf(stderr, " searched for include directives for old thrift file\n");
730 fprintf(stderr, " -Inew dir Add a directory to the list of directories\n");
731 fprintf(stderr, " searched for include directives for new thrift file\n");
732 fprintf(stderr, "\n");
David Reissbd0db882008-02-27 01:54:51 +0000733 fprintf(stderr, "Available generators (and options):\n");
734
735 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
736 t_generator_registry::gen_map_t::iterator iter;
737 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100738 fprintf(stderr,
739 " %s (%s):\n",
740 iter->second->get_short_name().c_str(),
741 iter->second->get_long_name().c_str());
David Reissbd0db882008-02-27 01:54:51 +0000742 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
743 }
Mark Slee31985722006-05-24 21:45:31 +0000744 exit(1);
745}
746
747/**
Mark Slee30152872006-11-28 01:24:07 +0000748 * You know, when I started working on Thrift I really thought it wasn't going
749 * to become a programming language because it was just a generator and it
750 * wouldn't need runtime type information and all that jazz. But then we
751 * decided to add constants, and all of a sudden that means runtime type
752 * validation and inference, except the "runtime" is the code generator
David Reiss3bb5e052010-01-25 19:31:31 +0000753 * runtime.
Mark Slee30152872006-11-28 01:24:07 +0000754 */
755void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
756 if (type->is_void()) {
757 throw "type error: cannot declare a void const: " + name;
758 }
759
760 if (type->is_base_type()) {
761 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
762 switch (tbase) {
763 case t_base_type::TYPE_STRING:
764 if (value->get_type() != t_const_value::CV_STRING) {
765 throw "type error: const \"" + name + "\" was declared as string";
766 }
767 break;
768 case t_base_type::TYPE_BOOL:
769 if (value->get_type() != t_const_value::CV_INTEGER) {
770 throw "type error: const \"" + name + "\" was declared as bool";
771 }
772 break;
773 case t_base_type::TYPE_BYTE:
774 if (value->get_type() != t_const_value::CV_INTEGER) {
775 throw "type error: const \"" + name + "\" was declared as byte";
776 }
777 break;
778 case t_base_type::TYPE_I16:
779 if (value->get_type() != t_const_value::CV_INTEGER) {
780 throw "type error: const \"" + name + "\" was declared as i16";
781 }
782 break;
783 case t_base_type::TYPE_I32:
784 if (value->get_type() != t_const_value::CV_INTEGER) {
785 throw "type error: const \"" + name + "\" was declared as i32";
786 }
787 break;
788 case t_base_type::TYPE_I64:
789 if (value->get_type() != t_const_value::CV_INTEGER) {
790 throw "type error: const \"" + name + "\" was declared as i64";
791 }
792 break;
793 case t_base_type::TYPE_DOUBLE:
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100794 if (value->get_type() != t_const_value::CV_INTEGER
795 && value->get_type() != t_const_value::CV_DOUBLE) {
Mark Slee30152872006-11-28 01:24:07 +0000796 throw "type error: const \"" + name + "\" was declared as double";
797 }
798 break;
799 default:
David Reissdd7796f2007-08-28 21:09:06 +0000800 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000801 }
802 } else if (type->is_enum()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000803 if (value->get_type() != t_const_value::CV_IDENTIFIER) {
Mark Slee30152872006-11-28 01:24:07 +0000804 throw "type error: const \"" + name + "\" was declared as enum";
805 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000806
Bryan Duxbury1606f252010-11-24 00:25:57 +0000807 // see if there's a dot in the identifier
808 std::string name_portion = value->get_identifier_name();
809
Bryan Duxbury2d804702009-12-18 19:41:11 +0000810 const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants();
811 vector<t_enum_value*>::const_iterator c_iter;
812 bool found = false;
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000813
Bryan Duxbury1606f252010-11-24 00:25:57 +0000814 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000815 if ((*c_iter)->get_name() == name_portion) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000816 found = true;
817 break;
818 }
819 }
820 if (!found) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100821 throw "type error: const " + name + " was declared as type " + type->get_name()
822 + " which is an enum, but " + value->get_identifier()
823 + " is not a valid value for that enum";
Bryan Duxbury2d804702009-12-18 19:41:11 +0000824 }
Mark Slee30152872006-11-28 01:24:07 +0000825 } else if (type->is_struct() || type->is_xception()) {
826 if (value->get_type() != t_const_value::CV_MAP) {
827 throw "type error: const \"" + name + "\" was declared as struct/xception";
828 }
829 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
830 vector<t_field*>::const_iterator f_iter;
831
832 const map<t_const_value*, t_const_value*>& val = value->get_map();
833 map<t_const_value*, t_const_value*>::const_iterator v_iter;
834 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
835 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
836 throw "type error: " + name + " struct key must be string";
837 }
838 t_type* field_type = NULL;
839 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
840 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
841 field_type = (*f_iter)->get_type();
842 }
843 }
844 if (field_type == NULL) {
845 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
846 }
847
848 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
849 }
850 } else if (type->is_map()) {
851 t_type* k_type = ((t_map*)type)->get_key_type();
852 t_type* v_type = ((t_map*)type)->get_val_type();
853 const map<t_const_value*, t_const_value*>& val = value->get_map();
854 map<t_const_value*, t_const_value*>::const_iterator v_iter;
855 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
856 validate_const_rec(name + "<key>", k_type, v_iter->first);
857 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000858 }
Mark Slee30152872006-11-28 01:24:07 +0000859 } else if (type->is_list() || type->is_set()) {
860 t_type* e_type;
861 if (type->is_list()) {
862 e_type = ((t_list*)type)->get_elem_type();
863 } else {
864 e_type = ((t_set*)type)->get_elem_type();
865 }
866 const vector<t_const_value*>& val = value->get_list();
867 vector<t_const_value*>::const_iterator v_iter;
868 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
869 validate_const_rec(name + "<elem>", e_type, *v_iter);
870 }
871 }
872}
873
874/**
Jens Geyer12c09f42013-08-25 14:16:27 +0200875 * Check simple identifier names
876 * It's easier to do it this way instead of rewriting the whole grammar etc.
877 */
878void validate_simple_identifier(const char* identifier) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100879 string name(identifier);
880 if (name.find(".") != string::npos) {
Jens Geyer12c09f42013-08-25 14:16:27 +0200881 yyerror("Identifier %s can't have a dot.", identifier);
882 exit(1);
883 }
884}
885
886/**
Mark Slee30152872006-11-28 01:24:07 +0000887 * Check the type of the parsed const information against its declared type
888 */
889void validate_const_type(t_const* c) {
890 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
891}
892
893/**
Mark Slee7ff32452007-02-01 05:26:18 +0000894 * Check the type of a default value assigned to a field.
895 */
896void validate_field_value(t_field* field, t_const_value* cv) {
897 validate_const_rec(field->get_name(), field->get_type(), cv);
898}
899
900/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000901 * Check that all the elements of a throws block are actually exceptions.
902 */
903bool validate_throws(t_struct* throws) {
904 const vector<t_field*>& members = throws->get_members();
905 vector<t_field*>::const_iterator m_iter;
906 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
Bryan Duxburycff83572011-08-24 20:53:03 +0000907 if (!t_generator::get_true_type((*m_iter)->get_type())->is_xception()) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000908 return false;
909 }
910 }
911 return true;
912}
913
914/**
Jens Geyer03d49442013-09-04 22:34:41 +0200915 * Skips UTF-8 BOM if there is one
916 */
917bool skip_utf8_bom(FILE* f) {
918
919 // pretty straightforward, but works
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100920 if (fgetc(f) == 0xEF) {
921 if (fgetc(f) == 0xBB) {
922 if (fgetc(f) == 0xBF) {
Jens Geyer03d49442013-09-04 22:34:41 +0200923 return true;
Roger Meier4f4b15b2014-11-05 16:51:04 +0100924 }
925 }
926 }
927
928 rewind(f);
Jens Geyer03d49442013-09-04 22:34:41 +0200929 return false;
930}
931
932/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000933 * Parses a program
934 */
Mark Slee2c44d202007-05-16 02:18:07 +0000935void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000936 // Get scope file path
937 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000938
Mark Sleef0712dc2006-10-25 19:03:57 +0000939 // Set current dir global, which is used in the include_file function
940 g_curdir = directory_name(path);
941 g_curpath = path;
942
943 // Open the file
Jens Geyer03d49442013-09-04 22:34:41 +0200944 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000945 yyin = fopen(path.c_str(), "r");
946 if (yyin == 0) {
947 failure("Could not open input file: \"%s\"", path.c_str());
948 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100949 if (skip_utf8_bom(yyin))
Jens Geyer03d49442013-09-04 22:34:41 +0200950 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
Roger Meier4f4b15b2014-11-05 16:51:04 +0100951
Mark Sleef0712dc2006-10-25 19:03:57 +0000952 // Create new scope and scan for includes
953 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000954 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000955 g_program = program;
956 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000957 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000958 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000959 if (yyparse() != 0) {
960 failure("Parser error during include pass.");
961 }
962 } catch (string x) {
963 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000964 }
965 fclose(yyin);
966
967 // Recursively parse all the include programs
968 vector<t_program*>& includes = program->get_includes();
969 vector<t_program*>::iterator iter;
970 for (iter = includes.begin(); iter != includes.end(); ++iter) {
971 parse(*iter, program);
972 }
973
Jens Geyere8379b52014-01-25 00:59:45 +0100974 // reset program doctext status before parsing a new file
975 reset_program_doctext_info();
976
David Reiss204420f2008-01-11 20:59:03 +0000977 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000978 g_parse_mode = PROGRAM;
979 g_program = program;
980 g_scope = program->scope();
981 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
982 g_parent_prefix = program->get_name() + ".";
983 g_curpath = path;
Jens Geyer03d49442013-09-04 22:34:41 +0200984
985 // Open the file
986 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000987 yyin = fopen(path.c_str(), "r");
988 if (yyin == 0) {
989 failure("Could not open input file: \"%s\"", path.c_str());
990 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100991 if (skip_utf8_bom(yyin))
Jens Geyer03d49442013-09-04 22:34:41 +0200992 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
Roger Meier4f4b15b2014-11-05 16:51:04 +0100993
Mark Sleef0712dc2006-10-25 19:03:57 +0000994 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000995 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000996 try {
997 if (yyparse() != 0) {
998 failure("Parser error during types pass.");
999 }
1000 } catch (string x) {
1001 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +00001002 }
1003 fclose(yyin);
1004}
1005
1006/**
1007 * Generate code
1008 */
David Reissbd0db882008-02-27 01:54:51 +00001009void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001010 // Oooohh, recursive code generation, hot!!
1011 if (gen_recurse) {
1012 const vector<t_program*>& includes = program->get_includes();
1013 for (size_t i = 0; i < includes.size(); ++i) {
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +01001014 // Propagate output path from parent to child programs
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001015 includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute());
Mark Slee5b743072007-11-13 04:00:29 +00001016
David Reissbd0db882008-02-27 01:54:51 +00001017 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +00001018 }
1019 }
1020
1021 // Generate code!
1022 try {
1023 pverbose("Program: %s\n", program->get_path().c_str());
1024
Jens Geyer83767a72013-09-23 22:09:12 +02001025 // Compute fingerprints. - not anymore, we do it on the fly now
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001026 // generate_all_fingerprints(program);
David Reiss18bf22d2007-08-28 20:49:17 +00001027
David Reiss1ac05802007-07-30 22:00:27 +00001028 if (dump_docs) {
1029 dump_docstrings(program);
1030 }
David Reissbd0db882008-02-27 01:54:51 +00001031
1032 vector<string>::const_iterator iter;
1033 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
1034 t_generator* generator = t_generator_registry::get_generator(program, *iter);
1035
1036 if (generator == NULL) {
1037 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
1038 } else {
1039 pverbose("Generating \"%s\"\n", iter->c_str());
1040 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +00001041 delete generator;
David Reissbd0db882008-02-27 01:54:51 +00001042 }
1043 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001044 } catch (string s) {
1045 printf("Error: %s\n", s.c_str());
1046 } catch (const char* exc) {
1047 printf("Error: %s\n", exc);
1048 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001049}
1050
Ben Craig262cfb42015-07-08 20:37:15 -05001051void audit(t_program* new_program, t_program* old_program, string new_thrift_include_path, string old_thrift_include_path)
1052{
1053 vector<string> temp_incl_searchpath = g_incl_searchpath;
1054 if(!old_thrift_include_path.empty()) {
1055 g_incl_searchpath.push_back(old_thrift_include_path);
1056 }
1057
1058 parse(old_program, NULL);
1059
1060 g_incl_searchpath = temp_incl_searchpath;
1061 if(!new_thrift_include_path.empty()) {
1062 g_incl_searchpath.push_back(new_thrift_include_path);
1063 }
1064
1065 parse(new_program, NULL);
1066
1067 compare_namespace(new_program, old_program);
1068 compare_services(new_program->get_services(), old_program->get_services());
1069 compare_enums(new_program->get_enums(), old_program->get_enums());
1070 compare_structs(new_program->get_structs(), old_program->get_structs());
1071 compare_structs(new_program->get_xceptions(), old_program->get_xceptions());
1072 compare_consts(new_program->get_consts(), old_program->get_consts());
1073}
1074
Mark Sleef0712dc2006-10-25 19:03:57 +00001075/**
Mark Sleef5377b32006-10-10 01:42:59 +00001076 * Parse it up.. then spit it back out, in pretty much every language. Alright
1077 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +00001078 */
1079int main(int argc, char** argv) {
1080 int i;
dweatherford65b70752007-10-31 02:18:14 +00001081 std::string out_path;
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001082 bool out_path_is_absolute = false;
Mark Sleef5377b32006-10-10 01:42:59 +00001083
Mark Sleeb15a68b2006-06-07 06:46:24 +00001084 // Setup time string
1085 time_t now = time(NULL);
1086 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +00001087
Mark Sleef0712dc2006-10-25 19:03:57 +00001088 // Check for necessary arguments, you gotta have at least a filename and
1089 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +00001090 if (argc < 2) {
1091 usage();
1092 }
Mark Slee31985722006-05-24 21:45:31 +00001093
David Reissbd0db882008-02-27 01:54:51 +00001094 vector<string> generator_strings;
Ben Craig262cfb42015-07-08 20:37:15 -05001095 string old_thrift_include_path;
1096 string new_thrift_include_path;
1097 string old_input_file;
David Reissbd0db882008-02-27 01:54:51 +00001098
David Reiss9cc2c132008-02-27 01:54:47 +00001099 // Set the current path to a dummy value to make warning messages clearer.
1100 g_curpath = "arguments";
1101
Mark Sleef5377b32006-10-10 01:42:59 +00001102 // Hacky parameter handling... I didn't feel like using a library sorry!
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001103 for (i = 1; i < argc - 1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +00001104 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +00001105
Mark Sleefdbee812006-09-27 18:50:48 +00001106 arg = strtok(argv[i], " ");
1107 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +00001108 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +00001109 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +00001110 ++arg;
1111 }
1112
Jake Farrell2fd8a152012-09-29 00:26:36 +00001113 if (strcmp(arg, "-help") == 0) {
1114 help();
1115 } else if (strcmp(arg, "-version") == 0) {
David Reissdd08f6d2008-06-30 20:24:24 +00001116 version();
jfarrell70969422013-09-09 20:33:38 -04001117 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001118 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001119 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001120 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001121 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +00001122 } else if (strcmp(arg, "-strict") == 0) {
Bryan Duxburya145b4d2009-04-03 17:29:25 +00001123 g_strict = 255;
Mark Sleef0712dc2006-10-25 19:03:57 +00001124 g_warn = 2;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001125 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001126 g_verbose = 1;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001127 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001128 gen_recurse = true;
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001129 } else if (strcmp(arg, "-allow-neg-keys") == 0) {
1130 g_allow_neg_field_keys = true;
Roger Meier887ff752011-08-19 11:25:39 +00001131 } else if (strcmp(arg, "-allow-64bit-consts") == 0) {
1132 g_allow_64bit_consts = true;
David Reissbd0db882008-02-27 01:54:51 +00001133 } else if (strcmp(arg, "-gen") == 0) {
1134 arg = argv[++i];
1135 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001136 fprintf(stderr, "Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +00001137 usage();
1138 }
1139 generator_strings.push_back(arg);
Martin Kraemer32c66e12006-11-09 00:06:36 +00001140 } else if (strcmp(arg, "-I") == 0) {
1141 // An argument of "-I\ asdf" is invalid and has unknown results
1142 arg = argv[++i];
1143
1144 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001145 fprintf(stderr, "Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001146 usage();
1147 }
1148 g_incl_searchpath.push_back(arg);
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001149 } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) {
1150 out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false;
Roger Meier6d7473d2013-05-06 01:08:36 +02001151 arg = argv[++i];
dweatherford65b70752007-10-31 02:18:14 +00001152 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001153 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001154 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001155 }
dweatherford65b70752007-10-31 02:18:14 +00001156 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001157
Ben Craige9576752013-10-11 08:19:16 -05001158#ifdef _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001159 // strip out trailing \ on Windows
Jim King9de9b1f2015-04-30 16:03:34 -04001160 std::string::size_type last = out_path.length() - 1;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001161 if (out_path[last] == '\\') {
David Reiss204420f2008-01-11 20:59:03 +00001162 out_path.erase(last);
1163 }
1164#endif
Roger Meier061d4a22012-10-07 11:51:00 +00001165 if (!check_is_directory(out_path.c_str()))
dweatherford65b70752007-10-31 02:18:14 +00001166 return -1;
Ben Craig262cfb42015-07-08 20:37:15 -05001167 } else if (strcmp(arg, "-audit") == 0) {
1168 g_audit = true;
1169 arg = argv[++i];
1170 if (arg == NULL) {
1171 fprintf(stderr, "Missing old thrift file name for audit operation\n");
1172 usage();
1173 }
1174 char old_thrift_file_rp[THRIFT_PATH_MAX];
1175
1176 if (saferealpath(arg, old_thrift_file_rp) == NULL) {
1177 failure("Could not open input file with realpath: %s", arg);
1178 }
1179 old_input_file = string(old_thrift_file_rp);
1180 } else if(strcmp(arg, "-audit-nofatal") == 0){
1181 g_audit_fatal = false;
1182 } else if (strcmp(arg, "-Iold") == 0) {
1183 arg = argv[++i];
1184 if (arg == NULL) {
1185 fprintf(stderr, "Missing Include directory for old thrift file\n");
1186 usage();
1187 }
1188 old_thrift_include_path = string(arg);
1189 } else if (strcmp(arg, "-Inew") == 0) {
1190 arg = argv[++i];
1191 if(arg == NULL) {
1192 fprintf(stderr, "Missing Include directory for new thrift file\n");
1193 usage();
1194 }
1195 new_thrift_include_path = string(arg);
Mark Sleefdbee812006-09-27 18:50:48 +00001196 } else {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001197 fprintf(stderr, "Unrecognized option: %s\n", arg);
Mark Sleefdbee812006-09-27 18:50:48 +00001198 usage();
1199 }
1200
1201 // Tokenize more
1202 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001203 }
1204 }
Mark Slee2c44d202007-05-16 02:18:07 +00001205
Jake Farrell2fd8a152012-09-29 00:26:36 +00001206 // display help
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001207 if ((strcmp(argv[argc - 1], "-help") == 0) || (strcmp(argv[argc - 1], "--help") == 0)) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001208 help();
1209 }
1210
David Reissdd08f6d2008-06-30 20:24:24 +00001211 // if you're asking for version, you have a right not to pass a file
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001212 if ((strcmp(argv[argc - 1], "-version") == 0) || (strcmp(argv[argc - 1], "--version") == 0)) {
David Reissdd08f6d2008-06-30 20:24:24 +00001213 version();
jfarrell8b1799f2014-04-10 22:06:11 -04001214 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001215 }
1216
Mark Sleef0712dc2006-10-25 19:03:57 +00001217 // Initialize global types
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001218 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
Mark Sleef0712dc2006-10-25 19:03:57 +00001219 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001220 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1221 ((t_base_type*)g_type_binary)->set_binary(true);
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001222 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Sleeb6200d82007-01-19 19:14:36 +00001223 ((t_base_type*)g_type_slist)->set_string_list(true);
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001224 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
1225 g_type_byte = new t_base_type("byte", t_base_type::TYPE_BYTE);
1226 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1227 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1228 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
Mark Sleef0712dc2006-10-25 19:03:57 +00001229 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001230
Ben Craig262cfb42015-07-08 20:37:15 -05001231 if(g_audit)
1232 {
1233 // Audit operation
Mark Slee31985722006-05-24 21:45:31 +00001234
Ben Craig262cfb42015-07-08 20:37:15 -05001235 if (old_input_file.empty()) {
1236 fprintf(stderr, "Missing file name of old thrift file for audit\n");
1237 usage();
1238 }
David Reiss9cc2c132008-02-27 01:54:47 +00001239
Ben Craig262cfb42015-07-08 20:37:15 -05001240 char new_thrift_file_rp[THRIFT_PATH_MAX];
1241 if (argv[i] == NULL) {
1242 fprintf(stderr, "Missing file name of new thrift file for audit\n");
1243 usage();
1244 }
1245 if (saferealpath(argv[i], new_thrift_file_rp) == NULL) {
1246 failure("Could not open input file with realpath: %s", argv[i]);
1247 }
1248 string new_input_file(new_thrift_file_rp);
1249
1250 t_program new_program(new_input_file);
1251 t_program old_program(old_input_file);
1252
1253 audit(&new_program, &old_program, new_thrift_include_path, old_thrift_include_path);
1254
1255 } else {
1256 // Generate options
1257
1258 // You gotta generate something!
1259 if (generator_strings.empty()) {
1260 fprintf(stderr, "No output language(s) specified\n");
1261 usage();
1262 }
1263
1264 // Real-pathify it
1265 char rp[THRIFT_PATH_MAX];
1266 if (argv[i] == NULL) {
1267 fprintf(stderr, "Missing file name\n");
1268 usage();
1269 }
1270 if (saferealpath(argv[i], rp) == NULL) {
1271 failure("Could not open input file with realpath: %s", argv[i]);
1272 }
1273 string input_file(rp);
1274
1275 // Instance of the global parse tree
1276 t_program* program = new t_program(input_file);
1277 if (out_path.size()) {
1278 program->set_out_path(out_path, out_path_is_absolute);
1279 }
1280
1281 // Compute the cpp include prefix.
1282 // infer this from the filename passed in
1283 string input_filename = argv[i];
1284 string include_prefix;
1285
1286 string::size_type last_slash = string::npos;
1287 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1288 include_prefix = input_filename.substr(0, last_slash);
1289 }
1290
1291 program->set_include_prefix(include_prefix);
1292
1293 // Parse it!
1294 parse(program, NULL);
1295
1296 // The current path is not really relevant when we are doing generation.
1297 // Reset the variable to make warning messages clearer.
1298 g_curpath = "generation";
1299 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1300 // That is what shows up during argument parsing.
1301 yylineno = 1;
1302
1303 // Generate it!
1304 generate(program, generator_strings);
1305 delete program;
1306 }
Mark Sleeb15a68b2006-06-07 06:46:24 +00001307
Mark Sleef0712dc2006-10-25 19:03:57 +00001308 // Clean up. Who am I kidding... this program probably orphans heap memory
1309 // all over the place, but who cares because it is about to exit and it is
1310 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001311
Mark Sleef0712dc2006-10-25 19:03:57 +00001312 delete g_type_void;
1313 delete g_type_string;
1314 delete g_type_bool;
1315 delete g_type_byte;
1316 delete g_type_i16;
1317 delete g_type_i32;
1318 delete g_type_i64;
1319 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001320
1321 // Finished
Ben Craig262cfb42015-07-08 20:37:15 -05001322 if (g_return_failure && g_audit_fatal) {
1323 exit(2);
1324 }
1325 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001326 return 0;
1327}