blob: 510d69f471a4f83f7f4c58100cb543d578f8ae9d [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
Nobuaki Sukegawa11da87e2016-09-10 14:02:19 +090047#include "common.h"
Mark Slee31985722006-05-24 21:45:31 +000048#include "globals.h"
49
Ben Craige9576752013-10-11 08:19:16 -050050#include "platform.h"
Mark Slee31985722006-05-24 21:45:31 +000051#include "main.h"
52#include "parse/t_program.h"
Mark Sleef0712dc2006-10-25 19:03:57 +000053#include "parse/t_scope.h"
David Reissbbbbe882009-02-17 20:27:48 +000054#include "generate/t_generator.h"
Ben Craig262cfb42015-07-08 20:37:15 -050055#include "audit/t_audit.h"
Nobuaki Sukegawa11da87e2016-09-10 14:02:19 +090056#ifdef THRIFT_ENABLE_PLUGIN
57#include "plugin/plugin_output.h"
58#endif
Mark Slee31985722006-05-24 21:45:31 +000059
David Reissdd08f6d2008-06-30 20:24:24 +000060#include "version.h"
61
Mark Slee31985722006-05-24 21:45:31 +000062using namespace std;
63
Mark Sleef5377b32006-10-10 01:42:59 +000064/**
65 * Global program tree
66 */
Mark Slee31985722006-05-24 21:45:31 +000067t_program* g_program;
68
Mark Sleef5377b32006-10-10 01:42:59 +000069/**
Mark Sleef0712dc2006-10-25 19:03:57 +000070 * Global scope
71 */
72t_scope* g_scope;
73
74/**
75 * Parent scope to also parse types
76 */
77t_scope* g_parent_scope;
78
79/**
80 * Prefix for putting types in parent scope
81 */
82string g_parent_prefix;
83
84/**
85 * Parsing pass
86 */
87PARSE_MODE g_parse_mode;
88
89/**
90 * Current directory of file being parsed
91 */
92string g_curdir;
93
94/**
95 * Current file being parsed
96 */
97string g_curpath;
98
99/**
Martin Kraemer32c66e12006-11-09 00:06:36 +0000100 * Search path for inclusions
101 */
Mark Slee2329a832006-11-09 00:23:30 +0000102vector<string> g_incl_searchpath;
Martin Kraemer32c66e12006-11-09 00:06:36 +0000103
104/**
Mark Sleef5377b32006-10-10 01:42:59 +0000105 * Global debug state
106 */
Mark Slee31985722006-05-24 21:45:31 +0000107int g_debug = 0;
108
Mark Sleef5377b32006-10-10 01:42:59 +0000109/**
Bryan Duxburya145b4d2009-04-03 17:29:25 +0000110 * Strictness level
111 */
112int g_strict = 127;
113
114/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000115 * Warning level
116 */
117int g_warn = 1;
118
119/**
120 * Verbose output
121 */
122int g_verbose = 0;
123
124/**
Mark Sleef5377b32006-10-10 01:42:59 +0000125 * Global time string
126 */
Mark Slee31985722006-05-24 21:45:31 +0000127char* g_time_str;
128
Mark Slee31985722006-05-24 21:45:31 +0000129/**
David Reisscbd4bac2007-08-14 17:12:33 +0000130 * The last parsed doctext comment.
131 */
132char* g_doctext;
133
134/**
Jens Geyere8379b52014-01-25 00:59:45 +0100135 * The First doctext comment
136 */
137char* g_program_doctext_candidate;
Jens Geyere8379b52014-01-25 00:59:45 +0100138
David Reisscbd4bac2007-08-14 17:12:33 +0000139/**
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000140 * Whether or not negative field keys are accepted.
141 */
142int g_allow_neg_field_keys;
143
144/**
Roger Meier887ff752011-08-19 11:25:39 +0000145 * Whether or not 64-bit constants will generate a warning.
146 */
147int g_allow_64bit_consts = 0;
148
149/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000150 * Flags to control code generation
151 */
Mark Sleef0712dc2006-10-25 19:03:57 +0000152bool gen_recurse = false;
153
154/**
Ben Craig262cfb42015-07-08 20:37:15 -0500155 * Flags to control thrift audit
156 */
157bool g_audit = false;
158
159/**
160 * Flag to control return status
161 */
162bool g_return_failure = false;
163bool g_audit_fatal = true;
Nobuaki Sukegawa11da87e2016-09-10 14:02:19 +0900164bool g_generator_failure = false;
Ben Craig262cfb42015-07-08 20:37:15 -0500165
166/**
Ben Craige9576752013-10-11 08:19:16 -0500167 * Win32 doesn't have realpath, so use fallback implementation in that case,
David Reiss204420f2008-01-11 20:59:03 +0000168 * otherwise this just calls through to realpath
169 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100170char* saferealpath(const char* path, char* resolved_path) {
Ben Craige9576752013-10-11 08:19:16 -0500171#ifdef _WIN32
David Reiss204420f2008-01-11 20:59:03 +0000172 char buf[MAX_PATH];
173 char* basename;
174 DWORD len = GetFullPathName(path, MAX_PATH, buf, &basename);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100175 if (len == 0 || len > MAX_PATH - 1) {
David Reiss204420f2008-01-11 20:59:03 +0000176 strcpy(resolved_path, path);
177 } else {
David Reiss204420f2008-01-11 20:59:03 +0000178 strcpy(resolved_path, buf);
179 }
Bryan Duxbury0137af62010-04-22 21:21:46 +0000180
181 // Replace backslashes with forward slashes so the
182 // rest of the code behaves correctly.
183 size_t resolved_len = strlen(resolved_path);
184 for (size_t i = 0; i < resolved_len; i++) {
185 if (resolved_path[i] == '\\') {
186 resolved_path[i] = '/';
187 }
188 }
David Reiss204420f2008-01-11 20:59:03 +0000189 return resolved_path;
190#else
191 return realpath(path, resolved_path);
192#endif
193}
194
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100195bool check_is_directory(const char* dir_name) {
Ben Craige9576752013-10-11 08:19:16 -0500196#ifdef _WIN32
Roger Meier061d4a22012-10-07 11:51:00 +0000197 DWORD attributes = ::GetFileAttributesA(dir_name);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100198 if (attributes == INVALID_FILE_ATTRIBUTES) {
199 fprintf(stderr,
200 "Output directory %s is unusable: GetLastError() = %ld\n",
201 dir_name,
202 GetLastError());
Roger Meier061d4a22012-10-07 11:51:00 +0000203 return false;
204 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100205 if ((attributes & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) {
Roger Meier061d4a22012-10-07 11:51:00 +0000206 fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name);
207 return false;
208 }
209 return true;
210#else
211 struct stat sb;
212 if (stat(dir_name, &sb) < 0) {
213 fprintf(stderr, "Output directory %s is unusable: %s\n", dir_name, strerror(errno));
214 return false;
215 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100216 if (!S_ISDIR(sb.st_mode)) {
Roger Meier061d4a22012-10-07 11:51:00 +0000217 fprintf(stderr, "Output directory %s exists but is not a directory\n", dir_name);
218 return false;
219 }
220 return true;
221#endif
222}
David Reiss204420f2008-01-11 20:59:03 +0000223
224/**
Mark Slee31985722006-05-24 21:45:31 +0000225 * Report an error to the user. This is called yyerror for historical
226 * reasons (lex and yacc expect the error reporting routine to be called
227 * this). Call this function to report any errors to the user.
228 * yyerror takes printf style arguments.
229 *
230 * @param fmt C format string followed by additional arguments
231 */
David Reiss0babe402008-06-10 22:56:12 +0000232void yyerror(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000233 va_list args;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100234 fprintf(stderr, "[ERROR:%s:%d] (last token was '%s')\n", g_curpath.c_str(), yylineno, yytext);
Mark Slee31985722006-05-24 21:45:31 +0000235
236 va_start(args, fmt);
237 vfprintf(stderr, fmt, args);
238 va_end(args);
239
240 fprintf(stderr, "\n");
241}
242
243/**
244 * Prints a debug message from the parser.
245 *
246 * @param fmt C format string followed by additional arguments
247 */
David Reiss0babe402008-06-10 22:56:12 +0000248void pdebug(const char* fmt, ...) {
Mark Slee31985722006-05-24 21:45:31 +0000249 if (g_debug == 0) {
250 return;
251 }
252 va_list args;
Mark Slee30152872006-11-28 01:24:07 +0000253 printf("[PARSE:%d] ", yylineno);
Mark Sleef0712dc2006-10-25 19:03:57 +0000254 va_start(args, fmt);
255 vprintf(fmt, args);
256 va_end(args);
257 printf("\n");
258}
259
260/**
261 * Prints a verbose output mode message
262 *
263 * @param fmt C format string followed by additional arguments
264 */
David Reiss0babe402008-06-10 22:56:12 +0000265void pverbose(const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000266 if (g_verbose == 0) {
267 return;
268 }
269 va_list args;
270 va_start(args, fmt);
271 vprintf(fmt, args);
272 va_end(args);
273}
274
275/**
276 * Prints a warning message
277 *
278 * @param fmt C format string followed by additional arguments
279 */
David Reiss0babe402008-06-10 22:56:12 +0000280void pwarning(int level, const char* fmt, ...) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000281 if (g_warn < level) {
282 return;
283 }
284 va_list args;
285 printf("[WARNING:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000286 va_start(args, fmt);
287 vprintf(fmt, args);
288 va_end(args);
289 printf("\n");
290}
291
292/**
293 * Prints a failure message and exits
294 *
295 * @param fmt C format string followed by additional arguments
296 */
Mark Slee30152872006-11-28 01:24:07 +0000297void failure(const char* fmt, ...) {
Mark Slee2c44d202007-05-16 02:18:07 +0000298 va_list args;
Mark Sleef0712dc2006-10-25 19:03:57 +0000299 fprintf(stderr, "[FAILURE:%s:%d] ", g_curpath.c_str(), yylineno);
Mark Slee31985722006-05-24 21:45:31 +0000300 va_start(args, fmt);
301 vfprintf(stderr, fmt, args);
302 va_end(args);
303 printf("\n");
304 exit(1);
305}
306
307/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000308 * Converts a string filename into a thrift program name
309 */
310string program_name(string filename) {
311 string::size_type slash = filename.rfind("/");
312 if (slash != string::npos) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100313 filename = filename.substr(slash + 1);
Mark Sleef0712dc2006-10-25 19:03:57 +0000314 }
315 string::size_type dot = filename.rfind(".");
316 if (dot != string::npos) {
317 filename = filename.substr(0, dot);
318 }
319 return filename;
320}
321
322/**
323 * Gets the directory path of a filename
324 */
325string directory_name(string filename) {
326 string::size_type slash = filename.rfind("/");
327 // No slash, just use the current directory
328 if (slash == string::npos) {
329 return ".";
330 }
331 return filename.substr(0, slash);
332}
333
334/**
335 * Finds the appropriate file path for the given filename
336 */
337string include_file(string filename) {
338 // Absolute path? Just try that
Martin Kraemer32c66e12006-11-09 00:06:36 +0000339 if (filename[0] == '/') {
340 // Realpath!
Ben Craige9576752013-10-11 08:19:16 -0500341 char rp[THRIFT_PATH_MAX];
Nobuaki Sukegawad479e232016-02-28 11:28:19 +0900342 // cppcheck-suppress uninitvar
David Reiss204420f2008-01-11 20:59:03 +0000343 if (saferealpath(filename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000344 pwarning(0, "Cannot open include file %s\n", filename.c_str());
345 return std::string();
346 }
Mark Slee2c44d202007-05-16 02:18:07 +0000347
348 // Stat this file
Martin Kraemer32c66e12006-11-09 00:06:36 +0000349 struct stat finfo;
350 if (stat(rp, &finfo) == 0) {
351 return rp;
352 }
353 } else { // relative path, start searching
354 // new search path with current dir global
355 vector<string> sp = g_incl_searchpath;
356 sp.insert(sp.begin(), g_curdir);
Mark Slee2c44d202007-05-16 02:18:07 +0000357
Martin Kraemer32c66e12006-11-09 00:06:36 +0000358 // iterate through paths
359 vector<string>::iterator it;
360 for (it = sp.begin(); it != sp.end(); it++) {
361 string sfilename = *(it) + "/" + filename;
Mark Slee2c44d202007-05-16 02:18:07 +0000362
Martin Kraemer32c66e12006-11-09 00:06:36 +0000363 // Realpath!
Ben Craige9576752013-10-11 08:19:16 -0500364 char rp[THRIFT_PATH_MAX];
Nobuaki Sukegawad479e232016-02-28 11:28:19 +0900365 // cppcheck-suppress uninitvar
David Reiss204420f2008-01-11 20:59:03 +0000366 if (saferealpath(sfilename.c_str(), rp) == NULL) {
Martin Kraemer32c66e12006-11-09 00:06:36 +0000367 continue;
368 }
Mark Slee2c44d202007-05-16 02:18:07 +0000369
Martin Kraemer32c66e12006-11-09 00:06:36 +0000370 // Stat this files
371 struct stat finfo;
372 if (stat(rp, &finfo) == 0) {
373 return rp;
374 }
375 }
Mark Sleef0712dc2006-10-25 19:03:57 +0000376 }
Mark Slee2c44d202007-05-16 02:18:07 +0000377
Mark Sleef0712dc2006-10-25 19:03:57 +0000378 // Uh oh
379 pwarning(0, "Could not find include file %s\n", filename.c_str());
380 return std::string();
381}
382
383/**
David Reisscbd4bac2007-08-14 17:12:33 +0000384 * Clears any previously stored doctext string.
385 * Also prints a warning if we are discarding information.
386 */
387void clear_doctext() {
388 if (g_doctext != NULL) {
389 pwarning(2, "Uncaptured doctext at on line %d.", g_doctext_lineno);
390 }
391 free(g_doctext);
392 g_doctext = NULL;
393}
394
395/**
Jens Geyere8379b52014-01-25 00:59:45 +0100396 * Reset program doctext information after processing a file
397 */
398void reset_program_doctext_info() {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100399 if (g_program_doctext_candidate != NULL) {
Jens Geyere8379b52014-01-25 00:59:45 +0100400 free(g_program_doctext_candidate);
401 g_program_doctext_candidate = NULL;
402 }
403 g_program_doctext_lineno = 0;
404 g_program_doctext_status = INVALID;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100405 pdebug("%s", "program doctext set to INVALID");
Jens Geyere8379b52014-01-25 00:59:45 +0100406}
407
408/**
409 * We are sure the program doctext candidate is really the program doctext.
410 */
411void declare_valid_program_doctext() {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100412 if ((g_program_doctext_candidate != NULL) && (g_program_doctext_status == STILL_CANDIDATE)) {
Roger Meier4f4b15b2014-11-05 16:51:04 +0100413 g_program_doctext_status = ABSOLUTELY_SURE;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100414 pdebug("%s", "program doctext set to ABSOLUTELY_SURE");
Jens Geyer813749d2014-01-31 23:42:57 +0100415 } else {
Roger Meier4f4b15b2014-11-05 16:51:04 +0100416 g_program_doctext_status = NO_PROGRAM_DOCTEXT;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100417 pdebug("%s", "program doctext set to NO_PROGRAM_DOCTEXT");
Jens Geyere8379b52014-01-25 00:59:45 +0100418 }
419}
420
421/**
David Reiss1ac05802007-07-30 22:00:27 +0000422 * Cleans up text commonly found in doxygen-like comments
423 *
424 * Warning: if you mix tabs and spaces in a non-uniform way,
425 * you will get what you deserve.
426 */
427char* clean_up_doctext(char* doctext) {
428 // Convert to C++ string, and remove Windows's carriage returns.
429 string docstring = doctext;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100430 docstring.erase(remove(docstring.begin(), docstring.end(), '\r'), docstring.end());
David Reiss1ac05802007-07-30 22:00:27 +0000431
432 // Separate into lines.
433 vector<string> lines;
434 string::size_type pos = string::npos;
435 string::size_type last;
436 while (true) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100437 last = (pos == string::npos) ? 0 : pos + 1;
David Reiss1ac05802007-07-30 22:00:27 +0000438 pos = docstring.find('\n', last);
439 if (pos == string::npos) {
440 // First bit of cleaning. If the last line is only whitespace, drop it.
441 string::size_type nonwhite = docstring.find_first_not_of(" \t", last);
442 if (nonwhite != string::npos) {
443 lines.push_back(docstring.substr(last));
444 }
445 break;
446 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100447 lines.push_back(docstring.substr(last, pos - last));
David Reiss1ac05802007-07-30 22:00:27 +0000448 }
449
450 // A very profound docstring.
451 if (lines.empty()) {
452 return NULL;
453 }
454
455 // Clear leading whitespace from the first line.
456 pos = lines.front().find_first_not_of(" \t");
457 lines.front().erase(0, pos);
458
459 // If every nonblank line after the first has the same number of spaces/tabs,
460 // then a star, remove them.
461 bool have_prefix = true;
462 bool found_prefix = false;
463 string::size_type prefix_len = 0;
464 vector<string>::iterator l_iter;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100465 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000466 if (l_iter->empty()) {
467 continue;
468 }
469
470 pos = l_iter->find_first_not_of(" \t");
471 if (!found_prefix) {
472 if (pos != string::npos) {
473 if (l_iter->at(pos) == '*') {
474 found_prefix = true;
475 prefix_len = pos;
476 } else {
477 have_prefix = false;
478 break;
479 }
480 } else {
481 // Whitespace-only line. Truncate it.
482 l_iter->clear();
483 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100484 } else if (l_iter->size() > pos && l_iter->at(pos) == '*' && pos == prefix_len) {
David Reiss1ac05802007-07-30 22:00:27 +0000485 // Business as usual.
486 } else if (pos == string::npos) {
487 // Whitespace-only line. Let's truncate it for them.
488 l_iter->clear();
489 } else {
490 // The pattern has been broken.
491 have_prefix = false;
492 break;
493 }
494 }
495
496 // If our prefix survived, delete it from every line.
497 if (have_prefix) {
498 // Get the star too.
499 prefix_len++;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100500 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000501 l_iter->erase(0, prefix_len);
502 }
503 }
504
505 // Now delete the minimum amount of leading whitespace from each line.
506 prefix_len = string::npos;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100507 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000508 if (l_iter->empty()) {
509 continue;
510 }
511 pos = l_iter->find_first_not_of(" \t");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100512 if (pos != string::npos && (prefix_len == string::npos || pos < prefix_len)) {
David Reiss1ac05802007-07-30 22:00:27 +0000513 prefix_len = pos;
514 }
515 }
516
517 // If our prefix survived, delete it from every line.
518 if (prefix_len != string::npos) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100519 for (l_iter = lines.begin() + 1; l_iter != lines.end(); ++l_iter) {
David Reiss1ac05802007-07-30 22:00:27 +0000520 l_iter->erase(0, prefix_len);
521 }
522 }
523
524 // Remove trailing whitespace from every line.
525 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
526 pos = l_iter->find_last_not_of(" \t");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100527 if (pos != string::npos && pos != l_iter->length() - 1) {
528 l_iter->erase(pos + 1);
David Reiss1ac05802007-07-30 22:00:27 +0000529 }
530 }
531
532 // If the first line is empty, remove it.
533 // Don't do this earlier because a lot of steps skip the first line.
534 if (lines.front().empty()) {
535 lines.erase(lines.begin());
536 }
537
538 // Now rejoin the lines and copy them back into doctext.
539 docstring.clear();
540 for (l_iter = lines.begin(); l_iter != lines.end(); ++l_iter) {
541 docstring += *l_iter;
542 docstring += '\n';
543 }
544
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100545 // assert(docstring.length() <= strlen(doctext)); may happen, see THRIFT-1755
546 if (docstring.length() <= strlen(doctext)) {
Jens Geyer8cd3efe2013-09-16 22:17:52 +0200547 strcpy(doctext, docstring.c_str());
548 } else {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100549 free(doctext); // too short
Jens Geyer8cd3efe2013-09-16 22:17:52 +0200550 doctext = strdup(docstring.c_str());
551 }
David Reiss1ac05802007-07-30 22:00:27 +0000552 return doctext;
553}
554
555/** Set to true to debug docstring parsing */
556static bool dump_docs = false;
557
558/**
559 * Dumps docstrings to stdout
David Reisscdffe262007-08-14 17:12:31 +0000560 * Only works for top-level definitions and the whole program doc
561 * (i.e., not enum constants, struct fields, or functions.
David Reiss1ac05802007-07-30 22:00:27 +0000562 */
563void dump_docstrings(t_program* program) {
David Reisscdffe262007-08-14 17:12:31 +0000564 string progdoc = program->get_doc();
David Reissc2532a92007-07-30 23:46:11 +0000565 if (!progdoc.empty()) {
566 printf("Whole program doc:\n%s\n", progdoc.c_str());
567 }
David Reiss1ac05802007-07-30 22:00:27 +0000568 const vector<t_typedef*>& typedefs = program->get_typedefs();
569 vector<t_typedef*>::const_iterator t_iter;
570 for (t_iter = typedefs.begin(); t_iter != typedefs.end(); ++t_iter) {
571 t_typedef* td = *t_iter;
572 if (td->has_doc()) {
David Reisscdffe262007-08-14 17:12:31 +0000573 printf("typedef %s:\n%s\n", td->get_name().c_str(), td->get_doc().c_str());
574 }
575 }
576 const vector<t_enum*>& enums = program->get_enums();
577 vector<t_enum*>::const_iterator e_iter;
578 for (e_iter = enums.begin(); e_iter != enums.end(); ++e_iter) {
579 t_enum* en = *e_iter;
580 if (en->has_doc()) {
581 printf("enum %s:\n%s\n", en->get_name().c_str(), en->get_doc().c_str());
582 }
583 }
584 const vector<t_const*>& consts = program->get_consts();
585 vector<t_const*>::const_iterator c_iter;
586 for (c_iter = consts.begin(); c_iter != consts.end(); ++c_iter) {
587 t_const* co = *c_iter;
588 if (co->has_doc()) {
589 printf("const %s:\n%s\n", co->get_name().c_str(), co->get_doc().c_str());
590 }
591 }
592 const vector<t_struct*>& structs = program->get_structs();
593 vector<t_struct*>::const_iterator s_iter;
594 for (s_iter = structs.begin(); s_iter != structs.end(); ++s_iter) {
595 t_struct* st = *s_iter;
596 if (st->has_doc()) {
597 printf("struct %s:\n%s\n", st->get_name().c_str(), st->get_doc().c_str());
598 }
599 }
600 const vector<t_struct*>& xceptions = program->get_xceptions();
601 vector<t_struct*>::const_iterator x_iter;
602 for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
603 t_struct* xn = *x_iter;
604 if (xn->has_doc()) {
605 printf("xception %s:\n%s\n", xn->get_name().c_str(), xn->get_doc().c_str());
606 }
607 }
608 const vector<t_service*>& services = program->get_services();
609 vector<t_service*>::const_iterator v_iter;
610 for (v_iter = services.begin(); v_iter != services.end(); ++v_iter) {
611 t_service* sv = *v_iter;
612 if (sv->has_doc()) {
613 printf("service %s:\n%s\n", sv->get_name().c_str(), sv->get_doc().c_str());
David Reiss1ac05802007-07-30 22:00:27 +0000614 }
615 }
616}
617
618/**
Jens Geyer6fe77e82014-03-16 16:48:53 +0200619 * Emits a warning on list<byte>, binary type is typically a much better choice.
620 */
621void check_for_list_of_bytes(t_type* list_elem_type) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100622 if ((g_parse_mode == PROGRAM) && (list_elem_type != NULL) && list_elem_type->is_base_type()) {
Jens Geyer6fe77e82014-03-16 16:48:53 +0200623 t_base_type* tbase = (t_base_type*)list_elem_type;
Jens Geyer40c28d32015-10-20 23:13:02 +0200624 if (tbase->get_base() == t_base_type::TYPE_I8) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100625 pwarning(1, "Consider using the more efficient \"binary\" type instead of \"list<byte>\".");
Jens Geyer6fe77e82014-03-16 16:48:53 +0200626 }
627 }
628}
629
Jens Geyer40c28d32015-10-20 23:13:02 +0200630static bool g_byte_warning_emitted = false;
631
632/**
633 * Emits a one-time warning on byte type, promoting the new i8 type instead
634 */
635void emit_byte_type_warning() {
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +0100636 if (!g_byte_warning_emitted) {
637 pwarning(1,
638 "The \"byte\" type is a compatibility alias for \"i8\". Use \"i8\" to emphasize the "
639 "signedness of this type.\n");
640 g_byte_warning_emitted = true;
641 }
Jens Geyer40c28d32015-10-20 23:13:02 +0200642}
643
David Reiss18bf22d2007-08-28 20:49:17 +0000644/**
Jens Geyer73880372015-11-14 15:21:57 +0100645 * Prints deprecation notice for old NS declarations that are no longer supported
646 * If new_form is NULL, old_form is assumed to be a language identifier, such as "cpp"
647 * If new_form is not NULL, both arguments are used exactly as given
648 */
Jens Geyereb5f1172015-12-11 20:58:45 +0100649void error_unsupported_namespace_decl(const char* old_form, const char* new_form) {
650 const char* remainder = "";
Jens Geyer73880372015-11-14 15:21:57 +0100651 if( new_form == NULL) {
652 new_form = old_form;
653 remainder = "_namespace";
654 }
655 failure("Unsupported declaration '%s%s'. Use 'namespace %s' instead.", old_form, remainder, new_form);
656}
657
658/**
David Reissdd08f6d2008-06-30 20:24:24 +0000659 * Prints the version number
660 */
661void version() {
Bryan Duxburya1e268c2010-05-03 21:33:00 +0000662 printf("Thrift version %s\n", THRIFT_VERSION);
David Reissdd08f6d2008-06-30 20:24:24 +0000663}
664
665/**
Jake Farrell2fd8a152012-09-29 00:26:36 +0000666 * Display the usage message and then exit with an error code.
Mark Slee31985722006-05-24 21:45:31 +0000667 */
668void usage() {
Jake Farrell2fd8a152012-09-29 00:26:36 +0000669 fprintf(stderr, "Usage: thrift [options] file\n\n");
670 fprintf(stderr, "Use thrift -help for a list of options\n");
671 exit(1);
672}
673
674/**
675 * Diplays the help message and then exits with an error code.
676 */
677void help() {
Mark Sleeb15a68b2006-06-07 06:46:24 +0000678 fprintf(stderr, "Usage: thrift [options] file\n");
679 fprintf(stderr, "Options:\n");
David Reissdd08f6d2008-06-30 20:24:24 +0000680 fprintf(stderr, " -version Print the compiler version\n");
dweatherford65b70752007-10-31 02:18:14 +0000681 fprintf(stderr, " -o dir Set the output directory for gen-* packages\n");
682 fprintf(stderr, " (default: current directory)\n");
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000683 fprintf(stderr, " -out dir Set the ouput location for generated files.\n");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100684 fprintf(stderr, " (no gen-* folder will be created)\n");
David Reissd779cbe2007-08-31 01:42:55 +0000685 fprintf(stderr, " -I dir Add a directory to the list of directories\n");
Mark Slee227ac2c2007-03-07 05:46:50 +0000686 fprintf(stderr, " searched for include directives\n");
Mark Slee2329a832006-11-09 00:23:30 +0000687 fprintf(stderr, " -nowarn Suppress all compiler warnings (BAD!)\n");
688 fprintf(stderr, " -strict Strict compiler warnings on\n");
689 fprintf(stderr, " -v[erbose] Verbose mode\n");
690 fprintf(stderr, " -r[ecurse] Also generate included files\n");
691 fprintf(stderr, " -debug Parse debug trace to stdout\n");
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100692 fprintf(stderr,
693 " --allow-neg-keys Allow negative field keys (Used to "
Bryan Duxburyc7206a42011-08-17 23:17:04 +0000694 "preserve protocol\n");
695 fprintf(stderr, " compatibility with older .thrift files)\n");
Roger Meier887ff752011-08-19 11:25:39 +0000696 fprintf(stderr, " --allow-64bit-consts Do not print warnings about using 64-bit constants\n");
David Reissbd0db882008-02-27 01:54:51 +0000697 fprintf(stderr, " --gen STR Generate code with a dynamically-registered generator.\n");
Jens Geyere8c51ed2014-04-18 02:27:57 +0200698 fprintf(stderr, " STR has the form language[:key1=val1[,key2[,key3=val3]]].\n");
David Reissbd0db882008-02-27 01:54:51 +0000699 fprintf(stderr, " Keys and values are options passed to the generator.\n");
700 fprintf(stderr, " Many options will not require values.\n");
701 fprintf(stderr, "\n");
Ben Craig262cfb42015-07-08 20:37:15 -0500702 fprintf(stderr, "Options related to audit operation\n");
703 fprintf(stderr, " --audit OldFile Old Thrift file to be audited with 'file'\n");
704 fprintf(stderr, " -Iold dir Add a directory to the list of directories\n");
705 fprintf(stderr, " searched for include directives for old thrift file\n");
706 fprintf(stderr, " -Inew dir Add a directory to the list of directories\n");
707 fprintf(stderr, " searched for include directives for new thrift file\n");
708 fprintf(stderr, "\n");
David Reissbd0db882008-02-27 01:54:51 +0000709 fprintf(stderr, "Available generators (and options):\n");
710
711 t_generator_registry::gen_map_t gen_map = t_generator_registry::get_generator_map();
712 t_generator_registry::gen_map_t::iterator iter;
713 for (iter = gen_map.begin(); iter != gen_map.end(); ++iter) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100714 fprintf(stderr,
715 " %s (%s):\n",
716 iter->second->get_short_name().c_str(),
717 iter->second->get_long_name().c_str());
David Reissbd0db882008-02-27 01:54:51 +0000718 fprintf(stderr, "%s", iter->second->get_documentation().c_str());
719 }
Mark Slee31985722006-05-24 21:45:31 +0000720 exit(1);
721}
722
723/**
Mark Slee30152872006-11-28 01:24:07 +0000724 * You know, when I started working on Thrift I really thought it wasn't going
725 * to become a programming language because it was just a generator and it
726 * wouldn't need runtime type information and all that jazz. But then we
727 * decided to add constants, and all of a sudden that means runtime type
728 * validation and inference, except the "runtime" is the code generator
David Reiss3bb5e052010-01-25 19:31:31 +0000729 * runtime.
Mark Slee30152872006-11-28 01:24:07 +0000730 */
731void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
732 if (type->is_void()) {
733 throw "type error: cannot declare a void const: " + name;
734 }
735
736 if (type->is_base_type()) {
737 t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
738 switch (tbase) {
739 case t_base_type::TYPE_STRING:
740 if (value->get_type() != t_const_value::CV_STRING) {
741 throw "type error: const \"" + name + "\" was declared as string";
742 }
743 break;
744 case t_base_type::TYPE_BOOL:
745 if (value->get_type() != t_const_value::CV_INTEGER) {
746 throw "type error: const \"" + name + "\" was declared as bool";
747 }
748 break;
Jens Geyer40c28d32015-10-20 23:13:02 +0200749 case t_base_type::TYPE_I8:
Mark Slee30152872006-11-28 01:24:07 +0000750 if (value->get_type() != t_const_value::CV_INTEGER) {
751 throw "type error: const \"" + name + "\" was declared as byte";
752 }
753 break;
754 case t_base_type::TYPE_I16:
755 if (value->get_type() != t_const_value::CV_INTEGER) {
756 throw "type error: const \"" + name + "\" was declared as i16";
757 }
758 break;
759 case t_base_type::TYPE_I32:
760 if (value->get_type() != t_const_value::CV_INTEGER) {
761 throw "type error: const \"" + name + "\" was declared as i32";
762 }
763 break;
764 case t_base_type::TYPE_I64:
765 if (value->get_type() != t_const_value::CV_INTEGER) {
766 throw "type error: const \"" + name + "\" was declared as i64";
767 }
768 break;
769 case t_base_type::TYPE_DOUBLE:
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100770 if (value->get_type() != t_const_value::CV_INTEGER
771 && value->get_type() != t_const_value::CV_DOUBLE) {
Mark Slee30152872006-11-28 01:24:07 +0000772 throw "type error: const \"" + name + "\" was declared as double";
773 }
774 break;
775 default:
David Reissdd7796f2007-08-28 21:09:06 +0000776 throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase) + name;
Mark Slee30152872006-11-28 01:24:07 +0000777 }
778 } else if (type->is_enum()) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000779 if (value->get_type() != t_const_value::CV_IDENTIFIER) {
Mark Slee30152872006-11-28 01:24:07 +0000780 throw "type error: const \"" + name + "\" was declared as enum";
781 }
Bryan Duxbury2d804702009-12-18 19:41:11 +0000782
Bryan Duxbury1606f252010-11-24 00:25:57 +0000783 // see if there's a dot in the identifier
784 std::string name_portion = value->get_identifier_name();
785
Bryan Duxbury2d804702009-12-18 19:41:11 +0000786 const vector<t_enum_value*>& enum_values = ((t_enum*)type)->get_constants();
787 vector<t_enum_value*>::const_iterator c_iter;
788 bool found = false;
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000789
Bryan Duxbury1606f252010-11-24 00:25:57 +0000790 for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
Bryan Duxbury9f0a7862010-09-12 14:38:36 +0000791 if ((*c_iter)->get_name() == name_portion) {
Bryan Duxbury2d804702009-12-18 19:41:11 +0000792 found = true;
793 break;
794 }
795 }
796 if (!found) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100797 throw "type error: const " + name + " was declared as type " + type->get_name()
798 + " which is an enum, but " + value->get_identifier()
799 + " is not a valid value for that enum";
Bryan Duxbury2d804702009-12-18 19:41:11 +0000800 }
Mark Slee30152872006-11-28 01:24:07 +0000801 } else if (type->is_struct() || type->is_xception()) {
802 if (value->get_type() != t_const_value::CV_MAP) {
803 throw "type error: const \"" + name + "\" was declared as struct/xception";
804 }
805 const vector<t_field*>& fields = ((t_struct*)type)->get_members();
806 vector<t_field*>::const_iterator f_iter;
807
808 const map<t_const_value*, t_const_value*>& val = value->get_map();
809 map<t_const_value*, t_const_value*>::const_iterator v_iter;
810 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
811 if (v_iter->first->get_type() != t_const_value::CV_STRING) {
812 throw "type error: " + name + " struct key must be string";
813 }
814 t_type* field_type = NULL;
815 for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
816 if ((*f_iter)->get_name() == v_iter->first->get_string()) {
817 field_type = (*f_iter)->get_type();
818 }
819 }
820 if (field_type == NULL) {
821 throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
822 }
823
824 validate_const_rec(name + "." + v_iter->first->get_string(), field_type, v_iter->second);
825 }
826 } else if (type->is_map()) {
827 t_type* k_type = ((t_map*)type)->get_key_type();
828 t_type* v_type = ((t_map*)type)->get_val_type();
829 const map<t_const_value*, t_const_value*>& val = value->get_map();
830 map<t_const_value*, t_const_value*>::const_iterator v_iter;
831 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
832 validate_const_rec(name + "<key>", k_type, v_iter->first);
833 validate_const_rec(name + "<val>", v_type, v_iter->second);
Mark Slee2c44d202007-05-16 02:18:07 +0000834 }
Mark Slee30152872006-11-28 01:24:07 +0000835 } else if (type->is_list() || type->is_set()) {
836 t_type* e_type;
837 if (type->is_list()) {
838 e_type = ((t_list*)type)->get_elem_type();
839 } else {
840 e_type = ((t_set*)type)->get_elem_type();
841 }
842 const vector<t_const_value*>& val = value->get_list();
843 vector<t_const_value*>::const_iterator v_iter;
844 for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
845 validate_const_rec(name + "<elem>", e_type, *v_iter);
846 }
847 }
848}
849
850/**
Jens Geyer12c09f42013-08-25 14:16:27 +0200851 * Check simple identifier names
852 * It's easier to do it this way instead of rewriting the whole grammar etc.
853 */
854void validate_simple_identifier(const char* identifier) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100855 string name(identifier);
856 if (name.find(".") != string::npos) {
Jens Geyer12c09f42013-08-25 14:16:27 +0200857 yyerror("Identifier %s can't have a dot.", identifier);
858 exit(1);
859 }
860}
861
862/**
Mark Slee30152872006-11-28 01:24:07 +0000863 * Check the type of the parsed const information against its declared type
864 */
865void validate_const_type(t_const* c) {
866 validate_const_rec(c->get_name(), c->get_type(), c->get_value());
867}
868
869/**
Mark Slee7ff32452007-02-01 05:26:18 +0000870 * Check the type of a default value assigned to a field.
871 */
872void validate_field_value(t_field* field, t_const_value* cv) {
873 validate_const_rec(field->get_name(), field->get_type(), cv);
874}
875
876/**
Mark Slee91f2b7b2008-01-31 01:49:16 +0000877 * Check that all the elements of a throws block are actually exceptions.
878 */
879bool validate_throws(t_struct* throws) {
880 const vector<t_field*>& members = throws->get_members();
881 vector<t_field*>::const_iterator m_iter;
882 for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
Bryan Duxburycff83572011-08-24 20:53:03 +0000883 if (!t_generator::get_true_type((*m_iter)->get_type())->is_xception()) {
Mark Slee91f2b7b2008-01-31 01:49:16 +0000884 return false;
885 }
886 }
887 return true;
888}
889
890/**
Jens Geyer03d49442013-09-04 22:34:41 +0200891 * Skips UTF-8 BOM if there is one
892 */
893bool skip_utf8_bom(FILE* f) {
894
895 // pretty straightforward, but works
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100896 if (fgetc(f) == 0xEF) {
897 if (fgetc(f) == 0xBB) {
898 if (fgetc(f) == 0xBF) {
Jens Geyer03d49442013-09-04 22:34:41 +0200899 return true;
Roger Meier4f4b15b2014-11-05 16:51:04 +0100900 }
901 }
902 }
903
904 rewind(f);
Jens Geyer03d49442013-09-04 22:34:41 +0200905 return false;
906}
907
908/**
Mark Sleef0712dc2006-10-25 19:03:57 +0000909 * Parses a program
910 */
Mark Slee2c44d202007-05-16 02:18:07 +0000911void parse(t_program* program, t_program* parent_program) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000912 // Get scope file path
913 string path = program->get_path();
Mark Slee2c44d202007-05-16 02:18:07 +0000914
Mark Sleef0712dc2006-10-25 19:03:57 +0000915 // Set current dir global, which is used in the include_file function
916 g_curdir = directory_name(path);
917 g_curpath = path;
918
919 // Open the file
Jens Geyer03d49442013-09-04 22:34:41 +0200920 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000921 yyin = fopen(path.c_str(), "r");
922 if (yyin == 0) {
923 failure("Could not open input file: \"%s\"", path.c_str());
924 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100925 if (skip_utf8_bom(yyin))
Jens Geyer03d49442013-09-04 22:34:41 +0200926 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
Roger Meier4f4b15b2014-11-05 16:51:04 +0100927
Mark Sleef0712dc2006-10-25 19:03:57 +0000928 // Create new scope and scan for includes
929 pverbose("Scanning %s for includes\n", path.c_str());
Mark Slee2c44d202007-05-16 02:18:07 +0000930 g_parse_mode = INCLUDES;
Mark Sleef0712dc2006-10-25 19:03:57 +0000931 g_program = program;
932 g_scope = program->scope();
Mark Slee30152872006-11-28 01:24:07 +0000933 try {
Mark Slee36bfa2e2007-01-19 20:09:51 +0000934 yylineno = 1;
Mark Slee30152872006-11-28 01:24:07 +0000935 if (yyparse() != 0) {
936 failure("Parser error during include pass.");
937 }
938 } catch (string x) {
939 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000940 }
941 fclose(yyin);
942
943 // Recursively parse all the include programs
944 vector<t_program*>& includes = program->get_includes();
945 vector<t_program*>::iterator iter;
946 for (iter = includes.begin(); iter != includes.end(); ++iter) {
947 parse(*iter, program);
948 }
949
Jens Geyere8379b52014-01-25 00:59:45 +0100950 // reset program doctext status before parsing a new file
951 reset_program_doctext_info();
952
David Reiss204420f2008-01-11 20:59:03 +0000953 // Parse the program file
Mark Sleef0712dc2006-10-25 19:03:57 +0000954 g_parse_mode = PROGRAM;
955 g_program = program;
956 g_scope = program->scope();
957 g_parent_scope = (parent_program != NULL) ? parent_program->scope() : NULL;
958 g_parent_prefix = program->get_name() + ".";
959 g_curpath = path;
Jens Geyer03d49442013-09-04 22:34:41 +0200960
961 // Open the file
962 // skip UTF-8 BOM if there is one
Mark Sleef0712dc2006-10-25 19:03:57 +0000963 yyin = fopen(path.c_str(), "r");
964 if (yyin == 0) {
965 failure("Could not open input file: \"%s\"", path.c_str());
966 }
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100967 if (skip_utf8_bom(yyin))
Jens Geyer03d49442013-09-04 22:34:41 +0200968 pverbose("Skipped UTF-8 BOM at %s\n", path.c_str());
Roger Meier4f4b15b2014-11-05 16:51:04 +0100969
Mark Sleef0712dc2006-10-25 19:03:57 +0000970 pverbose("Parsing %s for types\n", path.c_str());
Mark Slee36bfa2e2007-01-19 20:09:51 +0000971 yylineno = 1;
David Reiss877237a2007-07-27 00:40:19 +0000972 try {
973 if (yyparse() != 0) {
974 failure("Parser error during types pass.");
975 }
976 } catch (string x) {
977 failure(x.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +0000978 }
979 fclose(yyin);
980}
981
982/**
983 * Generate code
984 */
David Reissbd0db882008-02-27 01:54:51 +0000985void generate(t_program* program, const vector<string>& generator_strings) {
Mark Sleef0712dc2006-10-25 19:03:57 +0000986 // Oooohh, recursive code generation, hot!!
987 if (gen_recurse) {
988 const vector<t_program*>& includes = program->get_includes();
989 for (size_t i = 0; i < includes.size(); ++i) {
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100990 // Propagate output path from parent to child programs
Bryan Duxburybdca9f62011-03-01 19:53:07 +0000991 includes[i]->set_out_path(program->get_out_path(), program->is_out_path_absolute());
Mark Slee5b743072007-11-13 04:00:29 +0000992
David Reissbd0db882008-02-27 01:54:51 +0000993 generate(includes[i], generator_strings);
Mark Sleef0712dc2006-10-25 19:03:57 +0000994 }
995 }
996
997 // Generate code!
998 try {
999 pverbose("Program: %s\n", program->get_path().c_str());
1000
David Reiss1ac05802007-07-30 22:00:27 +00001001 if (dump_docs) {
1002 dump_docstrings(program);
1003 }
David Reissbd0db882008-02-27 01:54:51 +00001004
1005 vector<string>::const_iterator iter;
1006 for (iter = generator_strings.begin(); iter != generator_strings.end(); ++iter) {
1007 t_generator* generator = t_generator_registry::get_generator(program, *iter);
1008
1009 if (generator == NULL) {
Nobuaki Sukegawa11da87e2016-09-10 14:02:19 +09001010#ifdef THRIFT_ENABLE_PLUGIN
1011 switch (plugin_output::delegateToPlugin(program, *iter)) {
1012 case plugin_output::PLUGIN_NOT_FOUND:
1013 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
1014 g_generator_failure = true;
1015 break;
1016 case plugin_output::PLUGIN_FAILURE:
1017 pwarning(1, "Plugin generator for \"%s\" failed.\n", iter->c_str());
1018 g_generator_failure = true;
1019 break;
1020 case plugin_output::PLUGIN_SUCCEESS:
1021 break;
1022 default:
1023 assert(false);
1024 break;
1025 }
1026#else
David Reissbd0db882008-02-27 01:54:51 +00001027 pwarning(1, "Unable to get a generator for \"%s\".\n", iter->c_str());
Nobuaki Sukegawa11da87e2016-09-10 14:02:19 +09001028 g_generator_failure = true;
1029#endif
1030 } else if (generator) {
David Reissbd0db882008-02-27 01:54:51 +00001031 pverbose("Generating \"%s\"\n", iter->c_str());
1032 generator->generate_program();
David Reissc9342682008-03-27 21:39:49 +00001033 delete generator;
David Reissbd0db882008-02-27 01:54:51 +00001034 }
1035 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001036 } catch (string s) {
Jens Geyerd4722d92016-02-13 23:25:11 +01001037 failure("Error: %s\n", s.c_str());
Mark Sleef0712dc2006-10-25 19:03:57 +00001038 } catch (const char* exc) {
Jens Geyerd4722d92016-02-13 23:25:11 +01001039 failure("Error: %s\n", exc);
Mark Sleef0712dc2006-10-25 19:03:57 +00001040 }
Mark Sleef0712dc2006-10-25 19:03:57 +00001041}
1042
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +01001043void audit(t_program* new_program,
1044 t_program* old_program,
1045 string new_thrift_include_path,
1046 string old_thrift_include_path) {
Ben Craig262cfb42015-07-08 20:37:15 -05001047 vector<string> temp_incl_searchpath = g_incl_searchpath;
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +01001048 if (!old_thrift_include_path.empty()) {
Ben Craig262cfb42015-07-08 20:37:15 -05001049 g_incl_searchpath.push_back(old_thrift_include_path);
1050 }
1051
1052 parse(old_program, NULL);
1053
1054 g_incl_searchpath = temp_incl_searchpath;
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +01001055 if (!new_thrift_include_path.empty()) {
Ben Craig262cfb42015-07-08 20:37:15 -05001056 g_incl_searchpath.push_back(new_thrift_include_path);
1057 }
1058
1059 parse(new_program, NULL);
1060
1061 compare_namespace(new_program, old_program);
1062 compare_services(new_program->get_services(), old_program->get_services());
1063 compare_enums(new_program->get_enums(), old_program->get_enums());
1064 compare_structs(new_program->get_structs(), old_program->get_structs());
1065 compare_structs(new_program->get_xceptions(), old_program->get_xceptions());
1066 compare_consts(new_program->get_consts(), old_program->get_consts());
1067}
1068
Mark Sleef0712dc2006-10-25 19:03:57 +00001069/**
Mark Sleef5377b32006-10-10 01:42:59 +00001070 * Parse it up.. then spit it back out, in pretty much every language. Alright
1071 * not that many languages, but the cool ones that we care about.
Mark Slee31985722006-05-24 21:45:31 +00001072 */
1073int main(int argc, char** argv) {
1074 int i;
dweatherford65b70752007-10-31 02:18:14 +00001075 std::string out_path;
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001076 bool out_path_is_absolute = false;
Mark Sleef5377b32006-10-10 01:42:59 +00001077
Mark Sleeb15a68b2006-06-07 06:46:24 +00001078 // Setup time string
1079 time_t now = time(NULL);
1080 g_time_str = ctime(&now);
Mark Slee31985722006-05-24 21:45:31 +00001081
Mark Sleef0712dc2006-10-25 19:03:57 +00001082 // Check for necessary arguments, you gotta have at least a filename and
1083 // an output language flag
Mark Sleeb15a68b2006-06-07 06:46:24 +00001084 if (argc < 2) {
1085 usage();
1086 }
Mark Slee31985722006-05-24 21:45:31 +00001087
David Reissbd0db882008-02-27 01:54:51 +00001088 vector<string> generator_strings;
Ben Craig262cfb42015-07-08 20:37:15 -05001089 string old_thrift_include_path;
1090 string new_thrift_include_path;
1091 string old_input_file;
David Reissbd0db882008-02-27 01:54:51 +00001092
David Reiss9cc2c132008-02-27 01:54:47 +00001093 // Set the current path to a dummy value to make warning messages clearer.
1094 g_curpath = "arguments";
1095
Mark Sleef5377b32006-10-10 01:42:59 +00001096 // Hacky parameter handling... I didn't feel like using a library sorry!
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001097 for (i = 1; i < argc - 1; i++) {
Mark Sleefdbee812006-09-27 18:50:48 +00001098 char* arg;
Mark Slee2329a832006-11-09 00:23:30 +00001099
Mark Sleefdbee812006-09-27 18:50:48 +00001100 arg = strtok(argv[i], " ");
1101 while (arg != NULL) {
Mark Slee2329a832006-11-09 00:23:30 +00001102 // Treat double dashes as single dashes
Mark Slee52cb2232006-11-10 22:32:07 +00001103 if (arg[0] == '-' && arg[1] == '-') {
Mark Slee2329a832006-11-09 00:23:30 +00001104 ++arg;
1105 }
1106
Jake Farrell2fd8a152012-09-29 00:26:36 +00001107 if (strcmp(arg, "-help") == 0) {
1108 help();
1109 } else if (strcmp(arg, "-version") == 0) {
David Reissdd08f6d2008-06-30 20:24:24 +00001110 version();
jfarrell70969422013-09-09 20:33:38 -04001111 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001112 } else if (strcmp(arg, "-debug") == 0) {
Mark Sleefdbee812006-09-27 18:50:48 +00001113 g_debug = 1;
Mark Slee2329a832006-11-09 00:23:30 +00001114 } else if (strcmp(arg, "-nowarn") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001115 g_warn = 0;
Mark Slee2329a832006-11-09 00:23:30 +00001116 } else if (strcmp(arg, "-strict") == 0) {
Bryan Duxburya145b4d2009-04-03 17:29:25 +00001117 g_strict = 255;
Mark Sleef0712dc2006-10-25 19:03:57 +00001118 g_warn = 2;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001119 } else if (strcmp(arg, "-v") == 0 || strcmp(arg, "-verbose") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001120 g_verbose = 1;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001121 } else if (strcmp(arg, "-r") == 0 || strcmp(arg, "-recurse") == 0) {
Mark Sleef0712dc2006-10-25 19:03:57 +00001122 gen_recurse = true;
Bryan Duxburyc7206a42011-08-17 23:17:04 +00001123 } else if (strcmp(arg, "-allow-neg-keys") == 0) {
1124 g_allow_neg_field_keys = true;
Roger Meier887ff752011-08-19 11:25:39 +00001125 } else if (strcmp(arg, "-allow-64bit-consts") == 0) {
1126 g_allow_64bit_consts = true;
David Reissbd0db882008-02-27 01:54:51 +00001127 } else if (strcmp(arg, "-gen") == 0) {
1128 arg = argv[++i];
1129 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001130 fprintf(stderr, "Missing generator specification\n");
David Reissbd0db882008-02-27 01:54:51 +00001131 usage();
1132 }
1133 generator_strings.push_back(arg);
Martin Kraemer32c66e12006-11-09 00:06:36 +00001134 } else if (strcmp(arg, "-I") == 0) {
1135 // An argument of "-I\ asdf" is invalid and has unknown results
1136 arg = argv[++i];
1137
1138 if (arg == NULL) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001139 fprintf(stderr, "Missing Include directory\n");
Martin Kraemer32c66e12006-11-09 00:06:36 +00001140 usage();
1141 }
1142 g_incl_searchpath.push_back(arg);
Bryan Duxburybdca9f62011-03-01 19:53:07 +00001143 } else if ((strcmp(arg, "-o") == 0) || (strcmp(arg, "-out") == 0)) {
1144 out_path_is_absolute = (strcmp(arg, "-out") == 0) ? true : false;
Roger Meier6d7473d2013-05-06 01:08:36 +02001145 arg = argv[++i];
dweatherford65b70752007-10-31 02:18:14 +00001146 if (arg == NULL) {
David Reiss9d866ac2008-06-10 22:56:19 +00001147 fprintf(stderr, "-o: missing output directory\n");
dweatherford65b70752007-10-31 02:18:14 +00001148 usage();
Mark Slee5b743072007-11-13 04:00:29 +00001149 }
dweatherford65b70752007-10-31 02:18:14 +00001150 out_path = arg;
David Reiss204420f2008-01-11 20:59:03 +00001151
Ben Craige9576752013-10-11 08:19:16 -05001152#ifdef _WIN32
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001153 // strip out trailing \ on Windows
Jim King9de9b1f2015-04-30 16:03:34 -04001154 std::string::size_type last = out_path.length() - 1;
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001155 if (out_path[last] == '\\') {
David Reiss204420f2008-01-11 20:59:03 +00001156 out_path.erase(last);
1157 }
1158#endif
Roger Meier061d4a22012-10-07 11:51:00 +00001159 if (!check_is_directory(out_path.c_str()))
dweatherford65b70752007-10-31 02:18:14 +00001160 return -1;
Ben Craig262cfb42015-07-08 20:37:15 -05001161 } else if (strcmp(arg, "-audit") == 0) {
1162 g_audit = true;
1163 arg = argv[++i];
1164 if (arg == NULL) {
1165 fprintf(stderr, "Missing old thrift file name for audit operation\n");
1166 usage();
1167 }
1168 char old_thrift_file_rp[THRIFT_PATH_MAX];
1169
Nobuaki Sukegawad479e232016-02-28 11:28:19 +09001170 // cppcheck-suppress uninitvar
Ben Craig262cfb42015-07-08 20:37:15 -05001171 if (saferealpath(arg, old_thrift_file_rp) == NULL) {
1172 failure("Could not open input file with realpath: %s", arg);
1173 }
1174 old_input_file = string(old_thrift_file_rp);
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +01001175 } else if (strcmp(arg, "-audit-nofatal") == 0) {
Ben Craig262cfb42015-07-08 20:37:15 -05001176 g_audit_fatal = false;
1177 } else if (strcmp(arg, "-Iold") == 0) {
1178 arg = argv[++i];
1179 if (arg == NULL) {
1180 fprintf(stderr, "Missing Include directory for old thrift file\n");
1181 usage();
1182 }
1183 old_thrift_include_path = string(arg);
1184 } else if (strcmp(arg, "-Inew") == 0) {
1185 arg = argv[++i];
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +01001186 if (arg == NULL) {
1187 fprintf(stderr, "Missing Include directory for new thrift file\n");
1188 usage();
Ben Craig262cfb42015-07-08 20:37:15 -05001189 }
1190 new_thrift_include_path = string(arg);
Mark Sleefdbee812006-09-27 18:50:48 +00001191 } else {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001192 fprintf(stderr, "Unrecognized option: %s\n", arg);
Mark Sleefdbee812006-09-27 18:50:48 +00001193 usage();
1194 }
1195
1196 // Tokenize more
1197 arg = strtok(NULL, " ");
Mark Slee31985722006-05-24 21:45:31 +00001198 }
1199 }
Mark Slee2c44d202007-05-16 02:18:07 +00001200
Jake Farrell2fd8a152012-09-29 00:26:36 +00001201 // display help
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001202 if ((strcmp(argv[argc - 1], "-help") == 0) || (strcmp(argv[argc - 1], "--help") == 0)) {
Jake Farrell2fd8a152012-09-29 00:26:36 +00001203 help();
1204 }
1205
David Reissdd08f6d2008-06-30 20:24:24 +00001206 // if you're asking for version, you have a right not to pass a file
Konrad Grochowski16a23a62014-11-13 15:33:38 +01001207 if ((strcmp(argv[argc - 1], "-version") == 0) || (strcmp(argv[argc - 1], "--version") == 0)) {
David Reissdd08f6d2008-06-30 20:24:24 +00001208 version();
jfarrell8b1799f2014-04-10 22:06:11 -04001209 exit(0);
David Reissdd08f6d2008-06-30 20:24:24 +00001210 }
1211
Mark Sleef0712dc2006-10-25 19:03:57 +00001212 // Initialize global types
Nobuaki Sukegawa11da87e2016-09-10 14:02:19 +09001213 initGlobals();
Mark Sleee8540632006-05-30 09:24:40 +00001214
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +01001215 if (g_audit) {
Ben Craig262cfb42015-07-08 20:37:15 -05001216 // Audit operation
Mark Slee31985722006-05-24 21:45:31 +00001217
Ben Craig262cfb42015-07-08 20:37:15 -05001218 if (old_input_file.empty()) {
1219 fprintf(stderr, "Missing file name of old thrift file for audit\n");
1220 usage();
1221 }
David Reiss9cc2c132008-02-27 01:54:47 +00001222
Ben Craig262cfb42015-07-08 20:37:15 -05001223 char new_thrift_file_rp[THRIFT_PATH_MAX];
1224 if (argv[i] == NULL) {
1225 fprintf(stderr, "Missing file name of new thrift file for audit\n");
1226 usage();
1227 }
Nobuaki Sukegawad479e232016-02-28 11:28:19 +09001228 // cppcheck-suppress uninitvar
Ben Craig262cfb42015-07-08 20:37:15 -05001229 if (saferealpath(argv[i], new_thrift_file_rp) == NULL) {
1230 failure("Could not open input file with realpath: %s", argv[i]);
1231 }
1232 string new_input_file(new_thrift_file_rp);
1233
1234 t_program new_program(new_input_file);
1235 t_program old_program(old_input_file);
1236
1237 audit(&new_program, &old_program, new_thrift_include_path, old_thrift_include_path);
1238
1239 } else {
1240 // Generate options
Konrad Grochowski7f4be5f2015-11-05 20:23:11 +01001241
Ben Craig262cfb42015-07-08 20:37:15 -05001242 // You gotta generate something!
1243 if (generator_strings.empty()) {
1244 fprintf(stderr, "No output language(s) specified\n");
1245 usage();
1246 }
1247
1248 // Real-pathify it
1249 char rp[THRIFT_PATH_MAX];
1250 if (argv[i] == NULL) {
1251 fprintf(stderr, "Missing file name\n");
1252 usage();
1253 }
Nobuaki Sukegawad479e232016-02-28 11:28:19 +09001254 // cppcheck-suppress uninitvar
Ben Craig262cfb42015-07-08 20:37:15 -05001255 if (saferealpath(argv[i], rp) == NULL) {
1256 failure("Could not open input file with realpath: %s", argv[i]);
1257 }
1258 string input_file(rp);
1259
1260 // Instance of the global parse tree
1261 t_program* program = new t_program(input_file);
1262 if (out_path.size()) {
1263 program->set_out_path(out_path, out_path_is_absolute);
1264 }
1265
1266 // Compute the cpp include prefix.
1267 // infer this from the filename passed in
1268 string input_filename = argv[i];
1269 string include_prefix;
1270
1271 string::size_type last_slash = string::npos;
1272 if ((last_slash = input_filename.rfind("/")) != string::npos) {
1273 include_prefix = input_filename.substr(0, last_slash);
1274 }
1275
1276 program->set_include_prefix(include_prefix);
1277
1278 // Parse it!
1279 parse(program, NULL);
1280
1281 // The current path is not really relevant when we are doing generation.
1282 // Reset the variable to make warning messages clearer.
1283 g_curpath = "generation";
1284 // Reset yylineno for the heck of it. Use 1 instead of 0 because
1285 // That is what shows up during argument parsing.
1286 yylineno = 1;
1287
1288 // Generate it!
1289 generate(program, generator_strings);
1290 delete program;
1291 }
Mark Sleeb15a68b2006-06-07 06:46:24 +00001292
Mark Sleef0712dc2006-10-25 19:03:57 +00001293 // Clean up. Who am I kidding... this program probably orphans heap memory
1294 // all over the place, but who cares because it is about to exit and it is
1295 // all referenced and used by this wacky parse tree up until now anyways.
Nobuaki Sukegawa11da87e2016-09-10 14:02:19 +09001296 clearGlobals();
Mark Slee31985722006-05-24 21:45:31 +00001297
1298 // Finished
Ben Craig262cfb42015-07-08 20:37:15 -05001299 if (g_return_failure && g_audit_fatal) {
1300 exit(2);
1301 }
Nobuaki Sukegawa11da87e2016-09-10 14:02:19 +09001302 if (g_generator_failure) {
1303 exit(3);
1304 }
Ben Craig262cfb42015-07-08 20:37:15 -05001305 // Finished
Mark Slee31985722006-05-24 21:45:31 +00001306 return 0;
1307}