clang-tools  10.0.0svn
Tweak.cpp
Go to the documentation of this file.
1 //===--- Tweak.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 #include "Tweak.h"
9 #include "Logger.h"
10 #include "Path.h"
11 #include "SourceCode.h"
12 #include "llvm/ADT/None.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/Error.h"
18 #include "llvm/Support/Registry.h"
19 #include <functional>
20 #include <memory>
21 #include <utility>
22 
23 LLVM_INSTANTIATE_REGISTRY(llvm::Registry<clang::clangd::Tweak>)
24 
25 namespace clang {
26 namespace clangd {
27 
28 /// A handy typedef to save some typing.
29 typedef llvm::Registry<Tweak> TweakRegistry;
30 
31 namespace {
32 /// Asserts invariants on TweakRegistry. No-op with assertion disabled.
33 void validateRegistry() {
34 #ifndef NDEBUG
35  llvm::StringSet<> Seen;
36  for (const auto &E : TweakRegistry::entries()) {
37  // REGISTER_TWEAK ensures E.getName() is equal to the tweak class name.
38  // We check that id() matches it.
39  assert(E.instantiate()->id() == E.getName() &&
40  "id should be equal to class name");
41  assert(Seen.try_emplace(E.getName()).second && "duplicate check id");
42  }
43 #endif
44 }
45 } // namespace
46 
47 Tweak::Selection::Selection(ParsedAST &AST, unsigned RangeBegin,
48  unsigned RangeEnd)
49  : AST(AST), SelectionBegin(RangeBegin), SelectionEnd(RangeEnd),
50  ASTSelection(AST.getASTContext(), AST.getTokens(), RangeBegin, RangeEnd) {
51  auto &SM = AST.getSourceManager();
52  Code = SM.getBufferData(SM.getMainFileID());
53  Cursor = SM.getComposedLoc(SM.getMainFileID(), RangeBegin);
54 }
55 
56 std::vector<std::unique_ptr<Tweak>>
58  llvm::function_ref<bool(const Tweak &)> Filter) {
59  validateRegistry();
60 
61  std::vector<std::unique_ptr<Tweak>> Available;
62  for (const auto &E : TweakRegistry::entries()) {
63  std::unique_ptr<Tweak> T = E.instantiate();
64  if (!Filter(*T) || !T->prepare(S))
65  continue;
66  Available.push_back(std::move(T));
67  }
68  // Ensure deterministic order of the results.
69  llvm::sort(Available,
70  [](const std::unique_ptr<Tweak> &L,
71  const std::unique_ptr<Tweak> &R) { return L->id() < R->id(); });
72  return Available;
73 }
74 
75 llvm::Expected<std::unique_ptr<Tweak>> prepareTweak(StringRef ID,
76  const Tweak::Selection &S) {
77  auto It = llvm::find_if(
78  TweakRegistry::entries(),
79  [ID](const TweakRegistry::entry &E) { return E.getName() == ID; });
80  if (It == TweakRegistry::end())
81  return llvm::createStringError(llvm::inconvertibleErrorCode(),
82  "id of the tweak is invalid");
83  std::unique_ptr<Tweak> T = It->instantiate();
84  if (!T->prepare(S))
85  return llvm::createStringError(llvm::inconvertibleErrorCode(),
86  "failed to prepare() a check");
87  return std::move(T);
88 }
89 
90 llvm::Expected<std::pair<Path, Edit>>
91 Tweak::Effect::fileEdit(const SourceManager &SM, FileID FID,
92  tooling::Replacements Replacements) {
93  Edit Ed(SM.getBufferData(FID), std::move(Replacements));
94  if (auto FilePath = getCanonicalPath(SM.getFileEntryForID(FID), SM))
95  return std::make_pair(*FilePath, std::move(Ed));
96  return llvm::createStringError(
97  llvm::inconvertibleErrorCode(),
98  "Failed to get absolute path for edited file: " +
99  SM.getFileEntryForID(FID)->getName());
100 }
101 
102 llvm::Expected<Tweak::Effect>
103 Tweak::Effect::mainFileEdit(const SourceManager &SM,
104  tooling::Replacements Replacements) {
105  auto PathAndEdit = fileEdit(SM, SM.getMainFileID(), std::move(Replacements));
106  if (!PathAndEdit)
107  return PathAndEdit.takeError();
108  Tweak::Effect E;
109  E.ApplyEdits.try_emplace(PathAndEdit->first, PathAndEdit->second);
110  return E;
111 }
112 
113 } // namespace clangd
114 } // namespace clang
llvm::Expected< std::unique_ptr< Tweak > > prepareTweak(StringRef ID, const Tweak::Selection &S)
Definition: Tweak.cpp:75
llvm::StringRef Code
The text of the active document.
Definition: Tweak.h:51
Selection(ParsedAST &AST, unsigned RangeBegin, unsigned RangeEnd)
Definition: Tweak.cpp:47
llvm::Registry< Tweak > TweakRegistry
A handy typedef to save some typing.
Definition: Tweak.cpp:29
llvm::StringMap< Edit > ApplyEdits
A mapping from file path(the one used for accessing the underlying VFS) to edits. ...
Definition: Tweak.h:78
SourceLocation Cursor
A location of the cursor in the editor.
Definition: Tweak.h:56
Input to prepare and apply tweaks.
Definition: Tweak.h:48
static llvm::Expected< Tweak::Effect > mainFileEdit(const SourceManager &SM, tooling::Replacements Replacements)
Creates an effect with an Edit for the main file.
Definition: Tweak.cpp:103
Stores and provides access to parsed AST.
Definition: ParsedAST.h:46
SourceManager & getSourceManager()
Definition: ParsedAST.h:73
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static llvm::Expected< std::pair< Path, Edit > > fileEdit(const SourceManager &SM, FileID FID, tooling::Replacements Replacements)
Path is the absolute, symlink-resolved path for the file pointed by FID in SM.
Definition: Tweak.cpp:91
llvm::Optional< std::string > getCanonicalPath(const FileEntry *F, const SourceManager &SourceMgr)
Get the canonical path of F.
Definition: SourceCode.cpp:625
An interface base for small context-sensitive refactoring actions.
Definition: Tweak.h:45
A set of edits generated for a single file.
Definition: SourceCode.h:210
std::vector< std::unique_ptr< Tweak > > prepareTweaks(const Tweak::Selection &S, llvm::function_ref< bool(const Tweak &)> Filter)
Calls prepare() on all tweaks that satisfy the filter, returning those that can run on the selection...
Definition: Tweak.cpp:57