Browse Source

Don't rely on non-standard 'INT32_MAX' / 'INT32_MIN'.

Define safe constants to use when padding enums.

Fixes #80.
Tres Seaver 11 years ago
parent
commit
83671c0514

+ 2 - 2
src/google/protobuf/compiler/cpp/cpp_enum.cc

@@ -106,8 +106,8 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) {
     // INT32_MIN and INT32_MAX
     // INT32_MIN and INT32_MAX
     if (descriptor_->value_count() > 0) printer->Print(",\n");
     if (descriptor_->value_count() > 0) printer->Print(",\n");
     printer->Print(vars,
     printer->Print(vars,
-        "$classname$_$prefix$INT_MIN_SENTINEL_DO_NOT_USE_ = INT32_MIN,\n"
-        "$classname$_$prefix$INT_MAX_SENTINEL_DO_NOT_USE_ = INT32_MAX");
+        "$classname$_$prefix$INT_MIN_SENTINEL_DO_NOT_USE_ = PROTOBUF_ENUM_MIN,\n"
+        "$classname$_$prefix$INT_MAX_SENTINEL_DO_NOT_USE_ = PROTOBUF_ENUM_MAX");
   }
   }
 
 
   printer->Outdent();
   printer->Outdent();

+ 6 - 1
src/google/protobuf/compiler/cpp/cpp_file.cc

@@ -105,7 +105,12 @@ void FileGenerator::GenerateHeader(io::Printer* printer) {
     "#define PROTOBUF_$filename_identifier$__INCLUDED\n"
     "#define PROTOBUF_$filename_identifier$__INCLUDED\n"
     "\n"
     "\n"
     "#include <string>\n"
     "#include <string>\n"
-    "#include <stdint.h>\n"  // INT32_MIN, INT32_MAX
+    "#ifndef PROTOBUF_ENUM_MIN\n"
+    "#define PROTOBUF_ENUM_MIN		(int32_t)0x80000000\n"
+    "#endif\n"
+    "#ifndef PROTOBUF_ENUM_MAX\n"
+    "#define PROTOBUF_ENUM_MAX		(int32_t)0x7fffffff\n"
+    "#endif\n"
     "\n",
     "\n",
     "filename", file_->name(),
     "filename", file_->name(),
     "filename_identifier", filename_identifier);
     "filename_identifier", filename_identifier);

+ 6 - 1
src/google/protobuf/compiler/plugin.pb.h

@@ -5,7 +5,12 @@
 #define PROTOBUF_google_2fprotobuf_2fcompiler_2fplugin_2eproto__INCLUDED
 #define PROTOBUF_google_2fprotobuf_2fcompiler_2fplugin_2eproto__INCLUDED
 
 
 #include <string>
 #include <string>
-#include <stdint.h>
+#ifndef PROTOBUF_ENUM_MIN
+#define PROTOBUF_ENUM_MIN		(int32_t)0x80000000
+#endif
+#ifndef PROTOBUF_ENUM_MAX
+#define PROTOBUF_ENUM_MAX		(int32_t)0x7fffffff
+#endif
 
 
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/stubs/common.h>
 
 

+ 6 - 1
src/google/protobuf/descriptor.pb.h

@@ -5,7 +5,12 @@
 #define PROTOBUF_google_2fprotobuf_2fdescriptor_2eproto__INCLUDED
 #define PROTOBUF_google_2fprotobuf_2fdescriptor_2eproto__INCLUDED
 
 
 #include <string>
 #include <string>
-#include <stdint.h>
+#ifndef PROTOBUF_ENUM_MIN
+#define PROTOBUF_ENUM_MIN		(int32_t)0x80000000
+#endif
+#ifndef PROTOBUF_ENUM_MAX
+#define PROTOBUF_ENUM_MAX		(int32_t)0x7fffffff
+#endif
 
 
 #include <google/protobuf/stubs/common.h>
 #include <google/protobuf/stubs/common.h>