cpp_generator: Add support for --cpp_out=speed:...

Previously --cpp_out:lite was available to force the optimization mode
to the lite runtime. This adds --cpp_out=speed:...
which is needed to override the file specific options in cases where
libprotobuf-mutator is used for fuzzing since it depends on reflection.

Signed-off-by: Allen Webb <allenwebb@google.com>
This commit is contained in:
Allen Webb 2018-11-13 14:50:48 -08:00
parent 2f28980f07
commit 375d32d3eb
3 changed files with 22 additions and 7 deletions

View File

@ -92,9 +92,12 @@ bool CppGenerator::Generate(const FileDescriptor* file,
file_options.annotation_pragma_name = options[i].second;
} else if (options[i].first == "annotation_guard_name") {
file_options.annotation_guard_name = options[i].second;
} else if (options[i].first == "speed") {
file_options.enforce_mode = EnforceOptimizeMode::kSpeed;
} else if (options[i].first == "lite") {
file_options.enforce_lite = true;
file_options.enforce_mode = EnforceOptimizeMode::kLiteRuntime;
} else if (options[i].first == "lite_implicit_weak_fields") {
file_options.enforce_mode = EnforceOptimizeMode::kLiteRuntime;
file_options.lite_implicit_weak_fields = true;
if (!options[i].second.empty()) {
file_options.num_cc_files = strto32(options[i].second.c_str(),

View File

@ -353,7 +353,7 @@ inline bool HasFastArraySerialization(const FileDescriptor* file,
inline bool IsProto2MessageSet(const Descriptor* descriptor,
const Options& options) {
return !options.opensource_runtime &&
!options.enforce_lite &&
options.enforce_mode != EnforceOptimizeMode::kLiteRuntime &&
!options.lite_implicit_weak_fields &&
descriptor->options().message_set_wire_format() &&
descriptor->full_name() == "google.protobuf.bridge.MessageSet";
@ -362,7 +362,7 @@ inline bool IsProto2MessageSet(const Descriptor* descriptor,
inline bool IsProto2MessageSetFile(const FileDescriptor* file,
const Options& options) {
return !options.opensource_runtime &&
!options.enforce_lite &&
options.enforce_mode != EnforceOptimizeMode::kLiteRuntime &&
!options.lite_implicit_weak_fields &&
file->name() == "net/proto2/bridge/proto/message_set.proto";
}
@ -419,9 +419,15 @@ bool IsWellKnownMessage(const FileDescriptor* descriptor);
inline FileOptions_OptimizeMode GetOptimizeFor(const FileDescriptor* file,
const Options& options) {
return options.enforce_lite
? FileOptions::LITE_RUNTIME
: file->options().optimize_for();
switch (options.enforce_mode) {
case EnforceOptimizeMode::kSpeed:
return FileOptions::SPEED;
case EnforceOptimizeMode::kLiteRuntime:
return FileOptions::LITE_RUNTIME;
case EnforceOptimizeMode::kNoEnforcement:
default:
return file->options().optimize_for();
}
}
// This orders the messages in a .pb.cc as it's outputted by file.cc

View File

@ -42,6 +42,12 @@ class AccessInfoMap;
namespace cpp {
enum class EnforceOptimizeMode {
kNoEnforcement, // Use the runtime specified by the file specific options.
kSpeed, // This is the full runtime.
kLiteRuntime,
};
// Generator options (see generator.cc for a description of each):
struct Options {
std::string dllexport_decl;
@ -49,7 +55,7 @@ struct Options {
bool proto_h = false;
bool transitive_pb_h = true;
bool annotate_headers = false;
bool enforce_lite = false;
EnforceOptimizeMode enforce_mode = EnforceOptimizeMode::kNoEnforcement;
bool table_driven_parsing = false;
bool table_driven_serialization = false;
bool lite_implicit_weak_fields = false;