From a22b1b6267a08f9ad028675865501b174171e896 Mon Sep 17 00:00:00 2001 From: Derek Bailey Date: Mon, 13 Dec 2021 21:27:36 -0800 Subject: [PATCH] Prevent shadow with _{{FIELD_NAME}} (#6991) --- include/flatbuffers/reflection_generated.h | 32 +++++++++---------- src/idl_gen_cpp.cpp | 10 +++--- .../generated_cpp17/monster_test_generated.h | 16 +++++----- tests/monster_test_generated.h | 16 +++++----- 4 files changed, 37 insertions(+), 37 deletions(-) diff --git a/include/flatbuffers/reflection_generated.h b/include/flatbuffers/reflection_generated.h index 90b3b62f0..56d45fd82 100644 --- a/include/flatbuffers/reflection_generated.h +++ b/include/flatbuffers/reflection_generated.h @@ -261,8 +261,8 @@ struct KeyValue FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool KeyCompareLessThan(const KeyValue *o) const { return *key() < *o->key(); } - int KeyCompareWithValue(const char *val) const { - return strcmp(key()->c_str(), val); + int KeyCompareWithValue(const char *_key) const { + return strcmp(key()->c_str(), _key); } const flatbuffers::String *value() const { return GetPointer(VT_VALUE); @@ -338,8 +338,8 @@ struct EnumVal FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool KeyCompareLessThan(const EnumVal *o) const { return value() < o->value(); } - int KeyCompareWithValue(int64_t val) const { - return static_cast(value() > val) - static_cast(value() < val); + int KeyCompareWithValue(int64_t _value) const { + return static_cast(value() > _value) - static_cast(value() < _value); } const reflection::Type *union_type() const { return GetPointer(VT_UNION_TYPE); @@ -436,8 +436,8 @@ struct Enum FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool KeyCompareLessThan(const Enum *o) const { return *name() < *o->name(); } - int KeyCompareWithValue(const char *val) const { - return strcmp(name()->c_str(), val); + int KeyCompareWithValue(const char *_name) const { + return strcmp(name()->c_str(), _name); } const flatbuffers::Vector> *values() const { return GetPointer> *>(VT_VALUES); @@ -587,8 +587,8 @@ struct Field FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool KeyCompareLessThan(const Field *o) const { return *name() < *o->name(); } - int KeyCompareWithValue(const char *val) const { - return strcmp(name()->c_str(), val); + int KeyCompareWithValue(const char *_name) const { + return strcmp(name()->c_str(), _name); } const reflection::Type *type() const { return GetPointer(VT_TYPE); @@ -793,8 +793,8 @@ struct Object FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool KeyCompareLessThan(const Object *o) const { return *name() < *o->name(); } - int KeyCompareWithValue(const char *val) const { - return strcmp(name()->c_str(), val); + int KeyCompareWithValue(const char *_name) const { + return strcmp(name()->c_str(), _name); } const flatbuffers::Vector> *fields() const { return GetPointer> *>(VT_FIELDS); @@ -945,8 +945,8 @@ struct RPCCall FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool KeyCompareLessThan(const RPCCall *o) const { return *name() < *o->name(); } - int KeyCompareWithValue(const char *val) const { - return strcmp(name()->c_str(), val); + int KeyCompareWithValue(const char *_name) const { + return strcmp(name()->c_str(), _name); } const reflection::Object *request() const { return GetPointer(VT_REQUEST); @@ -1061,8 +1061,8 @@ struct Service FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool KeyCompareLessThan(const Service *o) const { return *name() < *o->name(); } - int KeyCompareWithValue(const char *val) const { - return strcmp(name()->c_str(), val); + int KeyCompareWithValue(const char *_name) const { + return strcmp(name()->c_str(), _name); } const flatbuffers::Vector> *calls() const { return GetPointer> *>(VT_CALLS); @@ -1180,8 +1180,8 @@ struct SchemaFile FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool KeyCompareLessThan(const SchemaFile *o) const { return *filename() < *o->filename(); } - int KeyCompareWithValue(const char *val) const { - return strcmp(filename()->c_str(), val); + int KeyCompareWithValue(const char *_filename) const { + return strcmp(filename()->c_str(), _filename); } /// Names of included files, relative to project root. const flatbuffers::Vector> *included_filenames() const { diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index df124e5a5..e8d0c8a09 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -2033,8 +2033,8 @@ class CppGenerator : public BaseGenerator { code_ += " }"; if (is_string) { - code_ += " int KeyCompareWithValue(const char *val) const {"; - code_ += " return strcmp({{FIELD_NAME}}()->c_str(), val);"; + code_ += " int KeyCompareWithValue(const char *_{{FIELD_NAME}}) const {"; + code_ += " return strcmp({{FIELD_NAME}}()->c_str(), _{{FIELD_NAME}});"; code_ += " }"; } else { FLATBUFFERS_ASSERT(IsScalar(field.value.type.base_type)); @@ -2045,10 +2045,10 @@ class CppGenerator : public BaseGenerator { } // Returns {fieldval: +1}. code_.SetValue("KEY_TYPE", type); - code_ += " int KeyCompareWithValue({{KEY_TYPE}} val) const {"; + code_ += " int KeyCompareWithValue({{KEY_TYPE}} _{{FIELD_NAME}}) const {"; code_ += - " return static_cast({{FIELD_NAME}}() > val) - " - "static_cast({{FIELD_NAME}}() < val);"; + " return static_cast({{FIELD_NAME}}() > _{{FIELD_NAME}}) - " + "static_cast({{FIELD_NAME}}() < _{{FIELD_NAME}});"; code_ += " }"; } } diff --git a/tests/cpp17/generated_cpp17/monster_test_generated.h b/tests/cpp17/generated_cpp17/monster_test_generated.h index 10228db8d..2b3cf6c31 100644 --- a/tests/cpp17/generated_cpp17/monster_test_generated.h +++ b/tests/cpp17/generated_cpp17/monster_test_generated.h @@ -694,8 +694,8 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS { bool KeyCompareLessThan(const Ability *o) const { return id() < o->id(); } - int KeyCompareWithValue(uint32_t val) const { - return static_cast(id() > val) - static_cast(id() < val); + int KeyCompareWithValue(uint32_t _id) const { + return static_cast(id() > _id) - static_cast(id() < _id); } uint32_t distance() const { return flatbuffers::EndianScalar(distance_); @@ -1017,8 +1017,8 @@ struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool KeyCompareLessThan(const Stat *o) const { return count() < o->count(); } - int KeyCompareWithValue(uint16_t val) const { - return static_cast(count() > val) - static_cast(count() < val); + int KeyCompareWithValue(uint16_t _count) const { + return static_cast(count() > _count) - static_cast(count() < _count); } template auto get_field() const { @@ -1130,8 +1130,8 @@ struct Referrable FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool KeyCompareLessThan(const Referrable *o) const { return id() < o->id(); } - int KeyCompareWithValue(uint64_t val) const { - return static_cast(id() > val) - static_cast(id() < val); + int KeyCompareWithValue(uint64_t _id) const { + return static_cast(id() > _id) - static_cast(id() < _id); } template auto get_field() const { @@ -1327,8 +1327,8 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool KeyCompareLessThan(const Monster *o) const { return *name() < *o->name(); } - int KeyCompareWithValue(const char *val) const { - return strcmp(name()->c_str(), val); + int KeyCompareWithValue(const char *_name) const { + return strcmp(name()->c_str(), _name); } const flatbuffers::Vector *inventory() const { return GetPointer *>(VT_INVENTORY); diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index 459c5ee00..9b49d0b81 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -788,8 +788,8 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS { bool KeyCompareLessThan(const Ability *o) const { return id() < o->id(); } - int KeyCompareWithValue(uint32_t val) const { - return static_cast(id() > val) - static_cast(id() < val); + int KeyCompareWithValue(uint32_t _id) const { + return static_cast(id() > _id) - static_cast(id() < _id); } uint32_t distance() const { return flatbuffers::EndianScalar(distance_); @@ -1053,8 +1053,8 @@ struct Stat FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool KeyCompareLessThan(const Stat *o) const { return count() < o->count(); } - int KeyCompareWithValue(uint16_t val) const { - return static_cast(count() > val) - static_cast(count() < val); + int KeyCompareWithValue(uint16_t _count) const { + return static_cast(count() > _count) - static_cast(count() < _count); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && @@ -1143,8 +1143,8 @@ struct Referrable FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool KeyCompareLessThan(const Referrable *o) const { return id() < o->id(); } - int KeyCompareWithValue(uint64_t val) const { - return static_cast(id() > val) - static_cast(id() < val); + int KeyCompareWithValue(uint64_t _id) const { + return static_cast(id() > _id) - static_cast(id() < _id); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && @@ -1321,8 +1321,8 @@ struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool KeyCompareLessThan(const Monster *o) const { return *name() < *o->name(); } - int KeyCompareWithValue(const char *val) const { - return strcmp(name()->c_str(), val); + int KeyCompareWithValue(const char *_name) const { + return strcmp(name()->c_str(), _name); } const flatbuffers::Vector *inventory() const { return GetPointer *>(VT_INVENTORY);