v1nh1shungry updated this revision to Diff 483114. v1nh1shungry added a comment.
apply review's suggestions Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D137817/new/ https://reviews.llvm.org/D137817 Files: clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp
Index: clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp +++ clang-tools-extra/clangd/unittests/tweaks/RemoveUsingNamespaceTests.cpp @@ -250,20 +250,33 @@ foo + 10; } )cpp"}, - {// Does not qualify user-defined literals - R"cpp( - namespace ns { - long double operator "" _w(long double); + { + R"cpp( + namespace a { + long double operator""_a(long double); + inline namespace b { + long double operator""_b(long double); + } // namespace b + } // namespace a + using namespace ^a; + int main() { + 1.0_a; + 1.0_b; } - using namespace n^s; - int main() { 1.5_w; } )cpp", - R"cpp( - namespace ns { - long double operator "" _w(long double); - } + R"cpp( + namespace a { + long double operator""_a(long double); + inline namespace b { + long double operator""_b(long double); + } // namespace b + } // namespace a - int main() { 1.5_w; } + int main() {using a::operator""_a; +using a::operator""_b; + 1.0_a; + 1.0_b; + } )cpp"}}; for (auto C : Cases) EXPECT_EQ(C.second, apply(C.first)) << C.first; Index: clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp =================================================================== --- clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp +++ clang-tools-extra/clangd/refactor/tweaks/RemoveUsingNamespace.cpp @@ -144,6 +144,9 @@ // Collect all references to symbols from the namespace for which we're // removing the directive. std::vector<SourceLocation> IdentsToQualify; + // Collect all using-declarations to add for user-defined literals declared + // in the namespace for which we're removing the directive + std::map<SourceLocation, llvm::StringSet<>> DeclsToAdd; for (auto &D : Inputs.AST->getLocalTopLevelDecls()) { findExplicitReferences( D, @@ -164,15 +167,24 @@ // Avoid adding qualifiers before user-defined literals, e.g. // using namespace std; // auto s = "foo"s; // Must not changed to auto s = "foo" std::s; - // FIXME: Add a using-directive for user-defined literals - // declared in an inline namespace, e.g. - // using namespace s^td; - // int main() { cout << "foo"s; } - // change to - // using namespace std::literals; - // int main() { std::cout << "foo"s; } - if (Kind == DeclarationName::NameKind::CXXLiteralOperatorName) + // Also add a using-declaration to keep codes well-formed, e.g. + // using namespace std; + // int main() { + // auto s = "foo"s; + // } + // will change to + // int main() { + // using std::operator""_s; + // auto s = "foo"s; + // } + // FIXME: Currently this only works for functions and methods + // declared right in the top-level + if (Kind == DeclarationName::NameKind::CXXLiteralOperatorName) { + if (const Stmt *Body = D->getBody()) + DeclsToAdd[Body->getBeginLoc()].insert( + T->getQualifiedNameAsString()); return; + } } SourceLocation Loc = Ref.NameLoc; if (Loc.isMacroID()) { @@ -217,6 +229,16 @@ /*Length=*/0, Qualifier))) return std::move(Err); } + // Produce replacements to add the using-declarations + for (const auto &[Loc, Decls] : DeclsToAdd) { + std::string UsingDecls; + for (const auto &D : Decls) + UsingDecls += llvm::formatv("using {0};\n", D.getKey()); + UsingDecls.pop_back(); + if (auto Err = R.add( + tooling::Replacement(SM, Loc.getLocWithOffset(1), 0, UsingDecls))) + return std::move(Err); + } return Effect::mainFileEdit(SM, std::move(R)); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits