This is an archive of the discontinued LLVM Phabricator instance.

[change-namespace] avoid adding leading '::' when possible.
ClosedPublic

Authored by ioeric on Mar 1 2017, 3:47 AM.

Details

Summary

When changing namespaces, the tool adds leading "::" to references that need to
be fully-qualified, which would affect readability.

We avoid adding "::" when the symbol name does not conflict with the new
namespace name. For example, a symbol name "na::nb::X" conflicts with "ns::na"
since it would be resolved to "ns::na::nb::X" in the new namespace.

Diff Detail

Repository
rL LLVM

Event Timeline

ioeric created this revision.Mar 1 2017, 3:47 AM
hokein edited edge metadata.Mar 2 2017, 6:38 AM

I like removing the leading "::" when possible. :)

change-namespace/ChangeNamespace.cpp
291 ↗(On Diff #90154)

Is this needed? Looks like you are removing the name of the symbol here, but from the code below, you only use the first element of it. The QualifiedSymbol should always be a fully-qualified name with at least 1 namespace qualifier in the code, right?

296 ↗(On Diff #90154)

Why skipping the first element? And use is_contained instead?

unittests/change-namespace/ChangeNamespaceTests.cpp
718 ↗(On Diff #90154)

The code style is not correct. Also fix it.

ioeric updated this revision to Diff 92138.Mar 17 2017, 6:23 AM
ioeric marked 3 inline comments as done.
  • Merged with origin/master
  • Addressed comments. Merged with origin/master.
change-namespace/ChangeNamespace.cpp
291 ↗(On Diff #90154)

QualifiedSymbol can be in the global namespace, so SymbolSplitted could be empty after pop_back.

296 ↗(On Diff #90154)

See newly added comments for reasoning.

hokein accepted this revision.Mar 21 2017, 5:14 AM

Sorry, I missed this patch.

LGTM with one nit.

change-namespace/ChangeNamespace.cpp
296 ↗(On Diff #90154)

I see. This sounds the conflictInNamespace is too coupled with caller because it relies on "it equals to the symbol's outermost namespace and the symbol name would have been shortened" assumption. It is not straightforward especially for readers who read the code at the first time. So I'd like to search from 0 (and this operation is trivial).

This revision is now accepted and ready to land.Mar 21 2017, 5:14 AM
ioeric added inline comments.Mar 21 2017, 5:41 AM
change-namespace/ChangeNamespace.cpp
296 ↗(On Diff #90154)

This is also for correctness since it is really not a conflict when symbol and namespace has the same outer-most namespace. I could've dropped "the symbol name would have been shortened" part.

ioeric updated this revision to Diff 92475.Mar 21 2017, 5:50 AM
  • fixed newly added tests.
This revision was automatically updated to reflect the committed changes.