From: Mark Slee Date: Wed, 28 Nov 2007 22:28:13 +0000 (+0000) Subject: Thrift compiler now enforces uniqueness of field identifiers X-Git-Tag: 0.2.0~1093 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=6f9ac3ffeb408392d1c3bb3945fe5b261f9e7817;p=common%2Fthrift.git Thrift compiler now enforces uniqueness of field identifiers Summary: The code would either not generate, or generate code with errors, if you did this beforehand. Now it's a die-fast stop hard error since this is absolultely always a wrong thing to do. Reviewed By: dreiss Test Plan: Test compiling a .thrift file with a repeated field identifier in a struct or arglist. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665379 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/compiler/cpp/src/parse/t_field.h b/compiler/cpp/src/parse/t_field.h index 25c50a56..35ee1a2f 100644 --- a/compiler/cpp/src/parse/t_field.h +++ b/compiler/cpp/src/parse/t_field.h @@ -106,12 +106,12 @@ class t_field { bool has_doc() { return has_doc_; - } + } - void set_doc(const std::string& doc) { - doc_ = doc; - has_doc_ = true; - } + void set_doc(const std::string& doc) { + doc_ = doc; + has_doc_ = true; + } // This is not the same function as t_type::get_fingerprint_material, // but it does the same thing. @@ -132,8 +132,8 @@ class t_field { bool xsd_nillable_; t_struct* xsd_attrs_; - std::string doc_; - bool has_doc_; + std::string doc_; + bool has_doc_; }; diff --git a/compiler/cpp/src/parse/t_struct.h b/compiler/cpp/src/parse/t_struct.h index 39da110d..bca51f62 100644 --- a/compiler/cpp/src/parse/t_struct.h +++ b/compiler/cpp/src/parse/t_struct.h @@ -85,6 +85,17 @@ class t_struct : public t_type { } } + bool validate_field(t_field* field) { + int key = field->get_key(); + std::vector::const_iterator m_iter; + for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) { + if ((*m_iter)->get_key() == key) { + return false; + } + } + return true; + } + private: std::vector members_; diff --git a/compiler/cpp/src/thrifty.yy b/compiler/cpp/src/thrifty.yy index 5d7cfcc6..21828ff4 100644 --- a/compiler/cpp/src/thrifty.yy +++ b/compiler/cpp/src/thrifty.yy @@ -761,6 +761,10 @@ FieldList: { pdebug("FieldList -> FieldList , Field"); $$ = $1; + if (!($$->validate_field($2))) { + yyerror("Field identifier %d for \"%s\" has already been used", $2->get_key(), $2->get_name().c_str()); + exit(1); + } $$->append($2); } |