<table><tr><td style="">ioeric created this revision.<br />ioeric added a reviewer: hokein.<br />ioeric added a subscriber: cfe-commits.
</td><a style="text-decoration: none; padding: 4px 8px; margin: 0 8px 8px; float: right; color: #464C5C; font-weight: bold; border-radius: 3px; background-color: #F7F7F9; background-image: linear-gradient(to bottom,#fff,#f1f0f1); display: inline-block; border: 1px solid rgba(71,87,120,.2);" href="https://reviews.llvm.org/D25065" rel="noreferrer">View Revision</a></tr></table><br /><div><p>Also test phabricator.</p></div><br /><div><a href="https://reviews.llvm.org/D25065" rel="noreferrer">https://reviews.llvm.org/D25065</a></div><br /><div><strong><strong>Files:</strong></strong><div><div>change-namespace/ChangeNamespace.cpp<br />
unittests/change-namespace/ChangeNamespaceTests.cpp</div></div></div><br /><div><div style="margin: 6px 0 12px 0;"><div style="border: 1px solid #C7CCD9; border-radius: 3px;"><div style="color: #74777d; background: #eff2f4; padding: 6px 8px; overflow: hidden;"><span style="color: #4b4d51; font-weight: bold;">unittests/change-namespace/ChangeNamespaceTests.cpp</span></div><div style="font: 11px/15px "Menlo", "Consolas", "Monaco", monospace; white-space: pre-wrap; clear: both; padding: 4px 0; margin: 0;"><div style="padding: 0 8px; margin: 0 4px; ">}
</div><div style="padding: 0 8px; margin: 0 4px; ">
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;">TEST_F(ChangeNamespaceTest, MoveIntoAnotherNestedNamespaceWithRef) {
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> NewNamespace = "na::nc";
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> std::string Code = "namespace na {\n"
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> "class A {};\n"
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> "namespace nb {\n"
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> "class X { A a; };\n"
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> "} // namespace nb\n"
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> "} // namespace na\n";
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> std::string Expected = "namespace na {\n"
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> "class A {};\n"
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> "\n"
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> "namespace nc {\n"
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> "class X { A a; };\n"
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> "} // namespace nc\n"
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> "} // namespace na\n";
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> EXPECT_EQ(format(Expected), runChangeNamespaceOnCode(Code));
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;">}
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;">
</div><div style="padding: 0 8px; margin: 0 4px; ">TEST_F(ChangeNamespaceTest, SimpleMoveNestedNamespace) {
</div><div style="padding: 0 8px; margin: 0 4px; "> NewNamespace = "na::x::y";
</div></div></div><br /><div style="border: 1px solid #C7CCD9; border-radius: 3px;"><div style="color: #74777d; background: #eff2f4; padding: 6px 8px; overflow: hidden;"><span style="color: #4b4d51; font-weight: bold;">change-namespace/ChangeNamespace.cpp</span></div><div style="font: 11px/15px "Menlo", "Consolas", "Monaco", monospace; white-space: pre-wrap; clear: both; padding: 4px 0; margin: 0;"><div style="padding: 0 8px; margin: 0 4px; ">// namespace `NsName`. For example, if `DeclName` is "a::b::X" and `NsName`
</div><div style="padding: 0 8px; margin: 0 4px; ">// is "a::c::d", then "b::X" will be returned.
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;">// \param DeclName A fully qualified name, "::a::b::X" or "a::b::X".
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;">// \param NsName A fully qualified name, "::a::b" or "a::b".
</div><div style="padding: 0 8px; margin: 0 4px; ">std::string getShortestQualifiedNameInNamespace(llvm::StringRef DeclName,
</div><div style="padding: 0 8px; margin: 0 4px; "> llvm::StringRef NsName) {
</div><div style="padding: 0 8px; margin: 0 4px; background: #ffd0d0;"> <span class="bright">llvm::SmallVector<llvm::StringRef, 4></span> DeclName<span class="bright">Splitted</span>;
</div><div style="padding: 0 8px; margin: 0 4px; background: #ffd0d0;"> <span class="bright">Decl</span>Name<span class="bright">.split(Decl</span>Name<span class="bright">Splitted, "::"</span>);
</div><div style="padding: 0 8px; margin: 0 4px; background: #ffd0d0;"> <span class="bright">if (</span>DeclName<span class="bright">Splitted.size() == 1)</span>
</div><div style="padding: 0 8px; margin: 0 4px; background: #ffd0d0;"> <span class="bright"> return DeclName;</span>
</div><div style="padding: 0 8px; margin: 0 4px; background: #ffd0d0;"> <span class="bright">const auto Unqualified</span>Name<span class="bright"> = DeclNameSplitted.back();</span>
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> <span class="bright">DeclName =</span> DeclName<span class="bright">.ltrim(':')</span>;
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> <span class="bright">Ns</span>Name<span class="bright"> = Ns</span>Name<span class="bright">.ltrim(':'</span>);
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> <span class="bright">// If `</span>DeclName<span class="bright">` is a global variable, we prepend "::" to it if it is not in</span>
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> <span class="bright">// the global namespace.</span>
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> <span class="bright">if (Decl</span>Name<span class="bright">.find(':') == llvm::StringRef::npos)</span>
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> return NsName.empty() ? DeclName.str() : ("::" + DeclName).str();
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;">
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> llvm::StringRef Prefix = NsName;
</div><div style="padding: 0 8px; margin: 0 4px; "> while (true) {
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> if (DeclName.consume_front((Prefix + "::").str()))
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> return DeclName.str();
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> NsName = Prefix;
</div><div style="padding: 0 8px; margin: 0 4px; "> const auto Pos = NsName.find_last_of(':');
</div><div style="padding: 0 8px; margin: 0 4px; "> if (Pos == llvm::StringRef::npos)
</div><div style="padding: 0 8px; margin: 0 4px; "> return DeclName;
</div><div style="padding: 0 8px; margin: 0 4px; background: #ffd0d0;"> <span class="bright">const auto </span>Prefix = NsName.substr(0, Pos - 1);
</div><div style="padding: 0 8px; margin: 0 4px; background: #ffd0d0;"> if (DeclName.startswith(Prefix))
</div><div style="padding: 0 8px; margin: 0 4px; background: #ffd0d0;"> return (Prefix + "::" + UnqualifiedName).str();
</div><div style="padding: 0 8px; margin: 0 4px; background: #ffd0d0;"> NsName = Prefix;
</div><div style="padding: 0 8px; margin: 0 4px; background: #d0ffd0;"> Prefix = NsName.substr(0, Pos - 1);
</div><div style="padding: 0 8px; margin: 0 4px; "> }
</div><div style="padding: 0 8px; margin: 0 4px; "> return DeclName;
</div></div></div></div></div><br />