Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
0d77c824
Commit
0d77c824
authored
Oct 25, 2013
by
Chris Conroy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Sanitize Enum names from collisions with reserved words.
parent
e4a9ace5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
50 additions
and
5 deletions
+50
-5
cpp_enum.cc
src/google/protobuf/compiler/cpp/cpp_enum.cc
+6
-5
cpp_helpers.cc
src/google/protobuf/compiler/cpp/cpp_helpers.cc
+8
-0
cpp_helpers.h
src/google/protobuf/compiler/cpp/cpp_helpers.h
+3
-0
cpp_test_bad_identifiers.proto
...ogle/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto
+18
-0
cpp_unittest.cc
src/google/protobuf/compiler/cpp/cpp_unittest.cc
+15
-0
No files found.
src/google/protobuf/compiler/cpp/cpp_enum.cc
View file @
0d77c824
...
@@ -82,7 +82,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) {
...
@@ -82,7 +82,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) {
const
EnumValueDescriptor
*
max_value
=
descriptor_
->
value
(
0
);
const
EnumValueDescriptor
*
max_value
=
descriptor_
->
value
(
0
);
for
(
int
i
=
0
;
i
<
descriptor_
->
value_count
();
i
++
)
{
for
(
int
i
=
0
;
i
<
descriptor_
->
value_count
();
i
++
)
{
vars
[
"name"
]
=
descriptor_
->
value
(
i
)
->
name
(
);
vars
[
"name"
]
=
EnumValueName
(
descriptor_
->
value
(
i
)
);
// In C++, an value of -2147483648 gets interpreted as the negative of
// In C++, an value of -2147483648 gets interpreted as the negative of
// 2147483648, and since 2147483648 can't fit in an integer, this produces a
// 2147483648, and since 2147483648 can't fit in an integer, this produces a
// compiler warning. This works around that issue.
// compiler warning. This works around that issue.
...
@@ -90,6 +90,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) {
...
@@ -90,6 +90,7 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) {
vars
[
"prefix"
]
=
(
descriptor_
->
containing_type
()
==
NULL
)
?
vars
[
"prefix"
]
=
(
descriptor_
->
containing_type
()
==
NULL
)
?
""
:
classname_
+
"_"
;
""
:
classname_
+
"_"
;
if
(
i
>
0
)
printer
->
Print
(
",
\n
"
);
if
(
i
>
0
)
printer
->
Print
(
",
\n
"
);
printer
->
Print
(
vars
,
"$prefix$$name$ = $number$"
);
printer
->
Print
(
vars
,
"$prefix$$name$ = $number$"
);
...
@@ -113,8 +114,8 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) {
...
@@ -113,8 +114,8 @@ void EnumGenerator::GenerateDefinition(io::Printer* printer) {
printer
->
Outdent
();
printer
->
Outdent
();
printer
->
Print
(
"
\n
};
\n
"
);
printer
->
Print
(
"
\n
};
\n
"
);
vars
[
"min_name"
]
=
min_value
->
name
(
);
vars
[
"min_name"
]
=
EnumValueName
(
min_value
);
vars
[
"max_name"
]
=
max_value
->
name
(
);
vars
[
"max_name"
]
=
EnumValueName
(
max_value
);
if
(
options_
.
dllexport_decl
.
empty
())
{
if
(
options_
.
dllexport_decl
.
empty
())
{
vars
[
"dllexport"
]
=
""
;
vars
[
"dllexport"
]
=
""
;
...
@@ -174,7 +175,7 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* printer) {
...
@@ -174,7 +175,7 @@ void EnumGenerator::GenerateSymbolImports(io::Printer* printer) {
printer
->
Print
(
vars
,
"typedef $classname$ $nested_name$;
\n
"
);
printer
->
Print
(
vars
,
"typedef $classname$ $nested_name$;
\n
"
);
for
(
int
j
=
0
;
j
<
descriptor_
->
value_count
();
j
++
)
{
for
(
int
j
=
0
;
j
<
descriptor_
->
value_count
();
j
++
)
{
vars
[
"tag"
]
=
descriptor_
->
value
(
j
)
->
name
(
);
vars
[
"tag"
]
=
EnumValueName
(
descriptor_
->
value
(
j
)
);
printer
->
Print
(
vars
,
printer
->
Print
(
vars
,
"static const $nested_name$ $tag$ = $classname$_$tag$;
\n
"
);
"static const $nested_name$ $tag$ = $classname$_$tag$;
\n
"
);
}
}
...
@@ -278,7 +279,7 @@ void EnumGenerator::GenerateMethods(io::Printer* printer) {
...
@@ -278,7 +279,7 @@ void EnumGenerator::GenerateMethods(io::Printer* printer) {
vars
[
"parent"
]
=
ClassName
(
descriptor_
->
containing_type
(),
false
);
vars
[
"parent"
]
=
ClassName
(
descriptor_
->
containing_type
(),
false
);
vars
[
"nested_name"
]
=
descriptor_
->
name
();
vars
[
"nested_name"
]
=
descriptor_
->
name
();
for
(
int
i
=
0
;
i
<
descriptor_
->
value_count
();
i
++
)
{
for
(
int
i
=
0
;
i
<
descriptor_
->
value_count
();
i
++
)
{
vars
[
"value"
]
=
descriptor_
->
value
(
i
)
->
name
(
);
vars
[
"value"
]
=
EnumValueName
(
descriptor_
->
value
(
i
)
);
printer
->
Print
(
vars
,
printer
->
Print
(
vars
,
"const $classname$ $parent$::$value$;
\n
"
);
"const $classname$ $parent$::$value$;
\n
"
);
}
}
...
...
src/google/protobuf/compiler/cpp/cpp_helpers.cc
View file @
0d77c824
...
@@ -176,6 +176,14 @@ string FieldName(const FieldDescriptor* field) {
...
@@ -176,6 +176,14 @@ string FieldName(const FieldDescriptor* field) {
return
result
;
return
result
;
}
}
string
EnumValueName
(
const
EnumValueDescriptor
*
enum_value
)
{
string
result
=
enum_value
->
name
();
if
(
kKeywords
.
count
(
result
)
>
0
)
{
result
.
append
(
"_"
);
}
return
result
;
}
string
FieldConstantName
(
const
FieldDescriptor
*
field
)
{
string
FieldConstantName
(
const
FieldDescriptor
*
field
)
{
string
field_name
=
UnderscoresToCamelCase
(
field
->
name
(),
true
);
string
field_name
=
UnderscoresToCamelCase
(
field
->
name
(),
true
);
string
result
=
"k"
+
field_name
+
"FieldNumber"
;
string
result
=
"k"
+
field_name
+
"FieldNumber"
;
...
...
src/google/protobuf/compiler/cpp/cpp_helpers.h
View file @
0d77c824
...
@@ -74,6 +74,9 @@ string SuperClassName(const Descriptor* descriptor);
...
@@ -74,6 +74,9 @@ string SuperClassName(const Descriptor* descriptor);
// anyway, so normally this just returns field->name().
// anyway, so normally this just returns field->name().
string
FieldName
(
const
FieldDescriptor
*
field
);
string
FieldName
(
const
FieldDescriptor
*
field
);
// Get the sanitized name that should be used for the given enum in C++ code.
string
EnumValueName
(
const
EnumValueDescriptor
*
enum_value
);
// Get the unqualified name that should be used for a field's field
// Get the unqualified name that should be used for a field's field
// number constant.
// number constant.
string
FieldConstantName
(
const
FieldDescriptor
*
field
);
string
FieldConstantName
(
const
FieldDescriptor
*
field
);
...
...
src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.proto
View file @
0d77c824
...
@@ -131,6 +131,24 @@ message TestConflictingSymbolNamesExtension { // NO_PROTO3
...
@@ -131,6 +131,24 @@ message TestConflictingSymbolNamesExtension { // NO_PROTO3
}
// NO_PROTO3
}
// NO_PROTO3
}
// NO_PROTO3
}
// NO_PROTO3
message
TestConflictingEnumNames
{
enum
NestedConflictingEnum
{
and
=
1
;
class
=
2
;
int
=
3
;
typedef
=
4
;
XOR
=
5
;
}
optional
NestedConflictingEnum
conflicting_enum
=
1
;
}
enum
ConflictingEnum
{
NOT_EQ
=
1
;
volatile
=
2
;
return
=
3
;
}
message
DummyMessage
{}
message
DummyMessage
{}
service
TestConflictingMethodNames
{
service
TestConflictingMethodNames
{
...
...
src/google/protobuf/compiler/cpp/cpp_unittest.cc
View file @
0d77c824
...
@@ -794,6 +794,21 @@ TEST(GeneratedMessageTest, TestConflictingSymbolNames) {
...
@@ -794,6 +794,21 @@ TEST(GeneratedMessageTest, TestConflictingSymbolNames) {
message
.
GetExtension
(
ExtensionMessage
::
repeated_int32_ext
,
0
));
message
.
GetExtension
(
ExtensionMessage
::
repeated_int32_ext
,
0
));
}
}
TEST
(
GeneratedMessageTest
,
TestConflictingEnumNames
)
{
protobuf_unittest
::
TestConflictingEnumNames
message
;
message
.
set_conflicting_enum
(
protobuf_unittest
::
TestConflictingEnumNames_NestedConflictingEnum_and_
);
EXPECT_EQ
(
1
,
message
.
conflicting_enum
());
message
.
set_conflicting_enum
(
protobuf_unittest
::
TestConflictingEnumNames_NestedConflictingEnum_XOR
);
EXPECT_EQ
(
5
,
message
.
conflicting_enum
());
protobuf_unittest
::
ConflictingEnum
conflicting_enum
;
conflicting_enum
=
protobuf_unittest
::
NOT_EQ
;
EXPECT_EQ
(
1
,
conflicting_enum
);
conflicting_enum
=
protobuf_unittest
::
return_
;
EXPECT_EQ
(
3
,
conflicting_enum
);
}
#ifndef PROTOBUF_TEST_NO_DESCRIPTORS
#ifndef PROTOBUF_TEST_NO_DESCRIPTORS
TEST
(
GeneratedMessageTest
,
TestOptimizedForSize
)
{
TEST
(
GeneratedMessageTest
,
TestOptimizedForSize
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment