1 //===--- RemoveUsingNamespace.cpp --------------------------------*- C++-*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "FindTarget.h"
10 #include "Selection.h"
11 #include "refactor/Tweak.h"
12 #include "support/Logger.h"
13 #include "clang/AST/Decl.h"
14 #include "clang/AST/DeclBase.h"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/RecursiveASTVisitor.h"
17 #include "clang/Basic/SourceLocation.h"
18 #include "clang/Tooling/Core/Replacement.h"
24 /// Removes the 'using namespace' under the cursor and qualifies all accesses in
25 /// the current file. E.g.,
26 /// using namespace std;
27 /// vector<int> foo(std::map<int, int>);
29 /// std::vector<int> foo(std::map<int, int>);
30 /// Currently limited to using namespace directives inside global namespace to
31 /// simplify implementation. Also the namespace must not contain using
33 class RemoveUsingNamespace
: public Tweak
{
35 const char *id() const override
;
37 bool prepare(const Selection
&Inputs
) override
;
38 Expected
<Effect
> apply(const Selection
&Inputs
) override
;
39 std::string
title() const override
{
40 return "Remove using namespace, re-qualify names instead";
42 llvm::StringLiteral
kind() const override
{
43 return CodeAction::REFACTOR_KIND
;
47 const UsingDirectiveDecl
*TargetDirective
= nullptr;
49 REGISTER_TWEAK(RemoveUsingNamespace
)
51 class FindSameUsings
: public RecursiveASTVisitor
<FindSameUsings
> {
53 FindSameUsings(const UsingDirectiveDecl
&Target
,
54 std::vector
<const UsingDirectiveDecl
*> &Results
)
55 : TargetNS(Target
.getNominatedNamespace()),
56 TargetCtx(Target
.getDeclContext()), Results(Results
) {}
58 bool VisitUsingDirectiveDecl(UsingDirectiveDecl
*D
) {
59 if (D
->getNominatedNamespace() != TargetNS
||
60 D
->getDeclContext() != TargetCtx
)
67 const NamespaceDecl
*TargetNS
;
68 const DeclContext
*TargetCtx
;
69 std::vector
<const UsingDirectiveDecl
*> &Results
;
72 /// Produce edit removing 'using namespace xxx::yyy' and the trailing semicolon.
73 llvm::Expected
<tooling::Replacement
>
74 removeUsingDirective(ASTContext
&Ctx
, const UsingDirectiveDecl
*D
) {
75 auto &SM
= Ctx
.getSourceManager();
76 std::optional
<Token
> NextTok
=
77 Lexer::findNextToken(D
->getEndLoc(), SM
, Ctx
.getLangOpts());
78 if (!NextTok
|| NextTok
->isNot(tok::semi
))
79 return error("no semicolon after using-directive");
80 // FIXME: removing the semicolon may be invalid in some obscure cases, e.g.
81 // if (x) using namespace std; else using namespace bar;
82 return tooling::Replacement(
84 CharSourceRange::getTokenRange(D
->getBeginLoc(), NextTok
->getLocation()),
85 "", Ctx
.getLangOpts());
88 // Returns true iff the parent of the Node is a TUDecl.
89 bool isTopLevelDecl(const SelectionTree::Node
*Node
) {
90 return Node
->Parent
&& Node
->Parent
->ASTNode
.get
<TranslationUnitDecl
>();
93 // Return true if `LHS` is declared in `RHS`
94 bool isDeclaredIn(const NamedDecl
*LHS
, const DeclContext
*RHS
) {
95 const auto *D
= LHS
->getDeclContext();
96 while (D
->isInlineNamespace() || D
->isTransparentContext()) {
101 return D
->Equals(RHS
);
104 bool RemoveUsingNamespace::prepare(const Selection
&Inputs
) {
105 // Find the 'using namespace' directive under the cursor.
106 auto *CA
= Inputs
.ASTSelection
.commonAncestor();
109 TargetDirective
= CA
->ASTNode
.get
<UsingDirectiveDecl
>();
110 if (!TargetDirective
)
112 if (!isa
<Decl
>(TargetDirective
->getDeclContext()))
114 // FIXME: Unavailable for namespaces containing using-namespace decl.
115 // It is non-trivial to deal with cases where identifiers come from the inner
116 // namespace. For example map has to be changed to aa::map.
118 // namespace bb { struct map {}; }
119 // using namespace bb;
121 // using namespace a^a;
122 // int main() { map m; }
123 // We need to make this aware of the transitive using-namespace decls.
124 if (!TargetDirective
->getNominatedNamespace()->using_directives().empty())
126 return isTopLevelDecl(CA
);
129 Expected
<Tweak::Effect
> RemoveUsingNamespace::apply(const Selection
&Inputs
) {
130 auto &Ctx
= Inputs
.AST
->getASTContext();
131 auto &SM
= Ctx
.getSourceManager();
132 // First, collect *all* using namespace directives that redeclare the same
134 std::vector
<const UsingDirectiveDecl
*> AllDirectives
;
135 FindSameUsings(*TargetDirective
, AllDirectives
).TraverseAST(Ctx
);
137 SourceLocation FirstUsingDirectiveLoc
;
138 for (auto *D
: AllDirectives
) {
139 if (FirstUsingDirectiveLoc
.isInvalid() ||
140 SM
.isBeforeInTranslationUnit(D
->getBeginLoc(), FirstUsingDirectiveLoc
))
141 FirstUsingDirectiveLoc
= D
->getBeginLoc();
144 // Collect all references to symbols from the namespace for which we're
145 // removing the directive.
146 std::vector
<SourceLocation
> IdentsToQualify
;
147 for (auto &D
: Inputs
.AST
->getLocalTopLevelDecls()) {
148 findExplicitReferences(
150 [&](ReferenceLoc Ref
) {
152 return; // This reference is already qualified.
154 for (auto *T
: Ref
.Targets
) {
155 if (!isDeclaredIn(T
, TargetDirective
->getNominatedNamespace()))
157 auto Kind
= T
->getDeclName().getNameKind();
158 // Avoid adding qualifiers before operators, e.g.
159 // using namespace std;
160 // cout << "foo"; // Must not changed to std::cout std:: << "foo"
161 if (Kind
== DeclarationName::CXXOperatorName
)
163 // Avoid adding qualifiers before user-defined literals, e.g.
164 // using namespace std;
165 // auto s = "foo"s; // Must not changed to auto s = "foo" std::s;
166 // FIXME: Add a using-directive for user-defined literals
167 // declared in an inline namespace, e.g.
168 // using namespace s^td;
169 // int main() { cout << "foo"s; }
171 // using namespace std::literals;
172 // int main() { std::cout << "foo"s; }
173 if (Kind
== DeclarationName::NameKind::CXXLiteralOperatorName
)
176 SourceLocation Loc
= Ref
.NameLoc
;
177 if (Loc
.isMacroID()) {
178 // Avoid adding qualifiers before macro expansions, it's probably
180 // namespace std { int foo(); }
181 // #define FOO 1 + foo()
182 // using namespace foo; // provides matrix
183 // auto x = FOO; // Must not changed to auto x = std::FOO
184 if (!SM
.isMacroArgExpansion(Loc
))
185 return; // FIXME: report a warning to the users.
186 Loc
= SM
.getFileLoc(Ref
.NameLoc
);
188 assert(Loc
.isFileID());
189 if (SM
.getFileID(Loc
) != SM
.getMainFileID())
190 return; // FIXME: report these to the user as warnings?
191 if (SM
.isBeforeInTranslationUnit(Loc
, FirstUsingDirectiveLoc
))
192 return; // Directive was not visible before this point.
193 IdentsToQualify
.push_back(Loc
);
195 Inputs
.AST
->getHeuristicResolver());
197 // Remove duplicates.
198 llvm::sort(IdentsToQualify
);
199 IdentsToQualify
.erase(
200 std::unique(IdentsToQualify
.begin(), IdentsToQualify
.end()),
201 IdentsToQualify
.end());
203 // Produce replacements to remove the using directives.
204 tooling::Replacements R
;
205 for (auto *D
: AllDirectives
) {
206 auto RemoveUsing
= removeUsingDirective(Ctx
, D
);
208 return RemoveUsing
.takeError();
209 if (auto Err
= R
.add(*RemoveUsing
))
210 return std::move(Err
);
212 // Produce replacements to add the qualifiers.
213 std::string Qualifier
= printUsingNamespaceName(Ctx
, *TargetDirective
) + "::";
214 for (auto Loc
: IdentsToQualify
) {
215 if (auto Err
= R
.add(tooling::Replacement(Ctx
.getSourceManager(), Loc
,
216 /*Length=*/0, Qualifier
)))
217 return std::move(Err
);
219 return Effect::mainFileEdit(SM
, std::move(R
));
223 } // namespace clangd