clang-tools  9.0.0
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 "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/StringMap.h"
12 #include "llvm/Support/Error.h"
13 #include "llvm/Support/Registry.h"
14 #include <functional>
15 #include <memory>
16 
17 LLVM_INSTANTIATE_REGISTRY(llvm::Registry<clang::clangd::Tweak>)
18 
19 namespace clang {
20 namespace clangd {
21 
22 /// A handy typedef to save some typing.
23 typedef llvm::Registry<Tweak> TweakRegistry;
24 
25 namespace {
26 /// Asserts invariants on TweakRegistry. No-op with assertion disabled.
27 void validateRegistry() {
28 #ifndef NDEBUG
29  llvm::StringSet<> Seen;
30  for (const auto &E : TweakRegistry::entries()) {
31  // REGISTER_TWEAK ensures E.getName() is equal to the tweak class name.
32  // We check that id() matches it.
33  assert(E.instantiate()->id() == E.getName() &&
34  "id should be equal to class name");
35  assert(Seen.try_emplace(E.getName()).second && "duplicate check id");
36  }
37 #endif
38 }
39 } // namespace
40 
41 Tweak::Selection::Selection(ParsedAST &AST, unsigned RangeBegin,
42  unsigned RangeEnd)
43  : AST(AST), SelectionBegin(RangeBegin), SelectionEnd(RangeEnd),
44  ASTSelection(AST.getASTContext(), RangeBegin, RangeEnd) {
45  auto &SM = AST.getSourceManager();
46  Code = SM.getBufferData(SM.getMainFileID());
47  Cursor = SM.getComposedLoc(SM.getMainFileID(), RangeBegin);
48 }
49 
50 std::vector<std::unique_ptr<Tweak>>
52  llvm::function_ref<bool(const Tweak &)> Filter) {
53  validateRegistry();
54 
55  std::vector<std::unique_ptr<Tweak>> Available;
56  for (const auto &E : TweakRegistry::entries()) {
57  std::unique_ptr<Tweak> T = E.instantiate();
58  if (!Filter(*T) || !T->prepare(S))
59  continue;
60  Available.push_back(std::move(T));
61  }
62  // Ensure deterministic order of the results.
63  llvm::sort(Available,
64  [](const std::unique_ptr<Tweak> &L,
65  const std::unique_ptr<Tweak> &R) { return L->id() < R->id(); });
66  return Available;
67 }
68 
69 llvm::Expected<std::unique_ptr<Tweak>> prepareTweak(StringRef ID,
70  const Tweak::Selection &S) {
71  auto It = llvm::find_if(
72  TweakRegistry::entries(),
73  [ID](const TweakRegistry::entry &E) { return E.getName() == ID; });
74  if (It == TweakRegistry::end())
75  return llvm::createStringError(llvm::inconvertibleErrorCode(),
76  "id of the tweak is invalid");
77  std::unique_ptr<Tweak> T = It->instantiate();
78  if (!T->prepare(S))
79  return llvm::createStringError(llvm::inconvertibleErrorCode(),
80  "failed to prepare() a check");
81  return std::move(T);
82 }
83 
84 } // namespace clangd
85 } // namespace clang
llvm::Expected< std::unique_ptr< Tweak > > prepareTweak(StringRef ID, const Tweak::Selection &S)
Definition: Tweak.cpp:69
llvm::StringRef Code
The text of the active document.
Definition: Tweak.h:45
Selection(ParsedAST &AST, unsigned RangeBegin, unsigned RangeEnd)
Definition: Tweak.cpp:41
llvm::Registry< Tweak > TweakRegistry
A handy typedef to save some typing.
Definition: Tweak.cpp:23
SourceLocation Cursor
A location of the cursor in the editor.
Definition: Tweak.h:50
Input to prepare and apply tweaks.
Definition: Tweak.h:42
Stores and provides access to parsed AST.
Definition: ClangdUnit.h:73
SourceManager & getSourceManager()
Definition: ClangdUnit.h:99
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
An interface base for small context-sensitive refactoring actions.
Definition: Tweak.h:39
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:51