Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++] Fix unqualified namespaces #5374

Closed

Conversation

Quincunx271
Copy link

Resolves #5308 .

There are tests in the first commit that don't pass until later commits. If necessary, I can rebase it to make the tests always pass.

Summary of changes:

  • flatbuffers -> ::flatbuffers
  • std -> ::std
  • flexbuffers -> ::flexbuffers
  • std::swap(a, b) -> using ::std::swap; swap(a, b);
  • cpp_ptr_type and cpp_str_type are fully qualified, both when specified via --cpp-ptr-type/--cpp-str-type and via the attributes cpp_ptr_type/cpp_str_type

The last commits are a more questionable change:

  • intN_t -> ::intN_t. All of the uintN_ts and intN_ts.
  • size_t -> ::size_t.

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@Quincunx271 Quincunx271 force-pushed the fix-cpp-unqualified-namespaces branch from cf2b0ef to 4b61f03 Compare May 28, 2019 00:41
Copy link
Collaborator

@aardappel aardappel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good, thanks!

grpc/src/compiler/cpp_generator.cc Show resolved Hide resolved
@@ -455,6 +455,17 @@ struct ServiceDef : public Definition {
SymbolTable<RPCCall> calls;
};

struct FullyQualifiedType {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use of this wrapper? If it was only useful during refactoring, please remove again.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was to enforce beginning with :: as an invariant of the type, rather than having hidden requirements on a std::string variable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a LOT of strings thruout FlatBuffers that have expectations of the format contained in them (e.g. paths). I don't feel this case is special enough to warrant encoding the invariant in a class.

TD(ULONG, "ulong", uint64_t, long, uint64, ulong, uint64, u64) /* end int */ \
TD(FLOAT, "float", float, float, float32, float, float32, f32) /* begin float */ \
TD(DOUBLE, "double", double, double, float64, double, float64, f64) /* end float/scalar */
TD(NONE, "", ::uint8_t, byte, byte, byte, uint8, u8) \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to do it for these basic types as well? They already end in _t which to me makes namespaces clashes very unlikely, and the clutter level is high.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly don't know if it makes sense. Technically, the user could define uint8_t etc types in their own namespace, which could break this, but this has a lower benefit than the other changes. I don't mind if it goes either way. Reverting the changes for these basic types seems fine to me.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that will be so rare, there is a benefit to readability to not have the :: in this case.

@@ -0,0 +1,49 @@
include(CMakeParseArguments)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for including comprehensive tests!
I would have liked it even better though if it had simply all been put in one test to curb the explosion of files/schemas/binaries.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the primary problem with putting them all in one test is the custom pointer/string as an attribute vs as a flag (the code handles it slightly differently), but it should be possible to combine it into two tests rather than eight.

@@ -29,6 +29,23 @@
#endif
// clang-format on

// Ensure we are robust against adversarial namespaces:
namespace MyGame {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this test things already sufficiently? What is covered in the adversarial tests dir that this doesn't do?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put this in later. The adversarial-namespaces tests are likely to fail for one reason, but this is likely to fail for many reasons.

This file is probably better, because it tests more possible failures. I think that this case doesn't test custom string and pointer types, whereas the adversarial-namespaces tests do include those tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test here is very cheap, the one in the adversarial tests dir are more expensive. If the ones here cover the most likely cases (of something that is already rare), I'd suggest removing the test dir. I know its not fun to remove things you already made, but if it helps maintainability going forward, I think its better to keep things simple.

@aardappel
Copy link
Collaborator

@vglavnyy looks good to you?

@Quincunx271
Copy link
Author

@googlebot I signed it

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@aardappel aardappel force-pushed the master branch 3 times, most recently from f116b80 to f12cca8 Compare May 5, 2020 20:57
@github-actions
Copy link

github-actions bot commented Nov 4, 2020

This pull request is stale because it has been open 6 months with no activity. Please comment or this will be closed in 14 days.

@github-actions github-actions bot added the stale label Nov 4, 2020
@github-actions github-actions bot closed this Nov 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[C++] Unqualified reference to "flatbuffers" namespace should be qualified
3 participants