Coverage Report

Created: 2020-10-24 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Tooling/Refactoring/Rename/RenamingAction.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- RenamingAction.cpp - Clang refactoring library -------------------===//
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
/// \file
10
/// Provides an action to rename every symbol at a point.
11
///
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
15
#include "clang/AST/ASTConsumer.h"
16
#include "clang/AST/ASTContext.h"
17
#include "clang/Basic/FileManager.h"
18
#include "clang/Frontend/CompilerInstance.h"
19
#include "clang/Frontend/FrontendAction.h"
20
#include "clang/Lex/Lexer.h"
21
#include "clang/Lex/Preprocessor.h"
22
#include "clang/Tooling/CommonOptionsParser.h"
23
#include "clang/Tooling/Refactoring.h"
24
#include "clang/Tooling/Refactoring/RefactoringAction.h"
25
#include "clang/Tooling/Refactoring/RefactoringDiagnostic.h"
26
#include "clang/Tooling/Refactoring/RefactoringOptions.h"
27
#include "clang/Tooling/Refactoring/Rename/SymbolName.h"
28
#include "clang/Tooling/Refactoring/Rename/USRFinder.h"
29
#include "clang/Tooling/Refactoring/Rename/USRFindingAction.h"
30
#include "clang/Tooling/Refactoring/Rename/USRLocFinder.h"
31
#include "clang/Tooling/Tooling.h"
32
#include "llvm/ADT/STLExtras.h"
33
#include "llvm/Support/Errc.h"
34
#include "llvm/Support/Error.h"
35
#include <string>
36
#include <vector>
37
38
using namespace llvm;
39
40
namespace clang {
41
namespace tooling {
42
43
namespace {
44
45
Expected<SymbolOccurrences>
46
12
findSymbolOccurrences(const NamedDecl *ND, RefactoringRuleContext &Context) {
47
12
  std::vector<std::string> USRs =
48
12
      getUSRsForDeclaration(ND, Context.getASTContext());
49
12
  std::string PrevName = ND->getNameAsString();
50
12
  return getOccurrencesOfUSRs(USRs, PrevName,
51
12
                              Context.getASTContext().getTranslationUnitDecl());
52
12
}
53
54
} // end anonymous namespace
55
56
0
const RefactoringDescriptor &RenameOccurrences::describe() {
57
0
  static const RefactoringDescriptor Descriptor = {
58
0
      "local-rename",
59
0
      "Rename",
60
0
      "Finds and renames symbols in code with no indexer support",
61
0
  };
62
0
  return Descriptor;
63
0
}
64
65
Expected<RenameOccurrences>
66
RenameOccurrences::initiate(RefactoringRuleContext &Context,
67
20
                            SourceRange SelectionRange, std::string NewName) {
68
20
  const NamedDecl *ND =
69
20
      getNamedDeclAt(Context.getASTContext(), SelectionRange.getBegin());
70
20
  if (!ND)
71
8
    return Context.createDiagnosticError(
72
8
        SelectionRange.getBegin(), diag::err_refactor_selection_no_symbol);
73
12
  return RenameOccurrences(getCanonicalSymbolDeclaration(ND),
74
12
                           std::move(NewName));
75
12
}
76
77
0
const NamedDecl *RenameOccurrences::getRenameDecl() const { return ND; }
78
79
Expected<AtomicChanges>
80
12
RenameOccurrences::createSourceReplacements(RefactoringRuleContext &Context) {
81
12
  Expected<SymbolOccurrences> Occurrences = findSymbolOccurrences(ND, Context);
82
12
  if (!Occurrences)
83
0
    return Occurrences.takeError();
84
  // FIXME: Verify that the new name is valid.
85
12
  SymbolName Name(NewName);
86
12
  return createRenameReplacements(
87
12
      *Occurrences, Context.getASTContext().getSourceManager(), Name);
88
12
}
89
90
Expected<QualifiedRenameRule>
91
QualifiedRenameRule::initiate(RefactoringRuleContext &Context,
92
                              std::string OldQualifiedName,
93
1
                              std::string NewQualifiedName) {
94
1
  const NamedDecl *ND =
95
1
      getNamedDeclFor(Context.getASTContext(), OldQualifiedName);
96
1
  if (!ND)
97
0
    return llvm::make_error<llvm::StringError>("Could not find symbol " +
98
0
                                                   OldQualifiedName,
99
0
                                               llvm::errc::invalid_argument);
100
1
  return QualifiedRenameRule(ND, std::move(NewQualifiedName));
101
1
}
102
103
0
const RefactoringDescriptor &QualifiedRenameRule::describe() {
104
0
  static const RefactoringDescriptor Descriptor = {
105
0
      /*Name=*/"local-qualified-rename",
106
0
      /*Title=*/"Qualified Rename",
107
      /*Description=*/
108
0
      R"(Finds and renames qualified symbols in code within a translation unit.
109
0
It is used to move/rename a symbol to a new namespace/name:
110
0
  * Supported symbols: classes, class members, functions, enums, and type alias.
111
0
  * Renames all symbol occurrences from the old qualified name to the new
112
0
    qualified name. All symbol references will be correctly qualified; For
113
0
    symbol definitions, only name will be changed.
114
0
For example, rename "A::Foo" to "B::Bar":
115
0
  Old code:
116
0
    namespace foo {
117
0
    class A {};
118
0
    }
119
0
120
0
    namespace bar {
121
0
    void f(foo::A a) {}
122
0
    }
123
0
124
0
  New code after rename:
125
0
    namespace foo {
126
0
    class B {};
127
0
    }
128
0
129
0
    namespace bar {
130
0
    void f(B b) {}
131
0
    })"
132
0
  };
133
0
  return Descriptor;
134
0
}
135
136
Expected<AtomicChanges>
137
1
QualifiedRenameRule::createSourceReplacements(RefactoringRuleContext &Context) {
138
1
  auto USRs = getUSRsForDeclaration(ND, Context.getASTContext());
139
1
  assert(!USRs.empty());
140
1
  return tooling::createRenameAtomicChanges(
141
1
      USRs, NewQualifiedName, Context.getASTContext().getTranslationUnitDecl());
142
1
}
143
144
Expected<std::vector<AtomicChange>>
145
createRenameReplacements(const SymbolOccurrences &Occurrences,
146
84
                         const SourceManager &SM, const SymbolName &NewName) {
147
  // FIXME: A true local rename can use just one AtomicChange.
148
84
  std::vector<AtomicChange> Changes;
149
459
  for (const auto &Occurrence : Occurrences) {
150
459
    ArrayRef<SourceRange> Ranges = Occurrence.getNameRanges();
151
459
    assert(NewName.getNamePieces().size() == Ranges.size() &&
152
459
           "Mismatching number of ranges and name pieces");
153
459
    AtomicChange Change(SM, Ranges[0].getBegin());
154
459
    for (const auto &Range : llvm::enumerate(Ranges)) {
155
459
      auto Error =
156
459
          Change.replace(SM, CharSourceRange::getCharRange(Range.value()),
157
459
                         NewName.getNamePieces()[Range.index()]);
158
459
      if (Error)
159
0
        return std::move(Error);
160
459
    }
161
459
    Changes.push_back(std::move(Change));
162
459
  }
163
84
  return std::move(Changes);
164
84
}
165
166
/// Takes each atomic change and inserts its replacements into the set of
167
/// replacements that belong to the appropriate file.
168
static void convertChangesToFileReplacements(
169
    ArrayRef<AtomicChange> AtomicChanges,
170
331
    std::map<std::string, tooling::Replacements> *FileToReplaces) {
171
1.11k
  for (const auto &AtomicChange : AtomicChanges) {
172
1.11k
    for (const auto &Replace : AtomicChange.getReplacements()) {
173
1.11k
      llvm::Error Err =
174
1.11k
          (*FileToReplaces)[std::string(Replace.getFilePath())].add(Replace);
175
1.11k
      if (Err) {
176
0
        llvm::errs() << "Renaming failed in " << Replace.getFilePath() << "! "
177
0
                     << llvm::toString(std::move(Err)) << "\n";
178
0
      }
179
1.11k
    }
180
1.11k
  }
181
331
}
182
183
class RenamingASTConsumer : public ASTConsumer {
184
public:
185
  RenamingASTConsumer(
186
      const std::vector<std::string> &NewNames,
187
      const std::vector<std::string> &PrevNames,
188
      const std::vector<std::vector<std::string>> &USRList,
189
      std::map<std::string, tooling::Replacements> &FileToReplaces,
190
      bool PrintLocations)
191
      : NewNames(NewNames), PrevNames(PrevNames), USRList(USRList),
192
69
        FileToReplaces(FileToReplaces), PrintLocations(PrintLocations) {}
193
194
69
  void HandleTranslationUnit(ASTContext &Context) override {
195
143
    for (unsigned I = 0; I < NewNames.size(); 
++I74
) {
196
      // If the previous name was not found, ignore this rename request.
197
74
      if (PrevNames[I].empty())
198
2
        continue;
199
200
72
      HandleOneRename(Context, NewNames[I], PrevNames[I], USRList[I]);
201
72
    }
202
69
  }
203
204
  void HandleOneRename(ASTContext &Context, const std::string &NewName,
205
                       const std::string &PrevName,
206
72
                       const std::vector<std::string> &USRs) {
207
72
    const SourceManager &SourceMgr = Context.getSourceManager();
208
209
72
    SymbolOccurrences Occurrences = tooling::getOccurrencesOfUSRs(
210
72
        USRs, PrevName, Context.getTranslationUnitDecl());
211
72
    if (PrintLocations) {
212
0
      for (const auto &Occurrence : Occurrences) {
213
0
        FullSourceLoc FullLoc(Occurrence.getNameRanges()[0].getBegin(),
214
0
                              SourceMgr);
215
0
        errs() << "clang-rename: renamed at: " << SourceMgr.getFilename(FullLoc)
216
0
               << ":" << FullLoc.getSpellingLineNumber() << ":"
217
0
               << FullLoc.getSpellingColumnNumber() << "\n";
218
0
      }
219
0
    }
220
    // FIXME: Support multi-piece names.
221
    // FIXME: better error handling (propagate error out).
222
72
    SymbolName NewNameRef(NewName);
223
72
    Expected<std::vector<AtomicChange>> Change =
224
72
        createRenameReplacements(Occurrences, SourceMgr, NewNameRef);
225
72
    if (!Change) {
226
0
      llvm::errs() << "Failed to create renaming replacements for '" << PrevName
227
0
                   << "'! " << llvm::toString(Change.takeError()) << "\n";
228
0
      return;
229
0
    }
230
72
    convertChangesToFileReplacements(*Change, &FileToReplaces);
231
72
  }
232
233
private:
234
  const std::vector<std::string> &NewNames, &PrevNames;
235
  const std::vector<std::vector<std::string>> &USRList;
236
  std::map<std::string, tooling::Replacements> &FileToReplaces;
237
  bool PrintLocations;
238
};
239
240
// A renamer to rename symbols which are identified by a give USRList to
241
// new name.
242
//
243
// FIXME: Merge with the above RenamingASTConsumer.
244
class USRSymbolRenamer : public ASTConsumer {
245
public:
246
  USRSymbolRenamer(const std::vector<std::string> &NewNames,
247
                   const std::vector<std::vector<std::string>> &USRList,
248
                   std::map<std::string, tooling::Replacements> &FileToReplaces)
249
259
      : NewNames(NewNames), USRList(USRList), FileToReplaces(FileToReplaces) {
250
259
    assert(USRList.size() == NewNames.size());
251
259
  }
252
253
259
  void HandleTranslationUnit(ASTContext &Context) override {
254
518
    for (unsigned I = 0; I < NewNames.size(); 
++I259
) {
255
      // FIXME: Apply AtomicChanges directly once the refactoring APIs are
256
      // ready.
257
259
      auto AtomicChanges = tooling::createRenameAtomicChanges(
258
259
          USRList[I], NewNames[I], Context.getTranslationUnitDecl());
259
259
      convertChangesToFileReplacements(AtomicChanges, &FileToReplaces);
260
259
    }
261
259
  }
262
263
private:
264
  const std::vector<std::string> &NewNames;
265
  const std::vector<std::vector<std::string>> &USRList;
266
  std::map<std::string, tooling::Replacements> &FileToReplaces;
267
};
268
269
69
std::unique_ptr<ASTConsumer> RenamingAction::newASTConsumer() {
270
69
  return std::make_unique<RenamingASTConsumer>(NewNames, PrevNames, USRList,
271
69
                                                FileToReplaces, PrintLocations);
272
69
}
273
274
259
std::unique_ptr<ASTConsumer> QualifiedRenamingAction::newASTConsumer() {
275
259
  return std::make_unique<USRSymbolRenamer>(NewNames, USRList, FileToReplaces);
276
259
}
277
278
} // end namespace tooling
279
} // end namespace clang