Partial handlling of 64-bit integer constants.
- Parse integer constants in Thrift files as 64-bit ints.
- Die if an overflow occurs.
- Warn if an enum value cannot fit in 32 bits.
- Add a simple test for the above.
I ran all of the generators over BrokenConstants.thrift before adding the
overflow, and they appeared to work. However, the code generated was not
always valid (for example, the 64-bit constant must have an LL suffix in C++).
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@672907 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/src/thrifty.yy b/compiler/cpp/src/thrifty.yy
index 654c85a..f4ff6ef 100644
--- a/compiler/cpp/src/thrifty.yy
+++ b/compiler/cpp/src/thrifty.yy
@@ -13,7 +13,10 @@
* @author Mark Slee <mcslee@facebook.com>
*/
+#define __STDC_LIMIT_MACROS
+#define __STDC_FORMAT_MACROS
#include <stdio.h>
+#include <inttypes.h>
#include "main.h"
#include "globals.h"
#include "parse/t_program.h"
@@ -35,7 +38,7 @@
*/
%union {
char* id;
- int iconst;
+ int64_t iconst;
double dconst;
bool tbool;
t_doc* tdoc;
@@ -496,6 +499,9 @@
if ($4 < 0) {
pwarning(1, "Negative value supplied for enum %s.\n", $2);
}
+ if ($4 > INT_MAX) {
+ pwarning(1, "64-bit value supplied for enum %s.\n", $2);
+ }
$$ = new t_enum_value($2, $4);
if ($1 != NULL) {
$$->set_doc($1);
@@ -569,6 +575,9 @@
pdebug("ConstValue => tok_int_constant");
$$ = new t_const_value();
$$->set_integer($1);
+ if ($1 < INT32_MIN || $1 > INT32_MAX) {
+ pwarning(1, "64-bit constant \"%"PRIi64"\" may not work in all languages.\n", $1);
+ }
}
| tok_dub_constant
{