Skip to content

Commit a200501

Browse files
committed
[clangd] Addusing tweak: find insertion point after definition
When type/function is defined in the middle of the file, previuosly we would sometimes insert a "using" line before that definition, leading to a compilation error. With this fix, we pick a point after such definition in translation unit. This is not a perfect solution. For example, it still doesn't handle "using namespace" directives. It is, however, a significant improvement. Differential Revision: https://reviews.llvm.org/D92053
1 parent f8317bb commit a200501

File tree

2 files changed

+95
-12
lines changed

2 files changed

+95
-12
lines changed

clang-tools-extra/clangd/refactor/tweaks/AddUsing.cpp

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class AddUsing : public Tweak {
4848
NestedNameSpecifierLoc QualifierToRemove;
4949
// The name following QualifierToRemove.
5050
llvm::StringRef Name;
51+
// If valid, the insertion point for "using" statement must come after this.
52+
// This is relevant when the type is defined in the main file, to make sure
53+
// the type/function is already defined at the point where "using" is added.
54+
SourceLocation MustInsertAfterLoc;
5155
};
5256
REGISTER_TWEAK(AddUsing)
5357

@@ -120,7 +124,8 @@ struct InsertionPointData {
120124
llvm::Expected<InsertionPointData>
121125
findInsertionPoint(const Tweak::Selection &Inputs,
122126
const NestedNameSpecifierLoc &QualifierToRemove,
123-
const llvm::StringRef Name) {
127+
const llvm::StringRef Name,
128+
const SourceLocation MustInsertAfterLoc) {
124129
auto &SM = Inputs.AST->getSourceManager();
125130

126131
// Search for all using decls that affect this point in file. We need this for
@@ -132,6 +137,11 @@ findInsertionPoint(const Tweak::Selection &Inputs,
132137
SM)
133138
.TraverseAST(Inputs.AST->getASTContext());
134139

140+
auto IsValidPoint = [&](const SourceLocation Loc) {
141+
return MustInsertAfterLoc.isInvalid() ||
142+
SM.isBeforeInTranslationUnit(MustInsertAfterLoc, Loc);
143+
};
144+
135145
bool AlwaysFullyQualify = true;
136146
for (auto &U : Usings) {
137147
// Only "upgrade" to fully qualified is all relevant using decls are fully
@@ -149,12 +159,13 @@ findInsertionPoint(const Tweak::Selection &Inputs,
149159
U->getName() == Name) {
150160
return InsertionPointData();
151161
}
162+
152163
// Insertion point will be before last UsingDecl that affects cursor
153164
// position. For most cases this should stick with the local convention of
154165
// add using inside or outside namespace.
155166
LastUsingLoc = U->getUsingLoc();
156167
}
157-
if (LastUsingLoc.isValid()) {
168+
if (LastUsingLoc.isValid() && IsValidPoint(LastUsingLoc)) {
158169
InsertionPointData Out;
159170
Out.Loc = LastUsingLoc;
160171
Out.AlwaysFullyQualify = AlwaysFullyQualify;
@@ -175,23 +186,25 @@ findInsertionPoint(const Tweak::Selection &Inputs,
175186
if (Tok == Toks.end() || Tok->endLocation().isInvalid()) {
176187
return error("Namespace with no {");
177188
}
178-
if (!Tok->endLocation().isMacroID()) {
189+
if (!Tok->endLocation().isMacroID() && IsValidPoint(Tok->endLocation())) {
179190
InsertionPointData Out;
180191
Out.Loc = Tok->endLocation();
181192
Out.Suffix = "\n";
182193
return Out;
183194
}
184195
}
185196
// No using, no namespace, no idea where to insert. Try above the first
186-
// top level decl.
197+
// top level decl after MustInsertAfterLoc.
187198
auto TLDs = Inputs.AST->getLocalTopLevelDecls();
188-
if (TLDs.empty()) {
189-
return error("Cannot find place to insert \"using\"");
199+
for (const auto &TLD : TLDs) {
200+
if (!IsValidPoint(TLD->getBeginLoc()))
201+
continue;
202+
InsertionPointData Out;
203+
Out.Loc = SM.getExpansionLoc(TLD->getBeginLoc());
204+
Out.Suffix = "\n\n";
205+
return Out;
190206
}
191-
InsertionPointData Out;
192-
Out.Loc = SM.getExpansionLoc(TLDs[0]->getBeginLoc());
193-
Out.Suffix = "\n\n";
194-
return Out;
207+
return error("Cannot find place to insert \"using\"");
195208
}
196209

197210
bool isNamespaceForbidden(const Tweak::Selection &Inputs,
@@ -254,6 +267,7 @@ bool AddUsing::prepare(const Selection &Inputs) {
254267
if (auto *II = D->getDecl()->getIdentifier()) {
255268
QualifierToRemove = D->getQualifierLoc();
256269
Name = II->getName();
270+
MustInsertAfterLoc = D->getDecl()->getBeginLoc();
257271
}
258272
} else if (auto *T = Node->ASTNode.get<TypeLoc>()) {
259273
if (auto E = T->getAs<ElaboratedTypeLoc>()) {
@@ -271,6 +285,15 @@ bool AddUsing::prepare(const Selection &Inputs) {
271285
QualifierToRemove, Inputs.AST->getASTContext().getPrintingPolicy());
272286
if (!Name.consume_front(QualifierToRemoveStr))
273287
return false; // What's spelled doesn't match the qualifier.
288+
289+
if (const auto *ET = E.getTypePtr()) {
290+
if (const auto *TDT =
291+
dyn_cast<TypedefType>(ET->getNamedType().getTypePtr())) {
292+
MustInsertAfterLoc = TDT->getDecl()->getBeginLoc();
293+
} else if (auto *TD = ET->getAsTagDecl()) {
294+
MustInsertAfterLoc = TD->getBeginLoc();
295+
}
296+
}
274297
}
275298
}
276299

@@ -312,7 +335,8 @@ Expected<Tweak::Effect> AddUsing::apply(const Selection &Inputs) {
312335
return std::move(Err);
313336
}
314337

315-
auto InsertionPoint = findInsertionPoint(Inputs, QualifierToRemove, Name);
338+
auto InsertionPoint =
339+
findInsertionPoint(Inputs, QualifierToRemove, Name, MustInsertAfterLoc);
316340
if (!InsertionPoint) {
317341
return InsertionPoint.takeError();
318342
}

clang-tools-extra/clangd/unittests/TweakTests.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2825,7 +2825,66 @@ using namespace one;
28252825
namespace {using two::cc;
28262826
28272827
cc C;
2828-
})cpp"}};
2828+
})cpp"},
2829+
// Type defined in main file, make sure using is after that.
2830+
{R"cpp(
2831+
namespace xx {
2832+
struct yy {};
2833+
}
2834+
2835+
x^x::yy X;
2836+
)cpp",
2837+
R"cpp(
2838+
namespace xx {
2839+
struct yy {};
2840+
}
2841+
2842+
using xx::yy;
2843+
2844+
yy X;
2845+
)cpp"},
2846+
// Type defined in main file via "using", insert after that.
2847+
{R"cpp(
2848+
#include "test.hpp"
2849+
2850+
namespace xx {
2851+
using yy = one::two::cc;
2852+
}
2853+
2854+
x^x::yy X;
2855+
)cpp",
2856+
R"cpp(
2857+
#include "test.hpp"
2858+
2859+
namespace xx {
2860+
using yy = one::two::cc;
2861+
}
2862+
2863+
using xx::yy;
2864+
2865+
yy X;
2866+
)cpp"},
2867+
// Using must come after function definition.
2868+
{R"cpp(
2869+
namespace xx {
2870+
void yy();
2871+
}
2872+
2873+
void fun() {
2874+
x^x::yy();
2875+
}
2876+
)cpp",
2877+
R"cpp(
2878+
namespace xx {
2879+
void yy();
2880+
}
2881+
2882+
using xx::yy;
2883+
2884+
void fun() {
2885+
yy();
2886+
}
2887+
)cpp"}};
28292888
llvm::StringMap<std::string> EditedFiles;
28302889
for (const auto &Case : Cases) {
28312890
for (const auto &SubCase : expandCases(Case.TestSource)) {

0 commit comments

Comments
 (0)