Debian Codegraph @

llvm-project/clang-tools-extra/clangd/refactor/tweaks/DefineOutline.cpp

raw
1 //===--- DefineOutline.cpp ---------------------------------------*- C++-*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "AST.h"
10 #include "FindTarget.h"
11 #include "HeaderSourceSwitch.h"
12 #include "ParsedAST.h"
13 #include "Selection.h"
14 #include "SourceCode.h"
15 #include "refactor/Tweak.h"
16 #include "support/Logger.h"
17 #include "support/Path.h"
18 #include "clang/AST/ASTTypeTraits.h"
19 #include "clang/AST/Attr.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclBase.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclTemplate.h"
24 #include "clang/AST/Stmt.h"
25 #include "clang/Basic/SourceLocation.h"
26 #include "clang/Basic/SourceManager.h"
27 #include "clang/Basic/TokenKinds.h"
28 #include "clang/Tooling/Core/Replacement.h"
29 #include "clang/Tooling/Syntax/Tokens.h"
30 #include "llvm/ADT/STLExtras.h"
31 #include "llvm/ADT/StringRef.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/Error.h"
34 #include <cstddef>
35 #include <optional>
36 #include <string>
37
38 namespace clang {
39 namespace clangd {
40 namespace {
41
42 // Deduces the FunctionDecl from a selection. Requires either the function body
43 // or the function decl to be selected. Returns null if none of the above
44 // criteria is met.
45 // FIXME: This is shared with define inline, move them to a common header once
46 // we have a place for such.
47 const FunctionDecl *getSelectedFunction(const SelectionTree::Node *SelNode) {
48 if (!SelNode)
49 return nullptr;
50 const DynTypedNode &AstNode = SelNode->ASTNode;
51 if (const FunctionDecl *FD = AstNode.get<FunctionDecl>())
52 return FD;
53 if (AstNode.get<CompoundStmt>() &&
54 SelNode->Selected == SelectionTree::Complete) {
55 if (const SelectionTree::Node *P = SelNode->Parent)
56 return P->ASTNode.get<FunctionDecl>();
57 }
58 return nullptr;
59 }
60
61 std::optional<Path> getSourceFile(llvm::StringRef FileName,
62 const Tweak::Selection &Sel) {
63 assert(Sel.FS);
64 if (auto Source = getCorrespondingHeaderOrSource(FileName, Sel.FS))
65 return *Source;
66 return getCorrespondingHeaderOrSource(FileName, *Sel.AST, Sel.Index);
67 }
68
69 // Synthesize a DeclContext for TargetNS from CurContext. TargetNS must be empty
70 // for global namespace, and endwith "::" otherwise.
71 // Returns std::nullopt if TargetNS is not a prefix of CurContext.
72 std::optional<const DeclContext *>
73 findContextForNS(llvm::StringRef TargetNS, const DeclContext *CurContext) {
74 assert(TargetNS.empty() || TargetNS.endswith("::"));
75 // Skip any non-namespace contexts, e.g. TagDecls, functions/methods.
76 CurContext = CurContext->getEnclosingNamespaceContext();
77 // If TargetNS is empty, it means global ns, which is translation unit.
78 if (TargetNS.empty()) {
79 while (!CurContext->isTranslationUnit())
80 CurContext = CurContext->getParent();
81 return CurContext;
82 }
83 // Otherwise we need to drop any trailing namespaces from CurContext until
84 // we reach TargetNS.
85 std::string TargetContextNS =
86 CurContext->isNamespace()
87 ? llvm::cast<NamespaceDecl>(CurContext)->getQualifiedNameAsString()
88 : "";
89 TargetContextNS.append("::");
90
91 llvm::StringRef CurrentContextNS(TargetContextNS);
92 // If TargetNS is not a prefix of CurrentContext, there's no way to reach
93 // it.
94 if (!CurrentContextNS.startswith(TargetNS))
95 return std::nullopt;
96
97 while (CurrentContextNS != TargetNS) {
98 CurContext = CurContext->getParent();
99 // These colons always exists since TargetNS is a prefix of
100 // CurrentContextNS, it ends with "::" and they are not equal.
101 CurrentContextNS = CurrentContextNS.take_front(
102 CurrentContextNS.drop_back(2).rfind("::") + 2);
103 }
104 return CurContext;
105 }
106
107 // Returns source code for FD after applying Replacements.
108 // FIXME: Make the function take a parameter to return only the function body,
109 // afterwards it can be shared with define-inline code action.
110 llvm::Expected<std::string>
111 getFunctionSourceAfterReplacements(const FunctionDecl *FD,
112 const tooling::Replacements &Replacements) {
113 const auto &SM = FD->getASTContext().getSourceManager();
114 auto OrigFuncRange = toHalfOpenFileRange(
115 SM, FD->getASTContext().getLangOpts(), FD->getSourceRange());
116 if (!OrigFuncRange)
117 return error("Couldn't get range for function.");
118 assert(!FD->getDescribedFunctionTemplate() &&
119 "Define out-of-line doesn't apply to function templates.");
120
121 // Get new begin and end positions for the qualified function definition.
122 unsigned FuncBegin = SM.getFileOffset(OrigFuncRange->getBegin());
123 unsigned FuncEnd = Replacements.getShiftedCodePosition(
124 SM.getFileOffset(OrigFuncRange->getEnd()));
125
126 // Trim the result to function definition.
127 auto QualifiedFunc = tooling::applyAllReplacements(
128 SM.getBufferData(SM.getMainFileID()), Replacements);
129 if (!QualifiedFunc)
130 return QualifiedFunc.takeError();
131 return QualifiedFunc->substr(FuncBegin, FuncEnd - FuncBegin + 1);
132 }
133
134 // Creates a modified version of function definition that can be inserted at a
135 // different location, qualifies return value and function name to achieve that.
136 // Contains function signature, except defaulted parameter arguments, body and
137 // template parameters if applicable. No need to qualify parameters, as they are
138 // looked up in the context containing the function/method.
139 // FIXME: Drop attributes in function signature.
140 llvm::Expected<std::string>
141 getFunctionSourceCode(const FunctionDecl *FD, llvm::StringRef TargetNamespace,
142 const syntax::TokenBuffer &TokBuf,
143 const HeuristicResolver *Resolver) {
144 auto &AST = FD->getASTContext();
145 auto &SM = AST.getSourceManager();
146 auto TargetContext = findContextForNS(TargetNamespace, FD->getDeclContext());
147 if (!TargetContext)
148 return error("define outline: couldn't find a context for target");
149
150 llvm::Error Errors = llvm::Error::success();
151 tooling::Replacements DeclarationCleanups;
152
153 // Finds the first unqualified name in function return type and name, then
154 // qualifies those to be valid in TargetContext.
155 findExplicitReferences(
156 FD,
157 [&](ReferenceLoc Ref) {
158 // It is enough to qualify the first qualifier, so skip references with
159 // a qualifier. Also we can't do much if there are no targets or name is
160 // inside a macro body.
161 if (Ref.Qualifier || Ref.Targets.empty() || Ref.NameLoc.isMacroID())
162 return;
163 // Only qualify return type and function name.
164 if (Ref.NameLoc != FD->getReturnTypeSourceRange().getBegin() &&
165 Ref.NameLoc != FD->getLocation())
166 return;
167
168 for (const NamedDecl *ND : Ref.Targets) {
169 if (ND->getDeclContext() != Ref.Targets.front()->getDeclContext()) {
170 elog("Targets from multiple contexts: {0}, {1}",
171 printQualifiedName(*Ref.Targets.front()),
172 printQualifiedName(*ND));
173 return;
174 }
175 }
176 const NamedDecl *ND = Ref.Targets.front();
177 const std::string Qualifier =
178 getQualification(AST, *TargetContext,
179 SM.getLocForStartOfFile(SM.getMainFileID()), ND);
180 if (auto Err = DeclarationCleanups.add(
181 tooling::Replacement(SM, Ref.NameLoc, 0, Qualifier)))
182 Errors = llvm::joinErrors(std::move(Errors), std::move(Err));
183 },
184 Resolver);
185
186 // Get rid of default arguments, since they should not be specified in
187 // out-of-line definition.
188 for (const auto *PVD : FD->parameters()) {
189 if (!PVD->hasDefaultArg())
190 continue;
191 // Deletion range spans the initializer, usually excluding the `=`.
192 auto DelRange = CharSourceRange::getTokenRange(PVD->getDefaultArgRange());
193 // Get all tokens before the default argument.
194 auto Tokens = TokBuf.expandedTokens(PVD->getSourceRange())
195 .take_while([&SM, &DelRange](const syntax::Token &Tok) {
196 return SM.isBeforeInTranslationUnit(
197 Tok.location(), DelRange.getBegin());
198 });
199 if (TokBuf.expandedTokens(DelRange.getAsRange()).front().kind() !=
200 tok::equal) {
201 // Find the last `=` if it isn't included in the initializer, and update
202 // the DelRange to include it.
203 auto Tok =
204 llvm::find_if(llvm::reverse(Tokens), [](const syntax::Token &Tok) {
205 return Tok.kind() == tok::equal;
206 });
207 assert(Tok != Tokens.rend());
208 DelRange.setBegin(Tok->location());
209 }
210 if (auto Err =
211 DeclarationCleanups.add(tooling::Replacement(SM, DelRange, "")))
212 Errors = llvm::joinErrors(std::move(Errors), std::move(Err));
213 }
214
215 auto DelAttr = [&](const Attr *A) {
216 if (!A)
217 return;
218 auto AttrTokens =
219 TokBuf.spelledForExpanded(TokBuf.expandedTokens(A->getRange()));
220 assert(A->getLocation().isValid());
221 if (!AttrTokens || AttrTokens->empty()) {
222 Errors = llvm::joinErrors(
223 std::move(Errors), error("define outline: Can't move out of line as "
224 "function has a macro `{0}` specifier.",
225 A->getSpelling()));
226 return;
227 }
228 CharSourceRange DelRange =
229 syntax::Token::range(SM, AttrTokens->front(), AttrTokens->back())
230 .toCharRange(SM);
231 if (auto Err =
232 DeclarationCleanups.add(tooling::Replacement(SM, DelRange, "")))
233 Errors = llvm::joinErrors(std::move(Errors), std::move(Err));
234 };
235
236 DelAttr(FD->getAttr<OverrideAttr>());
237 DelAttr(FD->getAttr<FinalAttr>());
238
239 auto DelKeyword = [&](tok::TokenKind Kind, SourceRange FromRange) {
240 bool FoundAny = false;
241 for (const auto &Tok : TokBuf.expandedTokens(FromRange)) {
242 if (Tok.kind() != Kind)
243 continue;
244 FoundAny = true;
245 auto Spelling = TokBuf.spelledForExpanded(llvm::ArrayRef(Tok));
246 if (!Spelling) {
247 Errors = llvm::joinErrors(
248 std::move(Errors),
249 error("define outline: couldn't remove `{0}` keyword.",
250 tok::getKeywordSpelling(Kind)));
251 break;
252 }
253 CharSourceRange DelRange =
254 syntax::Token::range(SM, Spelling->front(), Spelling->back())
255 .toCharRange(SM);
256 if (auto Err =
257 DeclarationCleanups.add(tooling::Replacement(SM, DelRange, "")))
258 Errors = llvm::joinErrors(std::move(Errors), std::move(Err));
259 }
260 if (!FoundAny) {
261 Errors = llvm::joinErrors(
262 std::move(Errors),
263 error("define outline: couldn't find `{0}` keyword to remove.",
264 tok::getKeywordSpelling(Kind)));
265 }
266 };
267
268 if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
269 if (MD->isVirtualAsWritten())
270 DelKeyword(tok::kw_virtual, {FD->getBeginLoc(), FD->getLocation()});
271 if (MD->isStatic())
272 DelKeyword(tok::kw_static, {FD->getBeginLoc(), FD->getLocation()});
273 }
274 if (const auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
275 if (CD->isExplicit())
276 DelKeyword(tok::kw_explicit, {FD->getBeginLoc(), FD->getLocation()});
277 }
278
279 if (Errors)
280 return std::move(Errors);
281 return getFunctionSourceAfterReplacements(FD, DeclarationCleanups);
282 }
283
284 struct InsertionPoint {
285 std::string EnclosingNamespace;
286 size_t Offset;
287 };
288 // Returns the most natural insertion point for \p QualifiedName in \p Contents.
289 // This currently cares about only the namespace proximity, but in feature it
290 // should also try to follow ordering of declarations. For example, if decls
291 // come in order `foo, bar, baz` then this function should return some point
292 // between foo and baz for inserting bar.
293 llvm::Expected<InsertionPoint> getInsertionPoint(llvm::StringRef Contents,
294 llvm::StringRef QualifiedName,
295 const LangOptions &LangOpts) {
296 auto Region = getEligiblePoints(Contents, QualifiedName, LangOpts);
297
298 assert(!Region.EligiblePoints.empty());
299 // FIXME: This selection can be made smarter by looking at the definition
300 // locations for adjacent decls to Source. Unfortunately pseudo parsing in
301 // getEligibleRegions only knows about namespace begin/end events so we
302 // can't match function start/end positions yet.
303 auto Offset = positionToOffset(Contents, Region.EligiblePoints.back());
304 if (!Offset)
305 return Offset.takeError();
306 return InsertionPoint{Region.EnclosingNamespace, *Offset};
307 }
308
309 // Returns the range that should be deleted from declaration, which always
310 // contains function body. In addition to that it might contain constructor
311 // initializers.
312 SourceRange getDeletionRange(const FunctionDecl *FD,
313 const syntax::TokenBuffer &TokBuf) {
314 auto DeletionRange = FD->getBody()->getSourceRange();
315 if (auto *CD = llvm::dyn_cast<CXXConstructorDecl>(FD)) {
316 // AST doesn't contain the location for ":" in ctor initializers. Therefore
317 // we find it by finding the first ":" before the first ctor initializer.
318 SourceLocation InitStart;
319 // Find the first initializer.
320 for (const auto *CInit : CD->inits()) {
321 // SourceOrder is -1 for implicit initializers.
322 if (CInit->getSourceOrder() != 0)
323 continue;
324 InitStart = CInit->getSourceLocation();
325 break;
326 }
327 if (InitStart.isValid()) {
328 auto Toks = TokBuf.expandedTokens(CD->getSourceRange());
329 // Drop any tokens after the initializer.
330 Toks = Toks.take_while([&TokBuf, &InitStart](const syntax::Token &Tok) {
331 return TokBuf.sourceManager().isBeforeInTranslationUnit(Tok.location(),
332 InitStart);
333 });
334 // Look for the first colon.
335 auto Tok =
336 llvm::find_if(llvm::reverse(Toks), [](const syntax::Token &Tok) {
337 return Tok.kind() == tok::colon;
338 });
339 assert(Tok != Toks.rend());
340 DeletionRange.setBegin(Tok->location());
341 }
342 }
343 return DeletionRange;
344 }
345
346 /// Moves definition of a function/method to an appropriate implementation file.
347 ///
348 /// Before:
349 /// a.h
350 /// void foo() { return; }
351 /// a.cc
352 /// #include "a.h"
353 ///
354 /// ----------------
355 ///
356 /// After:
357 /// a.h
358 /// void foo();
359 /// a.cc
360 /// #include "a.h"
361 /// void foo() { return; }
362 class DefineOutline : public Tweak {
363 public:
364 const char *id() const override;
365
366 bool hidden() const override { return false; }
367 llvm::StringLiteral kind() const override {
368 return CodeAction::REFACTOR_KIND;
369 }
370 std::string title() const override {
371 return "Move function body to out-of-line";
372 }
373
374 bool prepare(const Selection &Sel) override {
375 // Bail out if we are not in a header file.
376 // FIXME: We might want to consider moving method definitions below class
377 // definition even if we are inside a source file.
378 if (!isHeaderFile(Sel.AST->getSourceManager().getFilename(Sel.Cursor),
379 Sel.AST->getLangOpts()))
380 return false;
381
382 Source = getSelectedFunction(Sel.ASTSelection.commonAncestor());
383 // Bail out if the selection is not a in-line function definition.
384 if (!Source || !Source->doesThisDeclarationHaveABody() ||
385 Source->isOutOfLine())
386 return false;
387
388 // Bail out if this is a function template or specialization, as their
389 // definitions need to be visible in all including translation units.
390 if (Source->getDescribedFunctionTemplate())
391 return false;
392 if (Source->getTemplateSpecializationInfo())
393 return false;
394
395 // Bail out in templated classes, as it is hard to spell the class name, i.e
396 // if the template parameter is unnamed.
397 if (auto *MD = llvm::dyn_cast<CXXMethodDecl>(Source)) {
398 if (MD->getParent()->isTemplated())
399 return false;
400 }
401
402 // Note that we don't check whether an implementation file exists or not in
403 // the prepare, since performing disk IO on each prepare request might be
404 // expensive.
405 return true;
406 }
407
408 Expected<Effect> apply(const Selection &Sel) override {
409 const SourceManager &SM = Sel.AST->getSourceManager();
410 auto CCFile = getSourceFile(Sel.AST->tuPath(), Sel);
411
412 if (!CCFile)
413 return error("Couldn't find a suitable implementation file.");
414 assert(Sel.FS && "FS Must be set in apply");
415 auto Buffer = Sel.FS->getBufferForFile(*CCFile);
416 // FIXME: Maybe we should consider creating the implementation file if it
417 // doesn't exist?
418 if (!Buffer)
419 return llvm::errorCodeToError(Buffer.getError());
420 auto Contents = Buffer->get()->getBuffer();
421 auto InsertionPoint = getInsertionPoint(
422 Contents, Source->getQualifiedNameAsString(), Sel.AST->getLangOpts());
423 if (!InsertionPoint)
424 return InsertionPoint.takeError();
425
426 auto FuncDef = getFunctionSourceCode(
427 Source, InsertionPoint->EnclosingNamespace, Sel.AST->getTokens(),
428 Sel.AST->getHeuristicResolver());
429 if (!FuncDef)
430 return FuncDef.takeError();
431
432 SourceManagerForFile SMFF(*CCFile, Contents);
433 const tooling::Replacement InsertFunctionDef(
434 *CCFile, InsertionPoint->Offset, 0, *FuncDef);
435 auto Effect = Effect::mainFileEdit(
436 SMFF.get(), tooling::Replacements(InsertFunctionDef));
437 if (!Effect)
438 return Effect.takeError();
439
440 // FIXME: We should also get rid of inline qualifier.
441 const tooling::Replacement DeleteFuncBody(
442 Sel.AST->getSourceManager(),
443 CharSourceRange::getTokenRange(*toHalfOpenFileRange(
444 SM, Sel.AST->getLangOpts(),
445 getDeletionRange(Source, Sel.AST->getTokens()))),
446 ";");
447 auto HeaderFE = Effect::fileEdit(SM, SM.getMainFileID(),
448 tooling::Replacements(DeleteFuncBody));
449 if (!HeaderFE)
450 return HeaderFE.takeError();
451
452 Effect->ApplyEdits.try_emplace(HeaderFE->first,
453 std::move(HeaderFE->second));
454 return std::move(*Effect);
455 }
456
457 private:
458 const FunctionDecl *Source = nullptr;
459 };
460
461 REGISTER_TWEAK(DefineOutline)
462
463 } // namespace
464 } // namespace clangd
465 } // namespace clang