summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBryan Duxbury <bryanduxbury@apache.org>2010-09-12 14:38:36 +0000
committerBryan Duxbury <bryanduxbury@apache.org>2010-09-12 14:38:36 +0000
commit9f0a786dad1d6d0d737b89f96ba7ec90bf9096e2 (patch)
tree55f82378ab02bb36de3d6ea0ba1f25c16c963cf1
parentb6c50e56583d503ab7dcc843d4e09d99f8010ef3 (diff)
downloadthrift-9f0a786dad1d6d0d737b89f96ba7ec90bf9096e2.tar.gz
THRIFT-897. compiler: Don't allow unqualified constant access to enum values
This patch makes it illegal to refer to enum values by just their names in the IDL. Now, you must also provide the enum type's name as well. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@996320 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--compiler/cpp/src/main.cc8
-rw-r--r--compiler/cpp/src/thrifty.yy20
-rw-r--r--test/ThriftTest.thrift4
3 files changed, 25 insertions, 7 deletions
diff --git a/compiler/cpp/src/main.cc b/compiler/cpp/src/main.cc
index c7b1510d7..56b5c75d8 100644
--- a/compiler/cpp/src/main.cc
+++ b/compiler/cpp/src/main.cc
@@ -720,7 +720,13 @@ void validate_const_rec(std::string name, t_type* type, t_const_value* value) {
vector<t_enum_value*>::const_iterator c_iter;
bool found = false;
for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
- if ((*c_iter)->get_name() == value->get_identifier()) {
+ size_t sub_index = value->get_identifier().find('.');
+ if (sub_index == string::npos) {
+ throw "error: identifier " + value->get_identifier() + " is unqualified!";
+ }
+ std::string name_portion = value->get_identifier().substr(sub_index+1);
+
+ if ((*c_iter)->get_name() == name_portion) {
found = true;
break;
}
diff --git a/compiler/cpp/src/thrifty.yy b/compiler/cpp/src/thrifty.yy
index cdedee07f..fda49cddf 100644
--- a/compiler/cpp/src/thrifty.yy
+++ b/compiler/cpp/src/thrifty.yy
@@ -511,6 +511,20 @@ Enum:
$$ = $4;
$$->set_name($2);
$$->resolve_values();
+ // make constants for all the enum values
+ if (g_parse_mode == PROGRAM) {
+ const std::vector<t_enum_value*>& enum_values = $$->get_constants();
+ std::vector<t_enum_value*>::const_iterator c_iter;
+ for (c_iter = enum_values.begin(); c_iter != enum_values.end(); ++c_iter) {
+ std::string const_name = $$->get_name() + "." + (*c_iter)->get_name();
+ t_const_value* const_val = new t_const_value((*c_iter)->get_value());
+ const_val->set_enum($$);
+ g_scope->add_constant(const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
+ if (g_parent_scope != NULL) {
+ g_parent_scope->add_constant(g_parent_prefix + const_name, new t_const(g_type_i32, (*c_iter)->get_name(), const_val));
+ }
+ }
+ }
}
EnumDefList:
@@ -540,12 +554,6 @@ EnumDef:
if ($1 != NULL) {
$$->set_doc($1);
}
- if (g_parse_mode == PROGRAM) {
- g_scope->add_constant($2, new t_const(g_type_i32, $2, new t_const_value($4)));
- if (g_parent_scope != NULL) {
- g_parent_scope->add_constant(g_parent_prefix + $2, new t_const(g_type_i32, $2, new t_const_value($4)));
- }
- }
}
|
CaptureDocText tok_identifier CommaOrSemicolonOptional
diff --git a/test/ThriftTest.thrift b/test/ThriftTest.thrift
index a6788a9b4..d9f0fbefb 100644
--- a/test/ThriftTest.thrift
+++ b/test/ThriftTest.thrift
@@ -43,6 +43,10 @@ enum Numberz
EIGHT = 8
}
+const Numberz myNumberz = Numberz.ONE;
+// the following is expected to fail:
+// const Numberz urNumberz = ONE;
+
typedef i64 UserId
struct Bonk