Skip to content

Commit

Permalink
Merge pull request booyah#10 from dmaclach/master
Browse files Browse the repository at this point in the history
Add NS_RETURNS_NOT_RETAINED as appropriate
  • Loading branch information
jparise committed Nov 8, 2011
2 parents 4990bd0 + cf0d65e commit c23abdd
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
14 changes: 14 additions & 0 deletions src/compiler/objc_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ namespace google { namespace protobuf { namespace compiler {namespace objectivec
"value", *i);
}

printer->Print(
"#ifndef __has_feature\n"
" #define __has_feature(x) 0 // Compatibility with non-clang compilers.\n"
"#endif // __has_feature\n\n");

printer->Print(
"#ifndef NS_RETURNS_NOT_RETAINED\n"
" #if __has_feature(attribute_ns_returns_not_retained)\n"
" #define NS_RETURNS_NOT_RETAINED __attribute__((ns_returns_not_retained))\n"
" #else\n"
" #define NS_RETURNS_NOT_RETAINED\n"
" #endif\n"
"#endif\n\n");

// need to write out all enums first
for (int i = 0; i < file_->enum_type_count(); i++) {
EnumGenerator(file_->enum_type(i)).GenerateHeader(printer);
Expand Down
19 changes: 14 additions & 5 deletions src/compiler/objc_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ namespace google { namespace protobuf { namespace compiler { namespace objective
}
}

bool IsRetainedName(const string& name) {
static std::string retainednames[] = { "new", "alloc", "copy", "mutableCopy" };
for (size_t i = 0; i < sizeof(retainednames) / sizeof(retainednames[0]); ++i) {
if (name.compare(0, retainednames[i].length(), retainednames[i]) == 0) {
return true;
}
}
return false;
}

bool IsBootstrapFile(const FileDescriptor* file) {
return file->name() == "google/protobuf/descriptor.proto";
Expand Down Expand Up @@ -201,7 +210,7 @@ namespace google { namespace protobuf { namespace compiler { namespace objective

if (options.package() != "") {
path = options.package() + "/" + path;
}
}
}

return path;
Expand Down Expand Up @@ -279,8 +288,8 @@ namespace google { namespace protobuf { namespace compiler { namespace objective


string EnumValueName(const EnumValueDescriptor* descriptor) {
return
ClassName(descriptor->type()) +
return
ClassName(descriptor->type()) +
UnderscoresToCapitalizedCamelCase(SafeName(descriptor->name()));
}

Expand Down Expand Up @@ -396,7 +405,7 @@ namespace google { namespace protobuf { namespace compiler { namespace objective
}

hash_set<string> kKeywords = MakeKeywordsMap();
}
}


string SafeName(const string& name) {
Expand Down Expand Up @@ -483,7 +492,7 @@ namespace google { namespace protobuf { namespace compiler { namespace objective
if (AllAscii(field->default_value_string())) {
return "@\"" + CEscape(field->default_value_string()) + "\"";
} else {
return
return
"[NSString stringWithUTF8String:\"" +
CEscape(field->default_value_string()) +
"\"]";
Expand Down
5 changes: 4 additions & 1 deletion src/compiler/objc_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ string FilenameToCamelCase(const string& filename);
// Strips ".proto" or ".protodevel" from the end of a filename.
string StripProto(const string& filename);

//
// Returns true if the name requires a ns_returns_not_retained attribute applied
// to it.
bool IsRetainedName(const string& name);

bool IsBootstrapFile(const FileDescriptor* file);

// Gets the name of the file we're going to generate (sans the .pb.h
Expand Down
15 changes: 11 additions & 4 deletions src/compiler/objc_message_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,23 @@ namespace google { namespace protobuf { namespace compiler { namespace objective
namespace {
void SetMessageVariables(const FieldDescriptor* descriptor,
map<string, string>* variables) {
std::string name = UnderscoresToCamelCase(descriptor);
(*variables)["classname"] = ClassName(descriptor->containing_type());
(*variables)["name"] = UnderscoresToCamelCase(descriptor);
(*variables)["name"] = name;
(*variables)["capitalized_name"] = UnderscoresToCapitalizedCamelCase(descriptor);
(*variables)["list_name"] = UnderscoresToCamelCase(descriptor) + "Array";
(*variables)["number"] = SimpleItoa(descriptor->number());
(*variables)["type"] = ClassName(descriptor->message_type());
if (IsPrimitiveType(GetObjectiveCType(descriptor))) {
(*variables)["storage_type"] = ClassName(descriptor->message_type());
(*variables)["storage_attribute"] = "";
} else {
(*variables)["storage_type"] = string(ClassName(descriptor->message_type())) + "*";
if (IsRetainedName(name)) {
(*variables)["storage_attribute"] = " NS_RETURNS_NOT_RETAINED";
} else {
(*variables)["storage_attribute"] = "";
}
}
(*variables)["group_or_message"] =
(descriptor->type() == FieldDescriptor::TYPE_GROUP) ?
Expand All @@ -65,7 +72,7 @@ namespace google { namespace protobuf { namespace compiler { namespace objective


void MessageFieldGenerator::GenerateFieldHeader(io::Printer* printer) const {
printer->Print(variables_, "$storage_type$ $name$;\n");
printer->Print(variables_, "$storage_type$ $name$$storage_attribute$;\n");
}


Expand All @@ -75,13 +82,13 @@ namespace google { namespace protobuf { namespace compiler { namespace objective


void MessageFieldGenerator::GeneratePropertyHeader(io::Printer* printer) const {
printer->Print(variables_, "@property (readonly, retain) $storage_type$ $name$;\n");
printer->Print(variables_, "@property (readonly, retain)$storage_attribute$ $storage_type$ $name$;\n");
}


void MessageFieldGenerator::GenerateExtensionSource(io::Printer* printer) const {
printer->Print(variables_,
"@property (retain) $storage_type$ $name$;\n");
"@property (retain)$storage_attribute$ $storage_type$ $name$;\n");
}


Expand Down
17 changes: 12 additions & 5 deletions src/compiler/objc_primitive_field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,25 @@ namespace google { namespace protobuf { namespace compiler { namespace objective

void SetPrimitiveVariables(const FieldDescriptor* descriptor,
map<string, string>* variables) {
std::string name = UnderscoresToCamelCase(descriptor);
(*variables)["classname"] = ClassName(descriptor->containing_type());
(*variables)["name"] = UnderscoresToCamelCase(descriptor);
(*variables)["name"] = name;
(*variables)["capitalized_name"] = UnderscoresToCapitalizedCamelCase(descriptor);
(*variables)["list_name"] = UnderscoresToCamelCase(descriptor) + "Array";
(*variables)["number"] = SimpleItoa(descriptor->number());
(*variables)["type"] = PrimitiveTypeName(descriptor);

if (IsPrimitiveType(GetObjectiveCType(descriptor))) {
(*variables)["storage_type"] = PrimitiveTypeName(descriptor);
(*variables)["storage_attribute"] = "";
} else {
(*variables)["storage_type"] = string(PrimitiveTypeName(descriptor)) + "*";
}
if (IsRetainedName(name)) {
(*variables)["storage_attribute"] = " NS_RETURNS_NOT_RETAINED";
} else {
(*variables)["storage_attribute"] = "";
}
}

(*variables)["array_value_type"] = GetArrayValueType(descriptor);
(*variables)["array_value_type_name"] = GetArrayValueTypeName(descriptor);
Expand Down Expand Up @@ -235,7 +242,7 @@ namespace google { namespace protobuf { namespace compiler { namespace objective
void PrimitiveFieldGenerator::GeneratePropertyHeader(io::Printer* printer) const {
if (IsReferenceType(GetObjectiveCType(descriptor_))) {
printer->Print(variables_,
"@property (readonly, retain) $storage_type$ $name$;\n");
"@property (readonly, retain)$storage_attribute$ $storage_type$ $name$;\n");
} else if (GetObjectiveCType(descriptor_) == OBJECTIVECTYPE_BOOLEAN) {
printer->Print(variables_,
"- (BOOL) $name$;\n");
Expand All @@ -249,7 +256,7 @@ namespace google { namespace protobuf { namespace compiler { namespace objective
void PrimitiveFieldGenerator::GenerateExtensionSource(io::Printer* printer) const {
if (IsReferenceType(GetObjectiveCType(descriptor_))) {
printer->Print(variables_,
"@property (retain) $storage_type$ $name$;\n");
"@property (retain)$storage_attribute$ $storage_type$ $name$;\n");
} else {
printer->Print(variables_,
"@property $storage_type$ $name$;\n");
Expand Down Expand Up @@ -305,7 +312,7 @@ namespace google { namespace protobuf { namespace compiler { namespace objective
void PrimitiveFieldGenerator::GenerateBuilderMembersHeader(io::Printer* printer) const {
printer->Print(variables_,
"- (BOOL) has$capitalized_name$;\n"
"- ($storage_type$) $name$;\n"
"- ($storage_type$) $name$$storage_attribute$;\n"
"- ($classname$_Builder*) set$capitalized_name$:($storage_type$) value;\n"
"- ($classname$_Builder*) clear$capitalized_name$;\n");
}
Expand Down

0 comments on commit c23abdd

Please sign in to comment.