blob: 89dd9f97e54a980469b3c39f2e30500ce2b2b778 [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];
Nobuaki Sukegawad479e232016-02-28 11:28:19 +0900359 // cppcheck-suppress uninitvar
David Reiss204420f2008-01-11 20:59:03 +0000360 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000361 pwarning(0, "Cannot open include file %s\n", filename.c_str());
362 return std::string();
363 }
Mark Slee2c44d202007-05-16 02:18:07 +0000364
365 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000366 struct stat finfo;
367 if (stat(rp, &finfo) == 0) {
368 return rp;
369 }
370 } else { // relative path, start searching
371 // new search path with current dir global
372 vector<string> sp = g_incl_searchpath;
373 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000374
Martin Kraemer32c66e12006-11-09 00:06:36 +0000375 // iterate through paths
376 vector<string>::iterator it;
377 for (it = sp.begin(); it != sp.end(); it++) {
378 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000379
Martin Kraemer32c66e12006-11-09 00:06:36 +0000380 // Realpath!
Ben Craige9576752013-10-11 08:19:16 -0500381 char rp[THRIFT_PATH_MAX];
Nobuaki Sukegawad479e232016-02-28 11:28:19 +0900382 // cppcheck-suppress uninitvar
David Reiss204420f2008-01-11 20:59:03 +0000383 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000384 continue;
385 }
Mark Slee2c44d202007-05-16 02:18:07 +0000386
Martin Kraemer32c66e12006-11-09 00:06:36 +0000387 // Stat this files
388 struct stat finfo;
389 if (stat(rp, &finfo) == 0) {
390 return rp;
391 }
392 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000393 }
Mark Slee2c44d202007-05-16 02:18:07 +0000394
Mark Sleef0712dc2006-10-25 19:03:57 +0000395 // Uh oh
396 pwarning(0, "Could not find include file %s\n", filename.c_str());
397 return std::string();
398}
399
400/**
David Reisscbd4bac2007-08-14 17:12:33 +0000401 * Clears any previously stored doctext string.
402 * Also prints a warning if we are discarding information.
403 */
404void clear_doctext() {
405 if (g_doctext != NULL) {
406 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
407 }
408 free(g_doctext);
409 g_doctext = NULL;
410}
411
412/**
Jens Geyere8379b52014-01-25 00:59:45 +0100413 * Reset program doctext information after processing a file
414 */
415void reset_program_doctext_info() {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100416 if (g_program_doctext_candidate != NULL) {
Jens Geyere8379b52014-01-25 00:59:45 +0100417 free(g_program_doctext_candidate);
418 g_program_doctext_candidate = NULL;
419 }
420 g_program_doctext_lineno = 0;
421 g_program_doctext_status = INVALID;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100422 pdebug("%s", "program doctext set to INVALID");
Jens Geyere8379b52014-01-25 00:59:45 +0100423}
424
425/**
426 * We are sure the program doctext candidate is really the program doctext.
427 */
428void declare_valid_program_doctext() {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100429 if ((g_program_doctext_candidate != NULL) && (g_program_doctext_status == STILL_CANDIDATE)) {
Roger Meier4f4b15b2014-11-05 16:51:04 +0100430 g_program_doctext_status = ABSOLUTELY_SURE;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100431 pdebug("%s", "program doctext set to ABSOLUTELY_SURE");
Jens Geyer813749d2014-01-31 23:42:57 +0100432 } else {
Roger Meier4f4b15b2014-11-05 16:51:04 +0100433 g_program_doctext_status = NO_PROGRAM_DOCTEXT;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100434 pdebug("%s", "program doctext set to NO_PROGRAM_DOCTEXT");
Jens Geyere8379b52014-01-25 00:59:45 +0100435 }
436}
437
438/**
David Reiss1ac05802007-07-30 22:00:27 +0000439 * Cleans up text commonly found in doxygen-like comments
440 *
441 * Warning: if you mix tabs and spaces in a non-uniform way,
442 * you will get what you deserve.
443 */
444char* clean_up_doctext(char* doctext) {
445 // Convert to C++ string, and remove Windows's carriage returns.
446 string docstring = doctext;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100447 docstring.erase(remove(docstring.begin(), docstring.end(), '\r'), docstring.end());
David Reiss1ac05802007-07-30 22:00:27 +0000448
449 // Separate into lines.
450 vector<string> lines;
451 string::size_type pos = string::npos;
452 string::size_type last;
453 while (true) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100454 last = (pos == string::npos) ? 0 : pos + 1;
David Reiss1ac05802007-07-30 22:00:27 +0000455 pos = docstring.find('\n', last);
456 if (pos == string::npos) {
457 // First bit of cleaning. If the last line is only whitespace, drop it.
458 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
459 if (nonwhite != string::npos) {
460 lines.push_back(docstring.substr(last));
461 }
462 break;
463 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100464 lines.push_back(docstring.substr(last, pos - last));
David Reiss1ac05802007-07-30 22:00:27 +0000465 }
466
467 // A very profound docstring.
468 if (lines.empty()) {
469 return NULL;
470 }
471
472 // Clear leading whitespace from the first line.
473 pos = lines.front().find_first_not_of(" \t");
474 lines.front().erase(0, pos);
475
476 // If every nonblank line after the first has the same number of spaces/tabs,
477 // then a star, remove them.
478 bool have_prefix = true;
479 bool found_prefix = false;
480 string::size_type prefix_len = 0;
481 vector<string>::iterator l_iter;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100482 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000483 if (l_iter->empty()) {
484 continue;
485 }
486
487 pos = l_iter->find_first_not_of(" \t");
488 if (!found_prefix) {
489 if (pos != string::npos) {
490 if (l_iter->at(pos) == '*') {
491 found_prefix = true;
492 prefix_len = pos;
493 } else {
494 have_prefix = false;
495 break;
496 }
497 } else {
498 // Whitespace-only line. Truncate it.
499 l_iter->clear();
500 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100501 } else if (l_iter->size() > pos && l_iter->at(pos) == '*' && pos == prefix_len) {
David Reiss1ac05802007-07-30 22:00:27 +0000502 // Business as usual.
503 } else if (pos == string::npos) {
504 // Whitespace-only line. Let's truncate it for them.
505 l_iter->clear();
506 } else {
507 // The pattern has been broken.
508 have_prefix = false;
509 break;
510 }
511 }
512
513 // If our prefix survived, delete it from every line.
514 if (have_prefix) {
515 // Get the star too.
516 prefix_len++;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100517 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000518 l_iter->erase(0, prefix_len);
519 }
520 }
521
522 // Now delete the minimum amount of leading whitespace from each line.
523 prefix_len = string::npos;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100524 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000525 if (l_iter->empty()) {
526 continue;
527 }
528 pos = l_iter->find_first_not_of(" \t");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100529 if (pos != string::npos && (prefix_len == string::npos || pos < prefix_len)) {
David Reiss1ac05802007-07-30 22:00:27 +0000530 prefix_len = pos;
531 }
532 }
533
534 // If our prefix survived, delete it from every line.
535 if (prefix_len != string::npos) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100536 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000537 l_iter->erase(0, prefix_len);
538 }
539 }
540
541 // Remove trailing whitespace from every line.
542 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
543 pos = l_iter->find_last_not_of(" \t");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100544 if (pos != string::npos && pos != l_iter->length() - 1) {
545 l_iter->erase(pos + 1);
David Reiss1ac05802007-07-30 22:00:27 +0000546 }
547 }
548
549 // If the first line is empty, remove it.
550 // Don't do this earlier because a lot of steps skip the first line.
551 if (lines.front().empty()) {
552 lines.erase(lines.begin());
553 }
554
555 // Now rejoin the lines and copy them back into doctext.
556 docstring.clear();
557 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
558 docstring += *l_iter;
559 docstring += '\n';
560 }
561
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100562 // assert(docstring.length() <= strlen(doctext)); may happen, see THRIFT-1755
563 if (docstring.length() <= strlen(doctext)) {
Jens Geyer8cd3efe2013-09-16 22:17:52 +0200564 strcpy(doctext, docstring.c_str());
565 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100566 free(doctext); // too short
Jens Geyer8cd3efe2013-09-16 22:17:52 +0200567 doctext = strdup(docstring.c_str());
568 }
David Reiss1ac05802007-07-30 22:00:27 +0000569 return doctext;
570}
571
572/** Set to true to debug docstring parsing */
573static bool dump_docs = false;
574
575/**
576 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000577 * Only works for top-level definitions and the whole program doc
578 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000579 */
580void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000581 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000582 if (!progdoc.empty()) {
583 printf("Whole program doc:\n%s\n", progdoc.c_str());
584 }
David Reiss1ac05802007-07-30 22:00:27 +0000585 const vector<t_typedef*>& typedefs = program->get_typedefs();
586 vector<t_typedef*>::const_iterator t_iter;
587 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
588 t_typedef* td = *t_iter;
589 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000590 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
591 }
592 }
593 const vector<t_enum*>& enums = program->get_enums();
594 vector<t_enum*>::const_iterator e_iter;
595 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
596 t_enum* en = *e_iter;
597 if (en->has_doc()) {
598 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
599 }
600 }
601 const vector<t_const*>& consts = program->get_consts();
602 vector<t_const*>::const_iterator c_iter;
603 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
604 t_const* co = *c_iter;
605 if (co->has_doc()) {
606 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
607 }
608 }
609 const vector<t_struct*>& structs = program->get_structs();
610 vector<t_struct*>::const_iterator s_iter;
611 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
612 t_struct* st = *s_iter;
613 if (st->has_doc()) {
614 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
615 }
616 }
617 const vector<t_struct*>& xceptions = program->get_xceptions();
618 vector<t_struct*>::const_iterator x_iter;
619 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
620 t_struct* xn = *x_iter;
621 if (xn->has_doc()) {
622 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
623 }
624 }
625 const vector<t_service*>& services = program->get_services();
626 vector<t_service*>::const_iterator v_iter;
627 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
628 t_service* sv = *v_iter;
629 if (sv->has_doc()) {
630 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000631 }
632 }
633}
634
635/**
Jens Geyer6fe77e82014-03-16 16:48:53 +0200636 * Emits a warning on list<byte>, binary type is typically a much better choice.
637 */
638void check_for_list_of_bytes(t_type* list_elem_type) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100639 if ((g_parse_mode == PROGRAM) && (list_elem_type != NULL) && list_elem_type->is_base_type()) {
Jens Geyer6fe77e82014-03-16 16:48:53 +0200640 t_base_type* tbase = (t_base_type*)list_elem_type;
Jens Geyer40c28d32015-10-20 23:13:02 +0200641 if (tbase->get_base() == t_base_type::TYPE_I8) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100642 pwarning(1, "Consider using the more efficient \"binary\" type instead of \"list<byte>\".");
Jens Geyer6fe77e82014-03-16 16:48:53 +0200643 }
644 }
645}
646
Jens Geyer40c28d32015-10-20 23:13:02 +0200647static bool g_byte_warning_emitted = false;
648
649/**
650 * Emits a one-time warning on byte type, promoting the new i8 type instead
651 */
652void emit_byte_type_warning() {
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +0100653 if (!g_byte_warning_emitted) {
654 pwarning(1,
655 "The \"byte\" type is a compatibility alias for \"i8\". Use \"i8\" to emphasize the "
656 "signedness of this type.\n");
657 g_byte_warning_emitted = true;
658 }
Jens Geyer40c28d32015-10-20 23:13:02 +0200659}
660
David Reiss18bf22d2007-08-28 20:49:17 +0000661/**
Jens Geyer73880372015-11-14 15:21:57 +0100662 * Prints deprecation notice for old NS declarations that are no longer supported
663 * If new_form is NULL, old_form is assumed to be a language identifier, such as "cpp"
664 * If new_form is not NULL, both arguments are used exactly as given
665 */
Jens Geyereb5f1172015-12-11 20:58:45 +0100666void error_unsupported_namespace_decl(const char* old_form, const char* new_form) {
667 const char* remainder = "";
Jens Geyer73880372015-11-14 15:21:57 +0100668 if( new_form == NULL) {
669 new_form = old_form;
670 remainder = "_namespace";
671 }
672 failure("Unsupported declaration '%s%s'. Use 'namespace %s' instead.", old_form, remainder, new_form);
673}
674
675/**
David Reissdd08f6d2008-06-30 20:24:24 +0000676 * Prints the version number
677 */
678void version() {
Bryan Duxburya1e268c2010-05-03 21:33:00 +0000679 printf("Thrift version %s\n", THRIFT_VERSION);
David Reissdd08f6d2008-06-30 20:24:24 +0000680}
681
682/**
Jake Farrell2fd8a152012-09-29 00:26:36 +0000683 * Display the usage message and then exit with an error code.
Mark Slee31985722006-05-24 21:45:31 +0000684 */
685void usage() {
Jake Farrell2fd8a152012-09-29 00:26:36 +0000686 fprintf(stderr, "Usage: thrift [options] file\n\n");
687 fprintf(stderr, "Use thrift -help for a list of options\n");
688 exit(1);
689}
690
691/**
692 * Diplays the help message and then exits with an error code.
693 */
694void help() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000695 fprintf(stderr, "Usage: thrift [options] file\n");
696 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000697 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000698 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
699 fprintf(stderr, " (default: current directory)\n");
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000700 fprintf(stderr, " -out dir Set the ouput location for generated files.\n");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100701 fprintf(stderr, " (no gen-* folder will be created)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000702 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000703 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000704 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
705 fprintf(stderr, " -strict Strict compiler warnings on\n");
706 fprintf(stderr, " -v[erbose] Verbose mode\n");
707 fprintf(stderr, " -r[ecurse] Also generate included files\n");
708 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100709 fprintf(stderr,
710 " --allow-neg-keys Allow negative field keys (Used to "
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000711 "preserve protocol\n");
712 fprintf(stderr, " compatibility with older .thrift files)\n");
Roger Meier887ff752011-08-19 11:25:39 +0000713 fprintf(stderr, " --allow-64bit-consts Do not print warnings about using 64-bit constants\n");
David Reissbd0db882008-02-27 01:54:51 +0000714 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
Jens Geyere8c51ed2014-04-18 02:27:57 +0200715 fprintf(stderr, " STR has the form language[:key1=val1[,key2[,key3=val3]]].\n");
David Reissbd0db882008-02-27 01:54:51 +0000716 fprintf(stderr, " Keys and values are options passed to the generator.\n");
717 fprintf(stderr, " Many options will not require values.\n");
718 fprintf(stderr, "\n");
Ben Craig262cfb42015-07-08 20:37:15 -0500719 fprintf(stderr, "Options related to audit operation\n");
720 fprintf(stderr, " --audit OldFile Old Thrift file to be audited with 'file'\n");
721 fprintf(stderr, " -Iold dir Add a directory to the list of directories\n");
722 fprintf(stderr, " searched for include directives for old thrift file\n");
723 fprintf(stderr, " -Inew dir Add a directory to the list of directories\n");
724 fprintf(stderr, " searched for include directives for new thrift file\n");
725 fprintf(stderr, "\n");
David Reissbd0db882008-02-27 01:54:51 +0000726 fprintf(stderr, "Available generators (and options):\n");
727
728 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
729 t_generator_registry::gen_map_t::iterator iter;
730 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100731 fprintf(stderr,
732 " %s (%s):\n",
733 iter->second->get_short_name().c_str(),
734 iter->second->get_long_name().c_str());
David Reissbd0db882008-02-27 01:54:51 +0000735 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
736 }
Mark Slee31985722006-05-24 21:45:31 +0000737 exit(1);
738}
739
740/**
Mark Slee30152872006-11-28 01:24:07 +0000741 * You know, when I started working on Thrift I really thought it wasn't going
742 * to become a programming language because it was just a generator and it
743 * wouldn't need runtime type information and all that jazz. But then we
744 * decided to add constants, and all of a sudden that means runtime type
745 * validation and inference, except the "runtime" is the code generator
David Reiss3bb5e052010-01-25 19:31:31 +0000746 * runtime.
Mark Slee30152872006-11-28 01:24:07 +0000747 */
748void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
749 if (type->is_void()) {
750 throw "type error: cannot declare a void const: " + name;
751 }
752
753 if (type->is_base_type()) {
754 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
755 switch (tbase) {
756 case t_base_type::TYPE_STRING:
757 if (value->get_type() != t_const_value::CV_STRING) {
758 throw "type error: const \"" + name + "\" was declared as string";
759 }
760 break;
761 case t_base_type::TYPE_BOOL:
762 if (value->get_type() != t_const_value::CV_INTEGER) {
763 throw "type error: const \"" + name + "\" was declared as bool";
764 }
765 break;
Jens Geyer40c28d32015-10-20 23:13:02 +0200766 case t_base_type::TYPE_I8:
Mark Slee30152872006-11-28 01:24:07 +0000767 if (value->get_type() != t_const_value::CV_INTEGER) {
768 throw "type error: const \"" + name + "\" was declared as byte";
769 }
770 break;
771 case t_base_type::TYPE_I16:
772 if (value->get_type() != t_const_value::CV_INTEGER) {
773 throw "type error: const \"" + name + "\" was declared as i16";
774 }
775 break;
776 case t_base_type::TYPE_I32:
777 if (value->get_type() != t_const_value::CV_INTEGER) {
778 throw "type error: const \"" + name + "\" was declared as i32";
779 }
780 break;
781 case t_base_type::TYPE_I64:
782 if (value->get_type() != t_const_value::CV_INTEGER) {
783 throw "type error: const \"" + name + "\" was declared as i64";
784 }
785 break;
786 case t_base_type::TYPE_DOUBLE:
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100787 if (value->get_type() != t_const_value::CV_INTEGER
788 && value->get_type() != t_const_value::CV_DOUBLE) {
Mark Slee30152872006-11-28 01:24:07 +0000789 throw "type error: const \"" + name + "\" was declared as double";
790 }
791 break;
792 default:
David Reissdd7796f2007-08-28 21:09:06 +0000793 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000794 }
795 } else if (type->is_enum()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000796 if (value->get_type() != t_const_value::CV_IDENTIFIER) {
Mark Slee30152872006-11-28 01:24:07 +0000797 throw "type error: const \"" + name + "\" was declared as enum";
798 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000799
Bryan Duxbury1606f252010-11-24 00:25:57 +0000800 // see if there's a dot in the identifier
801 std::string name_portion = value->get_identifier_name();
802
Bryan Duxbury2d804702009-12-18 19:41:11 +0000803 const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants();
804 vector<t_enum_value*>::const_iterator c_iter;
805 bool found = false;
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000806
Bryan Duxbury1606f252010-11-24 00:25:57 +0000807 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000808 if ((*c_iter)->get_name() == name_portion) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000809 found = true;
810 break;
811 }
812 }
813 if (!found) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100814 throw "type error: const " + name + " was declared as type " + type->get_name()
815 + " which is an enum, but " + value->get_identifier()
816 + " is not a valid value for that enum";
Bryan Duxbury2d804702009-12-18 19:41:11 +0000817 }
Mark Slee30152872006-11-28 01:24:07 +0000818 } else if (type->is_struct() || type->is_xception()) {
819 if (value->get_type() != t_const_value::CV_MAP) {
820 throw "type error: const \"" + name + "\" was declared as struct/xception";
821 }
822 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
823 vector<t_field*>::const_iterator f_iter;
824
825 const map<t_const_value*, t_const_value*>& val = value->get_map();
826 map<t_const_value*, t_const_value*>::const_iterator v_iter;
827 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
828 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
829 throw "type error: " + name + " struct key must be string";
830 }
831 t_type* field_type = NULL;
832 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
833 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
834 field_type = (*f_iter)->get_type();
835 }
836 }
837 if (field_type == NULL) {
838 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
839 }
840
841 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
842 }
843 } else if (type->is_map()) {
844 t_type* k_type = ((t_map*)type)->get_key_type();
845 t_type* v_type = ((t_map*)type)->get_val_type();
846 const map<t_const_value*, t_const_value*>& val = value->get_map();
847 map<t_const_value*, t_const_value*>::const_iterator v_iter;
848 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
849 validate_const_rec(name + "<key>", k_type, v_iter->first);
850 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000851 }
Mark Slee30152872006-11-28 01:24:07 +0000852 } else if (type->is_list() || type->is_set()) {
853 t_type* e_type;
854 if (type->is_list()) {
855 e_type = ((t_list*)type)->get_elem_type();
856 } else {
857 e_type = ((t_set*)type)->get_elem_type();
858 }
859 const vector<t_const_value*>& val = value->get_list();
860 vector<t_const_value*>::const_iterator v_iter;
861 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
862 validate_const_rec(name + "<elem>", e_type, *v_iter);
863 }
864 }
865}
866
867/**
Jens Geyer12c09f42013-08-25 14:16:27 +0200868 * Check simple identifier names
869 * It's easier to do it this way instead of rewriting the whole grammar etc.
870 */
871void validate_simple_identifier(const char* identifier) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100872 string name(identifier);
873 if (name.find(".") != string::npos) {
Jens Geyer12c09f42013-08-25 14:16:27 +0200874 yyerror("Identifier %s can't have a dot.", identifier);
875 exit(1);
876 }
877}
878
879/**
Mark Slee30152872006-11-28 01:24:07 +0000880 * Check the type of the parsed const information against its declared type
881 */
882void validate_const_type(t_const* c) {
883 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
884}
885
886/**
Mark Slee7ff32452007-02-01 05:26:18 +0000887 * Check the type of a default value assigned to a field.
888 */
889void validate_field_value(t_field* field, t_const_value* cv) {
890 validate_const_rec(field->get_name(), field->get_type(), cv);
891}
892
893/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000894 * Check that all the elements of a throws block are actually exceptions.
895 */
896bool validate_throws(t_struct* throws) {
897 const vector<t_field*>& members = throws->get_members();
898 vector<t_field*>::const_iterator m_iter;
899 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
Bryan Duxburycff83572011-08-24 20:53:03 +0000900 if (!t_generator::get_true_type((*m_iter)->get_type())->is_xception()) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000901 return false;
902 }
903 }
904 return true;
905}
906
907/**
Jens Geyer03d49442013-09-04 22:34:41 +0200908 * Skips UTF-8 BOM if there is one
909 */
910bool skip_utf8_bom(FILE* f) {
911
912 // pretty straightforward, but works
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100913 if (fgetc(f) == 0xEF) {
914 if (fgetc(f) == 0xBB) {
915 if (fgetc(f) == 0xBF) {
Jens Geyer03d49442013-09-04 22:34:41 +0200916 return true;
Roger Meier4f4b15b2014-11-05 16:51:04 +0100917 }
918 }
919 }
920
921 rewind(f);
Jens Geyer03d49442013-09-04 22:34:41 +0200922 return false;
923}
924
925/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000926 * Parses a program
927 */
Mark Slee2c44d202007-05-16 02:18:07 +0000928void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000929 // Get scope file path
930 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000931
Mark Sleef0712dc2006-10-25 19:03:57 +0000932 // Set current dir global, which is used in the include_file function
933 g_curdir = directory_name(path);
934 g_curpath = path;
935
936 // Open the file
Jens Geyer03d49442013-09-04 22:34:41 +0200937 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000938 yyin = fopen(path.c_str(), "r");
939 if (yyin == 0) {
940 failure("Could not open input file: \"%s\"", path.c_str());
941 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100942 if (skip_utf8_bom(yyin))
Jens Geyer03d49442013-09-04 22:34:41 +0200943 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
Roger Meier4f4b15b2014-11-05 16:51:04 +0100944
Mark Sleef0712dc2006-10-25 19:03:57 +0000945 // Create new scope and scan for includes
946 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000947 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000948 g_program = program;
949 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000950 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000951 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000952 if (yyparse() != 0) {
953 failure("Parser error during include pass.");
954 }
955 } catch (string x) {
956 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000957 }
958 fclose(yyin);
959
960 // Recursively parse all the include programs
961 vector<t_program*>& includes = program->get_includes();
962 vector<t_program*>::iterator iter;
963 for (iter = includes.begin(); iter != includes.end(); ++iter) {
964 parse(*iter, program);
965 }
966
Jens Geyere8379b52014-01-25 00:59:45 +0100967 // reset program doctext status before parsing a new file
968 reset_program_doctext_info();
969
David Reiss204420f2008-01-11 20:59:03 +0000970 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000971 g_parse_mode = PROGRAM;
972 g_program = program;
973 g_scope = program->scope();
974 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
975 g_parent_prefix = program->get_name() + ".";
976 g_curpath = path;
Jens Geyer03d49442013-09-04 22:34:41 +0200977
978 // Open the file
979 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000980 yyin = fopen(path.c_str(), "r");
981 if (yyin == 0) {
982 failure("Could not open input file: \"%s\"", path.c_str());
983 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100984 if (skip_utf8_bom(yyin))
Jens Geyer03d49442013-09-04 22:34:41 +0200985 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
Roger Meier4f4b15b2014-11-05 16:51:04 +0100986
Mark Sleef0712dc2006-10-25 19:03:57 +0000987 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000988 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000989 try {
990 if (yyparse() != 0) {
991 failure("Parser error during types pass.");
992 }
993 } catch (string x) {
994 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000995 }
996 fclose(yyin);
997}
998
999/**
1000 * Generate code
1001 */
David Reissbd0db882008-02-27 01:54:51 +00001002void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001003 // Oooohh, recursive code generation, hot!!
1004 if (gen_recurse) {
1005 const vector<t_program*>& includes = program->get_includes();
1006 for (size_t i = 0; i < includes.size(); ++i) {
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +01001007 // Propagate output path from parent to child programs
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001008 includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute());
Mark Slee5b743072007-11-13 04:00:29 +00001009
David Reissbd0db882008-02-27 01:54:51 +00001010 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +00001011 }
1012 }
1013
1014 // Generate code!
1015 try {
1016 pverbose("Program: %s\n", program->get_path().c_str());
1017
David Reiss1ac05802007-07-30 22:00:27 +00001018 if (dump_docs) {
1019 dump_docstrings(program);
1020 }
David Reissbd0db882008-02-27 01:54:51 +00001021
1022 vector<string>::const_iterator iter;
1023 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
1024 t_generator* generator = t_generator_registry::get_generator(program, *iter);
1025
1026 if (generator == NULL) {
1027 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
1028 } else {
1029 pverbose("Generating \"%s\"\n", iter->c_str());
1030 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +00001031 delete generator;
David Reissbd0db882008-02-27 01:54:51 +00001032 }
1033 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001034 } catch (string s) {
Jens Geyerd4722d92016-02-13 23:25:11 +01001035 failure("Error: %s\n", s.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +00001036 } catch (const char* exc) {
Jens Geyerd4722d92016-02-13 23:25:11 +01001037 failure("Error: %s\n", exc);
Mark Sleef0712dc2006-10-25 19:03:57 +00001038 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001039}
1040
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +01001041void audit(t_program* new_program,
1042 t_program* old_program,
1043 string new_thrift_include_path,
1044 string old_thrift_include_path) {
Ben Craig262cfb42015-07-08 20:37:15 -05001045 vector<string> temp_incl_searchpath = g_incl_searchpath;
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +01001046 if (!old_thrift_include_path.empty()) {
Ben Craig262cfb42015-07-08 20:37:15 -05001047 g_incl_searchpath.push_back(old_thrift_include_path);
1048 }
1049
1050 parse(old_program, NULL);
1051
1052 g_incl_searchpath = temp_incl_searchpath;
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +01001053 if (!new_thrift_include_path.empty()) {
Ben Craig262cfb42015-07-08 20:37:15 -05001054 g_incl_searchpath.push_back(new_thrift_include_path);
1055 }
1056
1057 parse(new_program, NULL);
1058
1059 compare_namespace(new_program, old_program);
1060 compare_services(new_program->get_services(), old_program->get_services());
1061 compare_enums(new_program->get_enums(), old_program->get_enums());
1062 compare_structs(new_program->get_structs(), old_program->get_structs());
1063 compare_structs(new_program->get_xceptions(), old_program->get_xceptions());
1064 compare_consts(new_program->get_consts(), old_program->get_consts());
1065}
1066
Mark Sleef0712dc2006-10-25 19:03:57 +00001067/**
Mark Sleef5377b32006-10-10 01:42:59 +00001068 * Parse it up.. then spit it back out, in pretty much every language. Alright
1069 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +00001070 */
1071int main(int argc, char** argv) {
1072 int i;
dweatherford65b70752007-10-31 02:18:14 +00001073 std::string out_path;
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001074 bool out_path_is_absolute = false;
Mark Sleef5377b32006-10-10 01:42:59 +00001075
Mark Sleeb15a68b2006-06-07 06:46:24 +00001076 // Setup time string
1077 time_t now = time(NULL);
1078 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +00001079
Mark Sleef0712dc2006-10-25 19:03:57 +00001080 // Check for necessary arguments, you gotta have at least a filename and
1081 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +00001082 if (argc < 2) {
1083 usage();
1084 }
Mark Slee31985722006-05-24 21:45:31 +00001085
David Reissbd0db882008-02-27 01:54:51 +00001086 vector<string> generator_strings;
Ben Craig262cfb42015-07-08 20:37:15 -05001087 string old_thrift_include_path;
1088 string new_thrift_include_path;
1089 string old_input_file;
David Reissbd0db882008-02-27 01:54:51 +00001090
David Reiss9cc2c132008-02-27 01:54:47 +00001091 // Set the current path to a dummy value to make warning messages clearer.
1092 g_curpath = "arguments";
1093
Mark Sleef5377b32006-10-10 01:42:59 +00001094 // Hacky parameter handling... I didn't feel like using a library sorry!
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001095 for (i = 1; i < argc - 1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +00001096 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +00001097
Mark Sleefdbee812006-09-27 18:50:48 +00001098 arg = strtok(argv[i], " ");
1099 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +00001100 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +00001101 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +00001102 ++arg;
1103 }
1104
Jake Farrell2fd8a152012-09-29 00:26:36 +00001105 if (strcmp(arg, "-help") == 0) {
1106 help();
1107 } else if (strcmp(arg, "-version") == 0) {
David Reissdd08f6d2008-06-30 20:24:24 +00001108 version();
jfarrell70969422013-09-09 20:33:38 -04001109 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001110 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001111 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001112 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001113 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +00001114 } else if (strcmp(arg, "-strict") == 0) {
Bryan Duxburya145b4d2009-04-03 17:29:25 +00001115 g_strict = 255;
Mark Sleef0712dc2006-10-25 19:03:57 +00001116 g_warn = 2;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001117 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001118 g_verbose = 1;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001119 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001120 gen_recurse = true;
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001121 } else if (strcmp(arg, "-allow-neg-keys") == 0) {
1122 g_allow_neg_field_keys = true;
Roger Meier887ff752011-08-19 11:25:39 +00001123 } else if (strcmp(arg, "-allow-64bit-consts") == 0) {
1124 g_allow_64bit_consts = true;
David Reissbd0db882008-02-27 01:54:51 +00001125 } else if (strcmp(arg, "-gen") == 0) {
1126 arg = argv[++i];
1127 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001128 fprintf(stderr, "Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +00001129 usage();
1130 }
1131 generator_strings.push_back(arg);
Martin Kraemer32c66e12006-11-09 00:06:36 +00001132 } else if (strcmp(arg, "-I") == 0) {
1133 // An argument of "-I\ asdf" is invalid and has unknown results
1134 arg = argv[++i];
1135
1136 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001137 fprintf(stderr, "Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001138 usage();
1139 }
1140 g_incl_searchpath.push_back(arg);
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001141 } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) {
1142 out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false;
Roger Meier6d7473d2013-05-06 01:08:36 +02001143 arg = argv[++i];
dweatherford65b70752007-10-31 02:18:14 +00001144 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001145 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001146 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001147 }
dweatherford65b70752007-10-31 02:18:14 +00001148 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001149
Ben Craige9576752013-10-11 08:19:16 -05001150#ifdef _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001151 // strip out trailing \ on Windows
Jim King9de9b1f2015-04-30 16:03:34 -04001152 std::string::size_type last = out_path.length() - 1;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001153 if (out_path[last] == '\\') {
David Reiss204420f2008-01-11 20:59:03 +00001154 out_path.erase(last);
1155 }
1156#endif
Roger Meier061d4a22012-10-07 11:51:00 +00001157 if (!check_is_directory(out_path.c_str()))
dweatherford65b70752007-10-31 02:18:14 +00001158 return -1;
Ben Craig262cfb42015-07-08 20:37:15 -05001159 } else if (strcmp(arg, "-audit") == 0) {
1160 g_audit = true;
1161 arg = argv[++i];
1162 if (arg == NULL) {
1163 fprintf(stderr, "Missing old thrift file name for audit operation\n");
1164 usage();
1165 }
1166 char old_thrift_file_rp[THRIFT_PATH_MAX];
1167
Nobuaki Sukegawad479e232016-02-28 11:28:19 +09001168 // cppcheck-suppress uninitvar
Ben Craig262cfb42015-07-08 20:37:15 -05001169 if (saferealpath(arg, old_thrift_file_rp) == NULL) {
1170 failure("Could not open input file with realpath: %s", arg);
1171 }
1172 old_input_file = string(old_thrift_file_rp);
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +01001173 } else if (strcmp(arg, "-audit-nofatal") == 0) {
Ben Craig262cfb42015-07-08 20:37:15 -05001174 g_audit_fatal = false;
1175 } else if (strcmp(arg, "-Iold") == 0) {
1176 arg = argv[++i];
1177 if (arg == NULL) {
1178 fprintf(stderr, "Missing Include directory for old thrift file\n");
1179 usage();
1180 }
1181 old_thrift_include_path = string(arg);
1182 } else if (strcmp(arg, "-Inew") == 0) {
1183 arg = argv[++i];
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +01001184 if (arg == NULL) {
1185 fprintf(stderr, "Missing Include directory for new thrift file\n");
1186 usage();
Ben Craig262cfb42015-07-08 20:37:15 -05001187 }
1188 new_thrift_include_path = string(arg);
Mark Sleefdbee812006-09-27 18:50:48 +00001189 } else {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001190 fprintf(stderr, "Unrecognized option: %s\n", arg);
Mark Sleefdbee812006-09-27 18:50:48 +00001191 usage();
1192 }
1193
1194 // Tokenize more
1195 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001196 }
1197 }
Mark Slee2c44d202007-05-16 02:18:07 +00001198
Jake Farrell2fd8a152012-09-29 00:26:36 +00001199 // display help
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001200 if ((strcmp(argv[argc - 1], "-help") == 0) || (strcmp(argv[argc - 1], "--help") == 0)) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001201 help();
1202 }
1203
David Reissdd08f6d2008-06-30 20:24:24 +00001204 // if you're asking for version, you have a right not to pass a file
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001205 if ((strcmp(argv[argc - 1], "-version") == 0) || (strcmp(argv[argc - 1], "--version") == 0)) {
David Reissdd08f6d2008-06-30 20:24:24 +00001206 version();
jfarrell8b1799f2014-04-10 22:06:11 -04001207 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001208 }
1209
Mark Sleef0712dc2006-10-25 19:03:57 +00001210 // Initialize global types
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001211 g_type_void = new t_base_type("void", t_base_type::TYPE_VOID);
Mark Sleef0712dc2006-10-25 19:03:57 +00001212 g_type_string = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Slee8d725a22007-04-13 01:57:12 +00001213 g_type_binary = new t_base_type("string", t_base_type::TYPE_STRING);
1214 ((t_base_type*)g_type_binary)->set_binary(true);
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001215 g_type_slist = new t_base_type("string", t_base_type::TYPE_STRING);
Mark Sleeb6200d82007-01-19 19:14:36 +00001216 ((t_base_type*)g_type_slist)->set_string_list(true);
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001217 g_type_bool = new t_base_type("bool", t_base_type::TYPE_BOOL);
Jens Geyer40c28d32015-10-20 23:13:02 +02001218 g_type_i8 = new t_base_type("i8", t_base_type::TYPE_I8);
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001219 g_type_i16 = new t_base_type("i16", t_base_type::TYPE_I16);
1220 g_type_i32 = new t_base_type("i32", t_base_type::TYPE_I32);
1221 g_type_i64 = new t_base_type("i64", t_base_type::TYPE_I64);
Mark Sleef0712dc2006-10-25 19:03:57 +00001222 g_type_double = new t_base_type("double", t_base_type::TYPE_DOUBLE);
Mark Sleee8540632006-05-30 09:24:40 +00001223
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +01001224 if (g_audit) {
Ben Craig262cfb42015-07-08 20:37:15 -05001225 // Audit operation
Mark Slee31985722006-05-24 21:45:31 +00001226
Ben Craig262cfb42015-07-08 20:37:15 -05001227 if (old_input_file.empty()) {
1228 fprintf(stderr, "Missing file name of old thrift file for audit\n");
1229 usage();
1230 }
David Reiss9cc2c132008-02-27 01:54:47 +00001231
Ben Craig262cfb42015-07-08 20:37:15 -05001232 char new_thrift_file_rp[THRIFT_PATH_MAX];
1233 if (argv[i] == NULL) {
1234 fprintf(stderr, "Missing file name of new thrift file for audit\n");
1235 usage();
1236 }
Nobuaki Sukegawad479e232016-02-28 11:28:19 +09001237 // cppcheck-suppress uninitvar
Ben Craig262cfb42015-07-08 20:37:15 -05001238 if (saferealpath(argv[i], new_thrift_file_rp) == NULL) {
1239 failure("Could not open input file with realpath: %s", argv[i]);
1240 }
1241 string new_input_file(new_thrift_file_rp);
1242
1243 t_program new_program(new_input_file);
1244 t_program old_program(old_input_file);
1245
1246 audit(&new_program, &old_program, new_thrift_include_path, old_thrift_include_path);
1247
1248 } else {
1249 // Generate options
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +01001250
Ben Craig262cfb42015-07-08 20:37:15 -05001251 // You gotta generate something!
1252 if (generator_strings.empty()) {
1253 fprintf(stderr, "No output language(s) specified\n");
1254 usage();
1255 }
1256
1257 // Real-pathify it
1258 char rp[THRIFT_PATH_MAX];
1259 if (argv[i] == NULL) {
1260 fprintf(stderr, "Missing file name\n");
1261 usage();
1262 }
Nobuaki Sukegawad479e232016-02-28 11:28:19 +09001263 // cppcheck-suppress uninitvar
Ben Craig262cfb42015-07-08 20:37:15 -05001264 if (saferealpath(argv[i], rp) == NULL) {
1265 failure("Could not open input file with realpath: %s", argv[i]);
1266 }
1267 string input_file(rp);
1268
1269 // Instance of the global parse tree
1270 t_program* program = new t_program(input_file);
1271 if (out_path.size()) {
1272 program->set_out_path(out_path, out_path_is_absolute);
1273 }
1274
1275 // Compute the cpp include prefix.
1276 // infer this from the filename passed in
1277 string input_filename = argv[i];
1278 string include_prefix;
1279
1280 string::size_type last_slash = string::npos;
1281 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1282 include_prefix = input_filename.substr(0, last_slash);
1283 }
1284
1285 program->set_include_prefix(include_prefix);
1286
1287 // Parse it!
1288 parse(program, NULL);
1289
1290 // The current path is not really relevant when we are doing generation.
1291 // Reset the variable to make warning messages clearer.
1292 g_curpath = "generation";
1293 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1294 // That is what shows up during argument parsing.
1295 yylineno = 1;
1296
1297 // Generate it!
1298 generate(program, generator_strings);
1299 delete program;
1300 }
Mark Sleeb15a68b2006-06-07 06:46:24 +00001301
Mark Sleef0712dc2006-10-25 19:03:57 +00001302 // Clean up. Who am I kidding... this program probably orphans heap memory
1303 // all over the place, but who cares because it is about to exit and it is
1304 // all referenced and used by this wacky parse tree up until now anyways.
Mark Sleeb15a68b2006-06-07 06:46:24 +00001305
Mark Sleef0712dc2006-10-25 19:03:57 +00001306 delete g_type_void;
1307 delete g_type_string;
1308 delete g_type_bool;
Jens Geyer40c28d32015-10-20 23:13:02 +02001309 delete g_type_i8;
Mark Sleef0712dc2006-10-25 19:03:57 +00001310 delete g_type_i16;
1311 delete g_type_i32;
1312 delete g_type_i64;
1313 delete g_type_double;
Mark Slee31985722006-05-24 21:45:31 +00001314
1315 // Finished
Ben Craig262cfb42015-07-08 20:37:15 -05001316 if (g_return_failure && g_audit_fatal) {
1317 exit(2);
1318 }
1319 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001320 return 0;
1321}