Const correctness in generated code and in code generators. Added missing \reflection\generate_code.bat file. (#4679)

This commit is contained in:
Flaviu
2018-03-23 17:58:07 +02:00
committed by Wouter van Oortmerssen
parent c0a6e5120d
commit 79f62ee353
16 changed files with 306 additions and 270 deletions

View File

@@ -21,6 +21,8 @@
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include <unordered_set>
namespace flatbuffers {
// Pedantic warning free version of toupper().
@@ -38,7 +40,8 @@ class CppGenerator : public BaseGenerator {
const std::string &file_name)
: BaseGenerator(parser, path, file_name, "", "::"),
cur_name_space_(nullptr) {
const char *keywords[] = { "alignas",
static const char * const keywords[] = {
"alignas",
"alignof",
"and",
"and_eq",
@@ -142,7 +145,7 @@ class CppGenerator : public BaseGenerator {
std::string guard = file_name_;
// Remove any non-alpha-numeric characters that may appear in a filename.
struct IsAlnum {
bool operator()(char c) { return !isalnum(c); }
bool operator()(char c) const { return !isalnum(c); }
};
guard.erase(std::remove_if(guard.begin(), guard.end(), IsAlnum()),
guard.end());
@@ -445,7 +448,7 @@ class CppGenerator : public BaseGenerator {
private:
CodeWriter code_;
std::set<std::string> keywords_;
std::unordered_set<std::string> keywords_;
// This tracks the current namespace so we can insert namespace declarations.
const Namespace *cur_name_space_;
@@ -472,7 +475,7 @@ class CppGenerator : public BaseGenerator {
// Return a C++ type from the table in idl.h
std::string GenTypeBasic(const Type &type, bool user_facing_type) const {
static const char *ctypename[] = {
static const char * const ctypename[] = {
// clang-format off
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
#CTYPE,
@@ -734,7 +737,7 @@ class CppGenerator : public BaseGenerator {
void GenMiniReflectPre(const StructDef *struct_def) {
code_.SetValue("NAME", struct_def->name);
code_ += "inline flatbuffers::TypeTable *{{NAME}}TypeTable();";
code_ += "inline const flatbuffers::TypeTable *{{NAME}}TypeTable();";
code_ += "";
}
@@ -830,14 +833,14 @@ class CppGenerator : public BaseGenerator {
code_.SetValue("REFS", rs);
code_.SetValue("NAMES", ns);
code_.SetValue("VALUES", vs);
code_ += "inline flatbuffers::TypeTable *{{NAME}}TypeTable() {";
code_ += "inline const flatbuffers::TypeTable *{{NAME}}TypeTable() {";
if (num_fields) {
code_ += " static flatbuffers::TypeCode type_codes[] = {";
code_ += " static const flatbuffers::TypeCode type_codes[] = {";
code_ += " {{TYPES}}";
code_ += " };";
}
if (!type_refs.empty()) {
code_ += " static flatbuffers::TypeFunction type_refs[] = {";
code_ += " static const flatbuffers::TypeFunction type_refs[] = {";
code_ += " {{REFS}}";
code_ += " };";
}
@@ -847,11 +850,11 @@ class CppGenerator : public BaseGenerator {
auto has_names =
num_fields && parser_.opts.mini_reflect == IDLOptions::kTypesAndNames;
if (has_names) {
code_ += " static const char *names[] = {";
code_ += " static const char * const names[] = {";
code_ += " {{NAMES}}";
code_ += " };";
}
code_ += " static flatbuffers::TypeTable tt = {";
code_ += " static const flatbuffers::TypeTable tt = {";
code_ += std::string(" flatbuffers::{{SEQ_TYPE}}, {{NUM_FIELDS}}, ") +
(num_fields ? "type_codes, " : "nullptr, ") +
(!type_refs.empty() ? "type_refs, " : "nullptr, ") +
@@ -925,9 +928,9 @@ class CppGenerator : public BaseGenerator {
// Generate an array of all enumeration values
auto num_fields = NumToString(enum_def.vals.vec.size());
code_ += "inline {{ENUM_NAME}} (&EnumValues{{ENUM_NAME}}())[" + num_fields +
code_ += "inline const {{ENUM_NAME}} (&EnumValues{{ENUM_NAME}}())[" + num_fields +
"] {";
code_ += " static {{ENUM_NAME}} values[] = {";
code_ += " static const {{ENUM_NAME}} values[] = {";
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
++it) {
const auto &ev = **it;
@@ -951,8 +954,8 @@ class CppGenerator : public BaseGenerator {
static const int kMaxSparseness = 5;
if (range / static_cast<int64_t>(enum_def.vals.vec.size()) <
kMaxSparseness) {
code_ += "inline const char **EnumNames{{ENUM_NAME}}() {";
code_ += " static const char *names[] = {";
code_ += "inline const char * const *EnumNames{{ENUM_NAME}}() {";
code_ += " static const char * const names[] = {";
auto val = enum_def.vals.vec.front()->value;
for (auto it = enum_def.vals.vec.begin(); it != enum_def.vals.vec.end();
@@ -1548,7 +1551,7 @@ class CppGenerator : public BaseGenerator {
code_ += " typedef {{NATIVE_NAME}} NativeTableType;";
}
if (parser_.opts.mini_reflect != IDLOptions::kNone) {
code_ += " static flatbuffers::TypeTable *MiniReflectTypeTable() {";
code_ += " static const flatbuffers::TypeTable *MiniReflectTypeTable() {";
code_ += " return {{STRUCT_NAME}}TypeTable();";
code_ += " }";
}

View File

@@ -60,7 +60,7 @@ struct LanguageParameters {
};
const LanguageParameters &GetLangParams(IDLOptions::Language lang) {
static LanguageParameters language_parameters[] = {
static const LanguageParameters language_parameters[] = {
{
IDLOptions::kJava,
false,
@@ -191,7 +191,7 @@ class GeneralGenerator : public BaseGenerator {
// Save out the generated code for a single class while adding
// declaration boilerplate.
bool SaveType(const std::string &defname, const Namespace &ns,
const std::string &classcode, bool needs_includes) {
const std::string &classcode, bool needs_includes) const {
if (!classcode.length()) return true;
std::string code;
@@ -226,13 +226,13 @@ class GeneralGenerator : public BaseGenerator {
const Namespace *CurrentNameSpace() const { return cur_name_space_; }
std::string FunctionStart(char upper) {
std::string FunctionStart(char upper) const {
return std::string() + (lang_.language == IDLOptions::kJava
? static_cast<char>(tolower(upper))
: upper);
}
std::string GenNullableAnnotation(const Type &t) {
std::string GenNullableAnnotation(const Type &t) const {
return lang_.language == IDLOptions::kJava && parser_.opts.gen_nullable &&
!IsScalar(DestinationType(t, true).base_type)
? " @Nullable "
@@ -243,9 +243,9 @@ class GeneralGenerator : public BaseGenerator {
return type.enum_def != nullptr && IsInteger(type.base_type);
}
std::string GenTypeBasic(const Type &type, bool enableLangOverrides) {
std::string GenTypeBasic(const Type &type, bool enableLangOverrides) const {
// clang-format off
static const char *java_typename[] = {
static const char * const java_typename[] = {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
#JTYPE,
@@ -253,7 +253,7 @@ class GeneralGenerator : public BaseGenerator {
#undef FLATBUFFERS_TD
};
static const char *csharp_typename[] = {
static const char * const csharp_typename[] = {
#define FLATBUFFERS_TD(ENUM, IDLTYPE, \
CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
#NTYPE,
@@ -279,11 +279,11 @@ class GeneralGenerator : public BaseGenerator {
}
}
std::string GenTypeBasic(const Type &type) {
std::string GenTypeBasic(const Type &type) const {
return GenTypeBasic(type, true);
}
std::string GenTypePointer(const Type &type) {
std::string GenTypePointer(const Type &type) const {
switch (type.base_type) {
case BASE_TYPE_STRING: return lang_.string_type;
case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType());
@@ -296,13 +296,13 @@ class GeneralGenerator : public BaseGenerator {
}
}
std::string GenTypeGet(const Type &type) {
std::string GenTypeGet(const Type &type) const {
return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type);
}
// Find the destination type the user wants to receive the value in (e.g.
// one size higher signed types for unsigned serialized values in Java).
Type DestinationType(const Type &type, bool vectorelem) {
Type DestinationType(const Type &type, bool vectorelem) const {
if (lang_.language != IDLOptions::kJava) return type;
switch (type.base_type) {
// We use int for both uchar/ushort, since that generally means less
@@ -317,7 +317,7 @@ class GeneralGenerator : public BaseGenerator {
}
}
std::string GenOffsetType(const StructDef &struct_def) {
std::string GenOffsetType(const StructDef &struct_def) const {
if (lang_.language == IDLOptions::kCSharp) {
return "Offset<" + WrapInNameSpace(struct_def) + ">";
} else {
@@ -326,7 +326,7 @@ class GeneralGenerator : public BaseGenerator {
}
std::string GenOffsetConstruct(const StructDef &struct_def,
const std::string &variable_name) {
const std::string &variable_name) const {
if (lang_.language == IDLOptions::kCSharp) {
return "new Offset<" + WrapInNameSpace(struct_def) + ">(" +
variable_name + ")";
@@ -334,7 +334,7 @@ class GeneralGenerator : public BaseGenerator {
return variable_name;
}
std::string GenVectorOffsetType() {
std::string GenVectorOffsetType() const {
if (lang_.language == IDLOptions::kCSharp) {
return "VectorOffset";
} else {
@@ -343,12 +343,12 @@ class GeneralGenerator : public BaseGenerator {
}
// Generate destination type name
std::string GenTypeNameDest(const Type &type) {
std::string GenTypeNameDest(const Type &type) const {
return GenTypeGet(DestinationType(type, true));
}
// Mask to turn serialized value into destination type value.
std::string DestinationMask(const Type &type, bool vectorelem) {
std::string DestinationMask(const Type &type, bool vectorelem) const {
if (lang_.language != IDLOptions::kJava) return "";
switch (type.base_type) {
case BASE_TYPE_UCHAR: return " & 0xFF";
@@ -362,7 +362,7 @@ class GeneralGenerator : public BaseGenerator {
}
// Casts necessary to correctly read serialized data
std::string DestinationCast(const Type &type) {
std::string DestinationCast(const Type &type) const {
if (type.base_type == BASE_TYPE_VECTOR) {
return DestinationCast(type.VectorType());
} else {
@@ -389,7 +389,7 @@ class GeneralGenerator : public BaseGenerator {
// would be cast down to int before being put onto the buffer. In C#, one cast
// directly cast an Enum to its underlying type, which is essential before
// putting it onto the buffer.
std::string SourceCast(const Type &type, bool castFromDest) {
std::string SourceCast(const Type &type, bool castFromDest) const {
if (type.base_type == BASE_TYPE_VECTOR) {
return SourceCast(type.VectorType(), castFromDest);
} else {
@@ -413,17 +413,17 @@ class GeneralGenerator : public BaseGenerator {
return "";
}
std::string SourceCast(const Type &type) { return SourceCast(type, true); }
std::string SourceCast(const Type &type) const { return SourceCast(type, true); }
std::string SourceCastBasic(const Type &type, bool castFromDest) {
std::string SourceCastBasic(const Type &type, bool castFromDest) const {
return IsScalar(type.base_type) ? SourceCast(type, castFromDest) : "";
}
std::string SourceCastBasic(const Type &type) {
std::string SourceCastBasic(const Type &type) const {
return SourceCastBasic(type, true);
}
std::string GenEnumDefaultValue(const Value &value) {
std::string GenEnumDefaultValue(const Value &value) const {
auto enum_def = value.type.enum_def;
auto vec = enum_def->vals.vec;
auto default_value = StringToInt(value.constant.c_str());
@@ -440,7 +440,7 @@ class GeneralGenerator : public BaseGenerator {
return result;
}
std::string GenDefaultValue(const Value &value, bool enableLangOverrides) {
std::string GenDefaultValue(const Value &value, bool enableLangOverrides) const {
if (enableLangOverrides) {
// handles both enum case and vector of enum case
if (lang_.language == IDLOptions::kCSharp &&
@@ -466,12 +466,12 @@ class GeneralGenerator : public BaseGenerator {
}
}
std::string GenDefaultValue(const Value &value) {
std::string GenDefaultValue(const Value &value) const {
return GenDefaultValue(value, true);
}
std::string GenDefaultValueBasic(const Value &value,
bool enableLangOverrides) {
bool enableLangOverrides) const {
if (!IsScalar(value.type.base_type)) {
if (enableLangOverrides) {
if (lang_.language == IDLOptions::kCSharp) {
@@ -490,11 +490,11 @@ class GeneralGenerator : public BaseGenerator {
return GenDefaultValue(value, enableLangOverrides);
}
std::string GenDefaultValueBasic(const Value &value) {
std::string GenDefaultValueBasic(const Value &value) const {
return GenDefaultValueBasic(value, true);
}
void GenEnum(EnumDef &enum_def, std::string *code_ptr) {
void GenEnum(EnumDef &enum_def, std::string *code_ptr) const {
std::string &code = *code_ptr;
if (enum_def.generated) return;
@@ -569,7 +569,7 @@ class GeneralGenerator : public BaseGenerator {
}
// Returns the function name that is able to read a value of the given type.
std::string GenGetter(const Type &type) {
std::string GenGetter(const Type &type) const {
switch (type.base_type) {
case BASE_TYPE_STRING: return lang_.accessor_prefix + "__string";
case BASE_TYPE_STRUCT: return lang_.accessor_prefix + "__struct";
@@ -591,7 +591,7 @@ class GeneralGenerator : public BaseGenerator {
// Returns the function name that is able to read a value of the given type.
std::string GenGetterForLookupByKey(flatbuffers::FieldDef *key_field,
const std::string &data_buffer,
const char *num = nullptr) {
const char *num = nullptr) const {
auto type = key_field->value.type;
auto dest_mask = DestinationMask(type, true);
auto dest_cast = DestinationCast(type);
@@ -606,7 +606,7 @@ class GeneralGenerator : public BaseGenerator {
// Direct mutation is only allowed for scalar fields.
// Hence a setter method will only be generated for such fields.
std::string GenSetter(const Type &type) {
std::string GenSetter(const Type &type) const {
if (IsScalar(type.base_type)) {
std::string setter =
lang_.accessor_prefix + "bb." + FunctionStart('P') + "ut";
@@ -621,7 +621,7 @@ class GeneralGenerator : public BaseGenerator {
}
// Returns the method name for use with add/put calls.
std::string GenMethod(const Type &type) {
std::string GenMethod(const Type &type) const {
return IsScalar(type.base_type) ? MakeCamel(GenTypeBasic(type, false))
: (IsStruct(type) ? "Struct" : "Offset");
}
@@ -629,7 +629,7 @@ class GeneralGenerator : public BaseGenerator {
// Recursively generate arguments for a constructor, to deal with nested
// structs.
void GenStructArgs(const StructDef &struct_def, std::string *code_ptr,
const char *nameprefix) {
const char *nameprefix) const {
std::string &code = *code_ptr;
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end(); ++it) {
@@ -654,7 +654,7 @@ class GeneralGenerator : public BaseGenerator {
// builder.putType(name);
// and insert manual padding.
void GenStructBody(const StructDef &struct_def, std::string *code_ptr,
const char *nameprefix) {
const char *nameprefix) const {
std::string &code = *code_ptr;
code += " builder." + FunctionStart('P') + "rep(";
code += NumToString(struct_def.minalign) + ", ";
@@ -681,7 +681,7 @@ class GeneralGenerator : public BaseGenerator {
}
}
std::string GenByteBufferLength(const char *bb_name) {
std::string GenByteBufferLength(const char *bb_name) const {
std::string bb_len = bb_name;
if (lang_.language == IDLOptions::kCSharp)
bb_len += ".Length";
@@ -691,7 +691,7 @@ class GeneralGenerator : public BaseGenerator {
}
std::string GenOffsetGetter(flatbuffers::FieldDef *key_field,
const char *num = nullptr) {
const char *num = nullptr) const {
std::string key_offset = "";
key_offset += lang_.accessor_prefix_static + "__offset(" +
NumToString(key_field->value.offset) + ", ";
@@ -707,7 +707,7 @@ class GeneralGenerator : public BaseGenerator {
return key_offset;
}
std::string GenLookupKeyGetter(flatbuffers::FieldDef *key_field) {
std::string GenLookupKeyGetter(flatbuffers::FieldDef *key_field) const {
std::string key_getter = " ";
key_getter += "int tableOffset = " + lang_.accessor_prefix_static;
key_getter += "__indirect(vectorLocation + 4 * (start + middle)";
@@ -730,7 +730,7 @@ class GeneralGenerator : public BaseGenerator {
return key_getter;
}
std::string GenKeyGetter(flatbuffers::FieldDef *key_field) {
std::string GenKeyGetter(flatbuffers::FieldDef *key_field) const {
std::string key_getter = "";
auto data_buffer =
(lang_.language == IDLOptions::kCSharp) ? "builder.DataBuffer" : "_bb";
@@ -762,7 +762,7 @@ class GeneralGenerator : public BaseGenerator {
return key_getter;
}
void GenStruct(StructDef &struct_def, std::string *code_ptr) {
void GenStruct(StructDef &struct_def, std::string *code_ptr) const {
if (struct_def.generated) return;
std::string &code = *code_ptr;

View File

@@ -43,7 +43,7 @@ static std::string GeneratedFileName(const std::string &path,
namespace go {
// see https://golang.org/ref/spec#Keywords
static const char *g_golang_keywords[] = {
static const char * const g_golang_keywords[] = {
"break", "default", "func", "interface", "select", "case", "defer",
"go", "map", "struct", "chan", "else", "goto", "package",
"switch", "const", "fallthrough", "if", "range", "type", "continue",

View File

@@ -205,7 +205,7 @@ enum {
};
static std::string TokenToString(int t) {
static const char *tokens[] = {
static const char * const tokens[] = {
#define FLATBUFFERS_TOKEN(NAME, VALUE, STRING) STRING,
FLATBUFFERS_GEN_TOKENS(FLATBUFFERS_TOKEN)
#undef FLATBUFFERS_TOKEN
@@ -225,7 +225,7 @@ static std::string TokenToString(int t) {
}
// clang-format on
std::string Parser::TokenToStringId(int t) {
std::string Parser::TokenToStringId(int t) const {
return t == kTokenIdentifier ? attribute_ : TokenToString(t);
}
@@ -478,9 +478,9 @@ CheckedError Parser::Next() {
}
// Check if a given token is next.
bool Parser::Is(int t) { return t == token_; }
bool Parser::Is(int t) const { return t == token_; }
bool Parser::IsIdent(const char *id) {
bool Parser::IsIdent(const char *id) const {
return token_ == kTokenIdentifier && attribute_ == id;
}