This is an archive of the discontinued LLVM Phabricator instance.

[change-namespace] handle constructor initializer: Derived : Base::Base() {} and added conflict detections
ClosedPublic

Authored by ioeric on Nov 14 2016, 2:04 PM.

Details

Summary

namespace nx { namespace ny { class Base { public: Base(i) {}} } }
namespace na {
namespace nb {
class X : public nx::ny {
public:

X() : Base::Base(1) {}

};
}
}

When changing from na::nb to x::y, "Base::Base" will be changed to "nx::ny::Base" and
"Base::" in "Base::Base" will be replaced with "nx::ny::Base" too, which causes
conflict. This conflict should've been detected when adding replacements but was hidden by addOrMergeReplacement. We now also detect conflict when adding replacements where conflict must not happen.

The namespace lookup is tricky here, we simply replace "Base::Base()" with "nx::ny::Base()" as a workaround, which compiles but not perfect.

Diff Detail

Repository
rL LLVM

Event Timeline

ioeric updated this revision to Diff 77883.Nov 14 2016, 2:04 PM
ioeric retitled this revision from to [change-namespace] handle constructor initializer: Derived : Base::Base() {}.
ioeric updated this object.
ioeric updated this revision to Diff 77973.Nov 15 2016, 3:36 AM
  • Implemented a workaround.
ioeric updated this object.Nov 15 2016, 3:38 AM
ioeric added a reviewer: hokein.
ioeric retitled this revision from [change-namespace] handle constructor initializer: Derived : Base::Base() {} to [change-namespace] handle constructor initializer: Derived : Base::Base() {} and added conflict detections.
ioeric updated this object.
ioeric added a subscriber: cfe-commits.

Test comment to check if cfe-commits is in the email cc list

hokein added inline comments.Nov 15 2016, 6:08 AM
change-namespace/ChangeNamespace.cpp
343 ↗(On Diff #77973)

Maybe we can filter it out in run(MatcherResult) function rather than in AST matcher? If we meet a specifier like Base::Base() in CXXCtorInitializer, we just skip them.

ioeric updated this revision to Diff 78148.Nov 16 2016, 1:50 AM
  • Don't fix base class initializers at all.
ioeric updated this revision to Diff 78180.Nov 16 2016, 6:14 AM
  • Minor fix
hokein added inline comments.Nov 16 2016, 6:22 AM
change-namespace/ChangeNamespace.cpp
346 ↗(On Diff #78148)

Maybe pull this unless matcher out and name it to make code more understandable.

650 ↗(On Diff #78148)

use std::find?

test/change-namespace/lambda-function.cpp
2 ↗(On Diff #78148)

In practise, we don't use std headers directly in llvm lit test. You need to mock it by yourself...

unittests/change-namespace/ChangeNamespaceTests.cpp
47 ↗(On Diff #78148)

nit: code indentation.

bkramer added inline comments.
change-namespace/ChangeNamespace.cpp
546 ↗(On Diff #78180)

So is this an error or not? If you can hit this by using the tool it should bail out here. If not use llvm_unreachable instead of assert(false).

637 ↗(On Diff #78180)

same here.

650 ↗(On Diff #78180)

llvm::is_contained

707 ↗(On Diff #78180)

same here.

test/change-namespace/lambda-function.cpp
2 ↗(On Diff #78180)

You cannot rely on an STL implementation being around in a test. Unless we have a fake <functional> lying around this will fail in some environments. (I know this has been around for a while, just bringing it up now)

ioeric updated this revision to Diff 78183.Nov 16 2016, 6:45 AM
ioeric marked 8 inline comments as done.
  • Addressed comments.
hokein added inline comments.Nov 16 2016, 6:57 AM
test/change-namespace/lambda-function.cpp
2 ↗(On Diff #78148)

Hint: here is a simple fake function code, should be compiled.

template<class T> class function;
template<class R, class... ArgTypes> class function<R(ArgTypes...)> {
 public:
  template <typename Functor>
  function(Functor f) {}
};

void B(function<void(int)> t) {};

void g() {
  B([](int x){});
}
ioeric updated this revision to Diff 78188.Nov 16 2016, 7:28 AM
  • Add lambda-function.cpp test back.
hokein accepted this revision.Nov 16 2016, 7:54 AM
hokein edited edge metadata.

LGTM once nits get fixed.

change-namespace/ChangeNamespace.cpp
543 ↗(On Diff #78188)

No {}.

346 ↗(On Diff #78148)

Looks like you are missing this one.

This revision is now accepted and ready to land.Nov 16 2016, 7:54 AM
ioeric updated this revision to Diff 78190.Nov 16 2016, 8:05 AM
ioeric edited edge metadata.
  • nits
This revision was automatically updated to reflect the committed changes.