From 3e1587fd4bcfaa447aa5fb43198f4f8709c673e4 Mon Sep 17 00:00:00 2001 From: Thomas Van Lenten Date: Tue, 23 Jan 2018 13:38:28 -0500 Subject: [PATCH] Add an explicit import of stdatomic.h. The generated code for enums needs atomics support, so generate the import instead of relying on it via transitive imports. This will make future changes to this likely likely to break generated code and runtime support are mixed. Followup to https://github.com/google/protobuf/pull/4184. --- objectivec/GPBMessage_PackagePrivate.h | 2 -- objectivec/google/protobuf/Struct.pbobjc.m | 2 ++ objectivec/google/protobuf/Type.pbobjc.m | 2 ++ .../compiler/objectivec/objectivec_file.cc | 35 +++++++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/objectivec/GPBMessage_PackagePrivate.h b/objectivec/GPBMessage_PackagePrivate.h index 820bb7b453..366e5bbfa7 100644 --- a/objectivec/GPBMessage_PackagePrivate.h +++ b/objectivec/GPBMessage_PackagePrivate.h @@ -34,8 +34,6 @@ #import "GPBMessage.h" -#import - // TODO: Remove this import. Older generated code use the OSAtomic* apis, // so anyone that hasn't regenerated says building by having this. After // enough time has passed, this likely can be removed as folks should have diff --git a/objectivec/google/protobuf/Struct.pbobjc.m b/objectivec/google/protobuf/Struct.pbobjc.m index dff2f844cc..6d9d7b75d4 100644 --- a/objectivec/google/protobuf/Struct.pbobjc.m +++ b/objectivec/google/protobuf/Struct.pbobjc.m @@ -13,6 +13,8 @@ #import "GPBProtocolBuffers_RuntimeSupport.h" #endif +#import + #if GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS #import #else diff --git a/objectivec/google/protobuf/Type.pbobjc.m b/objectivec/google/protobuf/Type.pbobjc.m index 06795308d0..981ae4d452 100644 --- a/objectivec/google/protobuf/Type.pbobjc.m +++ b/objectivec/google/protobuf/Type.pbobjc.m @@ -13,6 +13,8 @@ #import "GPBProtocolBuffers_RuntimeSupport.h" #endif +#import + #if GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS #import #import diff --git a/src/google/protobuf/compiler/objectivec/objectivec_file.cc b/src/google/protobuf/compiler/objectivec/objectivec_file.cc index 954b26885b..a249c71b03 100644 --- a/src/google/protobuf/compiler/objectivec/objectivec_file.cc +++ b/src/google/protobuf/compiler/objectivec/objectivec_file.cc @@ -56,6 +56,20 @@ const int32 GOOGLE_PROTOBUF_OBJC_VERSION = 30002; const char* kHeaderExtension = ".pbobjc.h"; +// Checks if a message contains any enums definitions (on the message or +// a nested message under it). +bool MessageContainsEnums(const Descriptor* message) { + if (message->enum_type_count() > 0) { + return true; + } + for (int i = 0; i < message->nested_type_count(); i++) { + if (MessageContainsEnums(message->nested_type(i))) { + return true; + } + } + return false; +} + // Checks if a message contains any extension definitions (on the message or // a nested message under it). bool MessageContainsExtensions(const Descriptor* message) { @@ -70,6 +84,20 @@ bool MessageContainsExtensions(const Descriptor* message) { return false; } +// Checks if the file contains any enum definitions (at the root or +// nested under a message). +bool FileContainsEnums(const FileDescriptor* file) { + if (file->enum_type_count() > 0) { + return true; + } + for (int i = 0; i < file->message_type_count(); i++) { + if (MessageContainsEnums(file->message_type(i))) { + return true; + } + } + return false; +} + // Checks if the file contains any extensions definitions (at the root or // nested under a message). bool FileContainsExtensions(const FileDescriptor* file) { @@ -311,6 +339,13 @@ void FileGenerator::GenerateSource(io::Printer *printer) { // #import the runtime support. PrintFileRuntimePreamble(printer, "GPBProtocolBuffers_RuntimeSupport.h"); + // Enums use atomic in the generated code, so add the system import as needed. + if (FileContainsEnums(file_)) { + printer->Print( + "#import \n" + "\n"); + } + vector deps_with_extensions; CollectMinimalFileDepsContainingExtensions(file_, &deps_with_extensions);